上星期写了篇vue-cli下生成的webpack简单项目,关于模块和组件的代码分割式的项目开发,今天就总结下慕课网推出的纯webpack打包的一些细节,包括webpack1和2的一些不同和webpack配置文件的手动编写以及所有打包的东西如何浓缩成一个js文件
项目文件夹:
根目录:
src下的目录:
dist下打包后生成的目录:
下面是详细的流程步骤:
1、建立项目名
mkdir 项目名
2、定位项目名里面
cd 项目名
3、生成package.json文件
npm init --yes
4、安装webpack( 不加版本号默认为2.0以上 )
cnpm install webpack --save-dev
5、生成依赖包,即node_modules目录 ( 生成一小部分,后面需安装各种包,不像vue-cli本身就集成很多 )
cnpm install
6、建立src和dist文件夹,src目录下建styles和scripts( 可省略此步,手动新建 )
mkdir src mkdir dist ……
7、根目录下建立index.html,并暂时引入出口文件bundle.js( 前期预览效果,后面会自动引入 )
8、根目录建立webpack.config.js,先定义进出口文件对应的目录及随便写entry文件的内容
module.exports = {
entry: {
main: './src/scripts/main.js',
aaa: './src/scripts/aaa.js',
bbb: './src/scripts/bbb.js',
ccc: './src/scripts/ccc.js'
},
output: {
path: path.resolve(__dirname,'./dist'),
filename: 'js/[name]-bundle.js'
}
9、初次执行打包命令,可看到dist下生成打包后的js文件
直接在命令行输入 'webpack' ( 后面每一步操作都要webpack一下才可看到效果 )
'webpack --display-error-details' : 出错时错误的详情
'webpack -w' : 提供watch方法,实时进行打包更新( 推荐加上-w )
'webpack -p' : 对打包后的文件进行压缩
'webpack -d' : 提供SourceMaps,方便调试
10、生成页面中的html,安装html-webpack-plugin插件并配置好
cnpm install html-webpack-plugin --save-dev
module.exports = {
plugins: [
//根目录的index.html生成dist下的html,多new几个就可多个页面生成
new htmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: 'body', //script标签的放置
title: 'index title test',
minify: { //html压缩
removeComments: true, //移除注释
collapseWhitespace: true //移除空格
}
//chunks: ['main','aaa'], //生成html页面后的script文件的引入
//排除没有用到的script文件,其他的都引进来,比chunks更好匹配
excludeChunks: ['bbb','ccc'] //引入了main.js和aaa.js
})
}
11、接下来安装各种loader及配置:
js的loader安装:
cnpm install babel-preset-latest --save-dev
cnpm install babel loader babel-core babel-plugin-transform-runtime babel-preset-es2015 babel-runtime --save-dev
css的loader安装:
cnpm install style-loader css-loader --save-dev
//css3前缀补全:
cnpm install postcss-loader --save-dev
cnpm install autoprefixer --save-dev
sass的loader安装:
cnpm install sass-loader node-sass webpack --save-dev
模板中的loader安装:
cnpm install html-loader --save-dev ( html )
cnpm install ejs-loader --save-dev ( ejs )
图片:
cnpm install file-loader --save-dev
url:
cnpm install url-loader --save-dev
图片压缩:
cnpm install image-webpack-loader --save-dev
大体配置格式:
module.exports = {
module: {
rules: [
//处理js中的loader
{
test: /\.js$/,
loader: 'babel-loader',
include: path.resolve(__dirname,'/src'),
exclude: path.resolve(__dirname,'/node_modules')
},
//处理css中的loader
{
test: /\.css$/,
loader: 'style-loader!css-loader?importLoaders=1!postcss-loader'
},
//处理sass中的loader
{
test: /\.scss$/,
loader: 'style-loader!css-loader!postcss-loader!sass-loader'
},
//处理html模板中的loader
{
test: /\.html$/,
loader: 'html-loader'
},
//处理ejs模板中的loader,以.tpl后缀结尾的
{
test: /\.tpl$/,
loader: 'ejs-loader'
},
//处理图片中的loader( 通常url/file/image-webpack等loader配合 )
{
test: /\.(png|jpg|gif|svg)$/i,
loader: 'file-loader'
}
]
}
}
css3前缀要放在插件里,与webpack1的用法不同
module.exports = {
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
require("autoprefixer")({
browsers: ["last 5 versions"]
})
]
}
})
]
}
最终的webpack.config.js配置如下:
var webpack = require("webpack");
var path = require("path");
var htmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: './src/app.js', /*{
main: './src/scripts/main.js',
aaa: './src/scripts/aaa.js',
bbb: './src/scripts/bbb.js',
ccc: './src/scripts/ccc.js'
}*/
output: {
path: path.resolve(__dirname,'./dist'),
filename: 'js/[name]-bundle.js',
publicPath: 'http://cdn.com/' //上线的绝对路径
},
//插件
plugins: [
new htmlWebpackPlugin({ //根目录的index.html生成dist下的html,可以多个生成
filename: 'index.html',
template: 'index.html',
inject: 'body', //script标签的放置
//title: 'index title test',
minify: { //html压缩
removeComments: true, //移除注释
collapseWhitespace: true //移除空格
}
//chunks: ['main','aaa'], //生成html页面后的script文件的引入
//excludeChunks: ['bbb','ccc'] //排除没有用到的script文件,其他的都引进来,比chunks更好匹配
}),
new webpack.LoaderOptionsPlugin({
options: {
postcss: [ //浏览器自动补全前缀
require("autoprefixer")({
browsers: ["last 5 versions"]
})
]
}
})
/*,
new htmlWebpackPlugin({
filename: 'b.html',
template: 'index.html',
inject: 'body',
title: "this is b.html",
//chunks: ['bbb'],
//excludeChunks: ['aaa','ccc']
}),
new htmlWebpackPlugin({
filename: 'c.html',
template: 'index.html',
inject: 'body',
title: "this is c.html",
//chunks: ['ccc'],
//excludeChunks: ['aaa','bbb']
})*/
],
module:{
rules: [ //1.0的是loaders
//处理js中的loader
{
test: /\.js$/,
loader: 'babel-loader',
include: path.resolve(__dirname,'/src'), //指定打包的文件
exclude: path.resolve(__dirname,'/node_modules') //排除打包的文件,加速打包时间
},
//处理css中的loader
{
test: /\.css$/,
loader: 'style-loader!css-loader?importLoaders=1!postcss-loader' //@import进来的样式在问号后加
},
//处理sass中的loader
{
test: /\.scss$/,
loader: 'style-loader!css-loader!postcss-loader!sass-loader'
},
//处理html模板中的loader
{
test: /\.html$/,
loader: 'html-loader'
},
//处理ejs模板中的loader,以.tpl后缀结尾的
{
test: /\.tpl$/,
loader: 'ejs-loader'
},
//处理图片中的loader,file-loader,url-loader,image-webpack-loader相互配合(图片格式转换base64 图片压缩)
{
test: /\.(png|jpg|gif|svg)$/i, //模板中的图片放相对路径: src="${require('../imgs/aaa.jpg')}"
loader: 'url-loader'
}
]
}
}
配置后的东西可以在这里看到package.json( 也可在这里设置其他 ):
{
"name": "webpack-demo",
"version": "2.0.0",
"description": "wabpack demo",
"main": "index.js",
"babel": {
"presets": [
"latest"
]
},
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot --port 8081",
"build": "cross-env NODE_ENV=production webpack --progress --colors --display modules --display reason --hide-modules"
},
"keywords": [
"wabpcck"
],
"author": "wu",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^6.7.7",
"babel": "^6.23.0",
"babel-core": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-latest": "^6.24.0",
"babel-runtime": "^6.23.0",
"css-loader": "^0.28.0",
"ejs-loader": "^0.3.0",
"file-loader": "^0.11.1",
"html-loader": "^0.4.5",
"html-webpack-plugin": "^2.28.0",
"image-webpack-loader": "^3.3.0",
"imagemin-pngquant": "^5.0.0",
"install": "^0.8.8",
"loader": "^2.1.1",
"node-sass": "^4.5.2",
"postcss-loader": "^1.3.3",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.1",
"url-loader": "^0.5.8",
"webpack": "^2.3.3"
}
}