缩小的 JS 查找并替换有效的替换策略?

Minified JS find and replace a valid strategy for substitution?

出于对问题不重要的原因(使用 create-react-app 的输出,想参数化一些变量 POST 构建时间,即在部署时),我处于这样的场景中我想用一个值替换字符串的所有实例,即“REPLACE_ME”。

如果我可以访问未缩小的javascript,这将是微不足道的;但是,我想知道在缩小的 javascript 上进行查找和替换是否就足够了?

create-react-app 在后台使用 Webpack,您可以使用 webpack.DefinePlugin 在构建时实现它。

// webpack.config.js
const webpack = require('webpack');

const config = {
  entry: './src/index.js',
  output: {
    ...
  },
  plugins: [
    new webpack.DefinePlugin({
      REPLACE_ME: JSON.stringify('yourBuildTimeValueGoesHere'),
    }),
    ...
  ],
  ...
}

module.exports = config;