对webpack来说还是个新手,但目前webpack正在将样式注入到我的文档正文中,我想禁用它。我不确定如何做到这一点。
这是我的webpack.config.js
var webpack = require('webpack');
var autoprefixer = require('autoprefixer');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
output: {
filename: 'bundle.js'
},
entry: {
app: './js/app.js'
},
module: {
loaders: [
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({use:[{loader: 'css-loader', options: {importLoaders: 1}}, 'postcss-loader', 'sass-loader']})
}
]
},
plugins: [
new ExtractTextPlugin('styles.css'),
new webpack.LoaderOptionsPlugin({options: {
context: __dirname,
postcss: [
autoprefixer
]
}})
]
};html的输出是
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="/wp-content/themes/index/styles.css">
<style type="text/css">
body {
background-color: black; }
h1 {
color: #fff; }
p {
display: flex;
color: #fff; }
</style>
<style type="text/css"></style>
</head>
<body>
<h1>Test</h1>
<p>Test</p>
<script src="/wp-content/themes/index/dist/bundle.js"></script>
</body>
</html>正如你所看到的,它仍然是注入与我的scss文件不匹配的css。它也没有为flex属性加上前缀,也没有在我的sass文件中包含一个导入。
main.scss
@import 'test';
body {
background-color: black;
}
h1 {
color: #fff;
}
p {
display: flex;
color: #fff;
background-color: red;
}_test.scss
h2 {
color: blue;
}发布于 2017-11-30 04:34:29
您的配置需要解决三个问题。
首先,应该将module中的loaders属性重命名为rules。你现在用的方式是webpack 1的正确方式,webpack 2+用的是rules。https://webpack.js.org/guides/migrating/#module-loaders-is-now-module-rules
第二,LoaderOptionsPlugin是用来从webpack 1迁移到webpack 2的。你可以在here上读到。
推荐的将选项附加到postcss-loader的新方法如下所示。
{
loader: 'postcss-loader',
options: {
plugins: [autoprefixer()]
}
}有了上面的配置,您的css应该是前缀,您就可以安全地从plugins数组中删除webpack.LoaderOptionsPlugin了。
最后,如果上面提供的信息是正确的,那么_test.scss不会包含在最终包中,因为在main.scss中
@import 'test';将其更改为导入_test,您应该会看到它包含在包中。
https://stackoverflow.com/questions/47558438
复制相似问题