webpack3.6.0ノートの使用-添付ファイル


------ package.json ------
{
  "name": "webpackStudy",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "ser": "webpack-dev-server",
    "dev": "export aaa=dev&webpack",
    "build": "export aaa=build&webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@sandfox/uglifyjs-webpack-plugin": "^0.4.8",
    "babel-preset-es2015": "^6.24.1",
    "copy-webpack-plugin": "^4.1.1",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.1",
    "file-loader": "^1.1.5",
    "html-webpack-plugin": "^2.30.1",
    "style-loader": "^0.19.0",
    "uglifyjs-webpack-plugin": "^0.4.6",
    "url-loader": "^0.6.2",
    "watchpack": "^1.4.0",
    "webpack": "^3.6.0",
    "webpack-loader-options-merge": "^0.0.3"
  },
  "dependencies": {
    "html-withimg-loader": "^0.1.16",
    "jquery": "^3.2.1",
    "vue": "^2.5.0"
  }
}

------ webpack.config.js ------
const path = require("path");
const uglifyJsPlugin = require("uglifyjs-webpack-plugin");
const htmlWebpackPlugin = require("html-webpack-plugin");
const extractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require("webpack");
const entry = require("./webpack-config/entry.js");
const copyWebpackPlugin = require("copy-webpack-plugin");

console.log(encodeURIComponent(process.env.aaa));
if(process.env.aaa == "build"){
    var webset = {
        "publicPath" : "http://realyuing/",
        "version" : "1.0.0"
    };
}else{
    var webset = {
        "publicPath" : "localhost:9000/",
        "version" : "1.0.0"
    };
}

module.exports = {
    target : "web",  //web  / node- 

    //entry : entry.dev,
    entry : {
        build: "./src/assets/js/build.js",
        jquery : "jquery",
        vue : "vue"
    },
    output : {
        path : path.resolve(__dirname, "dist"),
        filename : "[name].js",
        publicPath: "/temp/"
        //,publicPath : webset.publicPath + "?" +webset.version + "/"

        ,hotUpdateChunkFilename: "../hot/hot-update.js"
        , hotUpdateMainFilename: "../hot/hot-update.json"


    },
    module : {
        rules: [
            {
                test: /\.css$/,
                use: extractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 100 // 20kb base64 js 
                            ,outputPath:"assets/images/" // 
                            ,name:"[name].[ext]" //[name]  [hash] 
                            ,publicPath:"http://cdn.qianduangongcheng.com/" // cdn  
                        }
                    }
                ]
            },
            {
                test: /\.(htm|html)$/i,
                use: ['html-withimg-loader']
            }
        ]
    },
    plugins : [
        // entry jquery 
        new webpack.optimize.CommonsChunkPlugin({
            name : ["jquery", "vue"],
            filename : "assets/js/[name].js",
            miniChunks:2
        }),

        new webpack.ProvidePlugin({  // 
            $ : "jquery",
            "jQuery" : "jquery"
        }),

        /*new uglifyJsPlugin({
            mangle: {
                props: true
            }
        }),*/
        new htmlWebpackPlugin({
            minify : {
                removeAttributeNode : true
            },
            hash : true,
            template : "./src/assets/index.html",
            title : "Hi Webpack~"
            //,projectPath : "http://localhost:9000/src/" // url
        })
        ,
        new extractTextPlugin("assets/css/style.css") // assets/css/[hash].css
        ,
        new webpack.HotModuleReplacementPlugin()


        ,new copyWebpackPlugin([{
            from : __dirname + "/src/public",
            to : "./public"
        }])
    ],
    devServer : {
        contentBase : path.resolve(__dirname, "./"),
        host : "localhost",
        compress : false, //compress html5
        port :9000
    },
    watchOptions:{
        poll : 1000,
        aggregeateTimeout : 500,  //0.5s
        ignored : /node_modules/,
    }
};

------  .babelrc ------
{
  "presets" : [
    "es2015"
  ]
}

------ build.js ------
import css from './assets/css/index.css';
document.getElementById("title").innerHTML = 'Hello You Webpack!~~~';