Skip to content

Commit

Permalink
fix(bs): Mapping commonjs pkg to browserify
Browse files Browse the repository at this point in the history
close #247
  • Loading branch information
thangngoc89 committed Aug 4, 2019
1 parent 2a63381 commit 047b35f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"git.ignoreLimitWarning": true,
"reason.diagnostics.tools": ["merlin", "bsb"],
"editor.formatOnSave": true,
"reason.format.width": 80,
"javascript.updateImportsOnFileMove.enabled": "never",
"reason_language_server.show_debug_errors": false,
"workbench.colorCustomizations": {
Expand Down
27 changes: 15 additions & 12 deletions client/src/container/Container_fetcher_npm.re
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ module Jsdelivr = {
// cdn_base/npm/thangngoc89@1.0.0/package.json
let url =
[|cdn_base, "npm", slug->Pkg.Slug.to_string, "package.json"|]->Url.join;
let decode_browser = json => {
D.dict(D.string, json)->Result.flatMapError(_ => Ok(Js.Dict.empty()));
};
let decoder = json => {
D.Pipeline.(
succeed((dependencies, browser) =>
Expand All @@ -160,7 +163,7 @@ module Jsdelivr = {
}
)
|> optionalField("dependencies", D.dict(D.string))
|> optionalField("browser", D.dict(D.string))
|> optionalField("browser", decode_browser)
|> run(json)
);
};
Expand Down Expand Up @@ -302,6 +305,15 @@ Try to require a full path, for example: require($(pkg_slug)/path_to_file.js)|j}
Try to require a full path, for example: require($(pkg_slug)/path_to_file.js)|j};

let fetch_commonjs = (~pkg, ~url) => {
[%log.info "pkg_info"; ("pkg", pkg)];
let pkg =
switch (
Container_fetcher_npm_setting.package_map->Js.Dict.get(pkg.Pkg.name)
) {
| Some(name) => {...pkg, name}
| None => pkg
};

let fetch_result:
Future.t(
Belt.Result.t(
Expand Down Expand Up @@ -381,16 +393,6 @@ let fetch_commonjs = (~pkg, ~url) => {
fetch_result;
};

/* TODO: This needs to come from kind of api */
let umd_pathname: Js.Dict.t(string) = [%bs.raw
{|
{
"react": "umd/react.development.min.js",
"react-dom": "umd/react-dom.development.min.js"
}
|}
];

let handle_npm = (~url, ~meta, ~pathname) => {
[%log.info
"npm fetching";
Expand All @@ -406,7 +408,8 @@ let handle_npm = (~url, ~meta, ~pathname) => {
("parsed_slug", Pkg.get_slug(pkg))
];

let is_umd = Js.Dict.get(umd_pathname, pkg.Pkg.name);
let is_umd =
Js.Dict.get(Container_fetcher_npm_setting.umd_pathname, pkg.Pkg.name);

let fetch_result =
switch (is_umd) {
Expand Down
16 changes: 16 additions & 0 deletions client/src/container/Container_fetcher_npm_setting.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let umd_pathname: Js.Dict.t(string) = [%bs.raw
{|
{
"react": "umd/react.development.min.js",
"react-dom": "umd/react-dom.development.min.js"
}
|}
];

let package_map: Js.Dict.t(string) = [%bs.raw
{|
{
"crypto": "crypto-browserify",
}
|}
];
5 changes: 4 additions & 1 deletion client/src/pages/edit/Edit_main.re
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module S = {
->style;
};

let default_value = {code|let str = React.string;
let default_value_react = {code|let str = React.string;
module Counter = {
[@react.component]
Expand All @@ -55,6 +55,9 @@ module Counter = {
ReactDOMRe.renderToElementWithId(<Counter name="Counter" />, "root");|code};

let default_value = {code|[@bs.module] external ulid: unit=>string="ulid";
Js.log(ulid());|code}
let initial_files = {
Belt.Map.String.empty
->Belt.Map.String.set("index.re", Edit_state.make_file(default_value))
Expand Down
21 changes: 14 additions & 7 deletions client/src/pages/edit/Edit_state.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let make_file = code => {
};

let get_extension: string => string = [%raw
filename => {| filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2)|}
filename => {| return filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2)|}
];

let get_extension = filename => {
Expand Down Expand Up @@ -56,14 +56,21 @@ let debounce_compile =
switch (filename->get_extension) {
| Some("re") => Some(Engine_bs.reason_compile(code))
| Some("ml") => Some(Engine_bs.ocaml_compile(code))
| _ => None
| _ =>
%log.info
"Can't get file_extension";
None;
}
)
|> (
fun
| Some(c) => send(Compile_result(filename, c))
| None => ()
)
->(
fun
| Some(c) => {
%log.info
"got compile_result";
send(Compile_result(filename, c));
}
| None => ()
)
);

let reducer = (action, state) => {
Expand Down

0 comments on commit 047b35f

Please sign in to comment.