4

虽然我显然不是 webpack 专家,但我通常会弄清楚/找出我需要修改什么才能实现我需要的东西。然而,我在这个问题上输了一天多,尽管它看起来很简单:

我想index.html在构建之后通过从文件夹中复制它来添加一个构建。

我尝试将此添加configureWebpackvue.config.js

plugins: [
  new CopyPlugin([{
    from: 'deploy',
    to: '/',
    force: true,
    copyUnmodified: true
  }])
]

(文件deploy夹中唯一的文件是这个index.html)。

我还尝试了各种版本的chainWebpack,主要是从 github 讨论中挑选出来的,试图利用插件html,例如move-indexcopy. 但是,我在 github 上找到的大部分内容都为我破坏了构建。

甚至没有简单的尝试,我只是尝试点击和控制台似乎不起作用:

chainWebpack: config => {
  config
    .plugin('html')
    .tap(args => {
      console.log(args);
      return args;
    });
},

输出:

Building for production as library (commonjs,umd,umd-min)...[]
ERROR  TypeError: Cannot read property '__expression' of undefined`
...
at module.exports.toConfig (D:\sgn\www\_g\berwow\BERWoW\Client\RecommenderV2\node_modules\webpack-chain\src\Config.js:129:40)

到目前为止我发现了什么:

  • CopyPlugin 要么不起作用,要么有例外.html文件有异常。
  • 至少有两个插件:(move-index& html)可能会干扰我的副本。我还没有弄清楚如何将我的更改推到队列的后面。

我也尝试使用 atest.html并且我还尝试在我的文件上放置一个不同的扩展名.txt并在复制它时覆盖它,回到.html. 大多数时候我都会遇到错误。

是否有一种相对直接的方法可以进入vue-cli-serve'build命令并简单地将 an 复制index.html到文件夹中?

我正在使用的构建命令是:

vue-cli-service build --target lib --name SomeName --inline-css --inline-vue src/main.ts

请注意,这是一个--target lib构建,它不会将 an 输出index.htmldist文件夹中,而是将demo.html. 所以我建议测试任何解决方案--target lib构建测试任何解决方案,因为它显然具有与正常构建不同的输出。

这是输出vue inspecthttps
://jsfiddle.net/websiter/rkh5ecvd/embedded/js/dark/#JavaScript 这是我的当前内容vue.config.jshttps ://jsfiddle.net/websiter/fou3y4zc/embedded/js/dark /#JavaScript,尝试解决/窥视上述问题的地方configWebpackchainWebpack

我正在使用@vue/cli 4.2.3vue 2.6.11webpack 4.42.1

4

2 回答 2

2

npm i copyfiles -D通过简单地运行并将这一位添加到build脚本中,我想出了一个解决方法:

 && copyfiles -f ./deploy/* dist/Recommender

这不是问题的正确答案,而是解决问题的方法。但它有效:)。

仍然对如何将其正确链接到 webpack 构建脚本感兴趣。

于 2020-04-15T19:42:25.030 回答
0

我知道这是一个老问题,但我想我会为在 Vue 3 中仍然遇到此问题的任何人添加更多细节。

使用以下配置,我能够使用vue 3.0.0、@vue/cli-service 4.5.0、webpack 4.46.0 和copy-webpack-plugin 6.4.1 实现类似的输出:

vue.config.js

const CopyPlugin = require("copy-webpack-plugin");

/**
 * @type {import('@vue/cli-service').ProjectOptions}
 */
module.exports = {
    chainWebpack: (config) => {
        // Disable HTML Generation.
        config.plugins.delete("html");
        config.plugins.delete("preload");
        config.plugins.delete("prefetch");

        // Copy all contents of the "static" src directory
        // to the destination's root.
        config.plugin("copy").use(CopyPlugin, [
          {
            patterns: [{ from: "./static", to: "./" }],
          },
        ]);
    },
};

npm 脚本:

vue-cli-service build --target lib --inline-vue --name app src/main.js

注意:“inline-vue”参数强制将 Vue 嵌入到包中(参见https://cli.vuejs.org/guide/build-targets.html#library)。

于 2021-08-13T07:50:23.813 回答