开发时如何配置webpack不压缩源码?
How to configure Webpack to not compress source code when developing?
我正在使用 Webpack v4.4.0
打包我的源代码,Node 版本是 v15.14.0
,并像这样解析 JavaScript 个文件:
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
]
现在我希望 Webpack 不压缩我的源代码以便于阅读,因为我想分析运行时代码以在开发时解决问题。当我部署到生产环境时,让它压缩源代码。可能吗?这是现在的完整 webpack.config.js
:
const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
module.exports = {
entry : {
bg : './src/background-scripts/' ,
content : ['./src/content-scripts/firefox-fix.js', './src/content-scripts/'] ,
options : [
'./src/options/'
],
popup : './src/popup/' ,
'bs-lite' : './src/public/bootstrap-lite.scss'
} ,
output : {
path : path.resolve(__dirname, '../src/bundle') ,
filename : '[name].js'
} ,
module : {
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
{
test : /\.woff$/ ,
loader : 'file-loader' ,
query : {
name : '[name].[ext]'
}
} ,
{
test : /\.html$/ ,
loader : 'vue-html-loader'
} ,
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test : /\.(scss)$/ ,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
sourceMap: true
}
},
"sass-loader"
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
commons1: {
name: 'commons1',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(popup|content)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons2: {
name: 'commons2',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(options|commons1)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons3: {
name: 'commons3',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(bg|commons2)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
}
}
},
plugins : [
new MiniCssExtractPlugin()
]
};
我应该怎么做才能保留来源?我试过:
devtool: 'source-map'
但是没有用。
我没有看到您指定 mode
, 应该是 production
(默认压缩资产的默认值)或 development
。设置它并像这样添加 devtool
(我在触摸的地方添加了评论):
const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
module.exports = {
// line I added
mode:"development"
entry : {
bg : './src/background-scripts/' ,
content : ['./src/content-scripts/firefox-fix.js', './src/content-scripts/'] ,
options : [
'./src/options/'
],
popup : './src/popup/' ,
'bs-lite' : './src/public/bootstrap-lite.scss'
} ,
output : {
path : path.resolve(__dirname, '../src/bundle') ,
filename : '[name].js'
} ,
// line I added
devtool:"source-map",
module : {
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
{
test : /\.woff$/ ,
loader : 'file-loader' ,
query : {
name : '[name].[ext]'
}
} ,
{
test : /\.html$/ ,
loader : 'vue-html-loader'
} ,
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test : /\.(scss)$/ ,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
sourceMap: true
}
},
"sass-loader"
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
commons1: {
name: 'commons1',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(popup|content)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons2: {
name: 'commons2',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(options|commons1)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons3: {
name: 'commons3',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(bg|commons2)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
}
}
},
plugins : [
new MiniCssExtractPlugin()
]
};
我正在使用 Webpack v4.4.0
打包我的源代码,Node 版本是 v15.14.0
,并像这样解析 JavaScript 个文件:
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
]
现在我希望 Webpack 不压缩我的源代码以便于阅读,因为我想分析运行时代码以在开发时解决问题。当我部署到生产环境时,让它压缩源代码。可能吗?这是现在的完整 webpack.config.js
:
const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
module.exports = {
entry : {
bg : './src/background-scripts/' ,
content : ['./src/content-scripts/firefox-fix.js', './src/content-scripts/'] ,
options : [
'./src/options/'
],
popup : './src/popup/' ,
'bs-lite' : './src/public/bootstrap-lite.scss'
} ,
output : {
path : path.resolve(__dirname, '../src/bundle') ,
filename : '[name].js'
} ,
module : {
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
{
test : /\.woff$/ ,
loader : 'file-loader' ,
query : {
name : '[name].[ext]'
}
} ,
{
test : /\.html$/ ,
loader : 'vue-html-loader'
} ,
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test : /\.(scss)$/ ,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
sourceMap: true
}
},
"sass-loader"
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
commons1: {
name: 'commons1',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(popup|content)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons2: {
name: 'commons2',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(options|commons1)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons3: {
name: 'commons3',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(bg|commons2)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
}
}
},
plugins : [
new MiniCssExtractPlugin()
]
};
我应该怎么做才能保留来源?我试过:
devtool: 'source-map'
但是没有用。
我没有看到您指定 mode
, 应该是 production
(默认压缩资产的默认值)或 development
。设置它并像这样添加 devtool
(我在触摸的地方添加了评论):
const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
module.exports = {
// line I added
mode:"development"
entry : {
bg : './src/background-scripts/' ,
content : ['./src/content-scripts/firefox-fix.js', './src/content-scripts/'] ,
options : [
'./src/options/'
],
popup : './src/popup/' ,
'bs-lite' : './src/public/bootstrap-lite.scss'
} ,
output : {
path : path.resolve(__dirname, '../src/bundle') ,
filename : '[name].js'
} ,
// line I added
devtool:"source-map",
module : {
rules : [
{
test : /\.js$/ ,
exclude : [ /node_modules(?!(\/|\?\)(translation\.js|selection-widget|connect\.io|chrome-env))/ ] ,
loader : 'babel-loader'
},
{
test : /\.woff$/ ,
loader : 'file-loader' ,
query : {
name : '[name].[ext]'
}
} ,
{
test : /\.html$/ ,
loader : 'vue-html-loader'
} ,
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test : /\.(scss)$/ ,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: true,
sourceMap: true
}
},
"sass-loader"
]
}
]
},
optimization: {
splitChunks: {
cacheGroups: {
commons1: {
name: 'commons1',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(popup|content)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons2: {
name: 'commons2',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(options|commons1)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
commons3: {
name: 'commons3',
chunks: 'all',
minChunks: 1,
test(module,chunks){
for (const chunk of module.chunksIterable) {
if (chunk.name && /(bg|commons2)/.test(chunk.name)) {
return true;
}
}
return false;
}
},
}
}
},
plugins : [
new MiniCssExtractPlugin()
]
};