[英]How do you fix “ERROR in Path must be a string. Received undefined” in webpack4/copy-webpack-plugin
What I'm trying to achieve is to create a separate webpack assets file for copying static assets from the src
folder to the web
folder. 我要实现的目标是创建一个单独的webpack资产文件,以将静态资产从
src
文件夹复制到web
文件夹。
node version: 8.15.0
yarn version: 1.13.0
webpack: 4.19.1
copy-webpack-plugin: 6.0.0
To start, I already have a webpack.common.js file which deals with all the js files, and I have created the assets file, which can be seen below. 首先,我已经有一个处理所有js文件的webpack.common.js文件,并且我已经创建了资产文件,如下所示。
When I run 当我跑步
webpack --config=webpack/webpack.assets.js --mode development --progress --color
or 要么
webpack --config=webpack/webpack.config.js --config=webpack/webpack.assets.js --mode development --progress --color --env development
I get this error ERROR in Path must be a string. Received undefined
我收到此错误
ERROR in Path must be a string. Received undefined
错误ERROR in Path must be a string. Received undefined
ERROR in Path must be a string. Received undefined
and I can't figure it out where it comes from. ERROR in Path must be a string. Received undefined
,我无法弄清楚它的来源。
By the way I just started dealing with webpack recently. 顺便说一下,我最近才刚开始处理webpack。
webpack.common.js
const webpack = require('webpack');
const path = require('path');
const PATHS = {
src: path.join(process.cwd(), 'src', 'js'),
dist: path.join(process.cwd(), 'web', 'js')
};
module.exports = {
entry: {
homepage: path.resolve(PATHS.src, 'pages/homepage.js'),
otherfile: path.resolve(PATHS.src, 'pages/othefile.js'),
}
output: {
path: PATHS.dist,
filename: '[name].js',
chunkFilename: '[name].js',
publicPath: '/js/'
},
...
}
webpack.assets.js
const webpack = require('webpack');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const PATHS = {
src: path.join(process.cwd(), 'src', 'svg'),
dist: path.join(process.cwd(), 'web', 'svg')
};
module.exports = (env) => {
const svgFormat = env === 'production' ? '[name].[hash].[ext]' : '[name].[ext]';
return merge(commmonConfig, {
entry: [
path.resolve(PATHS.src, 'logo1.svg'),
path.resolve(PATHS.src, 'logo2.svg')
],
output: {
path: PATHS.dist
},
module: {
rules: [
{
test: /\.(svg)$/,
use: [
{
loader: 'file-loader',
options: {
name: svgFormat,
},
},
],
},
]
},
plugins: [
new CopyPlugin([
{
from: PATHS.src,
to: PATHS.dist,
force: true,
toType: 'dir'
},
{
copyUnmodified: true,
debug: 'debug'
}
])
]
});
};
What I would like is to be able to run the assets commands with no errors, as the actual files get copied correctly. 我想要的是能够正确运行资产命令,因为可以正确复制实际文件。
Any ideas are very much appreciated! 任何想法都非常感谢!
You have passed your options object as a second pattern. 您已将options对象作为第二种模式传递。
Move it outside of the patterns array and pass it as the second parameter instead: 将其移出patterns数组之外,并将其作为第二个参数传递:
plugins: [
new CopyPlugin(
[
{
from: PATHS.src,
to: PATHS.dist,
force: true,
toType: 'dir'
}
],
{
copyUnmodified: true,
debug: 'debug'
}
)
]
You get the error as your options object is being treated as a pattern but does not have a from
property. 由于选项对象被视为模式但没有
from
属性,因此会出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.