安装
cnpm i webpack@3 vue vue-loader vue-template-compiler style-loader css-loader stylus-loader stylus url-loader file-loader --save
package.json
"scripts": {
// 注意这里的webpack是指node_modules里面的webpack,而不是全局的
"build": "webpack --config webpack.config.js"
}
webpack.config.js
const path = require('path');
module.exports = {
entry: path.join(__dirname, 'src/index.js'),
output: {
filename: 'bundle.js',
path: path.join(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
}
};
css 和 img
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(gif|jpg|jpeg|png|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 1024,
name: '[name]-aaa.[ext]'
}
}
]
}
然后可以直接这样引用
import './assets/styles/test.css';
import './assets/images/bg.jpeg';
css文件中也可以直接引用
background-image: url('../images/done.svg')
预处理
注意stylus-loader依赖stylus,所以两者要一起安装
{
test: /\.styl/,
use: [
'style-loader',
'css-loader',
'stylus-loader'
]
}
webpack-dev-server
cnpm install webpack-dev-server@2 --save
用来区分不同的环境,不同的平台设置环境变量的方式是不一样的
cnpm i cross-env html-webpack-plugin --save
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cross-env NODE_ENV=production webpack --config webpack.config.js",
"dev": "cross-env NODE_ENV=development webpack-dev-server --config webpack.config.js"
},
webpack.config.js
const path = require('path');
const HTMLPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const isDev = process.env.NODE_ENV === 'development';
const config = {
target: 'web',
module: {
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: isDev ? '"development"' : '"production"'
}
}),
new HTMLPlugin()
]
};
if(isDev) {
config.devServer = {
port: 8000,
host: '0.0.0.0',
overlay: {
errors: true
}
};
}
module.exports = config;
启动
npm run dev
调试和热加载
if(isDev) {
// 调试
config.devtool = '#cheap-module-eval-source-map';
config.devServer = {
port: 8000,
host: '0.0.0.0',
overlay: {
errors: true
},
// 不刷新页面的情况下热加载
hot: true
};
// 不刷新页面的情况下热加载
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
}
babelrc 和 postcss.config.js
cnpm install postcss-loader autoprefixer babel-loader babel-core --save
cnpm install babel-preset-env babel-plugin-transform-vue-jsx --save
cnpm install babel-helper-vue-jsx-merge-props --save
cnpm install babel-plugin-syntax-jsx --save
.babelrc
{
"presets": [
"env"
],
"plugins": [
"transform-vue-jsx"
]
}
postcss.config.js后处理器,帮助我们优化css
const autoprefixer = require('autoprefixer');
module.exports = {
plugins: [
autoprefixer()
]
};
webpack.config.js
{
test: /\.jsx$/,
loader: 'babel-loader'
}
{
test: /\.styl/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader'
]
},
出现问题删除node_modules重装试试
CSS打包
非JS代码的东西打包成静态文件
cnpm i extract-text-webpack-plugin --save
const ExtractPlugin = require('extract-text-webpack-plugin');
const config = {
output: {
filename: 'bundle.[hash:8].js',
path: path.join(__dirname, 'dist')
}
};
if(isDev) {
config.module.rules.push({
test: /\.styl/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader'
]
});
} else {
config.output.filename = '[name].[chunkhash:8].js';
config.module.rules.push(
{
test: /\.styl/,
use: ExtractPlugin.extract({
fallback: 'style-loader',
use: [
'css-loader',
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader'
]
})
}
);
config.plugins.push(
new ExtractPlugin('styles.[contentHash:8].css')
)
}
module.exports = config;
类库代码单独打包
if(isDev) {
} else {
config.entry = {
app: path.join(__dirname, 'src/index.js'),
vendor: ['vue']
};
config.plugins.push(
new ExtractPlugin('styles.[contentHash:8].css'),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'runtime'
})
)
}