Skip to content

Commit

Permalink
build: use import instead of _bundle
Browse files Browse the repository at this point in the history
Since package.json is re-written before publishing, we are free to point
`import` condition to `.ts` source files. However, CLI tests need to be
able to resolve to `dist/*.js` files, so a custom condition `prebuilt`
pointing to those are kept in every package.json.
  • Loading branch information
theseanl committed Jan 23, 2024
1 parent 768a308 commit af5e25d
Show file tree
Hide file tree
Showing 66 changed files with 173 additions and 194 deletions.
1 change: 0 additions & 1 deletion packages/structured-clone/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
8 changes: 4 additions & 4 deletions packages/structured-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/support-tables/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-builder/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
8 changes: 4 additions & 4 deletions packages/unified-latex-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
8 changes: 4 additions & 4 deletions packages/unified-latex-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
70 changes: 42 additions & 28 deletions packages/unified-latex-cli/tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,35 @@ console.log = (...args) => {
const exePath = path.resolve(__dirname, "../dist/unified-latex-cli.mjs");
const examplesPath = path.resolve(__dirname, "examples");

async function execCLI(args: string[]) {
return await executeCommand(`node`, [
// package.json points to typescript sources and it is replaced with the build output
// before publishing by scripts/make-package.mjs. In testing, we need to point to the
// build output explicitly by passing the conditions flag.
`-C`,
`prebuilt`,
exePath,
...args,
]);
}

describe(
"unified-latex-cli",
() => {
let stdout: string, stderr: string;
it("executable exists", async () => {
expect(fsLegacy.existsSync(exePath)).toBeTruthy();
});
it("can execute without error", async () => {
let { stdout, stderr } = await exec(`node ${exePath} -h`);
const stdout = await execCLI(["-h"]);
expect(stdout).toBeTruthy();
});
it("can format document", async () => {
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/needs-fixing.tex`
);
const stdout = await execCLI([`${examplesPath}/needs-fixing.tex`]);
expect(stdout).toMatchSnapshot();
});
it("can expand macro", async () => {
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/needs-expanding.tex`,
`-e`,
"\\newcommand{foo}[1]{FOO(#1)}",
Expand All @@ -50,8 +58,7 @@ describe(
}
{
// Make sure we don't lose spaces in math mode
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/needs-expanding.tex`,
`-e`,
"\\newcommand{foo}[1]{$\\x #1$}",
Expand All @@ -62,42 +69,44 @@ describe(
}
});
it("can expand macros defined in document", async () => {
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/has-definition.tex --stats-json`
);
const stdout = await execCLI([
`${examplesPath}/has-definition.tex`,
`--stats-json`,
]);
const { newcommands } = JSON.parse(stdout) as {
newcommands: { name: string }[];
};
const newcommandNames = newcommands.map((c) => c.name);
expect(newcommandNames).toEqual(["foo", "baz"]);

{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/has-definition.tex --expand-document-macro foo --expand-document-macro baz`
);
const stdout = await execCLI([
`${examplesPath}/has-definition.tex`,
`--expand-document-macro`,
`foo`,
`--expand-document-macro`,
`baz`,
]);
expect(stdout).toMatchSnapshot();
}
});
it("can override default macros", async () => {
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
]);
expect(stdout).toMatchSnapshot();
}
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
`-e`,
"\\newcommand{mathbb}{\\mathbb}",
]);
expect(stdout).toMatchSnapshot();
}
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
`-e`,
"\\newcommand{mathbb}[2]{\\mathbb{#1}{#2}}",
Expand All @@ -107,17 +116,19 @@ describe(
});
it("can convert to html", async () => {
{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/simple.tex --html`
);
const stdout = await execCLI([
`${examplesPath}/simple.tex`,
`--html`,
]);
expect(stdout).toMatchSnapshot();
}
});
it("can convert to markdown", async () => {
{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/simple.tex --markdown`
);
const stdout = await execCLI([
`${examplesPath}/simple.tex`,
`--markdown`,
]);
expect(stdout).toMatchSnapshot();
}
});
Expand All @@ -131,7 +142,10 @@ describe(
* Run commands with arguments using "cross-spawn", which correctly escapes arguments
* so that end results are the same across different shells.
*/
async function executeCommand(executablePath: string, args: string[]) {
function executeCommand(
executablePath: string,
args: string[]
): Promise<string> {
return new Promise((resolve, reject) => {
const childProcess = spawn(executablePath, args, { stdio: "pipe" });

Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-ctan/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
12 changes: 6 additions & 6 deletions packages/unified-latex-ctan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*/index": {
"_bundle": "./dist/*/index.ts",
"import": "./dist/*/index.js",
"prebuilt": "./dist/dist/*/index.js",
"import": "./dist/*/index.ts",
"require": "./dist/*/index.cjs"
},
"./*": {
"_bundle": "./*/index.ts",
"import": "./dist/*/index.js",
"prebuilt": "./dist/*/index.js",
"import": "./*/index.ts",
"require": "./dist/*/index.cjs"
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-lint/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
12 changes: 6 additions & 6 deletions packages/unified-latex-lint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*/index": {
"_bundle": "./dist/*/index.ts",
"import": "./dist/*/index.js",
"prebuilt": "./dist/dist/*/index.js",
"import": "./dist/*/index.ts",
"require": "./dist/*/index.cjs"
},
"./*": {
"_bundle": "./*/index.ts",
"import": "./dist/*/index.js",
"prebuilt": "./dist/*/index.js",
"import": "./*/index.ts",
"require": "./dist/*/index.cjs"
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-prettier/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
8 changes: 4 additions & 4 deletions packages/unified-latex-prettier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-to-hast/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
8 changes: 4 additions & 4 deletions packages/unified-latex-to-hast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-to-mdast/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isCjsPackage } from "../../scripts/esbuild-module-check.mjs";
format: "esm",
target: "node14",
external: [...explicitDeps],
conditions: ["_bundle"],
};

// Build the ESM
Expand Down
8 changes: 4 additions & 4 deletions packages/unified-latex-to-mdast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"import": "./dist/index.js",
"prebuilt": "./dist/index.js",
"import": "./index.ts",
"require": "./dist/index.cjs"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./*.ts",
"import": "./dist/*.js",
"prebuilt": "./dist/*.js",
"import": "./*.ts",
"require": "./dist/*.cjs"
}
},
Expand Down
6 changes: 2 additions & 4 deletions packages/unified-latex-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
],
"exports": {
".": {
"_bundle": "./index.ts",
"default": "./dist/index.js"
"default": "./index.ts"
},
"./*js": "./dist/*js",
"./*": {
"_bundle": "./dist/*.ts",
"default": "./dist/*.js"
"default": "./dist/*.ts"
}
},
"scripts": {
Expand Down
Loading

0 comments on commit af5e25d

Please sign in to comment.