Example of how to package and compress JavaScript and css in webpack, webpackjs

Source: Internet
Author: User

Example of how to package and compress JavaScript and css in webpack, webpackjs

Package and compress js and css

Because webpack itself integrates the UglifyJS plug-in (webpack. optimize. UglifyJsPlugin) to compress and confuse JavaScript with CSS, no additional plug-ins need to be referenced,

The webpack-p command calls UglifyJS to compress the code. Many other webpack plug-ins, such as html-webpack-plugin, also use UglifyJS by default.

The uglify-js release only supports ES5. If you want to compress ES6 + code, use the Development Branch.

Available options for UglifyJS include:

Parse explanation

Compress Compression

Mangle Obfuscation

Beau beautification

Minify minimal // used in the htmlins HtmlWebpackPlugin

CLI

Source code ing after sourcemap compilation for web page debugging

AST abstract syntax tree

Name, including the variable name, function name, and attribute name

Toplevel top-level scope

Unreachable code inaccessible

Option

STDIN standard input, which is directly input in the command line

STDOUT standard output

STDERR standard error output

Side effects function side effects, that is, the function has other functions besides the return, such as modifying global variables.

Configure one column:

// Use the plug-in html-webpack-plugin to package and merge html // use the plug-in extract-text-webpack-plugin to package independent css // use the UglifyJsPlugin compression code var HtmlWebpackPlugin = require ('html -webpack-plugin '); var ExtractTextPlugin = require ('extract-text-webpack-plugin'); var webpack = require ("webpack"); module. exports = {entry: {bundle :'. /src/js/main. js'}, output: {filename: "[name]-[hash]. js ", path: _ dirname + '/dist'}, module: {rules: [{test: /\. Css $/, use: ExtractTextPlugin. extract ({fallback: "style-loader", use: "css-loader"}) },{ test :/\. (png | jpg | jpeg | gif) $/, use: 'url-loader? Limit = 8192 '}]}, resolve: {extensions: Comment '.js', '.css ','. json'] // used to configure which file suffixes the program can complete}, plugins: [new HtmlWebpackPlugin ({title: 'Hello webpackage', template: 'src/component/index.html ', inject: 'body', minify: {// decompress the HTML file removeComments: true, // remove the comment collapseWhitespace: true // Delete the blank and linefeed} from the HTML file }}), new ExtractTextPlugin ("[name).[hash).css"), new webpack. optimize. uglifyJsPlugin ({compress: {// compress code dead_code: true, // remove the code warnings: false that has not been referenced. // when you delete code that is useless, a warning loops is displayed: true // when the judgment conditions of the do, while, and for loops can be determined to be, the conditions are optimized.}, else t: ['$ super',' $ ', 'exports ', 'require '] // obfuscation and excluding keywords})]};

Note that some keywords need to be excluded during compression and cannot be confused, such as $ or require. If obfuscation occurs, the normal operation of the code will be affected.

Several common attributes of column compression:

  1. Dead_code -- remove the code that is not referenced
  2. Loops-when the judgment conditions of do, while, and for loops can be determined, optimization is performed on them.
  3. Warnings -- displays a warning when you delete code that is useless.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.