如何配置 webpack 以运行 bootstrap 和 sass? [英] How do I configure webpack to run bootstrap and sass?

查看:32
本文介绍了如何配置 webpack 以运行 bootstrap 和 sass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不擅长 webpack.我还没有真正学会",我有一个模板/样板,我从我制作的回购中提取,它为我提供了在 React 中构建的环境.我正在努力为我的项目添加引导程序功能,因为我对 webpack 并不擅长,也不了解加载器等的工作原理.有人可以帮我一把吗?或许还有一个关于 webpack loader 的简单解释?(学习它在我的清单上,但不是优先事项).

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).

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

I get strange errors like:

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

ERROR in ./~/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.woff2

这是我的 webpack.config.js

Here is my 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

Here is my 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"
    ]
  }
}

推荐答案

查看 来自 sass-loader 的示例.你的 webpack.config.js 应该是这样的:

Checkout the example from the sass-loader. Your webpack.config.js should look like this:

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

然后你应该将你的 main.scss 作为 webpack 入口添加到 webpack.config.js...

Then you should add your main.scss either as webpack entry to webpack.config.js...

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

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

... or just require/import it in your main.js:

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

由于 bootstrap 需要配置其 url() 路径,因此您需要在导入 bootstrap 之前设置 $icon-font-path .你的 main.scss 应该是这样的:

Since bootstrap needs its url() paths configured, you need to set the $icon-font-path before importing bootstrap. Your main.scss should look like this:

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

如果你觉得 @import "~bootstrap-sass/assets/stylesheets/bootstrap"; 看起来很丑,你也可以给你的 webpack.config.js 添加一个别名代码>:

And if you think @import "~bootstrap-sass/assets/stylesheets/bootstrap"; looks ugly, you can also add an alias to your webpack.config.js:

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

那么你只需要写:

@import "~bootstrap-sass";

这篇关于如何配置 webpack 以运行 bootstrap 和 sass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆