twitter-bootstrap 如何配置 webpack 以运行 bootstrap 和 sass?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35542283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure webpack to run bootstrap and sass?
提问by MindfulBell
I am not great at webpack. I have not really 'learned it', I have a template/boilerplate I pull from a repo I made that gives me an environment to build in React. I am struggling to add bootstrap functionality to my projects because I am not great with webpack and understanding how loaders etc. work. Can someone give me a hand? And maybe a simple explanation about webpack loaders? (It is on my list to learn it, but just not a priority).
我不太擅长 webpack。我还没有真正“学会”,我有一个模板/样板,我从我制作的回购中提取,它为我提供了在 React 中构建的环境。我正在努力为我的项目添加引导程序功能,因为我对 webpack 并不擅长,也不了解加载程序等的工作方式。有人可以帮我吗?也许对 webpack loader 做一个简单的解释?(它在我的学习清单上,但不是优先事项)。
I get strange errors like:
我收到奇怪的错误,例如:
ERROR in ./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2
./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2 中的错误
Here is my webpack.config.js
这是我的 webpack.config.js
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel'
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]
}
};
module.exports = config;
Here is my package.json
这是我的 package.json
{
"name": "react-camper-leaderboard",
"version": "1.0.0",
"description": "freecodecamp leaderboard sorter",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack -d --watch",
"build": "webpack -p"
},
"author": "Tim Bell",
"license": "ISC",
"dependencies": {
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"bootstrap": "^4.0.0-alpha.2",
"bootstrap-loader": "^1.0.8",
"bootstrap-sass": "^3.3.6",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"node-sass": "^3.4.2",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"resolve-url-loader": "^1.4.3",
"sass-loader": "^3.1.2",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7"
},
"devDependencies": {
"css-loader": "^0.23.1",
"node-sass": "^3.4.2",
"sass-loader": "^3.1.2",
"webpack": "^1.12.13"
},
"babel": {
"presets": [
"es2015",
"react"
]
}
}
回答by Johannes Ewald
Checkout the example from the sass-loader. Your webpack.config.jsshould look like this:
查看sass-loader 中的示例。你webpack.config.js应该是这样的:
module.exports = {
...
module: {
loaders: [
{
test: /\.woff2?$|\.ttf$|\.eot$|\.svg$/,
loader: "file"
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]
}
};
Since you've added a bunch of loaders, you should make sure that all of them are installed:
由于您添加了一堆加载程序,因此您应该确保安装了所有加载程序:
npm i file-loader style-loader css-loader sass-loader --save-dev
Then you should add your main.scsseither as webpack entry to webpack.config.js...
然后你应该将你的main.scss作为 webpack 条目添加到webpack.config.js...
module.exports = {
...
entry: [
path.resolve(__dirname, '..', 'path', 'to', 'main.scss'),
// add other entries
],
...
... or just require/import it in your main.js:
...或者只是要求/导入它在您的main.js:
require("./path/to/main.scss");
Since bootstrap needs its url()paths configured, you need to set the $icon-font-pathbeforeimporting bootstrap. Your main.scssshould look like this:
由于引导程序需要url()配置其路径,因此您需要$icon-font-path在导入引导程序之前设置。你main.scss应该是这样的:
$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
@import "~bootstrap-sass/assets/stylesheets/bootstrap";
And if you think @import "~bootstrap-sass/assets/stylesheets/bootstrap";looks ugly, you can also add an alias to your webpack.config.js:
如果你觉得@import "~bootstrap-sass/assets/stylesheets/bootstrap";看起来很丑,你也可以给你的 加一个别名webpack.config.js:
module.exports = {
...
resolve: {
alias: {
"bootstrap-sass$": "bootstrap-sass/assets/stylesheets/bootstrap"
}
},
Then you just need to write:
然后你只需要写:
@import "~bootstrap-sass";
回答by zerkms
Every extension you import, requireor load any other way must have its own loader.
每个扩展你import,require或者加载任何其他方式必须有自己的装载机。
As of woff2you might have added something like:
至于woff2你可能已经添加了类似的东西:
{
test: /\.woff2$/,
loader: 'url',
query: {
limit: 10240,
name: 'static/[hash].[ext]'
}
}
You might want to check the url-loaderdocumentationabout all the parameters it accepts.
您可能需要查看有关它接受的所有参数的url-loader文档。
You also may match multiple different filename patterns to use with the same loader, eg:
您还可以匹配多个不同的文件名模式以用于同一个加载器,例如:
{
test: /\.(eot|ttf|svg|png|gif|woff2?$/,
loader: 'url',
query: {
limit: 10240,
name: 'static/[hash].[ext]'
}
}
This would load all those extensions.
这将加载所有这些扩展。
回答by Patryk Wojtasik
It's probably something wrong with ./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2path. Bootstrap-sass requires it through the sass file but webpack can't find it.
可能是./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2路径有问题。Bootstrap-sass 通过 sass 文件需要它,但 webpack 找不到它。

