如何阻止Webpack自动递增Chrome扩展的项目版本号?
问题描述
我正在开发一个用Webpack构建的Chrome扩展,构建命令是:cross-env NODE_ENV=production yarn webpack -c webpack.config.js --mode production
Webpack配置文件webpack.config.js如下:
const HTMLPlugin = require('html-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin'); const path = require('path'); const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); const BrowserExtensionPlugin = require("extension-build-webpack-plugin"); module.exports = { entry: { options: './src/options.tsx', popup: './src/popup.tsx', content: './src/content.tsx', background: './src/background.tsx', }, output: { filename: '[name].js', path: path.resolve(__dirname, 'build'), }, resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx', '.css'], modules: [path.resolve(__dirname, 'src'), 'node_modules'], alias: { react: 'preact/compat', 'react-dom': 'preact/compat', }, }, module: { rules: [ { test: /\.(tsx|jsx|ts|js)x?$/, exclude: /node_modules/, use: [ { loader: 'babel-loader', options: { presets: [ "@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript", ], }, }, ], }, { test: /\.svg$/, use: ['@svgr/webpack'], }, ], }, plugins: [ new HTMLPlugin({ chunks: ['options'], filename: 'options.html', title: 'Options page title', }), new HTMLPlugin({ chunks: ['popup'], filename: 'popup.html', }), new CopyPlugin([ { from: './src/_locales/', to: './_locales' }, { from: './src/assets', to: './assets' }, { from: './src/manifest.json', to: './manifest.json' }, ]), new BrowserExtensionPlugin({devMode: false, name: "build/chromium.zip", directory: "src", updateType: "minor"}), ], optimization: { minimizer: [ new UglifyJSPlugin({ uglifyOptions: { compress: { drop_console: true, drop_debugger: true, } } }) ] }, mode: 'production', stats: 'minimal', performance: { hints: false, maxEntrypointSize: 512000, maxAssetSize: 512000 } };
manifest.json内容如下:
{ "manifest_version": 3, "name": "__MSG_appName__", "description": "__MSG_appDesc__", "default_locale": "en", "version": "0.1.0", .... .... }
每次执行构建命令时,不仅build文件夹里的manifest.json版本会更新,src文件夹下的源文件manifest.json的version也会从0.1.0自动递增到0.2.0。我怀疑是某个Webpack插件导致的,请问怎么阻止这个自动递增的功能?
解决方案
导致源文件manifest.json版本自动递增的是BrowserExtensionPlugin,你在配置中传入的updateType: "minor"参数,会让该插件直接修改directory指定目录(即你设置的src)下的manifest.json来递增版本号。
要阻止这个行为,只需修改插件配置:
- 移除
updateType参数,或者将其设置为none - 若需要保持打包后的
manifest.json版本递增,但不想修改源文件,可将directory指向临时目录,或直接禁用版本自动更新功能
修改后的BrowserExtensionPlugin配置示例:
new BrowserExtensionPlugin({ devMode: false, name: "build/chromium.zip", directory: "src", updateType: "none" // 禁用版本自动递增 }),
或者完全移除updateType字段:
new BrowserExtensionPlugin({ devMode: false, name: "build/chromium.zip", directory: "src" }),
这样插件就不会再修改src目录下的源manifest.json文件了。
内容的提问来源于stack exchange,提问作者ishandutta2007
相关产品推荐
相关产品推荐

