javascript - 无法将我的应用程序部署到 Heroku。错误:clean-webpack-plugin 只接受一个选项对象
问题描述
const path = require("path");
const webpackMerge = require("webpack-merge");
const autoprefixer = require("autoprefixer");
const webpackCommon = require("./common.config");
// webpack plugins
const HtmlWebpackPlugin = require("html-webpack-plugin");
const DefinePlugin = require("webpack/lib/DefinePlugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");
module.exports = webpackMerge(webpackCommon, {
bail: true,
devtool: "source-map",
mode: "production",
output: {
path: path.resolve(__dirname, "../dist"),
filename: "[name]-[hash].min.js",
sourceMapFilename: "[name]-[hash].map",
chunkFilename: "[id]-[chunkhash].js",
publicPath: "/"
},
module: {
rules: [
{
test: /\.s?css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
sourceMap: true,
importLoaders: 2
}
},
{
loader: "postcss-loader",
options: {
config: {
path: path.resolve(__dirname, "postcss.config.js")
},
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
outputStyle: "expanded",
sourceMap: true,
sourceMapContents: true
}
}
]
})
}
]
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(__dirname, "../static/index.html"),
favicon: path.resolve(__dirname, "../static/favicon.ico"),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
new CopyWebpackPlugin([{ from: path.resolve(__dirname, "../static") }], {
ignore: ["index.html", "favicon.ico"]
}),
new CleanWebpackPlugin(['dist'], {
root: path.resolve(__dirname, '..'),
exclude: '.gitignore'
}),
new DefinePlugin({
"process.env": {
NODE_ENV: '"production"'
}
}),
new ExtractTextPlugin("[name]-[chunkhash].min.css"),
new UglifyJsPlugin({
uglifyOptions: {
compress: {
ie8: true,
warnings: false
},
mangle: {
ie8: true
},
output: {
comments: false,
ie8: true
}
},
sourceMap: true
}),
new LoaderOptionsPlugin({
options: {
context: "/",
sassLoader: {
includePaths: [path.resolve(__dirname, "../src")]
}
}
})
]
});
无法将我的应用程序部署到 Heroku。错误:clean-webpack-plugin 只接受选项对象。有什么想法可以帮助我解决这个错误吗?我检查了 clean-webpack-plugin 文档更新了一些部分,但仍然是同样的错误。
解决方案
看看 clean-webpack-plugin 可选配置。您的 CleanWebpackPlugin 抛出此错误:
constructor(options: Options = {}) {
if (isPlainObject(options) === false) {
throw new Error(`clean-webpack-plugin
// only accepts an options object. See:
// https://github.com/johnagan/clean-
// webpack-plugin#options-and-defaults-
// optional`);
}
}
您是否尝试将 CleanWebpackPlugin 选项添加到 webpack.config 文件?像这样的东西:
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpackConfig = {
plugins: [
/**
* All files inside webpack's output.path
directory will be removed once, but the
* directory itself will not be. If using
webpack 4+'s default configuration,
* everything under <PROJECT_DIR>/dist/
will be removed.
* Use cleanOnceBeforeBuildPatterns to override
this behavior.
*
* During rebuilds, all webpack assets that are
not used anymore
* will be removed automatically.
*
* See `Options and Defaults` for information
*/
new CleanWebpackPlugin(),
],
};
module.exports = webpackConfig;
如果一切都失败了,请查看 CleanWebpackPlugin 的完整选项/默认值。我认为您可以删除 [dist] 并简单地传入配置对象:https ://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional
我认为您的这段代码可能有问题。尝试不使用 ['dist']:
new CleanWebpackPlugin(['dist'], {
root: path.resolve(__dirname, '..'),
exclude: '.gitignore'
}),
我没有使用过 webpack 合并,所以我要补充一点,你应该确保你有正确的依赖项,安装,以及是否有任何冲突。
需要注意的是:使用零配置,clean-webpack-plugin 将删除您指定的路径目录中的文件。
推荐阅读
- python - 状态字段的 Django 查询集
- types - 如何在 Rust 中惯用地将 bool 转换为 Option 或 Result?
- php - 使用 apache 端口 8080 在 xampp 上为 zendframework 设置虚拟主机
- python - 如何从与被测类相同的模块中修补类
- python - Pymongo 在多个集合上选择值,其中条件和按日期分组
- c# - 尝试截屏时活动为空
- node.js - Express.js、Jest、Supertest:测试运行程序无法找到路由处理程序
- c# - ASP.NET MVC WebAPI JsonContent 返回流不是 WebServer 预期的 JSON
- java - 无法在 Spring Boot 中反序列化嵌套对象“Role”
- php - 使用 MySql 连接登录 PHP