Webpack working with CSS

Source: Internet
Author: User

1, install the plug-in

NPM i style-loader css-loader--save-devnpm i postcss-loader--save---save-devnpm Install Postcss -import--save-dev
The Style-loader plugin is: <style> add CSS to the DOM by injecting tags
Autoprefixer adding prefixes automatically
Postcss-import: Support for using@import引入css

2、项目目录结构:

Common.css is:

@Import './flex.css '; html,body{    padding: 0;     Margin: 0;     background-color: red;} ul {    list-style: none;     margin: 0;}

FLEX.CSS is:

. Flex-div {    display: flex;}

App.js is:

Import  './css/common.css'./components/layer/layer.js 'function() {    Console.log (Layer)}new App ()

3、webpack.config.js配置文件为:
varHtmlwebpackplugin = require (' Html-webpack-plugin ')); Module.exports={entry:'./src/app.js ', Output: {path: __dirname+ '/dist ', FileName:' Js/[name].js '}, module: {loaders: [{test:/\.js$/,                //The following directory does not handleExclude:/node_modules/,                //only the following directories are processedInclude:/src/, Loader:"Babel-loader",                //The configured target Runtime Environment (environment) automatically enables the required Babel plug-insquery: {presets: [' Latest ']                }            },             //CSS handles this piece {test:/\.css$/, use: [' Style-loader ', {loader:' Css-loader ', options: {//Support @important introduction of CSSImportloaders:1}}, {loader:' Postcss-loader ', Options: {plugins:function() {                                return [                                    //be sure to write in front of require ("autoprefixer"), otherwise require ("Autoprefixer") is invalidRequire (' Postcss-import ') (), Require ("Autoprefixer")({                                        "Browsers": ["Android >= 4.1", "IOS >= 7.0", "IE >= 8"]                                    })                                ]                            } }}]}]}, plugins: [Newhtmlwebpackplugin ({Template:' Index.html ', FileName:' Index.html '        })    ]}

4. Perform compilation & View results

NPM Run Webpack

 

Webpack working with CSS

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.