我的项目中有一个名为test.js
的文件
我没有在任何地方导入/需要它,这意味着我的webpack不会为该js文件调用babel-loader
。
问题:我想要的是将test.js移动到/dist文件夹中,但作为一个已编译/已转换的文件。最佳实践是什么?
**我尝试过的:**我尝试过使用copy-webpack-plugin并在复制文件之前使用它的转换参数,但是我似乎找不到好的babel包。
{
from: 'test.js',
to: '/dist/test.js',
transform(content, path) {
//what do I write here?
},
}
2条答案
按热度按时间nhaq1z211#
我能想到的最简单的方法是使用几个入口点,如下所示:
这将创建您的
a.js
主包和__dirname/dist
文件夹中的b.js
文件,如果您使用相应的加载程序,这两个文件都将被传输。从copy-webpack-plugin文档section中可以看到:
webpack-copy-plugin并非设计用于复制构建过程中生成的文件;而是复制源代码树中已经存在的文件,作为构建过程的一部分。
所以它似乎很难(如果可能的话)使它移动transpiled文件。
更新。如果您想将文件输出到不同的文件夹而不更改您的
src
文件夹,需要其他工具。对于您的情况(只有1个文件),我会编写一个简单的脚本,并将其添加到package.json脚本部分,并结合webpack调用,如:0g0grzrc2#
就像前面的答案一样,最初我使用
package.json
中的“scripts”条目来运行babel
。但是由于一些原因,我想使用webpack 5
来完成这项工作。因此,在webpack-copy-plugin
失败后,我进行了大量的挖掘,我使用了this solution:而且它做得很好,但要注意,它需要使用两个插件的补丁版本(除非补丁已经合并)!