win 下 package.json 与 webpack 配置容易被忽略的一个坑

本文讲述了作者在使用Webpack进行代码打包时遇到的一个奇怪现象:不设置NODE_ENV环境变量反而能让文件压缩得更小。通过深入排查,最终发现是由于设置过程中多输入的一个空格导致的问题。文章还分享了正确的配置方法及完整的webpack.production.config.js配置文件。

起因

今天用webpack 打包的时候发现 不加 set NODE_ENV 压缩 居然比加上 set NODE_ENV 还要小,命令行 分两次输入 set NODE_ENV=production (回车) webpack .... 的时候是正常的。反复实验多次,打印NODE_ENV 也正常。(如图)

加上 set NODE_ENV
clipboard.png

不加 set NODE_ENV
clipboard.png

配置项

clipboard.png

debug

多次打印

clipboard.png

clipboard.png

clipboard.png

大眼一看没什么问题,仔细看以下就会发现第一次的输出多了一个空格

原因

就是多了一个致命空格,导致我一下午时而压缩成功,时而压缩失败,非常灵异。
拐回头看我们的package.json 代码,我们来对比一下对错写法

wrong
clipboard.png

correct
clipboard.png

一个小失误 ,顺便附上我的 webpack.production.config.js

const webpack = require("webpack");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
    entry:__dirname + "/app/main.js",

    output: {
        filename:'bundle-[hash:8].js',
        path:__dirname+"/build"
    },

    module:{
        rules:[
            {
                test:/(\.jsx|\.js)$/,
                use:{
                    loader:'babel-loader'
                },
                exclude:/node_modules/

            },
            {
                test:/\.css$/,
                use: ExtractTextPlugin.extract({
                       fallback: "style-loader",
                      use: [
                          {
                              loader:'css-loader',
                              options:{
                                  module:true
                              }
                          },
                          {
                              loader:'postcss-loader'
                          }

                      ]
                })
                    
            },
            {
                test:/(\.jpg|\.png)$/i,
                use:{
                    loader:'url-loader',
                    options:{
                        limit:5000,
                        name:'img/[name].[hash:8].[ext]'
                    }
                }
            }
        ]
    }
    ,
    plugins:[
        new webpack.BannerPlugin("翻版必究"),
        new HtmlWebpackPlugin({
            template:__dirname+"/app/index.tmpl.html"
        }),
        new webpack.HotModuleReplacementPlugin(),
        new ExtractTextPlugin("styles-[hash:8].css"),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            output:{
                comments:false
            },
            compress:{
                warnings: false
            }
        }),
        new webpack.DefinePlugin({
            'process.env':{
                'NODE_ENV':JSON.stringify(process.env.NODE_ENV)
            }
        }),
        new webpack.DefinePlugin({
              __DEV__: JSON.stringify(JSON.parse((process.env.NODE_ENV == 'dev') || 'false'))
        }),
        new webpack.optimize.CommonsChunkPlugin({
              name: 'vendor',
              filename: './js/[name].[hash:8].js'
        }),

 
    ]

}

npm run build:portable > crm1@0.1.0 build:portable > vue-cli-service build --modern && electron-builder --win portable Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme All browser targets in the browserslist configuration have supported ES module. Therefore we don't build two separate bundles for differential loading. ⠇ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠋ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠼ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production... WARNING Compiled with 4 warnings 08:31:03 warning /js/chunk-vendors.1a0a0613.js is 2.28 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit. warning asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance. Assets: img/0001.bbc23d94.jpg (1.85 MiB) css/chunk-vendors.7d75f85b.css (306 KiB) js/chunk-vendors.1a0a0613.js (2.17 MiB) warning entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance. Entrypoints: app (2.6 MiB) css/chunk-vendors.7d75f85b.css js/chunk-vendors.1a0a0613.js css/app.e3c96718.css js/app.22cb88f3.js warning webpack performance recommendations: You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application. For more info visit https://webpack.js.org/guides/code-splitting/ File Size Gzipped dist\js\chunk-vendors.1a0a0613.js 2222.03 KiB 687.66 KiB dist\js\app.22cb88f3.js 130.49 KiB 17.91 KiB dist\workbox-5b385ed2.js 14.11 KiB 4.93 KiB dist\service-worker.js 1.20 KiB 0.76 KiB dist\css\chunk-vendors.7d75f85b.css 305.86 KiB 42.07 KiB dist\css\app.e3c96718.css 1.40 KiB 0.63 KiB Images and other types of assets omitted. Build at: 2025-07-03T00:31:03.176Z - Hash: 9fc57e568ce674b2 - Time: 40125ms DONE Build complete. The dist directory is ready to be deployed. INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html • electron-builder version=26.0.12 os=10.0.19041 • description is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • author is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • writing effective config file=dist\builder-effective-config.yaml • executing @electron/rebuild electronVersion=37.2.0 arch=x64 buildFromSource=false appDir=./ • installing native dependencies arch=x64 • completed installing native dependencies • packaging platform=win32 arch=x64 electron=37.2.0 appOutDir=dist\win-unpacked • downloading url=https://github.com/electron/electron/releases/download/v37.2.0/electron-v37.2.0-win32-x64.zip size=123 MB parts=8 • downloaded url=https://github.com/electron/electron/releases/download/v37.2.0/electron-v37.2.0-win32-x64.zip duration=2m44.871s • updating asar integrity executable resource executablePath=dist\win-unpacked\crm1.exe ⨯ Application entry file "index.js" in the "E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar" is corrupted: Error: "index.js" was not found in this archive failedTask=build stackTrace=Error: Application entry file "index.js" in the "E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar" is corrupted: Error: "index.js" was not found in this archive at error (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\asar\asarFileChecker.ts:6:12) at checkFileInArchive (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\asar\asarFileChecker.ts:15:11) at WinPackager.checkFileInPackage (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:638:25) at WinPackager.sanityCheckPackage (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:686:16) at WinPackager.doPack (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:358:5) at WinPackager.pack (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:168:5) at Packager.doBuild (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\packager.ts:491:9) at executeFinally (E:\soft\finnal\yuanma2\crm1\node_modules\builder-util\src\promise.ts:12:14) at Packager.build (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\packager.ts:425:31) at executeFinally (E:\soft\finnal\yuanma2\crm1\node_modules\builder-util\src\promise.ts:12:14)假如不管资源过大的问题
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值