嗨,我正在尝试使用define插件,以便我可以更新版本号以确保发布新版本后JS刷新。我似乎无法让DefinePlugin正常工作。我在webpack文件夹中看到了它,我正在尝试按照文档进行操作,但出现错误,但找不到该错误。这是我的配置:
const path = require('path'),
settings = require('./settings');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
module.exports = {
entry: {
'scrollerbundled': [settings.themeLocation + "js/scroller.js"],
'mapbundled': [settings.themeLocation + "js/shopmap.js"],
'sculptor': [settings.themeLocation + "js/sculptor.js"]
},
output: {
path: path.resolve(__dirname, settings.themeLocation + "js-dist"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
],
plugins: [new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(true),
VERSION: JSON.stringify('5fa3b9'),
})]
},
optimization: {
minimizer: [new UglifyJsPlugin({
uglifyOptions: {
mangle: true,
output: {
comments: false
}
}
})]
},
mode: 'production'
}
答案 0 :(得分:1)
{
"parser": "babel-eslint",
"extends": [
"airbnb",
"plugin:react/recommended",
"prettier",
"prettier/react"
],
"plugins": ["react", "import", "prettier"],
"env": {
"browser": true
},
"settings": {
"import/resolver": {
"webpack": {
"config": "webpack.dev.js"
}
}
}
}
那是我的eslintrc。这用于在您的webpack配置中使用模块别名创建的绝对导入。您需要安装eslint-import-resolver-webpack
答案 1 :(得分:0)
我有“ webpack”:“ ^ 4.28.4”并在webpack配置中定义
new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(true),
});
如果您管理该变量,则找不到它。我有条件地使用
if (PRODUCTION) {
//do stuff
}
另一种情况是在对象中设置全局变量并与webpack共享。
这是一个例子
new webpack.ProvidePlugin({
CONFIG: path.resolve(__dirname, './CONSTS.js')
}),
// the path is src/CONST.JS
在eslintrc文件中,您可以添加该变量以避免导入错误。
"settings": {
"import/resolver": {
"webpack": {
"config": "webpack.dev.js"
}
}
}
然后在任何文件中,您都可以使用'CONFIG'中的import {value}
答案 2 :(得分:0)
如果您使用的是laravel mix,则可以将new webpack.DefinePlugin
代码放入plugins
块的.webpackConfig
数组中:
webpack.mix.js:
mix
.webpackConfig({
devtool: 'source-map',
resolve: {
alias: {
'sass': path.resolve('resources/sass'),
}
},
plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill', // <--------------------- this right here
__VERSION__: JSON.stringify('12345')
})
]
})
.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.copy([
'resources/fonts/*',
], 'public/fonts');
通过推断,这意味着您还可以将此代码添加到常规(不是laravel混合)webpack配置中的类似块中。