更改 vue-cli 项目中的公共文件夹

Ang*_*nes 8 vue.js vuejs2

当我运行时,npm run build我收到以下错误消息。

[copy-webpack-plugin] 无法在“path\to\project\public”中找到“path\to\project\public”

我将public文件夹移动到src/main/resources/public. 但是我找不到更改路径的配置。我认为相关代码在node_modules\@vue\cli-service\lib\config\app.js

// copy static assets in public/
webpackConfig
  .plugin('copy')
    .use(require('copy-webpack-plugin'), [[{
      from: api.resolve('public'),
      to: api.resolve(options.outputDir),
      ignore: ['index.html', '.DS_Store']
    }]])
Run Code Online (Sandbox Code Playgroud)

如何在 vue.config.js 中覆盖它?

小智 9

这对我使用 vue-cli 3.0 有效。只需将其添加到您的 vue.config.js 文件中即可。

module.exports = {
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        return [{template: '/path/to/index.html'}]
      })
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然这在技术上可能更正确。

module.exports = {
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        args[0] = {
            template: '/path/to/index.html'
        }
        return args
      })
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

实际上,这将是这样做的首选方式,因此不会覆盖任何其他默认值。

module.exports = {
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        args[0].template = '/path/to/index.html'
        return args
      })
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是可行的,但路径必须以“./”开头,否则您会收到一些错误“模块无法解析”,因此“./path/to/index.html” (2认同)