Node.js/webpack:修订间差异
来自康健生活
< Node.js
| 第311行: | 第311行: | ||
* Minifiers(UglifyJS) | * Minifiers(UglifyJS) | ||
== 硬编码 == | == 硬编码 == | ||
== webpack-dev-server == | |||
== ERROR == | == ERROR == | ||
2020年6月29日 (一) 15:55的版本
引言
npm install -g webpack
webpack-cli
sudo npm install webpack-cli
命令行
[jian@kang /home/kwy]$
webpack --config webpack.config.js
Hash: 167bbdb89a5dbf82c4d0
Version: webpack 4.43.0
Child
Hash: 167bbdb89a5dbf82c4d0
Time: 151ms
Built at: 2020-06-28 10:07:55 ├F10: PM┤
Asset Size Chunks Chunk Names
index.167bbdb8.js 2.11 KiB 0, 1, 2 [emitted] [immutable] index
info.167bbdb8.js 2.11 KiB 1, 0, 2 [emitted] [immutable] info
list.167bbdb8.js 2.11 KiB 2, 0, 1 [emitted] [immutable] list
Entrypoint index = index.167bbdb8.js
Entrypoint info = info.167bbdb8.js
Entrypoint list = list.167bbdb8.js
[0] ./public/js/index.js 3.74 KiB {0} {1} {2} [built]
webpack-dev-server
配置文件
webpack.config.js
module.exports={ //入口文件的配置项 entry:{}, //出口文件的配置项 output:{}, //模块:例如解读CSS,图片如何转换,压缩 module:{}, //插件,用于生产模版和各项功能 plugins:[], //配置webpack开发服务功能 devServer:{} }
示例
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const webpack = require('webpack'); // 用于访问内置插件
var serverConfig = {
target: 'node',
mode: 'production',
//入口文件的配置项
entry: {
index: './public/js/index.js',
info: './public/js/index.js',
list: './public/js/index.js'
},
//出口文件的配置项
output: {
path: path.resolve(__dirname, 'public/dist/js'),
filename: '[name].[hash:8].js'
},
//模块:例如解读CSS,图片如何转换,压缩
module: {
rules: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true
}
},
{ test: /\.ts$/, use: 'ts-loader' },
{
test: /\.js$/,
use: {
loader: 'raw-loader'
},
exclude: '/node_modules/'
},
{
test: /\.(js|jsx)$/,
use: 'babel-loader'
}
]
},
//插件,用于生产模版和各项功能
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
],
//配置webpack开发服务功能
devServer: {}
};
var clientConfig = {
target: 'web', // <=== 默认是 'web',可省略
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'lib.js'
}
//…
};
module.exports = [ serverConfig, clientConfig ];
ES6、7
npm i --save-dev babel-preset-env @babel/core babel-loader
webpack.config.js
{
module: {
rules: [
{
test: /\.js$/, // 值一个正则,符合这些正则的资源会用一个loade来处理
use: {
loader: 'babel-loader', // 使用bable-loader来处理
options: { // 指定参数
presets: [
['babel-preset-env', {
targets: {
browsers: ['> 1%', 'last 2 version'] //具体可以去babel-preset里面查看
}
}]
] // 指定哪些语法编译
}
},
exclude: '/node_module/' // 排除在外
}
]
},
}
.babelrc
{
"presets": [
["babel-preset-env", {
"targets": {
"browsers": ["> 1%", "last 2 version"]
}
}]
],
"plugins": ["transform-runtime"]
}
组件
| html-webpack-plugin | SplitChunksPlugin | HashedModuleIdsPlugin |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
babel-cli
npm install babel-cli -g
babel-preset-env
npm install babel-preset-env --save-dev
loader
style-loader
将css-loader打包好的css代码以<style>标签的形式插入到html文件中。 功能 webpack 的服务器热加载服务进行特殊配置。 对css文件二次处理(更改类名,添加额外css属性之类)。 合并 <style> 标签(默认是不合并的)。 启用sourceMap等。 路径转换(相对路径转为绝对路径)。 给 <style> 标签添加自定义属性。 手动挂载、移除 <style> 标签等。 属性:
| attrs | {Object} | {} | 添加自定义 attrs 到 style 标签 | attrs: {
id: 'foo'
}
|
| transform | {Function} | false | 转换/条件加载 CSS,通过传递转换/条件函数 | transform: 'transform.js'
transform.js // 这里只有一个参数,即css字符串
module.exports = function (css) {
console.log(css)
const transformed = css.replace(/}/g, 'box-sizing: border-box;\n}')
return transformed
}
|
| insertAt | Object} | bottom | 给定位置处插入style标签 | |
| insertInto | {String} | 给定位置中插入style标签 | ||
| sourceMap | {Boolean} | false | 启用/禁用 Sourcemap | |
| convertToAbsoluteUrls | {Boolean} | false | 启用 source map 后,将相对 URL 转换为绝对 URL | |
| useable | {Object} | {} | ||
| singleton | {Boolean} | true | ||
| hmr | {Boolean} | true | ||
| base | {Number} | true | 设置模块 ID 基础 (DLLPlugin) |
css-loader
vue-loader
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
compilerOptions: {
preserveWhitespace: false
}
}
}
合并JS
entry:"./src/index.js"
entry:["./src/main.js","./src/main.js"]
entry:{
index:["./src/index.js","./src/main.js"]
}
合并CSS
npm install style-loader css-loader --save-dev
webpack.config.js
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader:"style-loader" },
{ loader:"css-loader" }
]
}
]
}
extract-text-webpack-plugin
npm install --save-dev extract-text-webpack-plugin@next
mini-css-extract-plugin
mini-css-extract-plugin 在 webpack4 中代替 extract-text-webpack-plugin
optimize-css-assets-webpack-plugin
uglifyjs-webpack-plugin
UglifyJS Webpack Plugin插件用来缩小(压缩优化)js文件,至少需要Node v6.9.0和Webpack v4.0.0版本。
webpack 4之前的版本是通过webpack.optimize.CommonsChunkPlugin来压缩js,webpack 4版本之后被移除了,使用config.optimization.splitChunks来代替。
图片
postcss
- autoprefixer是PostCSS最著名的一款插件,就不过多介绍了,相信同学们都使用过。
- postcss-cssnext (内置autoprefixer) 允许你使用未来的css语法,如css4(可以理解为css中的Babel)
- postcss-sprites 自动制作雪碧图,不用手动拼接啦,哈哈哈
- cssnano 压缩css代码(如果你是用webpack的话,css-loader集成了cssnano,你不需要再次引入)
- postcss-hash-classname 把转换后的css文件名附上哈希值
- pixrem 将rem转换为px
- postcss-px-to-viewport 将px转换为vh和vw(推荐作为移动端的计量单位,而不是rem)
- postcss-pxtorem 将px转换为rem
webpack-bundle-analyzer
commonJS、AMD
(来源摘自互联网示例,作者 博客园-二婷啵)
devtool
quality
Source Map
一般与下列类型的预处理器搭配使用:
- 转译器(Babel)
- 编译器(TypeScript)
- Minifiers(UglifyJS)
硬编码
webpack-dev-server
ERROR
- ReferenceError: _dirname is not defined
- FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
- Module build failed: ReferenceError: document is not defined
- Module parse failed: Unexpected character '�' (1:0)
No categories