我试图使用webpack从bootstrap 4.0.0 beta scss输出css文件。我按照https://getbootstrap.com/docs/4.0/getting-started/webpack/成功处理了带内联样式注入的scss。将样式注入文档的头部有一些问题。
这是我的package.json:
{
"name": "testproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/knockout": "^3.4.46",
"bootstrap": "^4.0.0-beta",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.1",
"html-webpack-plugin": "^2.30.1",
"jquery": "^3.2.1",
"knockout": "^3.4.2",
"node-sass": "^4.5.3",
"popper.js": "^1.12.5",
"postcss-loader": "^2.0.8",
"precss": "^2.0.0",
"sass-loader": "^6.0.6",
"style-loader": "^0.19.0",
"ts-loader": "^3.0.2",
"typescript": "^2.5.3",
"url-loader": "^0.6.2",
"webpack": "^3.7.1",
"webpack-notifier": "^1.5.0"
},
"-vs-binding": {
"BeforeBuild": [
"build"
]
},
"dependencies": {}
}
和我的webpack.config.js(已更新):
var path = require('path');
var webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractScss = new ExtractTextPlugin({
filename: 'dist/[name].[hash].css',
allChunks: true,
});
module.exports = {
entry: {
site: [
'./wwwroot/js/site.js',
'./Script/testkoviewmodel.ts'],
},
output: {
publicPath: "/dist/",
path: path.join(__dirname, '/wwwroot/dist/'),
filename: '[name].[hash].build.js',
},
plugins: [
extractScss,
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warnings: false
},
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
}),
new HtmlWebpackPlugin({
inject: false,
template: 'Views/Shared/_LayoutTemplate.cshtml',
filename: '../../Views/Shared/_Layout.cshtml',
}),
],
devtool: 'source-map',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
{
test: /\.scss$/,
use: extractScss.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader']
}),
exclude: /node_modules/,
},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
{ test: /\.(woff|woff2)$/, loader: "url?prefix=font/&limit=5000" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.scss'],
}
};
我已尝试对webpack配置进行各种修改,因为很多人都有相同或相关的问题。到目前为止,我显然没有成功。
**编辑!重要:整个问题是排除属性应该是QUOTED,因为它们不是正则表达式,而是在这种情况下的字符串。感谢BudHead!
答案 0 :(得分:1)
假设您在条目js中导入bootstrap 4:
import 'bootstrap/scss/bootstrap.scss';
...尝试以下插件设置:
const extractScss = new ExtractTextPlugin({
filename: 'assets/css/[name].[contenthash].css',
allChunks: true
});
...将以上内容添加到您的插件数组中:
plugins: [extractScss]
...然后在你的规则中:
{
test: /\.scss$/,
use: extractScss.extract({
fallback: 'style-loader',
//resolve-url-loader may be chained before sass-loader if necessary
use: ['css-loader', 'sass-loader']
})
}
所以构建你的ExtracTextPlugin非常重要,我在这里设置了
const extractScss = new ExtractTextPlugin(...)
稍后在规则配置中,您将传递相同的对象。在上面的代码中,您使用的是导入的ExtractTextPlugin实例,而不是您在插件数组中创建的新实例(没有设置选项)。
您可能需要调整插件选项中的文件名,我的设置为 assets / css / main.awef32fq23faf23aasdf.css