Webpack common module (js used for each page) compilation, webpackjs

Source: Internet
Author: User
Tags postcss

Webpack common module (js used for each page) compilation, webpackjs

1. project directory

 

2. configuration file: webpack. config. js

Var htmlWebpackPlugin = require ('html-webpack-plugin'); var webpack = require ('webpackage'); module. exports = {// entry :'. /src/app. js', entry: {// common module 'common ':['. /src/page/common/index. js'], '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 '}, modul E: {loaders: [{test :/\. js $/, // The following directories do not process exclude:/node_modules/, // only process the following directories include:/src/, loader: "babel-loader ", // The configured target Runtime environment (environment) automatically enables the required babel plug-in query: {presets: ['latest '] }}, // css processes 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 in r Before equire ("autoprefixer"), otherwise require ("autoprefixer") ('postcss-import') (), require ("autoprefixer") ({"browsers ": ["Android> = 4.1", "iOS> = 7.0", "ie> = 8"]})] }}, // less process this part {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 in require ("aut Oprefixer "). Otherwise, require (" autoprefixer ") ('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', // The generated file name filename: 'js/common. js'})]}

 

3. execute commands

npm run webpack

 

4. Execution result

The new directory structure of the project:

Common. js code:

 

 

Related Article

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.