我刚刚开始学习Webpack来管理项目中的依赖项。我正在尝试使用它为我的打字稿和javascript文件构建捆绑包。对于打字稿文件,我正在使用ts-loader
插件进行处理。对于CSS,我正在使用mini-css-extract
和optimize-css-assets
插件。当我尝试运行webpack时,出现以下错误,并且我无法找出可能导致此错误的原因。
user@system spl % npm run build
> spl@1.0.0 build /Users/user/Downloads/spl
> webpack --config webpack.config.js
/Users/user/Downloads/spl/node_modules/neo-async/async.js:16
throw new Error('Callback was already called.');
^
Error: Callback was already called.
at throwError (/Users/user/Downloads/spl/node_modules/neo-async/async.js:16:11)
at /Users/user/Downloads/spl/node_modules/neo-async/async.js:2818:7
at processTicksAndRejections (internal/process/task_queues.js:79:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! spl@1.0.0 build: `webpack --config webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the spl@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/user/.npm/_logs/2020-05-14T14_23_32_985Z-debug.log
以下是我用来构建dist文件的webpack.config文件。
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
module.exports = {
mode: 'production',
entry: "./static/js/index.js",
output: {
filename: "bundle.[contentHash].js", // File name with hash, based on content
path: path.resolve(__dirname, 'dist')
},
optimization: {
minimizer: [
new OptimizeCssAssetsPlugin(),
new TerserPlugin(),
new HtmlWebpackPlugin({
template: "./static/index.html",
minify: {
removeAttributeQuotes: true,
collapseWhitespace: true,
removeComments: true
}
})
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].[contentHash].css"
}),
new CleanWebpackPlugin(),
],
module: {
rules: [
{
test: /\.html$/,
use: [ "html-loader" ]
},
{
test: /\.[tj]s$/,
use: "ts-loader",
exclude: /(node_modules|tests)/
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
],
},
resolve: {
alias: {
src: path.resolve(__dirname, 'src'),
static: path.resolve(__dirname, 'static'),
},
extensions: [ '.ts', '.js' ]
}
}
答案 0 :(得分:2)
我遇到了同样的问题,并且意识到我正在从index.html
文件中导入CSS文件:
<link rel="stylesheet" href="./css/main.css">
尽管应该使用index.js
从条目文件(import
)中导入CSS文件:
import '../css/main.css';
所以我删除了行<link rel="stylesheet" href="./css/main.css">
并解决了问题。
您可以查看HTML文件,并检查是否有从HTML文件导入的资产。希望对您有所帮助。
答案 1 :(得分:1)
tl;dr: 将 webpack 升级到新版本为我解决了这个问题。
我进入了 node_modules/neo-async/async.js
并修改了 onlyOnce
以便它提供更具描述性的堆栈跟踪,如下所示:
/**
* @private
* @param {Function} func
*/
function onlyOnce(func) {
const defined = new Error('onlyOnce first used here')
return function(err, res) {
var fn = func;
func = () => {
console.error(defined);
throwError();
};
fn(err, res);
};
}
堆栈跟踪指向 webpack 的内部代码,当我升级到最新版本时,它解决了这个问题。