vue webpack v1 & v2 基础配置篇(初学者--1)

mikeliu

Webpack现在有v1和v2两种版本。

  1. 安装nodejs。这就不细说了,度娘告诉你。

  2. cmd定位到开发文件夹,按照提示生成package.json。npm init

  3. 安装es6编译插件。$ npm install css-loader babel-loader babel-core babel-preset-es2015 --save-dev

  4. 安装webpack。$ npm install webpack html-webpack-plugin extract-text-webpack-plugin vue-template-compiler --save-dev
    如果是v2版本,还需npm install --save-dev extract-text-webpack-plugin@beta

  5. 安装vue,vuex。$ npm install vue vuex

  6. 配置webpack.config.js。参考文章最后的代码。

  7. run webpack。$ webpack

//webpack v1
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        app: './app.js', //入口js
        vendors: [
            'vue', 'vuex' //单独抽出vue,vuex打包成一个js
        ]
    },
    output: {
        path: 'dist',
        filename: '[name].js'
    },
    resolve: {
        extensions: ['', '.js', '.css', '.vue']
    },
    module: {
        loaders: [
            {
                test: /\.vue$/, 
                loader: 'vue',
                exclude: /node_modules/
            },
            { 
                test: /\.js$/, 
                  loader: 'babel',
                  exclude: /node_modules/
            },
            {test: /\.css$/,loader: "style!css"},
        ]
    },
    vue: { //从vue中抽出独立的css文件
        loaders: {
            css: ExtractTextPlugin.extract("css")
        }
    },
    babel: { //es6转为es5代码
        presets: ['es2015'],
        plugins: ['transform-runtime'] //这个必须install babel-plugin-transform-runtime
    },
    plugins: [
        // 提取css为单文件
        new ExtractTextPlugin("[name].[contenthash].css"),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendors',
            filename: 'vendors.js',
        }),
    //在index.html中引用编译后的js,css
        new HtmlWebpackPlugin({
          filename: 'index.html',
          template: 'index.html',
          inject: true
        })
    ]
};
//webpack v2
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        app: './app.js',
        vendors: [
            'vue', 'vuex', 'vue-router'
        ]
    },
    output: {
        path: 'dist',
        filename: '[name].js'
    },
    resolve: {
       // modules: ["node_modules"],
        extensions: ['.css', '.vue', '.js']
    },
    module: {
        rules: [
            {
                test: /\.vue$/, 
                loader: "vue-loader",
                options: {
                    loaders: {
                        "css": ExtractTextPlugin.extract("css-loader")
                    }                    
                },
                exclude: /node_modules/
            },
            { 
                test: /\.js$/, 
                loader: "babel-loader",
                options: {
                    presets: ['es2015'],
                    plugins: ['transform-runtime'] 
                },
                exclude: /node_modules/
            },
            {test: /\.css$/,use: ["style-loader", "css-loader"]},
        ]
    },
    resolve: {
        alias: {
          'vue': 'vue/dist/vue.common.js',
          //'vue-router': path.join(__dirname, '..', 'src')
        }
      },
    plugins: [
        // 提取css为单文件
        new ExtractTextPlugin({
            filename: "[name].[contenthash].css",
            allChunks: true
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendors',
            filename: 'vendors.js',
        }),
        new HtmlWebpackPlugin({
          filename: 'index.html',
          template: 'index.html',
          inject: true
        })
    ]
};
阅读 3.4k

前端
越学越慌,越看越迷茫
27 声望
0 粉丝
0 条评论
27 声望
0 粉丝
宣传栏