diff --git a/src/bsconfig.rs b/src/bsconfig.rs index 813a3994..38beb224 100644 --- a/src/bsconfig.rs +++ b/src/bsconfig.rs @@ -1,6 +1,8 @@ -use serde::{Deserialize, Serialize}; +use serde::Deserialize; +use std::fs; use std::path::{Path, PathBuf}; -use std::{fmt, fs}; + +pub static DEFAULT_SUFFIX: &str = ".mjs"; #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] @@ -121,28 +123,6 @@ pub enum JsxModule { React, } -#[derive(Deserialize, Serialize, Debug, Clone)] -pub enum Suffix { - #[serde(rename = ".js")] - Js, - #[serde(rename = ".mjs")] - Mjs, - #[serde(rename = ".cjs")] - Cjs, - #[serde(rename = ".bs.js")] - BsJs, - #[serde(rename = ".bs.mjs")] - BsMjs, - #[serde(rename = ".bs.cjs")] - BsCjs, -} - -impl fmt::Display for Suffix { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", serde_json::to_value(self).unwrap().as_str().unwrap()) - } -} - #[derive(Deserialize, Debug, Clone)] pub struct JsxSpecs { pub version: Option, @@ -161,7 +141,7 @@ pub struct T { #[serde(rename = "package-specs")] pub package_specs: Option>, pub warnings: Option, - pub suffix: Option, + pub suffix: Option, #[serde(rename = "pinned-dependencies")] pub pinned_dependencies: Option>, #[serde(rename = "bs-dependencies")] diff --git a/src/build/build_types.rs b/src/build/build_types.rs index 6713ad24..37a551e4 100644 --- a/src/build/build_types.rs +++ b/src/build/build_types.rs @@ -117,7 +117,7 @@ pub struct AstModule { pub last_modified: SystemTime, pub ast_file_path: String, pub is_root: bool, - pub suffix: Option, + pub suffix: Option, } pub struct CompileAssetsState { diff --git a/src/build/clean.rs b/src/build/clean.rs index 63f24e3a..8ee32492 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -31,7 +31,7 @@ fn remove_iast(source_file: &str, package_path: &str, root_path: &str, is_root: )); } -fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) { +fn remove_mjs_file(source_file: &str, suffix: &String) { let _ = std::fs::remove_file(helpers::change_extension( source_file, // suffix.to_string includes the ., so we need to remove it @@ -107,12 +107,12 @@ pub fn clean_mjs_files(build_state: &BuildState) { .bsconfig .suffix .to_owned() - .unwrap_or(bsconfig::Suffix::Mjs), + .unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)), )) } _ => None, }) - .collect::>(); + .collect::>(); rescript_file_locations .par_iter() @@ -168,7 +168,9 @@ pub fn cleanup_previous_build( ); remove_mjs_file( &res_file_location, - &suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs), + &suffix + .to_owned() + .unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)), ); remove_iast( res_file_location, diff --git a/src/build/compile.rs b/src/build/compile.rs index a0b9ba6e..64ab73db 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -499,7 +499,7 @@ fn compile_file( // TODO: Also read suffix from package-spec. let suffix = match root_package.bsconfig.suffix.to_owned() { Some(suffix) => suffix, - None => bsconfig::Suffix::Mjs, + None => String::from(bsconfig::DEFAULT_SUFFIX) }; vec![ diff --git a/testrepo/packages/dep01/package.json b/testrepo/packages/dep01/package.json index 7f5035ec..e60d8095 100644 --- a/testrepo/packages/dep01/package.json +++ b/testrepo/packages/dep01/package.json @@ -12,7 +12,7 @@ "author": "", "license": "MIT", "dependencies": { - "rescript": "*", - "@testrepo/dep02": "*" + "@testrepo/dep02": "*", + "rescript": "*" } } diff --git a/testrepo/packages/dep01/src/Dep01.mjs b/testrepo/packages/dep01/src/Dep01.mjs index ea2a8df6..97bd8649 100644 --- a/testrepo/packages/dep01/src/Dep01.mjs +++ b/testrepo/packages/dep01/src/Dep01.mjs @@ -2,9 +2,9 @@ import * as Dep02 from "@testrepo/dep02/src/Dep02.mjs"; -function log(param) { +function log() { console.log("02"); - Dep02.log(undefined); + Dep02.log(); } export { diff --git a/testrepo/packages/dep02/src/Array.mjs b/testrepo/packages/dep02/src/Array.mjs index 4db448cc..8d4a3f1e 100644 --- a/testrepo/packages/dep02/src/Array.mjs +++ b/testrepo/packages/dep02/src/Array.mjs @@ -62,13 +62,13 @@ function filter(prim0, prim1) { function reject(t, fn) { return t.filter(function (el) { - return !Curry._1(fn, el); + return !fn(el); }); } function sortBy(t, fn) { return Belt_SortArray.stableSortBy(t, (function (a, b) { - var match = Curry._2(fn, a, b); + var match = fn(a, b); if (match === "greater_than") { return 1; } else if (match === "less_than") { @@ -104,7 +104,7 @@ function eqBy(_xs, _ys, fn) { if (match$1 === undefined) { return false; } - if (!Curry._2(fn, Caml_option.valFromOption(match), Caml_option.valFromOption(match$1))) { + if (!fn(Caml_option.valFromOption(match), Caml_option.valFromOption(match$1))) { return false; } _ys = Belt_Array.sliceToEnd(ys, 1); @@ -125,7 +125,7 @@ function takeWhile(t, fn) { return ; } var item = t[idx]; - if (!Curry._1(fn, item)) { + if (!fn(item)) { return ; } a.contents = Belt_Array.concat(a.contents, [item]); @@ -150,7 +150,7 @@ function distinct(t, eq) { var y = t[idx]; var acc$1 = Belt_Array.some(acc, (function(y){ return function (x) { - return Curry._2(eq, x, y); + return eq(x, y); } }(y))) ? acc : Belt_Array.concat(acc, [y]); _idx = idx + 1 | 0; @@ -176,7 +176,7 @@ function partition(t, fn) { } var item = t[idx]; var idx$1 = idx + 1 | 0; - if (Curry._1(fn, item)) { + if (fn(item)) { _idx = idx$1; _a = Belt_Array.concat(a, [item]); continue ; @@ -205,7 +205,7 @@ function indexOfBy(t, fn, value) { if (value$p === undefined) { return ; } - if (Curry._2(fn, value, Caml_option.valFromOption(value$p))) { + if (fn(value, Caml_option.valFromOption(value$p))) { return idx; } _idx = idx + 1 | 0; diff --git a/testrepo/packages/dep02/src/Dep02.mjs b/testrepo/packages/dep02/src/Dep02.mjs index 856730e9..86cea085 100644 --- a/testrepo/packages/dep02/src/Dep02.mjs +++ b/testrepo/packages/dep02/src/Dep02.mjs @@ -3,7 +3,7 @@ import * as $$Array from "./Array.mjs"; import * as NS_alias$$atNewNamespace from "@testrepo/new-namespace/src/NS_alias.mjs"; -function log(param) { +function log() { $$Array.forEach([ "a", "b" @@ -12,7 +12,7 @@ function log(param) { })); } -console.log(NS_alias$$atNewNamespace.hello_world(undefined)); +console.log(NS_alias$$atNewNamespace.hello_world()); export { log , diff --git a/testrepo/packages/main/package.json b/testrepo/packages/main/package.json index 66680fb0..5bf3dca7 100644 --- a/testrepo/packages/main/package.json +++ b/testrepo/packages/main/package.json @@ -12,7 +12,7 @@ "author": "", "license": "MIT", "dependencies": { - "rescript": "*", - "@testrepo/dep01": "*" + "@testrepo/dep01": "*", + "rescript": "*" } } diff --git a/testrepo/packages/main/src/Main.mjs b/testrepo/packages/main/src/Main.mjs index 95eb8952..887a8e63 100644 --- a/testrepo/packages/main/src/Main.mjs +++ b/testrepo/packages/main/src/Main.mjs @@ -4,7 +4,7 @@ import * as Dep01 from "@testrepo/dep01/src/Dep01.mjs"; console.log("01"); -Dep01.log(undefined); +Dep01.log(); var $$Array; diff --git a/testrepo/packages/new-namespace/src/NS_alias.mjs b/testrepo/packages/new-namespace/src/NS_alias.mjs index 7ffff5cc..6ab210fe 100644 --- a/testrepo/packages/new-namespace/src/NS_alias.mjs +++ b/testrepo/packages/new-namespace/src/NS_alias.mjs @@ -2,11 +2,11 @@ import * as Other_module$$atNewNamespace from "./Other_module.mjs"; -function hello_world(param) { +function hello_world() { return "Hello world"; } -Other_module$$atNewNamespace.bla(undefined); +Other_module$$atNewNamespace.bla(); export { hello_world , diff --git a/testrepo/packages/new-namespace/src/Other_module.mjs b/testrepo/packages/new-namespace/src/Other_module.mjs index dde37330..c4e5f162 100644 --- a/testrepo/packages/new-namespace/src/Other_module.mjs +++ b/testrepo/packages/new-namespace/src/Other_module.mjs @@ -1,7 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE -function bla(param) { +function bla() { console.log("bla"); } diff --git a/testrepo/yarn.lock b/testrepo/yarn.lock index 80d7fbe7..2a3f684a 100644 --- a/testrepo/yarn.lock +++ b/testrepo/yarn.lock @@ -3,6 +3,6 @@ rescript@*: - version "10.1.2" - resolved "https://registry.yarnpkg.com/rescript/-/rescript-10.1.2.tgz#ed3b8c762d6e22139803c16e765356685693e38c" - integrity sha512-PPdhOiN+lwqxQ0qvzHc1KW0TL12LDy2X+qo/JIMIP3bMfiH0vxQH2e/lXuoutWWm04fGQGTv3hWH+gKW3/UyfA== + version "11.0.0" + resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.0.0.tgz#9a0b6fc998c360543c459aba49b77a572a0306cd" + integrity sha512-uIUwDZZmDUb7ymGkBiiGioxMg8hXh1mze/2k/qhYQcZGgi7PrLHQIW9AksM7gb9WnpjCAvFsA8U2VgC0nA468w== diff --git a/tests/lib/rewatch.lock b/tests/lib/rewatch.lock new file mode 100644 index 00000000..da27e6c5 --- /dev/null +++ b/tests/lib/rewatch.lock @@ -0,0 +1 @@ +57150 \ No newline at end of file diff --git a/tests/snapshots/remove-file.txt b/tests/snapshots/remove-file.txt index a21b563b..283c429e 100644 --- a/tests/snapshots/remove-file.txt +++ b/tests/snapshots/remove-file.txt @@ -16,8 +16,7 @@ The module or file Dep02 can't be found. - If it's a third-party dependency: - - Did you list it in bsconfig.json? - - Did you run `rescript build` instead of `rescript build -with-deps` - (latter builds third-parties)? - - Did you include the file's directory in bsconfig.json? + - Did you add it to the "bs-dependencies" or "bs-dev-dependencies" in bsconfig.json? + - Did you include the file's directory to the "sources" in bsconfig.json? + diff --git a/tests/suffix.sh b/tests/suffix.sh new file mode 100755 index 00000000..a7fe9787 --- /dev/null +++ b/tests/suffix.sh @@ -0,0 +1,56 @@ +source "./utils.sh" +cd ../testrepo + +bold "Test: It should support custom suffixes" + +# Clean Repo +sleep 1 +if rewatch clean &> /dev/null; +then + success "Repo Cleaned" +else + error "Error Cleaning Repo" + exit 1 +fi + +# Replace suffix +replace "s/.mjs/.res.js/g" bsconfig.json + +if rewatch build &> /dev/null; +then + success "Repo Built" +else + error "Error building repo" + exit 1 +fi + +# Count files with new extension +file_count=$(find . -name *.res.js | wc -l) + +if [ "$file_count" -eq 9 ]; +then + success "Found files with correct suffix" +else + error "Suffix not correctly used" + exit 1 +fi + +if rewatch clean &> /dev/null; +then + success "Repo Cleaned" +else + error "Error Cleaning Repo" + exit 1 +fi + +# Restore Suffix +replace "s/.res.js/.mjs/g" bsconfig.json + +# Restore original build +if rewatch build &> /dev/null; +then + success "Repo Built" +else + error "Error building repo" + exit 1 +fi diff --git a/tests/suite-ci.sh b/tests/suite-ci.sh index d0f883ff..bcec5677 100755 --- a/tests/suite-ci.sh +++ b/tests/suite-ci.sh @@ -22,4 +22,4 @@ else exit 1 fi -./compile.sh && ./watch.sh && ./lock.sh +./compile.sh && ./watch.sh && ./lock.sh && ./suffix.sh