首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Webpack编写SASS文件

用Webpack编写SASS文件
EN

Stack Overflow用户
提问于 2018-07-19 17:40:08
回答 1查看 535关注 0票数 0

我想使用webpack,这样它将自动将我/src/app文件夹中的任何和所有.scss文件编译成一个.css文件,而无需我解释指向所有的.scss文件/导入它们。

我正在尝试使用ExtractTextPlugin来完成这个任务,但是它似乎不起作用。我需要提供一个更具体的入口点吗?我的装载机配置不正确吗?或者还有什么不对劲吗?谢谢!

webpack.config.js:

代码语言:javascript
复制
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const DIST_DIR = path.resolve(__dirname, "dist");
const SRC_DIR = path.resolve(__dirname, "src");

const config = {
  devtool: 'inline-source-map',
  entry: [
    "babel-polyfill",
    SRC_DIR + "/app/index.js"
    SRC_DIR + "/app/"
  ],
  target: 'web',
  output: {
    path: DIST_DIR + "/app/",
    filename: "bundle.js",
    publicPath: "/app/"
  },
  devServer: {
    contentBase: './dist',
    historyApiFallback: true,
    hot: true,
    proxy: {
      '/api': {
        target: 'http://localhost:5001',
        secure: false,
      },
    }
  },
  plugins: [
    new ExtractTextPlugin({filename: "foo.css", allChunks: true})
  ],
  module: {
    rules: [
      {
        enforce: "pre",
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "eslint-loader",
        options: {
          failOnWarning: false,
          failOnError: true
        }
      },
      {
        test: /\.js$/,
        include: SRC_DIR,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'stage-2']
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: 'css-loader?importLoaders=1',
        })
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract(['css-loader', 'sass-loader']),
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loaders: ['file-loader?context=src/images&name=images/[path][name].[ext]', {
          loader: 'image-webpack-loader',
          query: {
            mozjpeg: {
              progressive: true,
            },
            gifsicle: {
              interlaced: false,
            },
            optipng: {
              optimizationLevel: 7,
            },
            pngquant: {
              quality: '75-90',
              speed: 3,
            },
          },
        }],
        exclude: path.resolve(__dirname, "node_modules"),
        include: __dirname,
      },
      {
        test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        // loader: "url?limit=10000"
        use: "url-loader"
      },
      {
        test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
        use: 'file-loader'
      },
    ]
  },
};

module.exports = config;

package.json

代码语言:javascript
复制
{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "watch": "webpack --progress --watch",
    "start": "yarn build",
    "build": "webpack -d && cp src/index.html dist/index.html && webpack-dev-server --inline --hot --history-api-fallback",
    "build:dev": "webpack && cp src/index.html dist/index.html",
    "build:prod": "webpack -p && cp src/index.html dist/index.html"
  },
  "author": "",
  "license": "UNLICENSED",
  "devDependencies": {
    "babel-cli": "7.0.0-beta.3",
    "babel-eslint": "7",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "css-loader": "^0.28.7",
    "enzyme": "^3.1.0",
    "enzyme-adapter-react-16": "^1.0.1",
    "eslint": "3.x",
    "eslint-config-airbnb": "^15.1.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.4.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "fetch-mock": "^6.0.0-beta.7",
    "file-loader": "^0.11.2",
    "image-webpack-loader": "^3.4.2",
    "jest": "^23.1.0",
    "jest-enzyme": "^4.0.0",
    "jest-fetch-mock": "^1.6.4",
    "node-sass": "^4.9.0",
    "redux-mock-store": "^1.5.3",
    "sass-loader": "^6.0.6",
    "url-loader": "^0.5.9",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  },
  "dependencies": {
    "@trendmicro/react-toggle-switch": "^0.5.7",
    "babel-polyfill": "^6.26.0",
    "cross-fetch": "^1.1.1",
    "font-awesome": "^4.7.0",
    "highcharts": "^6.0.4",
    "history": "^4.7.2",
    "js-cookie": "^2.2.0",
    "less-loader": "^4.0.5",
    "libphonenumber-js": "^0.4.42",
    "lodash": "^4.17.4",
    "moment": "^2.19.1",
    "prop-types": "^15.6.0",
    "query-string": "^5.0.1",
    "rc-time-picker": "^3.1.0",
    "react": "^16.0.0",
    "react-animations": "^1.0.0",
    "react-autosuggest": "^9.3.4",
    "react-circular-progressbar": "^0.8.0",
    "react-datepicker": "^0.59.0",
    "react-dom": "^16.0.0",
    "react-highcharts": "^15.0.0",
    "react-list": "^0.8.8",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^5.0.0-alpha.6",
    "react-select": "^1.0.0-rc.10",
    "react-transition-group": "^1.2.0",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "styled-components": "3.2.3",
    "twilio-client": "^1.4.33"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js"
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-19 17:44:28

无需我解释,就可以指向所有的.scss文件/导入它们。

你不能这样做,webpack不像gulp或其他任务跑步者,你只需指出模式,它就会将转换应用到他们身上。

Webpack处理依赖关系图,这些图表将从您的入口点开始。在每个入口点上,它读取依赖项并应用为每个文件扩展名指定的加载器。Webpack只知道该文件的存在,如果该文件是在依赖图的任何文件中导入的。

如果您想以您所描述的方式转换css,我建议您转向更多的任务运行库,例如gulp。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51428872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档