您好,欢迎访问一九零五行业门户网

webpack进阶之插件篇_html/css_WEB-ITnose

上一篇博客讲解了webpack环境的基本,这一篇讲解一些更深入的内容和开发技巧。基本环境搭建就不展开讲了
一、插件篇 1. 自动补全css3前缀 autoprefixer
官方是这样说的: parse css and add vendor prefixes to css rules using values from the can i use website ,也就是说它是一个自动检测兼容性给各个浏览器加个内核前缀的插件。
举个栗子:最新的弹性盒模型flux实际代码:
:fullscreen a { display: flex}

插件自动补充后
a { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex}

效果显而易见,我们可以更专注于css布局和美化,而不需要花过多的精力都写相同的外码而加上不同的前缀,也减少了冗余代码。
使用方法:
cnpm install --save-dev autoprefixer postcss-loader
var autoprefixer = require('autoprefixer');module.exports={ //其他配置这里就不写了 module:{ loaders:[ { test://.css$/, //在原有基础上加上一个postcss的loader就可以了 loaders:['style-loader','css-loader','postcss-loader'] } ] }, postcss:[autoprefixer({browsers:['last 2 versions']})]}

2. 自动生成html插件 html-webpack-plugin
cnpm install html-webpack-plugin --save-dev
//webpack.config.js var htmlwebpackplugin = require('html-webpack-plugin'); module.exports={ entry:'./index.js', output:{ path:__dirname+'/dist', filename:'bundle.js' } plugins:[ new htmlwebpackplugin() ] }

作用:它会在dist目录下自动生成一个index.html
webpack app

其他配置参数:
{ entry: 'index.js', output: { path: 'dist', filename: 'bundle.js' }, plugins: [ new htmlwebpackplugin({ title: 'my app', filename: 'admin.html', template:'header.html', inject: 'body', favicon:'./images/favico.ico', minify:true, hash:true, cache:false, showerrors:false, chunks: { head: { entry: assets/head_bundle.js, css: [ main.css ] }, xhtml:false }) ]}

--- header.html ---

作用:
title: 设置title的名字 filename: 设置这个html的文件名 template:要使用的模块的路径 inject: 把模板注入到哪个标签后 'body', favicon: 给html添加一个favicon './images/favico.ico', minify:是否压缩 true false hash:是否hash化 true false , cache:是否缓存, showerrors:是否显示错误, chunks:目前没太明白 xhtml:是否自动毕业标签 默认false

3. 提取样式插件 extract-text-webpack-plugin
官网是这么解释的 extract text from bundle into a file. ,把额外的数据加到编译好的文件中
var extracttextplugin = require(extract-text-webpack-plugin);module.exports = { module: { loaders: [ { test: //.css$/, loader: extracttextplugin.extract(style-loader, css-loader) } ] }, plugins: [ new htmlwebpackplugin({ template: './src/public/index.html', inject: 'body' }), new extracttextplugin([name].[hash].css) ]}

说明:将css放到index.html的body上面
4. 拷贝资源插件 copy-webpack-plugin
官方这样解释 copy files and directories in webpack ,在webpack中拷贝文件和文件夹
cnpm install --save-dev copy-webpack-pluginnew copywebpackplugin([{ from: __dirname + '/src/public'}]),

作用:把public 里面的内容全部拷贝到编译目录
参数 作用 其他说明
from 定义要拷贝的源目录 from: __dirname + ‘/src/public’
to 定义要烤盘膛的目标目录 from: __dirname + ‘/dist’
totype file 或者 dir 可选,默认是文件
force 强制覆盖先前的插件 可选 默认false
context 不知道作用 可选 默认 base context 可用 specific context
flatten 只拷贝文件不管文件夹 默认是false
ignore 忽略拷贝指定的文件 可以用模糊匹配
5. 全局挂载插件 webpack.provideplugin [webpack内置插件 ]
new webpack.provideplugin({ $: jquery, jquery: jquery, window.jquery: jquery}))new webpack.noerrorsplugin(),new webpack.optimize.dedupeplugin(),new webpack.optimize.uglifyjsplugin(),new webpack.optimize.commonschunkplugin('common.js')

作用: 和上面5个一一对应
把一些需要的东西绑定到window上,暴露出来 成为全局变量 不显示错误插件 具体不是太清楚,先记录着 丑化js 混淆代码而用 提取公共代码的插件

二、一个完整的栗子 'use strict';// modulesvar webpack = require('webpack');var autoprefixer = require('autoprefixer');var htmlwebpackplugin = require('html-webpack-plugin');var extracttextplugin = require('extract-text-webpack-plugin');var copywebpackplugin = require('copy-webpack-plugin');/** * env * get npm lifecycle event to identify the environment */var env = process.env.npm_lifecycle_event;var istest = env === 'test' || env === 'test-watch';var isprod = env === 'build';module.exports = function makewebpackconfig() { var config = {}; config.entry = istest ? {} : { app: './src/app/app.js' }; config.output = istest ? {} : { // absolute output directory path: __dirname + '/dist', publicpath: isprod ? '/' : 'http://localhost:8080/', filename: isprod ? '[name].[hash].js' : '[name].bundle.js', chunkfilename: isprod ? '[name].[hash].js' : '[name].bundle.js' }; if (istest) { config.devtool = 'inline-source-map'; } else if (isprod) { config.devtool = 'source-map'; } else { config.devtool = 'eval-source-map'; } config.module = { preloaders: [], loaders: [{ test: //.js$/, loader: 'babel', exclude: /node_modules/ }, { test: //.css/, loader: istest ? 'null' : extracttextplugin.extract('style', 'css?sourcemap!postcss') }, { test: //.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/, loader: 'file' }, { test: //.json$/, loader: 'json' }, { test: //.scss/, loader: 'style!css!sass' }, { test: //.html$/, loader: 'raw' }] }; if (istest) { config.module.preloaders.push({ test: //.js$/, exclude: [ /node_modules/, //.spec/.js$/ ], loader: 'isparta-instrumenter' }) } config.postcss = [ autoprefixer({ browsers: ['last 2 version'] }) ]; config.plugins = []; if (!istest) { config.plugins.push( new htmlwebpackplugin({ template: './src/public/index.html', inject: 'body' }), new extracttextplugin('[name].[hash].css', {disable: !isprod}) ) } if (isprod) { config.plugins.push( new webpack.noerrorsplugin(), new webpack.optimize.dedupeplugin(), new webpack.optimize.uglifyjsplugin(), new copywebpackplugin([{ from: __dirname + '/src/public' }]), new webpack.provideplugin({ $: jquery, jquery: jquery, window.jquery: jquery })) } config.devserver = { contentbase: './src/public', stats: 'minimal' }; return config;}();

三、调试技巧 if (istest) { config.devtool = 'inline-source-map';} else if (isprod) { config.devtool = 'source-map';} else { config.devtool = 'eval-source-map';}

作用: 使用source-map可以在debug的时候看到源代码,方便 查错
tags: webpack
category: webpack插件 上一篇博客讲解了webpack环境的基本,这一篇讲解一些更深入的内容和开发技巧。基本环境搭建就不展开讲了
一、插件篇 1. 自动补全css3前缀 autoprefixer
官方是这样说的: parse css and add vendor prefixes to css rules using values from the can i use website
,也就是说它是一个自动检测兼容性给各个浏览器加个内核前缀的插件。
举个栗子:最新的弹性盒模型flux实际代码:
:fullscreen a { display: flex}

插件自动补充后
a { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex}

效果显而易见,我们可以更专注于css布局和美化,而不需要花过多的精力都写相同的外码而加上不同的前缀,也减少了冗余代码。
使用方法:
cnpm install --save-dev autoprefixer postcss-loader
var autoprefixer = require('autoprefixer');module.exports={ //其他配置这里就不写了 module:{ loaders:[ { test://.css$/, //在原有基础上加上一个postcss的loader就可以了 loaders:['style-loader','css-loader','postcss-loader'] } ] }, postcss:[autoprefixer({browsers:['last 2 versions']})]}

2. 自动生成html插件 html-webpack-plugin
cnpm install html-webpack-plugin --save-dev
//webpack.config.js var htmlwebpackplugin = require('html-webpack-plugin'); module.exports={ entry:'./index.js', output:{ path:__dirname+'/dist', filename:'bundle.js' } plugins:[ new htmlwebpackplugin() ] }

作用:它会在dist目录下自动生成一个index.html
webpack app

其他配置参数:
{ entry: 'index.js', output: { path: 'dist', filename: 'bundle.js' }, plugins: [ new htmlwebpackplugin({ title: 'my app', filename: 'admin.html', template:'header.html', inject: 'body', favicon:'./images/favico.ico', minify:true, hash:true, cache:false, showerrors:false, chunks: { head: { entry: assets/head_bundle.js, css: [ main.css ] }, xhtml:false }) ]}

--- header.html ---

作用:
title: 设置title的名字 filename: 设置这个html的文件名 template:要使用的模块的路径 inject: 把模板注入到哪个标签后 'body', favicon: 给html添加一个favicon './images/favico.ico', minify:是否压缩 true false hash:是否hash化 true false , cache:是否缓存, showerrors:是否显示错误, chunks:目前没太明白 xhtml:是否自动毕业标签 默认false

3. 提取样式插件 extract-text-webpack-plugin
官网是这么解释的 extract text from bundle into a file. ,把额外的数据加到编译好的文件中
var extracttextplugin = require(extract-text-webpack-plugin);module.exports = { module: { loaders: [ { test: //.css$/, loader: extracttextplugin.extract(style-loader, css-loader) } ] }, plugins: [ new htmlwebpackplugin({ template: './src/public/index.html', inject: 'body' }), new extracttextplugin([name].[hash].css) ]}

说明:将css放到index.html的body上面
4. 拷贝资源插件 copy-webpack-plugin
官方这样解释 copy files and directories in webpack ,在webpack中拷贝文件和文件夹
cnpm install --save-dev copy-webpack-pluginnew copywebpackplugin([{ from: __dirname + '/src/public'}]),

作用:把public 里面的内容全部拷贝到编译目录
参数 作用 其他说明
from 定义要拷贝的源目录 from: __dirname + ‘/src/public’
to 定义要烤盘膛的目标目录 from: __dirname + ‘/dist’
totype file 或者 dir 可选,默认是文件
force 强制覆盖先前的插件 可选 默认false
context 不知道作用 可选 默认 base context 可用 specific context
flatten 只拷贝文件不管文件夹 默认是false
ignore 忽略拷贝指定的文件 可以用模糊匹配
5. 全局挂载插件 webpack.provideplugin [webpack内置插件 ]
new webpack.provideplugin({ $: jquery, jquery: jquery, window.jquery: jquery}))new webpack.noerrorsplugin(),new webpack.optimize.dedupeplugin(),new webpack.optimize.uglifyjsplugin(),new webpack.optimize.commonschunkplugin('common.js')

作用: 和上面5个一一对应
把一些需要的东西绑定到window上,暴露出来 成为全局变量 不显示错误插件 具体不是太清楚,先记录着 丑化js 混淆代码而用 提取公共代码的插件

二、一个完整的栗子 'use strict';// modulesvar webpack = require('webpack');var autoprefixer = require('autoprefixer');var htmlwebpackplugin = require('html-webpack-plugin');var extracttextplugin = require('extract-text-webpack-plugin');var copywebpackplugin = require('copy-webpack-plugin');/** * env * get npm lifecycle event to identify the environment */var env = process.env.npm_lifecycle_event;var istest = env === 'test' || env === 'test-watch';var isprod = env === 'build';module.exports = function makewebpackconfig() { var config = {}; config.entry = istest ? {} : { app: './src/app/app.js' }; config.output = istest ? {} : { // absolute output directory path: __dirname + '/dist', publicpath: isprod ? '/' : 'http://localhost:8080/', filename: isprod ? '[name].[hash].js' : '[name].bundle.js', chunkfilename: isprod ? '[name].[hash].js' : '[name].bundle.js' }; if (istest) { config.devtool = 'inline-source-map'; } else if (isprod) { config.devtool = 'source-map'; } else { config.devtool = 'eval-source-map'; } config.module = { preloaders: [], loaders: [{ test: //.js$/, loader: 'babel', exclude: /node_modules/ }, { test: //.css/, loader: istest ? 'null' : extracttextplugin.extract('style', 'css?sourcemap!postcss') }, { test: //.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/, loader: 'file' }, { test: //.json$/, loader: 'json' }, { test: //.scss/, loader: 'style!css!sass' }, { test: //.html$/, loader: 'raw' }] }; if (istest) { config.module.preloaders.push({ test: //.js$/, exclude: [ /node_modules/, //.spec/.js$/ ], loader: 'isparta-instrumenter' }) } config.postcss = [ autoprefixer({ browsers: ['last 2 version'] }) ]; config.plugins = []; if (!istest) { config.plugins.push( new htmlwebpackplugin({ template: './src/public/index.html', inject: 'body' }), new extracttextplugin('[name].[hash].css', {disable: !isprod}) ) } if (isprod) { config.plugins.push( new webpack.noerrorsplugin(), new webpack.optimize.dedupeplugin(), new webpack.optimize.uglifyjsplugin(), new copywebpackplugin([{ from: __dirname + '/src/public' }]), new webpack.provideplugin({ $: jquery, jquery: jquery, window.jquery: jquery })) } config.devserver = { contentbase: './src/public', stats: 'minimal' }; return config;}();

三、调试技巧 if (istest) { config.devtool = 'inline-source-map';} else if (isprod) { config.devtool = 'source-map';} else { config.devtool = 'eval-source-map';}

作用: 使用source-map可以在debug的时候看到源代码,方便 查错
其它类似信息

推荐信息