0

// 包.json


"scripts": {
    "start": "cross-env NODE_ENV=development webpack serve --config config/webpack.dev.js",
...

"devDependencies": {
    "copy-webpack-plugin": "^9.0.1",
    "cross-env": "^7.0.3",
    "webpack": "^5.58.2",
    "webpack-cli": "^4.9.0",
    "webpack-dev-server": "^4.3.1",
    "webpack-merge": "^5.8.0"
...

// 配置/webpack.dev.js

plugins: [
    // https://github.com/webpack-contrib/copy-webpack-plugin/issues/29#issuecomment-348031891
    new WriteFilePlugin(),
    // Copies files from target to destination folder
    new CopyWebpackPlugin({
      patterns: [
        {
          from: paths.setup + '/env.dev.json',
          to: paths.public + '/env.json',
          noErrorOnMissing: false,
        },
      ],
    }),

    // Only update what has changed on hot reload
    new webpack.HotModuleReplacementPlugin(),
  ],

我想要做的是复制my-project/setup/env.dev.jsonmy-project/public/env.json

当我运行npm run start时,我得到这个输出:

asset ../public/env.json 28 bytes [emitted] [from: setup/env.dev.json] [copied]

但是目录上没有复制public文件。

我想这样做的原因是有一种通用的方法来从 json 文件中检索一些项目变量,所以我的应用程序可以执行类似的操作

fetch('env.json').parse()

因此,在开发模式下,我尝试复制上述文件,而在生产模式下,我正在做类似的事情,但目标dist是目录(并且按预期工作)

在开发模式下运行应用程序时,webpack 无法复制 env.dev.json 是否有某种原因?

4

0 回答 0