webpack v4
安装
安装node:https://nodejs.org/zh-cn/ -查看node版本:node --version安装npm:node安装后自动安装npm -查看npm版本:npm --version -运行依赖:--save -开发依赖:-dev -全局安装:-g -查询全局安装过的包:npm list -g --depth 0 -查询项目安装过的包:npm list --depth 0配置package.json(手动或npm init)安装包: -全局安装webpack: npm install --save-dev -g webpack webpack-cli -项目安装webpack: npm install --save-dev webpack webpack-cli
js压缩
文件引入 import otherjs from "./other.js";配置webpack.config.js
const {resolve} = require('path');
module.exports = {
entry: './entry.js',
output: {
filename: 'output1.js',
path: resolve(__dirname, 'output')
},
mode: 'production',
};
打包:webpack
js语法检查
安装包: npm install --save-dev eslint npm install --save-dev eslint-loader npm install --save-dev eslint-config-airbnb-base npm install --save-dev eslint-plugin-import配置webpack.config.js
const {resolve} = require('path');
module.exports = {
entry: './entry.js',
output: {
filename: 'output.js',
path: resolve(__dirname, 'output')
},
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
loader: 'eslint-loader',
options: {
fix: true
}
}
]
}
};
配置package.json
"eslintConfig": {
"extends": "airbnb-base"
}
打包:webpack
js语法转化(加载ES2015 +代码并转换为ES5)
安装包: npm install --save-dev babel-loader @babel/core @babel/preset-env配置webpack.config.js
const {resolve} = require('path');
module.exports = {
entry: './entry.js',
output: {
filename: 'output.js',
path: resolve(__dirname, 'output')
},
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};
打包:webpack
处理less
文件引入 import less from "./less.less";安装包: npm install --save-dev css-loader style-loader less-loader less配置webpack.config.js
const {resolve} = require('path');
module.exports = {
entry: './entry.js',
output: {
filename: 'output.js',
path: resolve(__dirname, 'output')
},
mode: 'development',
module: {
rules: [
{
test: /\.less$/,
use: [{
loader: "style-loader"
},
{
loader: "css-loader"
},
{
loader: "less-loader"
}]
}
]
}
};
打包:webpack
处理图片
文件import引入、css中背景图片引入 import image from "./image.jpg"; .bg{background:url(./image.jpg);}安装包: npm install --save-dev file-loader url-loader配置webpack.config.js
const {resolve} = require('path');
module.exports = {
entry: './entry.js',
output: {
filename: 'output.js',
path: resolve(__dirname, 'output')
},
mode: 'development',
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images/',
publicPath: '../dist/images/',
limit: 8192
}
}
]
}
]
}
};
打包:webpack
转载请注明原文地址:https://ipadbbs.8miu.com/read-148.html