[英]Environment variables in manifest.json - Chrome Extension
Is it possible to set environment variables in the manifest.json file of a Chrome Extension?是否可以在 Chrome 扩展的 manifest.json 文件中设置环境变量?
Like wOxxOm said, I used webpack to proccess manifest.json.就像 wOxxOm 所说,我使用 webpack 来处理 manifest.json。
In my case, I needed to set version automatically on manifest file.就我而言,我需要在清单文件上自动设置版本。
I added to webpack script:我添加到 webpack 脚本:
plugins: [
new CopyWebpackPlugin([
{
from: "public/manifest.json",
to: "manifest.json",
transform(content, path) {
return modify(content)
}
}
]),
]
And the modify function replaces version on file for the parameter:并且修改 function 替换了参数文件中的版本:
function modify(buffer) {
var manifest = JSON.parse(buffer.toString());
let argv = process.argv[2];
if (argv) manifest.version = argv.split("=")[1];
let manifest_JSON = JSON.stringify(manifest, null, 2);
return manifest_JSON;
}
So, I build like "yarn build --version=xx" and webpack do what I need.所以,我像“yarn build --version=xx”一样构建,webpack 做我需要的。
PS: if you're going to use this, remember to change: PS:如果你要使用这个,记得更改:
the manifest.json directory, if necessary; manifest.json 目录,如有必要;
the value in the modify function, in my case it was version修改 function 中的值,在我的情况下是版本
As the OP has mentioned in her answer , using the copy-webpack-plugin in the webpack.config.js
file is the way to go if you're building your Chrome Extension with React.正如 OP 在她的回答中提到的那样,如果您使用 React 构建 Chrome 扩展程序,则使用
webpack.config.js
文件中的copy-webpack-plugin是 go 的方法。 However, if your React app is based on create-react-app
, directly editing the webpack.config.js
file (which is located in node_modules/react-scripts/config
) is not recommended .但是,如果您的 React 应用基于
create-react-app
, 则不建议直接编辑webpack.config.js
文件(位于node_modules/react-scripts/config
中)。
In such a case, use craco , which is an npm package that can be used to customize an app based on create-react-app
.在这种情况下,请使用craco ,它是一个 npm package ,可用于自定义基于
create-react-app
。 Here's how you do it:这是你如何做到的:
npm i @craco/craco
.npm i @craco/craco
将 craco 安装到您的项目中。copy-webpack-plugin
as a dev-dependency in your project using npm i --save-dev copy-webpack-plugin
.npm i --save-dev copy-webpack-plugin
在您的项目中安装copy-webpack-plugin
作为开发依赖项。"version": "0.1.0"
in our Chrome Extension's manifest.json
.manifest.json
中分配了"version": "0.1.0"
。 Depending on the build type, we'd like to assign accordingly the version_name
field in our Chrome Extension's manifest.json
, eg, "version_name": "0.1.0 dev"
for development and "version_name": "0.1.0"
for production.manifest.json
中相应地分配version_name
字段,例如, "version_name": "0.1.0 dev"
用于开发, "version_name": "0.1.0"
用于生产. In your React app's package.json
, introduce two fields (the script names can be whatever you wish) as follows:package.json
中,引入两个字段(脚本名称可以是任何你想要的),如下所示: "scripts": {... "build-dev": "CRX_ENV=dev craco build", // or "set CRX_ENV=dev&& craco build" in the case of Windows "build-prod": "CRX_ENV=prod craco build", // or "set CRX_ENV=prod&& craco build" in the case of Windows... }
craco.config.js
in the root of your project.craco.config.js
的新文件。 As per your need, do something similar to the following in the craco.config.js
file:craco.config.js
文件中执行类似于以下的操作: const CopyPlugin = require("copy-webpack-plugin") module.exports = { webpack: { plugins: [ new CopyPlugin({ patterns: [ { from: "public/manifest.json", to: "manifest.json", transform(content, path) { return modifyManifest(content) }, }, ], }), ], }, } function modifyManifest(buffer) { const manifest = JSON.parse(buffer.toString()) if (process.env.CRX_ENV === "dev") { manifest.version_name = `${manifest.version} dev` } else if (process.env.CRX_ENV === "prod") { manifest.version_name = `${manifest.version}` } const manifestJson = JSON.stringify(manifest, null, 2) return manifestJson }
npm run build-dev
.npm run build-dev
。 It will create a folder called build
in your project root.build
的文件夹。 This build
folder is your unpacked Chrome Extension, which you can load into Chrome using the "Load unpacked" button on the chrome://extensions
page.build
文件夹是您解压后的 Chrome 扩展程序,您可以使用chrome://extensions
页面上的“加载解压”按钮将其加载到 Chrome 中。 Once loaded, you should be able to see 0.1.0 dev
as the version name of your Chrome Extension.0.1.0 dev
作为 Chrome 扩展程序的版本名称。build
folder created from the previous step and run npm run build-prod
, and repeat the same steps.build
文件夹并运行npm run build-prod
,并重复相同的步骤。 You should be able to see 0.1.0
as the version name of your Chrome Extension on the chrome://extensions
page.chrome://extensions
页面上看到0.1.0
作为 Chrome 扩展程序的版本名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.