【问题标题】:How do I exclude a file (eg config file) in Vue.js?如何在 Vue.js 中排除文件(例如配置文件)?
【发布时间】:2019-04-01 18:24:51
【问题描述】:

我试过了:

chainWebpack: config => {
    config.merge({
        module: {
            rules: [{
                test: /\.jsx?$/,
                exclude: {
                    exclude: [path.resolve(__dirname, "public/my-config.js")]
                }
            }]
        }
    })
}

或者

config.module.rule('js')
  .exclude({
    exclude: path.resolve(__dirname, "public/my-config.js")
  })

但它不起作用。

我想在pages/index.html 中导入带有脚本标记的public/my-config.js,或者只导入import { config1, config2 } from '../public/my-config'

虽然我可以使用 externals 在 webpack 中不包含模块,但使用 Vue.js 不太直观。

我必须在dist/ 上提供my-config.js,以便可以对其进行编辑。

【问题讨论】:

  • 在 Vue CLI 项目中,public 目录包含 only copied 的文件,未由 webpack 处理,这似乎是您想要的。将其排除在处理之外是没有意义的,因为这应该已经完成​​了。你真的想从什么中排除文件?
  • @tony19 是的。感谢您提供指向 Vue 文档的链接。我可以通过vue inspect > output.js 确认有CopyWebpackPlugin 具有from: '/path/to/my_project/public', to: '/path/to/my_project/dist'ignore: ['index.html', '.DS_Store']。我想要做的是不包含my-config.js 文件,但有my-config.js.gitkeep 文件并让用户通过复制文件cp my-config.js.gitkeep my-config.js 创建他/她自己的my-config.js 文件。我也将使用我自己的my-config.js 文件。
  • 如果我在.gitignore 中有整个dist/public/my-config.js 可能并不重要,但我想知道如何排除文件,好吧,我想具体public 目录中的那些。

标签: vue.js webpack


【解决方案1】:

参考:

我在vue.config.js中写的:

const path = require("path");

module.exports = {
    baseUrl: ".",
    chainWebpack: config => {
        config.plugin('copy').tap((args) => [[
              {
                from: '/path/to/my_project/public',
                to: '/path/to/my_project/dist',
                toType: 'dir',
                ignore: [
                  'index.html',
                  '.DS_Store',
                  'config.data.js'
                ]
              }
          ]]
        );
    }
}

我使用了$ vue inspect > output.js,然后检查了output.js 文件,以了解config.plugin('copy') 使用了哪些参数,而config.plugin('copy') 恰好是new CopyWebpackPlugin 的一个实例。

【讨论】:

    【解决方案2】:

    参考:https://github.com/vuejs/vue-cli/issues/2231#issuecomment-413441633

    试试这个,更简单:

    module.exports = {
      chainWebpack: config => {
        config.plugin('copy').tap(([options]) => {
          options[0].ignore.push('api/**/*')
          return [options]
        })
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2020-07-22
      • 2011-02-13
      相关资源
      最近更新 更多