1、安裝外掛程式 npm i style-loader css-loader --save-dev npm i postcss-loader --save-dev npm i autoprefixer --save-dev npm install postcss-import --save-dev登入後複製style-loader外掛程式為:透過注入標籤將CSS加入DOM中</p><p>autoprefixer 自動加入前綴<br/></p>##postcss-import:支援使用@import引入css<p><br/></p>2、專案目錄結構:<p></p><p><img src="https://img.php.cn/upload/article/000/000/194/37a612e2cd3095444e530b7035912824-0.png" alt=""/><br/></p><p>common.css為:<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">登入後複製</div></div></p><p>flex.css為:<span style="font-family: 宋体; font-size: 18px;"></span></p><p class="cnblogs_code">##<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">.flex-p{ display: flex; }</pre><div class="contentsignin">登入後複製</div></div><br/></p>app.js為:<p><span style="font-family: 宋体; font-size: 18px;"></span></p><p class="cnblogs_code">#<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">登入後複製</div></div><br/></p><p class="cnblogs_code">##3、webpack .config.js設定檔為:<br/></p><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">登入後複製</div></div><p class="cnblogs_code"><br/>4、執行編譯&檢視結果</p><p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:css;toolbar:false;">npm run webpack</pre><div class="contentsignin">登入後複製</div></div><span style="font-family: 宋体; font-size: 18px;"></span><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>