我正在努力使Webpack与我的理性应用一起工作。
我已经使用以下命令安装了webpack:
npm install --save-dev webpack
我已经使用以下命令安装了style-loader和css-loader:
npm install --save-dev style-loader css-loader
这是我的package.json文件的示例:
"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"
}
这是我的webpack.config.js文件的示例:
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" }
]
}]
},
我已将以下命令添加到我的Index.re文件中,以加载我的CSS文件:
[%raw {|require('../css/index.css')|}]
这是生成的js文件:
// 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 */
启动webpack时没有任何错误,但是我的css文件未在“ build”文件夹中生成... 这是我的项目文件夹:
答案 0 :(得分:1)
之所以不会生成,是因为style-loader在样式标签上按需添加了html文件头。
要生成物理的CSS文件,请使用mini-css-extract-plugin