webpack learn2-vue的jsx寫法和postcss 1

首先輸入命令安裝css

npm i postcss-loader autoprefixer babel-loader  babel-core

在根目錄建立文件 .babelrc和postcss.config.jshtml

其中postcss.config.js內容:vue

const autoprefixer = require('autoprefixer')

module.exports = {
    plugins: [
        autoprefixer()
    ]
}

.babelrc內容:webpack

{
    "presets": [
        "env"
    ],
    "plugins": [
        "transform-vue-jsx"
    ]
}

再次配置webpack.config.js文件,內容:web

const path = require('path')

const VueLoaderPlugin = require('vue-loader/lib/plugin')

const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
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:/\.(jpg|svg|jpeg|png|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024,
              name: '[name]-aa.[ext]'
            }
          }
        ]
      }
    ]
  },
  plugins:[
      new VueLoaderPlugin(),
      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
    //open: true 改配置後就自動打開瀏覽器
  }
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
}

module.exports = config
View Code

最後npm run devnpm

相關文章
相關標籤/搜索