刚接触 webpack,但目前 webpack 正在将样式注入(inject)到我的文档正文中,我想禁用它。我不知道如何实现这一点。
这是我的 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>
如您所见,它仍然注入(inject)与我拥有的 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;
}
最佳答案
您的配置需要解决三个问题。
首先,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
不会包含在最终 bundle 中,因为在 main.scss
中您有
@import 'test';
将其更改为导入_test
,您应该会看到它包含在 bundle 中。
关于javascript - 禁用 webpack 注入(inject) CSS/Autoprefixer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47558438/