Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable suffix #86

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<i32>,
Expand All @@ -161,7 +141,7 @@ pub struct T {
#[serde(rename = "package-specs")]
pub package_specs: Option<OneOrMore<PackageSpec>>,
pub warnings: Option<Warnings>,
pub suffix: Option<Suffix>,
pub suffix: Option<String>,
#[serde(rename = "pinned-dependencies")]
pub pinned_dependencies: Option<Vec<String>>,
#[serde(rename = "bs-dependencies")]
Expand Down
2 changes: 1 addition & 1 deletion src/build/build_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct AstModule {
pub last_modified: SystemTime,
pub ast_file_path: String,
pub is_root: bool,
pub suffix: Option<crate::bsconfig::Suffix>,
pub suffix: Option<String>,
}

pub struct CompileAssetsState {
Expand Down
10 changes: 6 additions & 4 deletions src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<Vec<(String, bsconfig::Suffix)>>();
.collect::<Vec<(String, String)>>();

rescript_file_locations
.par_iter()
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
4 changes: 2 additions & 2 deletions testrepo/packages/dep01/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"rescript": "*",
"@testrepo/dep02": "*"
"@testrepo/dep02": "*",
"rescript": "*"
}
}
4 changes: 2 additions & 2 deletions testrepo/packages/dep01/src/Dep01.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions testrepo/packages/dep02/src/Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
Expand All @@ -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;
Expand All @@ -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 ;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions testrepo/packages/dep02/src/Dep02.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,7 +12,7 @@ function log(param) {
}));
}

console.log(NS_alias$$atNewNamespace.hello_world(undefined));
console.log(NS_alias$$atNewNamespace.hello_world());

export {
log ,
Expand Down
4 changes: 2 additions & 2 deletions testrepo/packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"rescript": "*",
"@testrepo/dep01": "*"
"@testrepo/dep01": "*",
"rescript": "*"
}
}
2 changes: 1 addition & 1 deletion testrepo/packages/main/src/Main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Dep01 from "@testrepo/dep01/src/Dep01.mjs";

console.log("01");

Dep01.log(undefined);
Dep01.log();

var $$Array;

Expand Down
4 changes: 2 additions & 2 deletions testrepo/packages/new-namespace/src/NS_alias.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ,
Expand Down
2 changes: 1 addition & 1 deletion testrepo/packages/new-namespace/src/Other_module.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function bla(param) {
function bla() {
console.log("bla");
}

Expand Down
6 changes: 3 additions & 3 deletions testrepo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
1 change: 1 addition & 0 deletions tests/lib/rewatch.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
57150
7 changes: 3 additions & 4 deletions tests/snapshots/remove-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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?


Expand Down
56 changes: 56 additions & 0 deletions tests/suffix.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/suite-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ else
exit 1
fi

./compile.sh && ./watch.sh && ./lock.sh
./compile.sh && ./watch.sh && ./lock.sh && ./suffix.sh
Loading