Webpack configuration: CSS file packaging and JS compression packaging

Source: Internet
Author: User

One, CSS file packaging

1. Create a new CSS file under SRC, create a new index.css file under the CSS file, and enter the following code

body{    color:red;    Font-size:20px;}

2, CSS set up, need to introduce to the import file, where we introduced into the Index.js

 from ' ./css/index.css ' ;d ocument.write ("It works. ");

3. Install Style-loader and Css-loader in terminal

4. After installation, we start to configure module options in Webpack.config.js

// modules: such as interpreting CSS, how images are converted, compressed module:{    rules:[        {            test:/\.css$/,            use:['style-loader ','css-loader'        }    ]},

5, in the terminal input NPM run server, you can see the effect has a style.

Second, JS compression packaging

1, first introduced in the Webpack.config.js

const uglify = require ('uglifyjs-webpack-plugin');

2. Then configure in plugins

// plugins for the production of templates and functions plugins:[    new  uglify ()],//   plugin, multiple plugins, so is an array
ConstPath = require ('Path');ConstUglify = require ('Uglifyjs-webpack-plugin'); Module.exports={    //configuration entries for portal filesentry:{Entry:'./src/index.js'    },    //configuration items for export filesoutput:{//The path of the output, using the node syntaxPath:path.resolve (__dirname,'Dist'),        //the file name of the outputFileName'Bundle.js'}, Mode:"Development",    //modules: such as interpreting CSS, how images are converted, compressedmodule:{rules:[{test:/\.css$/, use:['Style-loader','Css-loader']            }        ]    },    //plugins for the production of templates and functionsplugins:[Newuglify ()],//plug-ins, multiple plugins, so are arrays//Configuring the Webpack development Services featuredevserver:{contentBase:path.resolve (__dirname,'Dist'),//The directory where the page loaded by the local server is locatedHost'192.168.118.221', Compress:true, Port:8081    }//Configuring the Webpack service}

3, in the terminal input Webpack, you will find that the JS code has been compressed

Webpack configuration: CSS file packaging and JS compression packaging

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.