运用 Webpack1.x 搭建 (MultiPage Application,MPA) 基本框架

初始化项目

webpack官方文档:https://www.webpackjs.com/con…
github项目地点:https://github.com/Zhanghongw…

项目搭建简朴纪录一下敕令,网上有许多文章不多做引见。
愿望能够供应一个基本的架构。

延续更新……..

执行敕令

// 全局装置 webapck、webpack-cli, 之前装置过疏忽此步骤

npm install webpack -g 

npm install webpack-cli -g

初始化 npm

npm init

项目目次构造

+dist
+src
++assets
+++images
++common
++page
++view

装置相干依靠,注重版本

npm install xxx@版本号 --save-dev
{
    "css-loader": "^2.1.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "html-loader": "^0.4.5",
    "html-webpack-plugin": "^2.28.0",
    "style-loader": "^0.23.1",
    "url-loader": "^0.5.8",
    "webpack": "^1.15.0",
    "webpack-dev-server": "^1.16.5"
  }

webpack.config.js 配置文件

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

// 猎取html-webpack-plugin参数的要领 
var getHtmlConfig = function(name){
  return {
      template    : './src/view/' + name + '.html',
      filename    : 'view/' + name + '.html',
      inject      : true,
      hash        : true,
      chunks      : ['common', name]
  };
};

var config = {
  entry: {
    'common':['./src/common/index.js'],
    'index':['./src/page/index/index.js'],
    'home':['./src/page/home/home.js']
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    publicPath: "/dist",
    filename: 'js/[name].js'
  },
  module: {
    loaders: [

      // 处置惩罚 css
      { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader") },

      // 处置惩罚图片
      { test: /\.(gif|png|jpg|jpeg)\??.*$/, loader: 'url-loader?limit=100&name=resoure/[name].[ext]' }

    ]
  },
  plugins: [

    new webpack.optimize.CommonsChunkPlugin({
      // 大众模块提取
        name : 'common',
        filename : 'js/base.js'
    }),

    // 把css零丁打包到文件里
    new ExtractTextPlugin("css/[name].css"),

    // html 模板处置惩罚
    new HtmlWebpackPlugin(getHtmlConfig('index')),
    new HtmlWebpackPlugin(getHtmlConfig('home'))
  ]
};

module.exports = config;
    原文作者:zhanghongwei
    原文地址: https://segmentfault.com/a/1190000018855795
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞