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

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
        })
    ]
};
    原文作者:mikeliu
    原文地址: https://segmentfault.com/a/1190000007739729
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞