问题
I am trying to use my VSCODDE liver server: https://github.com/ritwickdey/vscode-live-server for development without having to run a (parceljs would be my favorite, since it has hot loading, but it is having issues with reasonml).
This almost works, but the the bucklescript compiler expects that the the node-modules to be on the path, and it appears bundlers find them, but not if one does a load this way:
es5index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<link rel=stylesheet href="./style.css">
<title>Web Data Client</title>
<script src="../src/client/App.bs.js" type=module></script>
</head>
<body>
<div id="index"></div>
</body>
</html>
This is the error I get:
ncaught TypeError: Failed to resolve module specifier "react". Relative references must start with either "/", "./", or "../".
which is to be expected since BSB created this in
App.bs.js
import * as $$Array from "./../../node_modules/bs-platform/lib/es6/array.js";
import * as Block from "./../../node_modules/bs-platform/lib/es6/block.js";
import * as Curry from "./../../node_modules/bs-platform/lib/es6/curry.js";
import * as React from "react";
import * as Glamor from "./../../node_modules/bs-glamor/src/glamor.js";
import * as Js_exn from "./../../node_modules/bs-platform/lib/es6/js_exn.js";
import * as Js_dict from "./../../node_modules/bs-platform/lib/es6/js_dict.js";
import * as Js_json from "./../../node_modules/bs-platform/lib/es6/js_json.js";
import * as ReactDOMRe from "./../../node_modules/reason-react/src/ReactDOMRe.js";
import * as Caml_option from "./../../node_modules/bs-platform/lib/es6/caml_option.js";
import * as ReasonReact from "./../../node_modules/reason-react/src/ReasonReact.js";
import * as Editor$Webdataclient from "./Editor.bs.js";
import * as Loader$Webdataclient from "./Loader.bs.js";
import * as Mutations$Webdataclient from "./Mutations.bs.js";
now if the imports had were from the ../node-modules/* folder, I think all would work
bsconfig.json file:
// This is the configuration file used by BuckleScript's build system bsb.
// Its documentation lives here: http://bucklescript.github.io/bucklescript/docson/#build-schema.json
{
"name": "webdataclient",
"version": "0.1.0",
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"package-specs": {
"module": "es6-global",
"in-source": true
},
"suffix": ".bs.js",
"namespace": true,
"reason": {
"react-jsx": 2
},
"refmt": 3,
"ppx-flags": [
"graphql_ppx/ppx"
],
"bs-dependencies": [
"reason-react",
"bs-fetch",
"bs-glamor"
]
}
回答1:
It doesn't look like BuckleScript can import external JavaScript modules with es6-global
, including React. The relevant GitHub issue is here.
The problem is that BuckleScript currently has no way of knowing the path to the JavaScript file that "react"
is supposed to resolve to. BuckleScript knows the paths to the modules it compiles, so they work fine. But it doesn't know anything about externals, so it outputs their paths as-is.
来源:https://stackoverflow.com/questions/60304777/can-one-load-reasonml-es6-modules-without-a-bundler