首页 > 解决方案 > webpack css loader with reason-react

问题描述

I am struggling to get webpack working with my reason app.

I have installed webpack using the following command :

npm install --save-dev webpack

I have installed style-loader and css-loader using the following command :

npm install --save-dev style-loader css-loader

Here is a sample of my package.json file :

 "devDependencies": {
    "bs-platform": "^4.0.18",
    "css-loader": "^2.1.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "sw-precache-webpack-plugin": "^0.11.5",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^3.1.14",
    "webpack-pwa-manifest": "^4.0.0"
  },
  "dependencies": {
    "react": "^16.8.1",
    "react-dom": "^16.8.1",
    "reason-react": ">=0.5.3"
  }

Here is a sample of my webpack.config.js file :

const outputDir = path.join(__dirname, 'build/');

module.exports = {
  entry: './src/Index.bs.js',
  mode: isProd ? 'production' : 'development',
  output: {
    path: outputDir,
    filename: 'index.js',
    publicPath: PUBLIC_PATH
  },
  module: {
    rules: [{
      test: /\.css$/,
      use: [
        { loader: "style-loader" },
        { loader: "css-loader" }
      ]
    }]
  },

I have added the following command to my Index.re file in order to load my css file :

[%raw {|require('../css/index.css')|}] 

Here is the generated js file :

// Generated by BUCKLESCRIPT VERSION 4.0.18, PLEASE EDIT WITH CARE
'use strict';

var Component1 = require("./Component1.bs.js");
var ReactDOMRe = require("reason-react/src/ReactDOMRe.js");
var ReasonReact = require("reason-react/src/ReasonReact.js");

((require('../css/index.css')));

ReactDOMRe.renderToElementWithId(ReasonReact.element(undefined, undefined, Component1.make("Hello!", /* array */[])), "index1");

/*  Not a pure module */

I don't have any error when I launch webpack, but my css file is not generated in the "build" folder... Here are my project folders :

Project folders

标签: csswebpackreasonreason-react

解决方案


It is not generated because style-loader adds on demand to your html file header, on the style tag.

To generate physical css files, use mini-css-extract-plugin


推荐阅读