【问题标题】:Webpack more than triples original module sizeWebpack 是原始模块大小的三倍多
【发布时间】:2020-11-14 19:59:59
【问题描述】:

我设置了环境,在其中使用 TypeScript 编写客户端 JS 代码(以获得使用 TS 的好处)并使用 Webpack 将其捆绑到我的 Firebase 项目的公共目录中。我发现 Webpack 似乎给我的代码增加了很多开销。当原始文件为

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

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。我该怎么做才能使我的捆绑包大小不增加三倍?

【问题讨论】:

  • 你应该删除你原来的帖子。如果你不这样做,它会引起问题。
  • 你在计算依赖的大小吗?
  • @RobertHarvey,我删除了我原来的帖子。至于计算依赖项的大小,我所做的只是在那个入口文件index.ts 上运行 webpack。该文件所做的只是导入 firestore 模块。 Webpack 输出一个超过 300KiB 的 main.js 文件。原始的 Firestore 模块小于 100KiB。我对 Webpack 是如何将原始代码大小增加三倍以上感到困惑。
  • Webpack 在构建中添加了很多自己的脚手架。您应该手动将其剥离以查看剩下的内容。但是您需要保留它,以便在您运行应用程序时它可以工作。
  • @DougStevenson 如果这正是 Webpack 做事的方式,那么我真的不知道为什么人们会在这种情况下使用它,因为它只会否定小的原始代码的性能提升。我早些时候看到了一个 YouTube 视频,其中一位 Google 工程师说他们能够为一个项目获得一个

标签: typescript firebase webpack bundle


【解决方案1】:

好的,这看起来像是一个已知问题。它是在 Firebase JS SDK GitHub 的 this issue 中首次提到的(对话移动到 here)。看起来有一个相当复杂的解决方法here,或者我可以使用 Firebase JS SDK 的实验版本。

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多