简体   繁体   English

从.sass到.css文件与webpack

[英]from .sass to .css file with webpack

So I have SASS files and I want to use webpack to transpile it down to css, but I don't want an ugly looking <style></style> tag to be added on my document when it is loaded and I don't want to do require('style!sass!./file.sass') to my every react components but rather I want all my sass files to be transpiled into one file called app.css but I can't make it work. 所以我有SASS文件,我想使用webpack将其转换为css,但我不希望在我的文档加载时添加一个丑陋的<style></style>标记,而不是我希望对我的每个反应组件都做require('style!sass!./file.sass') ,而我希望将所有我的sass文件转换成一个名为app.css的文件,但我无法使其工作。 Please help me do this. 请帮我这样做。

  • webpack version 2.2.1 webpack 2.2.1版

My webpack.config.js 我的webpack.config.js

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

module.exports = {
    entry: [
        __dirname + '/resources/assets/js/app.js',
        __dirname + '/resources/assets/sass/app.sass'
    ],

    output: {
        path: __dirname + '/public/js',
        filename: 'app.js'
    },

    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            },

            {
                test: /\.sass$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract([
                    'style-loader',
                    'sass-loader'
                ])
            }
        ]
    },

    plugins: [
        new ExtractTextPlugin({
            filename: __dirname + '/public/css/app.css',
            allChunks: true
        })
    ]
}

using that webpack.config.js file gives me the following error when I run webpack . 使用该webpack.config.js文件在运行webpack时出现以下错误。

C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Chunk.js:59
                throw new Error("Chunk.entry was removed. Use hasRuntime()");
                ^

Error: Chunk.entry was removed. Use hasRuntime()
    at Chunk.get entry [as entry] (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Chunk.js:59:9)
    at C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\extract-text-webpack-plugin\index.js:201:13
    at Array.filter (native)
    at Compilation.<anonymous> (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\extract-text-webpack-plugin\index.js:200:37)
    at Compilation.applyPlugins0 (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\tapable\lib\Tapable.js:68:14)
    at Compilation.seal (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Compilation.js:558:8)
    at C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Compiler.js:474:16
    at C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\tapable\lib\Tapable.js:225:11
    at _addModuleChain (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Compilation.js:472:11)
    at processModuleDependencies.err (C:\Users\me\Documents\projects\web\experiments\3-projectname\node_modules\webpack\lib\Compilation.js:443:13)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v6.9.1
npm ERR! npm  v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! 3-projectname@1.0.0 build: `node ./node_modules/webpack/bin/webpack.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the 3-projectname@1.0.0 build script 'node ./node_modules/webpack/bin/webpack.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the 3-projectname package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./node_modules/webpack/bin/webpack.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs 3-projectname
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls 3-projectname
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\me\Documents\projects\web\experiments\3-projectname\npm-debug.log

My sass file 我的sass文件

@import './partials/globals'

As of webpack 2 this is how you configure the plugin: 从webpack 2开始,这是你配置插件的方式:

// In the loaders (now 'rules') array
{
  test: /\.sass$/,
  exclude: /node_modules/,
  use: ExtractTextPlugin.extract({
    fallbackLoader: "style-loader", // Will inject the style tag if plugin fails
    loader: "css-loader!sass-loader",
  }),
}

plugins: [
  new ExtractTextPlugin({ filename: 'bundle.css', disable: false, allChunks: true })
]

You had also forgotten to use the css loader after the sass loader. 你也忘了在sass loader之后使用css loader。

Make sure you have v2 of the Extract Text plugin installed. 确保安装了Extract Text插件的v2。 v1 does not work with webpack 2. v1不适用于webpack 2。

Your webpack config looks like it was made for webpack 1. https://webpack.js.org/guides/migrating/ 你的WebPack配置看起来好像是对的WebPack 1.取得https://webpack.js.org/guides/migrating/

I agree with @epiqueras , if you are using webpack2 then you need to also use version 2.x of extract-text-webpack-plugin or your build will fail. 我同意@epiqueras ,如果您使用webpack2那么您还需要使用2.x版本的extract-text-webpack-plugin否则您的构建将失败。

So in your case, it's going to be like this. 所以在你的情况下,它会是这样的。

module.exports = {
entry: [
    __dirname + '/resources/assets/js/app.js',
    __dirname + '/resources/assets/sass/app.sass'
],

output: {
    path: __dirname + '/public/js',
    filename: 'app.js'
},

module: {
    rules: [ // from 'loaders' to 'rules'
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
                presets: ['es2015', 'react']
            }
        },

        {
            test: /\.sass$/,
            exclude: /node_modules/,
            loader: ExtractTextPlugin.extract({
              fallbackLoader: 'style-loader',
              loader: ['style-loader','sass-loader']
            })
        }
    ]
},

plugins: [
    new ExtractTextPlugin('bundle.css') // what the output file (css) is going to be
]

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM