Using Webpack 2和sass-loader 4.11
webpack --config webpack.config.js
这是我的webpack.config.js
var path = require('path');
var sass = require("./sass/lifeleveler.scss");
module.exports = {
entry: './dist/main.js',
output: {
filename: 'lifeleveler.app.js',
path: path.resolve(__dirname, 'dist')
},
watch: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
module: {
loaders: [
{
test: /\.scss$/,
include: [
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, './sass')
],
loaders: ["style-loader", "css-loader", "sass-loader"]
}
],
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{
enforce: 'pre',
test: /\.tsx?$/,
use: "source-map-loader"
}
]
},
sassLoader: {
includePaths: [path.resolve(__dirname, "./sass")]
},
resolve: {
modulesDirectories: ['./sass'],
extensions: [".tsx", ".ts", ".js", "scss"]
},
devtool: 'inline-source-map'
};
请注意我在配置顶部尝试此操作时:
var css = require("!raw-loader!sass-loader!./sass/lifeleveler.scss");
var css = require("!css-loader!sass-loader!./sass/lifeleveler.scss");
出现此错误:
错误:找不到模块'!raw-loader!sass-loader!./ sass / lifeleveler.scss'
这就是为什么我这样做的原因:var sass = require("./sass/lifeleveler.scss");
我的lifeleveler.scss文件(from my SASS project starter kit):
@import "vendors/normalize"; // Normalize stylesheet
@import "vendors/reset"; // Reset stylesheet
@import "modules/base"; // Load base files
@import "modules/defaults"; // Defaults elements
@import "modules/inputs"; // Inputs & Selects
@import "modules/buttons"; // Buttons
@import "modules/layout"; // Load Layouts
@import "modules/svg"; // Load SVG
@import "modules/queries"; // Media Queries
最后我的文件夹结构,你看到任何明显的问题吗?
I found this solution here,但我将./sass
路径添加到modulesDirectories
数组但仍然收到错误。
经过多次挖掘后,我也发现了this Github issue and solution here,但他的修复对我来说也不起作用:(
答案 0 :(得分:9)
我在这里重现了你的问题,并且能够解决它。
首先,您需要更改webpack配置文件。一些要点:
scss
加载器置于rules
选项中,并将每个加载器分隔为一个对象(如this); var sass = require("./sass/lifeleveler.scss");
。应该从入口点JS调用该文件。在这种情况下:dist/main.js
。此时(在读取配置之前),webpack未配置为加载任何内容。这导致你出现的错误。sassLoader.includePaths
和resolve.modulesDirectories
,因为它们无效 webpack 2 键。Webpack 2 配置结构与 Webpack 1 略有不同(您可以查看官方迁移指南here)..
工作webpack.config.js
文件将是这样的:
var path = require('path');
module.exports = {
entry: './dist/main.js',
output: {
filename: 'lifeleveler.app.js',
path: path.resolve(__dirname, 'dist')
},
watch: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
module: {
rules: [
{
test: /\.scss$/,
include: [
path.resolve(__dirname, "sass")
],
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{ loader: "sass-loader" }
]
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{
enforce: 'pre',
test: /\.tsx?$/,
use: "source-map-loader"
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js", "scss"]
},
devtool: 'inline-source-map'
};
在main.js
中,现在您可以要求调用scss文件:
require('../sass/lifeleveler.scss'); //assuming main.js is at ./dist
确保您已安装npm style-loader
,css-loader
,node-sass
和sass-loader
。
现在编译正确!
答案 1 :(得分:3)
尝试将此配置发送到webpack.config.js
...
module: {
rules: [
{
test: /.s?css$/,
use: ExtractTextWebpack.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoader: true
}
},
{
loader: 'postcss-loader',
options: {
plugins: [
... // fill your config
]
}
},
{
loader: 'sass-loader',
options: {
... // fill your config
}
}
]
})
}
]
},
plugins: [
new ExtractTextWebpack({
filename: 'bundle.css'
})
]
...
这是您需要的依赖项版本。
"devDependencies": {
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "beta",
"node-sass": "^4.5.0",
"postcss-loader": "^1.2.2",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"webpack": "^2.2.1",
}
不要忘记将style.scss
root用于您的输入文件。
示例:强>
在您的./dist/main.js
中,您需要写下require('../sass/liveleverer.scss')