gpt4 book ai didi

javascript - 无法让 bootstrap-sass 与 webpack 一起工作

转载 作者:可可西里 更新时间:2023-11-01 02:39:25 26 4
gpt4 key购买 nike

我正在按照 the bootstrap-sass-loader page 上的说明进行操作

在我的 package.json 中我得到了

"bootstrap-sass": "^3.3.6",
"bootstrap-sass-loader": "^1.0.9"

这是我的 webpack.config.js

module.exports = {
entry: ['./app.js'],
output: {
publicPath: 'http://localhost:8080/',
filename: 'build/bundle.js'
},
devtool: 'eval',
module: {
/* used on code before it's transformed */
preLoaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: 'source-map'
}
],
/* used to modify code */
loaders: [

{test: /bootstrap\/js\//, loader: 'imports?jQuery=jquery'},
{test: /\.obj|\.mtl|\.html|\.dae|\.txt/, loader: "raw"},
{test: /\.jsx?$/, exclude: /(node_modules)/, loader: 'babel'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file"},
{test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff'},
{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"},
{
test: /\.scss$/,
/* order from bottom to top, so first sass, autoprefix, css and finally style */
loaders: ['style', 'css', 'autoprefixer?browsers=last 3 versions', 'sass?outputStyle=expanded']
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: ['url?limit=8192', 'img']
},
{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: 'babel'
}
],
/* used to modify generated code */
postLoader: []
}
};

据我所知,我只需要使用

require("bootstrap-sass-loader");

在我的 app.js 中完成。但我不能使用任何 Bootstrap 样式。我在这里错过了什么?

最佳答案

它并不简单,但无需执行 json 即可工作。

首先在您的 main.js/ts 中导入 boostrap-sass(通过 npm 安装)

require("bootstrap-sass");

或者如果您使用的是 ts/ES6:

import "bootstrap-sass";

然后只需将这两个导入组件的样式(Angular 2 中的内联或外部 sass/scss 文件):

@import "~bootstrap-sass/assets/stylesheets/bootstrap-sprockets"
@import "../styles/bootstrap/bootstrap"

然后您需要一个包含以下内容的文件夹 ../styles/bootstrap:

  • variables.scss:只需从 node_modules/bootstrap-sass/assets/stylesheets/bootstrap/_variables.scss 复制和定制
  • bootstrap.scss:这是主 Bootstrap 文件的“webpack 化”版本:

//

  @import "variables"; //this is the key to easy customisation
@import "~bootstrap-sass/assets/stylesheets/bootstrap/mixins";
@import "~bootstrap-sass/assets/stylesheets/~bootstrap-sass/assets/stylesheets/bootstrap/normalize";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/print";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/glyphicons";

// Core CSS
@import "~bootstrap-sass/assets/stylesheets/bootstrap/scaffolding";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/type";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/code";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/grid";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/tables";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/forms";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/buttons";

// Components
@import "~bootstrap-sass/assets/stylesheets/bootstrap/component-animations";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/dropdowns";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/button-groups";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/input-groups";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/navs";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/navbar";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/breadcrumbs";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/pagination";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/pager";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/labels";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/badges";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/jumbotron";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/thumbnails";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/alerts";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/progress-bars";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/media";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/list-group";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/panels";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/responsive-embed";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/wells";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/close";

// Components w/ JavaScript
@import "~bootstrap-sass/assets/stylesheets/bootstrap/modals";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/tooltip";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/popovers";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/carousel";

// Utility classes
@import "~bootstrap-sass/assets/stylesheets/bootstrap/utilities";
@import "~bootstrap-sass/assets/stylesheets/bootstrap/responsive-utilities";

最后,webpack 配置位。这可能是装载机:

{
test: /\.(sass|scss)$/,
loader: "file?name=[path][name].css!sass-loader"
}

这是一种加载 jquery 的方法:

new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})

关于javascript - 无法让 bootstrap-sass 与 webpack 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213013/

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