账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    使用webpack打包出现 it exceeds the max of "500KB" 怎么解决?
    34
    0
     40% building modules 257/263 modules 6 active ...web/src/dashboard/router/404/index.js[BABEL] Note: The code generator has deoptimised the styling of "/Users/lisiqi/Desktop/bty/catfood_web/src/dashboard/slides/abbott-test/map/echarts-gl-2.2.2.js" as it exceeds the max of "500KB".
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 靑春、已散場 普通会员 1楼

      Webpack 是一个用于构建和打包 Web 应用程序的 JavaScript 解析器和打包工具。Webpack 中的最大输出文件大小限制是 500KB,这意味着在打包过程中,如果输出文件的大小超过这个限制,Webpack 将停止打包过程,因为超过了最大输出文件大小。

      要解决这个问题,可以尝试以下几种方法:

      1. 降低输出文件大小:在配置文件 webpack.config.js 中,你可以使用以下代码来降低输出文件大小:

      javascript module.exports = { output: { path: './dist', filename: 'output.js', publicPath: './', }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env', '@babel/preset-react'], exclude: /node_modules/, }, }, }, ], }, };

      在这个配置中,我们配置了以下内容:

      • output 属性指定了输出文件的路径和名称,输出文件的名称是根据文件内容动态生成的。
      • filename 属性指定了输出文件的文件名,与文件内容相关,比如输出文件的扩展名是 .js
      • publicPath 属性指定了输出文件的公共路径,默认为 /,用于服务器环境的输出,如果设置为 ./,则在本地环境中输出。
      • module 属性定义了一个处理模块的配置,testexclude 属性指定了匹配的模块类型,use 属性指定了使用哪个打包插件。
      • babel-loader 使用了 @babel/preset-env@babel/preset-react 框架,将 JavaScript 代码转换为浏览器环境和 React 库兼容的 JavaScript 代码。
      • exclude 属性指定了不匹配的模块类型,@babel/preset-env@babel/preset-react 会排除包含 node_modules 目录的文件。

      • 使用 chunksize 选项:在配置文件 webpack.config.js 中,你可以使用 module.exports = { chunksize: 1024 * 1024 } 选项来限制输出文件的大小:

      javascript module.exports = { output: { path: './dist', filename: 'output.js', publicPath: './', }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env', '@babel/preset-react'], exclude: /node_modules/, }, }, }, ], }, module: { plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'common', filename: 'common.js', chunks: ['build'], // Add the built 'common.js' file to the output }), ], }, };

      在这个配置中,我们创建了一个名为 common.js 的公共文件,其大小是 1024 * 1024 字节。这样,Webpack 将 build 配置的 chunksize 选项设置为 1024 * 1024 字节,将 common.js 文件打包到输出文件中。

      1. 使用 sizesizeAtRuntime 属性:在配置文件 webpack.config.js 中,你可以使用 module.exports = { size: 1024 * 1024, sizeAtRuntime: true } 选项来限制输出文件的大小:

      javascript module.exports = { output: { path: './dist', filename: 'output.js', publicPath: './', }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env', '@babel/preset-react'], exclude: /node_modules/, }, }, }, ], }, module: { plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'common', filename: 'common.js', chunks: ['build'], // Add the built 'common.js' file to the output }), new webpack.optimize.SizeAtRuntimePlugin({ target: 'web', moduleFilenameTemplate: `!__webpack__[local]__/exports/${encodeURIComponent(module.name)}!${encodeURIComponent(module.id)}!${encodeURIComponent(module.type)}!${encodeURIComponent(module.ext)}`, sizeAtRuntime: true, }), ], }, };

      在这个配置中,我们创建了一个名为 common.js 的公共文件,其大小是 1024 * 1024 字节。然后,我们添加了一个 SizeAtRuntimePlugin 插件,该插件会在运行时检查文件大小,如果文件大小超过 500KB,则将其打包到输出文件中。这样,Webpack 将 build 配置的 sizesizeAtRuntime 属性设置为 1024 * 1024 字节,将 common.js 文件打包到输出文件中。

      注意,这三种方法都会在每次运行Webpack 时检查输出文件的大小,并在需要时进行打包,因此在生产环境中,可能需要定期检查和清理累积的打包文件,以避免文件过大导致的问题。在开发环境中,可能不需要进行这个操作。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部