1. Install the plug-in ##npm i style-loader css-loader --save-dev npm i postcss-loader --save-dev npm i autoprefixer --save-dev npm install postcss-import --save-devCopy after login The style-loader plug-in is: insert the CSS by injecting the tag Add to DOM<p></p>autoprefixer automatically adds prefix<p><br/></p>postcss-import: supports using @import to introduce css<p><br/></p>2. Project directory structure: <p></p><p><img src="https://img.php.cn/upload/article/000/000/194/37a612e2cd3095444e530b7035912824-0.png" alt=""/><br/></p><p>common.css is: <span style="max-width:90%"></span><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">@import './flex.css'; html,body{ padding: 0; margin: 0; background-color: red; }ul{ list-style: none; margin: 0; }</pre><div class="contentsignin">Copy after login</div></div></p><p>flex.css is: <span style="font-family: 宋体; font-size: 18px;"></span></p><p class="cnblogs_code"><br/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">.flex-p{ display: flex; }</pre><div class="contentsignin">Copy after login</div></div></p><p>app.js is: <span style="font-family: 宋体; font-size: 18px;"></span></p><p class="cnblogs_code"><br/><div class="code" style="position:relative; padding:0px; margin:0px;"><pre>import './css/common.css'; import layer from './components/layer/layer.js'const App = function(){ console.log(layer) }new App()</pre><div class="contentsignin">Copy after login</div></div></p><p class="cnblogs_code"><br/>##3. webpack The .config.js configuration file is: </p><p class="cnblogs_code"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">var htmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output: { path: __dirname + '/dist', filename: 'js/[name].js' }, module: { loaders: [{ test: /\.js$/, //以下目录不处理 exclude: /node_modules/, //只处理以下目录 include: /src/, loader: "babel-loader", //配置的目标运行环境(environment)自动启用需要的 babel 插件 query: { presets: ['latest'] } }, //css 处理这一块 { test: /\.css$/, use: [ 'style-loader', { loader: 'css-loader', options: { //支持@important引入css importLoaders: 1 } }, { loader: 'postcss-loader', options: { plugins: function() { return [ //一定要写在require("autoprefixer")前面,否则require("autoprefixer")无效 require('postcss-import')(), require("autoprefixer")({ "browsers": ["Android >= 4.1", "iOS >= 7.0", "ie >= 8"] }) ] } } } ] } ] }, plugins: [ new htmlWebpackPlugin({ template: 'index.html', filename: 'index.html' }) ] }</pre><div class="contentsignin">Copy after login</div></div><br/></p>4. Execute compilation & view the results<p><span style="font-family: 宋体; font-size: 18px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">npm run webpack</pre><div class="contentsignin">Copy after login</div></div></p> <p><span style="font-family: 宋体; font-size: 18px;"><img src="https://img.php.cn/upload/article/000/000/194/37a612e2cd3095444e530b7035912824-1.png" alt=""> </span> </p>