gpt4 book ai didi

twitter-bootstrap - 如何配置 webpack 来运行 bootstrap 和 sass?

转载 作者:行者123 更新时间:2023-12-03 13:25:18 25 4
gpt4 key购买 nike

我不太擅长 webpack。我还没有真正“学会”,我有一个从我制作的存储库中提取的模板/样板,它为我提供了一个在 React 中构建的环境。我正在努力向我的项目添加 Bootstrap 功能,因为我不太熟悉 webpack 也不了解加载器等的工作原理。有人可以帮我吗?也许还有关于 webpack 加载器的简单解释? (它在我的学习列表中,但不是优先事项)。

我收到奇怪的错误,例如:

./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2 中出现错误

这是我的 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;

这是我的 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"
]
}
}

最佳答案

查看 example from the sass-loader 。您的 webpack.config.js 应如下所示:

module.exports = {
...
module: {
loaders: [
{
test: /\.woff2?$|\.ttf$|\.eot$|\.svg$/,
loader: "file"
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]
}
};

由于您添加了一堆加载程序,因此您应该确保所有这些加载程序都已安装:

npm i file-loader style-loader css-loader sass-loader --save-dev

然后您应该将 main.scss 作为 webpack 条目添加到 webpack.config.js...

module.exports = {
...
entry: [
path.resolve(__dirname, '..', 'path', 'to', 'main.scss'),
// add other entries
],
...

...或者只是在您的 main.js 中需要/导入它:

require("./path/to/main.scss");

由于 bootstrap 需要配置其 url() 路径,因此您需要在导入 bootstrap 之前设置 $icon-font-path。您的 main.scss 应如下所示:

$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
@import "~bootstrap-sass/assets/stylesheets/bootstrap";

如果您认为 @import "~bootstrap-sass/assets/stylesheets/bootstrap"; 看起来很丑,您还可以向 webpack.config.js 添加别名:

module.exports = {
...
resolve: {
alias: {
"bootstrap-sass$": "bootstrap-sass/assets/stylesheets/bootstrap"
}
},

那么你只需要写:

@import "~bootstrap-sass";

关于twitter-bootstrap - 如何配置 webpack 来运行 bootstrap 和 sass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com