你如何在 webpack4/copy-webpack-plugin 中修复 "ERROR in Path must be a string. Received undefined"
How do you fix "ERROR in Path must be a string. Received undefined" in webpack4/copy-webpack-plugin
我想要实现的是创建一个单独的 webpack 资产文件,用于将静态资产从 src
文件夹复制到 web
文件夹。
node version: 8.15.0
yarn version: 1.13.0
webpack: 4.19.1
copy-webpack-plugin: 6.0.0
首先,我已经有一个处理所有 js 文件的 webpack.common.js 文件,并且我已经创建了资产文件,如下所示。
当我运行
webpack --config=webpack/webpack.assets.js --mode development --progress --color
或
webpack --config=webpack/webpack.config.js --config=webpack/webpack.assets.js --mode development --progress --color --env development
我收到此错误 ERROR in Path must be a string. Received undefined
,但我无法弄清楚它的来源。
顺便说一句,我最近才开始接触 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'
}
])
]
});
};
我希望能够 运行 assets 命令没有错误,因为实际文件已正确复制。
非常感谢任何想法!
您已将选项对象作为第二个模式传递。
将其移出模式数组并将其作为第二个参数传递:
plugins: [
new CopyPlugin(
[
{
from: PATHS.src,
to: PATHS.dist,
force: true,
toType: 'dir'
}
],
{
copyUnmodified: true,
debug: 'debug'
}
)
]
您收到错误消息是因为您的选项对象被视为模式但没有 from
属性.
我想要实现的是创建一个单独的 webpack 资产文件,用于将静态资产从 src
文件夹复制到 web
文件夹。
node version: 8.15.0
yarn version: 1.13.0
webpack: 4.19.1
copy-webpack-plugin: 6.0.0
首先,我已经有一个处理所有 js 文件的 webpack.common.js 文件,并且我已经创建了资产文件,如下所示。
当我运行
webpack --config=webpack/webpack.assets.js --mode development --progress --color
或
webpack --config=webpack/webpack.config.js --config=webpack/webpack.assets.js --mode development --progress --color --env development
我收到此错误 ERROR in Path must be a string. Received undefined
,但我无法弄清楚它的来源。
顺便说一句,我最近才开始接触 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'
}
])
]
});
};
我希望能够 运行 assets 命令没有错误,因为实际文件已正确复制。
非常感谢任何想法!
您已将选项对象作为第二个模式传递。
将其移出模式数组并将其作为第二个参数传递:
plugins: [
new CopyPlugin(
[
{
from: PATHS.src,
to: PATHS.dist,
force: true,
toType: 'dir'
}
],
{
copyUnmodified: true,
debug: 'debug'
}
)
]
您收到错误消息是因为您的选项对象被视为模式但没有 from
属性.