首页 >> js开发 >> jswebpack 如何同时输出压缩和未压缩的文件的实现步骤js大全
jswebpack 如何同时输出压缩和未压缩的文件的实现步骤js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
有的时候我们想要同时生成压缩和未压缩的文件,比如我们构建 lib 包的时候,我们希望用户能够使用压缩过后的代码文件作为 cdn 文件,最简单的一个方式就是通过指定环境变量,比如指定 MINIFY,如下:
const path = require('path')
const isMinify = process.env.MINIFY
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js'
},
output: {
filename: isMinify ? '[name].min.js' : '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: isMinify ? true : false
}
}
module.exports = config
const path = require('path')
const isMinify = process.env.MINIFY
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js'
},
output: {
filename: isMinify ? '[name].min.js' : '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: isMinify ? true : false
}
}
module.exports = config我们在使用的时候通过指定环境变量就可以打包成不同的版本:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:min": "MINIFY=1 webpack --config=webpack.config.js",
"build": "webpack --config=webpack.config.js"
}
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:min": "MINIFY=1 webpack --config=webpack.config.js",
"build": "webpack --config=webpack.config.js"
}不过这样的缺点就是我们需要运行两次。第二个方法是安装unminified-webpack-plugin,通过这个插件可以生成没有压缩的文件:unminified-webpack-plugin
const path = require('path')
const UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
},
output: {
filename: '[name].min.js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
plugins: [
new UnminifiedWebpackPlugin({})
]
}
module.exports = config
const path = require('path')
const UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
},
output: {
filename: '[name].min.js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
plugins: [
new UnminifiedWebpackPlugin({})
]
}
module.exports = config不过这个有个缺点就是未压缩的文件没有 sourcemap。第三种方法通过指定多个打包入口,然后手动指定压缩插件(uglifyjs、terser等)压缩哪些文件,如我们指定 index.min.js 这个文件才需要压缩,配置如下:index.min.js
const path = require('path')
const TerserWebpackPlugin = require('terser-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
'index.min': './src/index.js'
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin({
include: /min/,
sourceMap: true
})
]
}
}
module.exports = config
const path = require('path')
const TerserWebpackPlugin = require('terser-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
'index.min': './src/index.js'
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin({
include: /min/,
sourceMap: true
})
]
}
}
module.exports = config关键点如下:这个时候生成的就完美了。
const path = require('path')
const isMinify = process.env.MINIFY
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js'
},
output: {
filename: isMinify ? '[name].min.js' : '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: isMinify ? true : false
}
}
module.exports = config
const path = require('path')
const isMinify = process.env.MINIFY
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js'
},
output: {
filename: isMinify ? '[name].min.js' : '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: isMinify ? true : false
}
}
module.exports = config我们在使用的时候通过指定环境变量就可以打包成不同的版本:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:min": "MINIFY=1 webpack --config=webpack.config.js",
"build": "webpack --config=webpack.config.js"
}
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:min": "MINIFY=1 webpack --config=webpack.config.js",
"build": "webpack --config=webpack.config.js"
}不过这样的缺点就是我们需要运行两次。第二个方法是安装unminified-webpack-plugin,通过这个插件可以生成没有压缩的文件:unminified-webpack-plugin
const path = require('path')
const UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
},
output: {
filename: '[name].min.js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
plugins: [
new UnminifiedWebpackPlugin({})
]
}
module.exports = config
const path = require('path')
const UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
},
output: {
filename: '[name].min.js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
plugins: [
new UnminifiedWebpackPlugin({})
]
}
module.exports = config不过这个有个缺点就是未压缩的文件没有 sourcemap。第三种方法通过指定多个打包入口,然后手动指定压缩插件(uglifyjs、terser等)压缩哪些文件,如我们指定 index.min.js 这个文件才需要压缩,配置如下:index.min.js
const path = require('path')
const TerserWebpackPlugin = require('terser-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
'index.min': './src/index.js'
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin({
include: /min/,
sourceMap: true
})
]
}
}
module.exports = config
const path = require('path')
const TerserWebpackPlugin = require('terser-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
entry: {
index: './src/index.js',
'index.min': './src/index.js'
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
devtool: 'cheap-source-map',
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin({
include: /min/,
sourceMap: true
})
]
}
}
module.exports = config关键点如下:这个时候生成的就完美了。
相关文章:
- js解决vue的router组件component在import时不能使用变量问题js大全
- jsjquery实现简单拖拽效果js大全
- js解决Vue使用bus总线时,第一次路由跳转时数据没成功传递问题js大全
- js解决vue中的无限循环问题js大全
- jsvue 监听窗口变化对页面部分元素重新渲染操作js大全
- jsvue键盘事件点击事件加native操作js大全
- jsvue+element-ui JYAdmin后台管理系统模板解析js大全
- jsJS的时间格式化和时间戳转换函数示例详解js大全
- jsJS实现audio音频剪裁剪切复制播放与上传(步骤详解)js大全
- js代码处理JavaScript值为undefined的7个小技巧