简体   繁体   English

manifest.json 中的环境变量 - Chrome 扩展

[英]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:这是你如何做到的:

  • Install craco into your project using npm i @craco/craco .使用npm i @craco/craco将 craco 安装到您的项目中。
  • Install 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作为开发依赖项。
  • Let's suppose we're creating a development and a production build of our Chrome Extension.假设我们正在创建 Chrome 扩展程序的开发和生产版本。 Let's also suppose we've already assigned "version": "0.1.0" in our Chrome Extension's manifest.json .假设我们已经在 Chrome 扩展的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.根据构建类型,我们希望在 Chrome 扩展的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:在你的 React 应用程序的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... }
  • Create a new file called 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 }
  • Run 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 扩展程序的版本名称。
  • Delete the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM