- 安装postcss, autoprefixer, babel-loader, babel-core
npm i postcss-loader autoprefixer babel-loader babel-core
- 项目根目录下新建.babelrc, .postcss.config.js两个文件
- postcss.config.js 如下
const autoprefixer = require('autoprefixer')
module.exports = {
"plugins": {
// 增加浏览器前缀
"autoprefixer": {}
}
}
- .babelrc可以用于写render函数和jsx
{
"presets": [
"env"
],
"plugins": [
"transform-vue-jsx"
]
}
npm i babel-preset-env babel-plugin-transform-vue-jsx
# 还需安装
npm i babel-plugin-syntax-jsx babel-helper-vue-jsx-merge-props
- 对webpack.config.js增加jsx和的配置
(jsx类型的文件使用babel-loader,styl类型的文件增加postcss-loader和相应选项)
const path = require('path')
const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
// 判断是否为dev开发模式
const isDev = process.env.NODE_ENV === 'development'
const config = {
target: 'web',
entry: path.join(__dirname, 'src/index.js'),
output: {
filename: 'bundle.js',
path:path.join(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
// 更新
{
test: /\.jsx$/,
loader: 'babel-loader'
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.styl/,
use: [
'style-loader',
'css-loader',
// 更新
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
'stylus-loader'
]
},
{
test: /\.(gif|jpg|jpeg|png|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 1024,
name: '[name].[ext]'
}
}
]
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: isDev ? '"development"' : '"production"'
}
}),
new HTMLPlugin()
]
}
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()
)
}
module.exports = config