Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

reactjs - How do I configure webpack to run bootstrap and sass?

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:

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

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;

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"
    ]
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
    ],
    ...

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

require("./path/to/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";

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"
        }
    },

Then you just need to write:

@import "~bootstrap-sass";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...