Git Product home page Git Product logo

webpackconfig's Introduction

webpack config

个人写的webpack基本配置
包括自定义postcss插件、webpack插件和loader开发,仅供参考!

webpack.config.js

module.exports = function(env) {
	return require(`./webpack.${env}.js`)
}

webpack.dev.js

const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const BearWebpackPlugin = require('./plugins/BearWebpackPlugin')

module.exports = {
	devtool: 'inline-source-map',
	entry: {
		app: './src/index.js',
	},
	output: {
		filename: '[name].bundle.js'
	},
	module: {
		rules: [
			{
				test: /\.styl$/,
				use: [
					'style-loader',
					'css-loader',
					{
						loader: 'postcss-loader',
						options: {
							sourceMap: 'inline'
						}
					},
					'stylus-loader'
				]
			},
			{
				test: /\.jsx$/,
				use: ['bear-loader']
			},
			{
				test: /\.js$/,
				exclude: /node_modules/,
				use: {
					loader: 'babel-loader',
					options: {
						presets: ['env'],
						plugins: ['dynamic-import-webpack']
					}
				}
			},
			{
				test: /\.(png|jpg|gif)$/,
				use: ['url-loader']
			},
			{
				test: /\.(woff|woff2|eot|ttf|otf)$/,
				use: ['file-loader']
			}
		]
	},
	//自定义loader别名
	resolveLoader: {
	    alias: {
	        'bear-loader': require('path').resolve('./plugins/bear-loader'),
	    },
	},
	plugins: [
		new HtmlWebpackPlugin({
			filename: 'index.html',
			template: './src/views/index.html'
		}),
		new webpack.HotModuleReplacementPlugin(),
		//自定义插件
		new BearWebpackPlugin()
	]
}

webpack.prod.js

const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")

module.exports = {
	entry: {
		app: './src/index.js'
	},
	output: {
		filename: 'static/[name].[chunkhash:8].js',
		chunkFilename: 'static/[name].[chunkhash:8].js',
		path: path.resolve(__dirname, 'dist')
	},
	module: {
		rules: [
			{
				test: /\.js$/,
				exclude: /node_modules/,
				use: {
					loader: 'babel-loader',
					options: {
						presets: ['env'],
						plugins: ['dynamic-import-webpack']
					}
				}
			},
			{
				test: /\.styl$/,
				use: ExtractTextPlugin.extract({
					use: [
						{
							loader: 'css-loader',
							options: {
								minimize: true
							}
						}, 
						'postcss-loader',
						'stylus-loader'
					]
				})
			},
			{
				test: /\.(png|jpg|gif)$/,
				use: [
					{
						loader: 'url-loader',
						options: {
							limit: 10000,
							name: 'images/[name].[hash:8].[ext]'
						}
					}
				]
			},
			{
				test: /\.(woff|woff2|eot|ttf|otf)$/,
				use: ['file-loader']
			}
		]
	},
	plugins: [
		//构建时清除dist文件夹
		new CleanWebpackPlugin(['dist/*']),
		//生成html首页
		new HtmlWebpackPlugin({
			filename: 'index.html',
			template: './src/views/index.html',
			minify: {
		        removeComments: true,
		        collapseWhitespace: true,
		        removeRedundantAttributes: true,
		        useShortDoctype: true,
		        removeEmptyAttributes: true,
		        removeStyleLinkTypeAttributes: true,
		        keepClosingSlash: true,
		        minifyJS: true,
		        minifyCSS: true,
		        minifyURLs: true
		    }
		}),
		//生成css文件
		new ExtractTextPlugin('static/[name].[contenthash:8].css'),
		//保持vendor hash不变
		new webpack.HashedModuleIdsPlugin(),
		//提取公共代码
		new webpack.optimize.CommonsChunkPlugin({
			name: 'vendor',
			minChunks: function(module, count) {
		        return module.context && module.context.includes('node_modules')
			}
		}),
		//提取样板,缓存vendor
		new webpack.optimize.CommonsChunkPlugin({
			name: 'runtime',
			chunks: ['vendor']
		}),
		//js文件压缩
		new webpack.optimize.UglifyJsPlugin()
	]
}

custom postcss plugins bearcss.js

const postcss = require('postcss')

module.exports = postcss.plugin('bearcss', (options = {}) => {
	return function(css) {
		css.walkRules(rule => {
			rule.walkDecls(/^background$/, decl => {
				//整个{...}
				let rule = decl.parent
				//console.log(decl.value)
				//插入样式
				rule.append({
					prop: 'color',
					value: 'red'
				})
			})
		})
	}
})

custom webpack plugins BearWebpackPlugin.js

function AppleBear(options) {
	this.options = options
}
AppleBear.prototype.apply = function(compiler) {
	compiler.plugin('compile', function(compilation) {
		console.log('compile!')
	})
	compiler.plugin('emit', function(compilation, callback) {
		console.log('emit!')
		callback()
	})
	compiler.plugin('done', function(compilation) {
		console.log('done!')
	})
}
module.exports = AppleBear

custom webpack loader bear-loader.js

module.exports = function(source) {
	console.log('bear-loader')
	console.log(source)
	const str = `

/*
	this is bear-loader test!
*/
`
	return source.replace(/applebear/g, 'APPLEBEAR!!!') + str
}

webpackconfig's People

Stargazers

 avatar

Watchers

James Cloos avatar leuvi avatar

Forkers

coolpo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.