Webpack 集成示例#

按快速开始 安装最小示例;Webpack 固定为 5.110.3。

1// webpack.config.mjs:核心配置
2import { resolve } from 'node:path';
3import fileBasedRouter from '@feoe/fs-router/webpack';
4import HtmlWebpackPlugin from 'html-webpack-plugin';
5
6export default {
7  mode: 'production',
8  entry: './src/main.tsx',
9  output: { path: resolve('dist/webpack'), filename: 'app.js', publicPath: '/', clean: true },
10  resolve: { extensions: ['.tsx', '.ts', '.js'] },
11  module: { rules: [{
12    test: /\.tsx?$/, exclude: /node_modules/,
13    use: { loader: 'ts-loader', options: { transpileOnly: true, compilerOptions: { noEmit: false, allowImportingTsExtensions: false } } },
14  }] },
15  plugins: [fileBasedRouter(), new HtmlWebpackPlugin({ template: './bundler.html' })],
16};

实际配置固定 context/output 路径。transpileOnly 仅负责转译,独立 tsc --noEmit 必须在生成后运行;不能把成功打包视为类型检查通过。

1npm --prefix examples/minimal-react run build:webpack
2npm --prefix examples/minimal-react run preview:webpack

脚本通过 Webpack API 构建、检查错误并关闭 compiler,不依赖全局 CLI。生产服务器还需要正确的 SPA 深链接回退。

完整配置。CommonJS 用户改用 .cjs 与 require('@feoe/fs-router/webpack').default,不要混用 ESM 文件与 require。