1. 程式人生 > >webpack-sass-postcss-loader-單個css檔案

webpack-sass-postcss-loader-單個css檔案

1:初始化webpack

mkdir webpack-demo && cd webpack-demo
npm init -y
npm install --save-dev webpack

2: package.json 檔案配置

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
, "build": "webpack", "watch": "webpack --watch" }
, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "autoprefixer": "^7.1.4", "css-loader": "^0.28.7", "extract-text-webpack-plugin": "^3.0.0", "html-webpack-plugin": "^2.30.1", "node-sass": "^4.5.3"
, "postcss-loader": "^2.0.6", "raw-loader": "^0.5.1", "sass": "^1.0.0-beta.2", "style-loader": "^0.18.2", "ts-loader": "^2.3.4", "webpack": "^3.5.5" }
, "dependencies": { "lodash": "^4.17.4" } }

3:postcss.config.js 檔案配置

module.exports = {  
  plugins: {  
    'autoprefixer'
: {browsers: 'last 5 version'} } }

4:webpack.config.js

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin');

// const webpack = require('webpack');

const config = {
    entry: {
        a: './src/sass/a.scss',
        b: './src/sass/b.scss'
    },
    output: {
        filename: '[name].css',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [{
            test: /\.scss$/,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: [
                    { loader: 'css-loader', options: { importLoaders: 1 } },
                    'postcss-loader',
                    { loader: 'sass-loader' }
                ]
            })
        }]
    },
    plugins: [
        // new webpack.optimize.UglifyJsPlugin(),
        // new HtmlWebpackPlugin({
        //     template: './index.html'
        // }),
        new ExtractTextPlugin('[name].css')
    ]
}

module.exports = config;

5:安裝module

npm install

6:執行

npm run watch