Webpack CommonsChunkPlugin extracts public code,

Source: Internet
Author: User

Webpack CommonsChunkPlugin extracts public code,

1. Project Structure

2. Some code

Module. js

console.log('module.js');

Index. js in the index folder

Require ('../module. js'); console. log ('homepage-index file ');

Index. js in the login folder

 

Require ('../module. js'); console. log ('logon page-index file ');

3. webpack. config. js

Var htmlWebpackPlugin = require ('html-webpack-plugin'); var webpack = require ('webpackage'); module. exports = {// entry :'. /src/app. js', entry: {'login ':['. /src/page/login/index. js'], 'index ':['. /src/page/index. js']}, output: {path: _ dirname + '/dist', filename: 'js/[name]. js '}, // load external variables or modules into externals: {'jquery': 'window. jQuery '}, module: {loaders: [{test :/\. js $/, // The following directory does not process exclude :/Node_modules/, // only process the following directories: include:/src/, loader: "babel-loader", // configure the target Runtime environment (environment) automatically enable the required babel plug-in query: {presets: ['latest '] }}, // css to process this section {test :/\. css $/, use: ['style-loader ', {loader: 'css-loader', options: {// @ important introduces css importLoaders: 1 }}, {loader: 'postcss-loader ', options: {plugins: function () {return [// be sure to write it in front of require ("autoprefixer"); otherwise, require ("autoprefixer") Invalid require ('postcss-import') (), require ("autoprefixer") ({"browsers": ["Android> = 4.1", "iOS> = 7.0 ", "ie >=8"]})] }}]}, // less process this piece {test :/\. less $/, use: ['style-loader ', {loader: 'css-loader', options: {// @ important introduces css importLoaders: 1 }}, {loader: 'postcss-loader ', options: {plugins: function () {return [// be sure to write it in front of require ("autoprefixer"); otherwise, require ("autoprefixer ") invalid require ('Postcss-import') (), require ("autoprefixer") ({"browsers": ["Android >=4.1", "iOS >=7.0 ", "ie> = 8"]})] }}, 'less-loader ']}, // process the html template {test :/\. html $/, use: {loader: 'html-loader '}, // process the image {test :/\. (png | jpg | gif | svg) $/I, loaders: [// base64 for images smaller than 8 K. For images larger than 10 K, use file-loader 'url-loader? Limit = 8192 & name: img/[name]-[hash: 5]. [ext] ', // image Compression 'image-webpack-loader']}, plugins: [new htmlWebpackPlugin ({template: 'index.html ', filename: 'index.html '}), // independent general modules to js/base. js new webpack. optimize. commonsChunkPlugin ({// block name of the public block: 'common', filename: 'js/base. js'})]}

4. Test

(1) webpack. config. js

// Independent general module to js/base. js new webpack. optimize. CommonsChunkPlugin ({// block name of public block: 'common '})

 

Output:

 

(2)

// Independent general modules to js/base. js new webpack. optimize. commonsChunkPlugin ({// block name of the public block: 'common', // The generated file name filename: 'js/common. js '})

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.