From 2457a93ebfa65c47da1c9b7b6caeafc04d905101 Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Wed, 4 Jan 2023 15:49:37 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(tokens)=20add=20the=20support=20for?= =?UTF-8?q?=20Typescript=20config=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the support to load cunningham.ts local config file based on ts-node. Those files will be loaded as CommonJS to preserve to coherence regarding the way the cunningham bin is ran by node. --- packages/tokens/src/bin/ConfigLoader.ts | 6 ++++++ packages/tokens/src/bin/tests/Cunningham.spec.ts | 9 --------- turbo.json | 3 +++ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/tokens/src/bin/ConfigLoader.ts b/packages/tokens/src/bin/ConfigLoader.ts index 496f8d6..5e403cd 100644 --- a/packages/tokens/src/bin/ConfigLoader.ts +++ b/packages/tokens/src/bin/ConfigLoader.ts @@ -26,7 +26,13 @@ const getLocalConfig = async () => { return config.default; }; +/** + * Register ts-node to load typescript config files with import. ( In fact, with `require` after transpiling ). + */ const registerTypescriptLoader = () => { + // Specifying to load all .ts files as CJS is really important, otherwise ts-node will try to load them as ESM in + // projects where cunningham is used as a dependency where package.json contains "type": "module". + // So, by doing this we tell ts-node "Load all .ts files as CJS, even if they are in a project with ESM". register({ moduleTypes: { "**/*.ts": "cjs", diff --git a/packages/tokens/src/bin/tests/Cunningham.spec.ts b/packages/tokens/src/bin/tests/Cunningham.spec.ts index cc049b4..a175fda 100644 --- a/packages/tokens/src/bin/tests/Cunningham.spec.ts +++ b/packages/tokens/src/bin/tests/Cunningham.spec.ts @@ -11,15 +11,6 @@ jest.mock("../Paths", () => ({ workPath: () => __dirname, })); -jest.mock("../cunningham.dist.js", () => ({ - theme: { - colors: { - primary: "#055FD2", - secondary: "#DA0000", - }, - }, -})); - const runBin = async (args: string) => { const exec = util.promisify(child_process.exec); const promise = exec( diff --git a/turbo.json b/turbo.json index 04c7483..a39f23d 100644 --- a/turbo.json +++ b/turbo.json @@ -34,6 +34,9 @@ "lint" ], "outputs": [] + }, + "@openfun/cunningham-tokens#test": { + "dependsOn": ["build"] } } }