Webpack 是原始模块大小的三倍多

Webpack more than triples original module size

我设置了我的环境,我在 TypeScript 中编写客户端 JS 代码(以获得使用 TS 的好处)并使用 Webpack 将其捆绑到我的 Firebase 项目中的 public 目录中。我发现 Webpack 似乎给我的代码增加了很多开销。当原始文件小于 100KiB 时,打包仅导入 Firestore 模块的文件最终会产生大约 300KiB 的代码。

这是一个仍然具有相同行为的最小配置:

index.ts

import 'firebase/firestore';

webpack.config.ts

import { Configuration } from 'webpack';
import path from 'path';

const config: Configuration = {
    mode: 'production',
    entry: path.resolve(__dirname, 'index.ts'),
    module: {
        rules: [
            {
                test: /\.ts?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/',
        filename: '[name].js',
        chunkFilename: '[chunkhash].js'
      },
    resolve: {
        extensions: [ '.ts', '.js' ],
        modules: ['node_modules'],
    },
    optimization: {
        minimize: true,
    },
}

export default config;

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "outDir": "lib",
        "sourceMap": false,
        "strict": true,
        "target": "es2017",
        "esModuleInterop": true,
    },
    "compileOnSave": true,
}

package.json

{
  "name": "webpack-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "firebase": "^8.0.2"
  },
  "devDependencies": {
    "ts-loader": "^8.0.11",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.5",
    "webpack": "^5.4.0",
    "webpack-cli": "^4.2.0"
  }
}

如上所述,当导入的文件小于 100 KiB 时,最终的包大小为 300+ KiB。我该怎么做才能使我的捆绑包大小不增加三倍?

好的,这看起来像是一个已知问题。在this issue in the Firebase JS SDK GitHub (with conversation moving here). It looks like there's a pretty involved workaround here中首次提到,或者我可以使用 Firebase JS SDK 的实验版本。