Skip to content

Commit

Permalink
Fix .d.ts resolve in TS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Feb 9, 2025
1 parent d733a7f commit c5811fc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 174 deletions.
46 changes: 27 additions & 19 deletions packages/prepack/src/Prepack.res
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,35 @@ let filesMapping = [
("setGlobalConfig", "S.setGlobalConfig"),
]

NodeJs.Fs.writeFileSyncWith(
"./src/S.mjs",
["import * as S from \"./S_Core.res.mjs\";"]
->Array.concat(filesMapping->Array.map(((name, value)) => `export const ${name} = ${value}`))
->Array.join("\n")
->NodeJs.Buffer.fromString,
{
encoding: "utf8",
},
)
sourePaths->Array.forEach(path => {
FsX.cpSync(
~src=NodeJs.Path.join2(projectPath, path),
~dest=NodeJs.Path.join2(artifactsPath, path),
{recursive: true},
)
})

let writeSjsEsm = path => {
NodeJs.Fs.writeFileSyncWith(
path,
["import * as S from \"./S_Core.res.mjs\";"]
->Array.concat(filesMapping->Array.map(((name, value)) => `export const ${name} = ${value}`))
->Array.join("\n")
->NodeJs.Buffer.fromString,
{
encoding: "utf8",
},
)
}

// Sync the original source as well. Call it S.js to make .d.ts resolve correctly
writeSjsEsm("./src/S.js")

writeSjsEsm(NodeJs.Path.join2(artifactsPath, "./src/S.mjs"))

// This should overwrite S.js with the commonjs version
NodeJs.Fs.writeFileSyncWith(
"./src/S.js",
NodeJs.Path.join2(artifactsPath, "./src/S.js"),
["var S = require(\"./S_Core.res.js\");"]
->Array.concat(filesMapping->Array.map(((name, value)) => `exports.${name} = ${value}`))
->Array.join("\n")
Expand All @@ -208,14 +224,6 @@ NodeJs.Fs.writeFileSyncWith(
},
)

sourePaths->Array.forEach(path => {
FsX.cpSync(
~src=NodeJs.Path.join2(projectPath, path),
~dest=NodeJs.Path.join2(artifactsPath, path),
{recursive: true},
)
})

let updateJsonFile = (~src, ~path, ~value) => {
let packageJsonData = NodeJs.Fs.readFileSyncWith(
src,
Expand Down
28 changes: 17 additions & 11 deletions packages/prepack/src/Prepack.res.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,30 @@ var filesMapping = [
]
];

Nodefs.writeFileSync("./src/S.mjs", Buffer.from(["import * as S from \"./S_Core.res.mjs\";"].concat(filesMapping.map(function (param) {
return "export const " + param[0] + " = " + param[1];
})).join("\n")), {
encoding: "utf8"
sourePaths.forEach(function (path) {
Fs.cpSync(Nodepath.join(projectPath, path), Nodepath.join(artifactsPath, path), {
recursive: true
});
});

Nodefs.writeFileSync("./src/S.js", Buffer.from(["var S = require(\"./S_Core.res.js\");"].concat(filesMapping.map(function (param) {
function writeSjsEsm(path) {
Nodefs.writeFileSync(path, Buffer.from(["import * as S from \"./S_Core.res.mjs\";"].concat(filesMapping.map(function (param) {
return "export const " + param[0] + " = " + param[1];
})).join("\n")), {
encoding: "utf8"
});
}

writeSjsEsm("./src/S.js");

writeSjsEsm(Nodepath.join(artifactsPath, "./src/S.mjs"));

Nodefs.writeFileSync(Nodepath.join(artifactsPath, "./src/S.js"), Buffer.from(["var S = require(\"./S_Core.res.js\");"].concat(filesMapping.map(function (param) {
return "exports." + param[0] + " = " + param[1];
})).join("\n")), {
encoding: "utf8"
});

sourePaths.forEach(function (path) {
Fs.cpSync(Nodepath.join(projectPath, path), Nodepath.join(artifactsPath, path), {
recursive: true
});
});

function updateJsonFile(src, path, value) {
var packageJsonData = Nodefs.readFileSync(src, {
encoding: "utf8"
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StandardSchemaV1 } from "./../../../../src/S.d";

Check failure on line 1 in packages/tests/src/core/S_test.ts

View workflow job for this annotation

GitHub Actions / Test

Cannot find module './../../../../src/S.d' or its corresponding type declarations.
import test, { ExecutionContext } from "ava";
import test from "ava";
import { expectType, TypeEqual } from "ts-expect";

import * as S from "../../../../src/S.mjs";
import * as S from "../../../../src/S.js";
import { stringSchema } from "../genType/GenType.gen.js";

type SchemaEqual<
Expand Down
142 changes: 71 additions & 71 deletions src/S.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
'use strict';
var S = require("./S_Core.res.mjs");
exports.Error = S.$$Error.$$class
exports.string = S.string
exports.boolean = S.bool
exports.int32 = S.$$int
exports.number = S.$$float
exports.bigint = S.bigint
exports.json = S.json
exports.never = S.never
exports.unknown = S.unknown
exports.undefined = S.unit
exports.optional = S.js_optional
exports.nullable = S.$$null
exports.nullish = S.nullable
exports.array = S.array
exports.unnest = S.unnest
exports.record = S.dict
exports.jsonString = S.jsonString
exports.union = S.js_union
exports.object = S.object
exports.schema = S.js_schema
exports.safe = S.js_safe
exports.safeAsync = S.js_safeAsync
exports.reverse = S.reverse
exports.convertOrThrow = S.convertOrThrow
exports.convertToJsonOrThrow = S.convertToJsonOrThrow
exports.convertToJsonStringOrThrow = S.convertToJsonStringOrThrow
exports.reverseConvertOrThrow = S.reverseConvertOrThrow
exports.reverseConvertToJsonOrThrow = S.reverseConvertToJsonOrThrow
exports.reverseConvertToJsonStringOrThrow = S.reverseConvertToJsonStringOrThrow
exports.parseOrThrow = S.parseOrThrow
exports.parseJsonOrThrow = S.parseJsonOrThrow
exports.parseJsonStringOrThrow = S.parseJsonStringOrThrow
exports.parseAsyncOrThrow = S.parseAsyncOrThrow
exports.assertOrThrow = S.assertOrThrow
exports.recursive = S.recursive
exports.merge = S.js_merge
exports.strict = S.strict
exports.deepStrict = S.deepStrict
exports.strip = S.strip
exports.deepStrip = S.deepStrip
exports.custom = S.js_custom
exports.standard = S.standard
exports.tuple = S.tuple
exports.asyncParserRefine = S.js_asyncParserRefine
exports.refine = S.js_refine
exports.transform = S.js_transform
exports.description = S.description
exports.describe = S.describe
exports.name = S.js_name
exports.setName = S.setName
exports.removeTypeValidation = S.removeTypeValidation
exports.compile = S.compile
exports.port = S.port
exports.numberMin = S.floatMin
exports.numberMax = S.floatMax
exports.arrayMinLength = S.arrayMinLength
exports.arrayMaxLength = S.arrayMaxLength
exports.arrayLength = S.arrayLength
exports.stringMinLength = S.stringMinLength
exports.stringMaxLength = S.stringMaxLength
exports.stringLength = S.stringLength
exports.email = S.email
exports.uuid = S.uuid
exports.cuid = S.cuid
exports.url = S.url
exports.pattern = S.pattern
exports.datetime = S.datetime
exports.trim = S.trim
exports.setGlobalConfig = S.setGlobalConfig
import * as S from "./S_Core.res.mjs";
export const Error = S.$$Error.$$class;
export const string = S.string;
export const boolean = S.bool;
export const int32 = S.$$int;
export const number = S.$$float;
export const bigint = S.bigint;
export const json = S.json;
export const never = S.never;
export const unknown = S.unknown;
export const undefined = S.unit;
export const optional = S.js_optional;
export const nullable = S.$$null;
export const nullish = S.nullable;
export const array = S.array;
export const unnest = S.unnest;
export const record = S.dict;
export const jsonString = S.jsonString;
export const union = S.js_union;
export const object = S.object;
export const schema = S.js_schema;
export const safe = S.js_safe;
export const safeAsync = S.js_safeAsync;
export const reverse = S.reverse;
export const convertOrThrow = S.convertOrThrow;
export const convertToJsonOrThrow = S.convertToJsonOrThrow;
export const convertToJsonStringOrThrow = S.convertToJsonStringOrThrow;
export const reverseConvertOrThrow = S.reverseConvertOrThrow;
export const reverseConvertToJsonOrThrow = S.reverseConvertToJsonOrThrow;
export const reverseConvertToJsonStringOrThrow =
S.reverseConvertToJsonStringOrThrow;
export const parseOrThrow = S.parseOrThrow;
export const parseJsonOrThrow = S.parseJsonOrThrow;
export const parseJsonStringOrThrow = S.parseJsonStringOrThrow;
export const parseAsyncOrThrow = S.parseAsyncOrThrow;
export const assertOrThrow = S.assertOrThrow;
export const recursive = S.recursive;
export const merge = S.js_merge;
export const strict = S.strict;
export const deepStrict = S.deepStrict;
export const strip = S.strip;
export const deepStrip = S.deepStrip;
export const custom = S.js_custom;
export const standard = S.standard;
export const tuple = S.tuple;
export const asyncParserRefine = S.js_asyncParserRefine;
export const refine = S.js_refine;
export const transform = S.js_transform;
export const description = S.description;
export const describe = S.describe;
export const name = S.js_name;
export const setName = S.setName;
export const removeTypeValidation = S.removeTypeValidation;
export const compile = S.compile;
export const port = S.port;
export const numberMin = S.floatMin;
export const numberMax = S.floatMax;
export const arrayMinLength = S.arrayMinLength;
export const arrayMaxLength = S.arrayMaxLength;
export const arrayLength = S.arrayLength;
export const stringMinLength = S.stringMinLength;
export const stringMaxLength = S.stringMaxLength;
export const stringLength = S.stringLength;
export const email = S.email;
export const uuid = S.uuid;
export const cuid = S.cuid;
export const url = S.url;
export const pattern = S.pattern;
export const datetime = S.datetime;
export const trim = S.trim;
export const setGlobalConfig = S.setGlobalConfig;
70 changes: 0 additions & 70 deletions src/S.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion wallaby.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default () => ({
"package.json",
"src/S.res.mjs",
"src/S_Core.res.mjs",
"src/S.mjs",
"src/S.js",
"packages/tests/src/utils/U.res.mjs",
],
tests,
Expand Down

1 comment on commit c5811fc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: c5811fc Previous: a44eee7 Ratio
S.schema - make 1332256 ops/sec (±1.34%) 1354467 ops/sec (±1.69%) 1.02
S.schema - make + parse 105406 ops/sec (±1.21%) 107084 ops/sec (±0.76%) 1.02
S.schema - parse 57738115 ops/sec (±2.68%) 59295398 ops/sec (±2.15%) 1.03
S.schema - parse strict 22118437 ops/sec (±1.47%) 23128983 ops/sec (±1.08%) 1.05
S.schema - make + reverse 900234 ops/sec (±1.48%) 485145 ops/sec (±1.01%) 0.54
S.schema - make + reverse convert 188099 ops/sec (±0.65%) 153040 ops/sec (±1.04%) 0.81
S.schema - reverse convert 88810348 ops/sec (±3.22%) 67243399 ops/sec (±2.39%) 0.76
S.schema - reverse convert (compiled) 132953498 ops/sec (±5.04%) 132052574 ops/sec (±4.16%) 0.99
S.schema - assert 61338046 ops/sec (±2.86%) 63079730 ops/sec (±3.00%) 1.03
S.schema - assert (compiled) 70005907 ops/sec (±3.24%) 72857233 ops/sec (±2.59%) 1.04
S.schema - assert strict 19445576 ops/sec (±0.92%) 23133197 ops/sec (±1.07%) 1.19
S.object - make 993413 ops/sec (±0.32%) 995948 ops/sec (±0.34%) 1.00
S.object - make + parse 89096 ops/sec (±0.38%) 91218 ops/sec (±0.17%) 1.02
S.object - parse 35658096 ops/sec (±2.07%) 35466302 ops/sec (±1.60%) 0.99
S.object - make + reverse 131239 ops/sec (±1.13%) 131621 ops/sec (±1.09%) 1.00
S.object - make + reverse convert 83740 ops/sec (±1.14%) 83663 ops/sec (±0.93%) 1.00
S.object - reverse convert 45273497 ops/sec (±2.26%) 47267567 ops/sec (±1.74%) 1.04
S.string - parse 66356217 ops/sec (±1.91%) 67102209 ops/sec (±2.23%) 1.01
S.string - reverse convert 67149187 ops/sec (±2.43%) 69673135 ops/sec (±2.34%) 1.04

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.