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