使用webpack进行Scss提取

时间:2016-05-08 15:57:22

标签: javascript css node.js sass webpack

我正在尝试使用ExtractTextPlugin将我的scss提取并转换为css并将其与client.min.js一起移动到src文件夹,但在构建webpack.config.js文件时,当前遇到以下问题:

   ./scss/styles.js中的错误   找不到模块:错误:无法解析模块' main.scss'在C:\ Users \ ajoku \ Desktop \ Web Projects \ learn2node \ http \ scss中    @ ./scss/styles.js 3:0-20

我的webpack.config.js文件:

"use strict";
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
let extractSCSS = new ExtractTextPlugin('./scss/main.scss',{allChunks: true});
module.exports = {
  context: path.join(__dirname, "http"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: {
    js:"./react/client.js",
    scss: "./scss/styles.js",},
  module: {
      loaders: [
        {
          test: /\.js?$/,
          exclude: /node_modules/,
          loader: 'babel-loader',
          query: {
            presets: ['react', 'es2015', 'stage-0'],
            plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
          }},
        {test: /\.scss$/i, loader: ExtractTextPlugin.extract('css!scss')},
      ]
    },
  output: {
    path: "./src/",
    filename: "client.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
  plugins: [
    new ExtractTextPlugin('main.css', {
            allChunks: true
        })
  ]
};

我的styles.js文件

require("main.scss");

我的scss文件与styles.js文件一起位于http/scss 可能导致问题的原因是什么?谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

这可以使用sass-loader和extract-text-webpack-plugin

webpack.config

module.exports = {
  module: {
    {
      test: /\.scss$/,
      include: path.resolve(__dirname, 'http/scss'),
      loader: ExtractTextPlugin.extract('style', 'css?modules&localIdentName=[name]-[local]!autoprefixer!sass')
    }
  }
  plugins: [
    new ExtractTextPlugin('[name]-[hash].min.css')
  ]
}

的package.json

"devDependencies": {
    "autoprefixer-loader": "^3.1.0",
    "css-loader": "^0.23.0",
    "node-sass": "^3.4.2",
    "sass-loader": "^3.1.2",
    "extract-text-webpack-plugin": "^0.9.1",
    "style-loader": "^0.13.0"
}