From 28d12eccf6b686f0a00ba0223286afd22e22478a Mon Sep 17 00:00:00 2001 From: Avi Vahl Date: Sun, 1 Sep 2024 22:02:40 +0300 Subject: [PATCH] refactor(typescript): use node16 moduleResolution added missing project references worked around vlq having no "types" inside its "exports" --- .../build-tools/src/load-stylable-config.ts | 6 +----- .../create-stylable-app/src/create-project.ts | 17 ++++++++++------- packages/dom-test-kit/test/tsconfig.json | 1 + packages/esbuild/test/e2e/esbuild-testkit.ts | 10 +++++----- packages/runtime/src/tsconfig.esm.json | 3 ++- packages/uni-driver/test/tsconfig.json | 1 + tsconfig.base.json | 4 ++-- typings/externals/index.d.ts | 6 ++++++ 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/build-tools/src/load-stylable-config.ts b/packages/build-tools/src/load-stylable-config.ts index 730f3cf21..921ba2cff 100644 --- a/packages/build-tools/src/load-stylable-config.ts +++ b/packages/build-tools/src/load-stylable-config.ts @@ -30,10 +30,6 @@ export function loadStylableConfig( return undefined; } -// use eval to preserve esm import from typescript compiling -// it to require, because of our current build to cjs -const esmImport: (url: URL) => any = eval(`(path) => import(path)`); - export async function loadStylableConfigEsm( context: string, extract: (config: any) => T, @@ -44,7 +40,7 @@ export async function loadStylableConfigEsm( let config; if (path) { try { - config = await esmImport(pathToFileURL(path)); + config = await import(pathToFileURL(path).href); } catch (e) { throw new Error( `Failed to load "stylable.config.js" from ${path}\n${(e as Error)?.stack}`, diff --git a/packages/create-stylable-app/src/create-project.ts b/packages/create-stylable-app/src/create-project.ts index dccc21947..2118cd4c6 100644 --- a/packages/create-stylable-app/src/create-project.ts +++ b/packages/create-stylable-app/src/create-project.ts @@ -1,8 +1,9 @@ -import path from 'path'; -import { promises } from 'fs'; -import type { SpawnOptions } from 'child_process'; +import type { SpawnOptions } from 'node:child_process'; +import { promises } from 'node:fs'; +import path from 'node:path'; +import { pathToFileURL } from 'node:url'; import validatePackageName from 'validate-npm-package-name'; -import { statSafe, spawnSafe, directoryDeepChildren, executeWithProgress } from './helpers'; +import { directoryDeepChildren, executeWithProgress, spawnSafe, statSafe } from './helpers'; const templateDefinitionFileName = 'template.js'; @@ -36,9 +37,11 @@ export async function createProjectFromTemplate({ }: CreateProjectFromTemplateOptions) { const templateDefinitionPath = path.join(templatePath, templateDefinitionFileName); - const { dependencies, devDependencies, packageJson, postinstall } = (await import( - templateDefinitionPath - )) as TemplateDefinition; + const { + default: { dependencies, devDependencies, packageJson, postinstall }, + } = (await import(pathToFileURL(templateDefinitionPath).href)) as { + default: TemplateDefinition; + }; // package name validation const targetDirectoryName = path.basename(targetDirectoryPath); diff --git a/packages/dom-test-kit/test/tsconfig.json b/packages/dom-test-kit/test/tsconfig.json index 8c8fe988a..79b26bd0c 100644 --- a/packages/dom-test-kit/test/tsconfig.json +++ b/packages/dom-test-kit/test/tsconfig.json @@ -7,6 +7,7 @@ "references": [ { "path": "../../runtime/src" }, { "path": "../../core-test-kit/src" }, + { "path": "../../e2e-test-kit/src" }, { "path": "../src" } ] } diff --git a/packages/esbuild/test/e2e/esbuild-testkit.ts b/packages/esbuild/test/e2e/esbuild-testkit.ts index 5508fcbc5..81941eb6d 100644 --- a/packages/esbuild/test/e2e/esbuild-testkit.ts +++ b/packages/esbuild/test/e2e/esbuild-testkit.ts @@ -1,9 +1,9 @@ -import { dirname, join } from 'node:path'; -import { readFileSync, symlinkSync, writeFileSync } from 'node:fs'; import fs from '@file-services/node'; -import { BuildContext, BuildOptions, context, Plugin } from 'esbuild'; import { createTempDirectorySync, runServer } from '@stylable/e2e-test-kit'; - +import { BuildContext, BuildOptions, context, Plugin } from 'esbuild'; +import { readFileSync, symlinkSync, writeFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { pathToFileURL } from 'node:url'; import playwright from 'playwright-core'; type BuildFn = ( @@ -48,7 +48,7 @@ export class ESBuildTestKit { this.options.log && console.log(`created temp project ${projectDir} and linked node_modules`); } - const moduleExports = await import(buildFile); + const moduleExports = await import(pathToFileURL(buildFile).href); const run = moduleExports[buildExport || 'run'] as BuildFn; if (!run) { throw new Error(`could not find ${buildExport || 'run'} export in ${buildFile}`); diff --git a/packages/runtime/src/tsconfig.esm.json b/packages/runtime/src/tsconfig.esm.json index fcbff8c92..2ef68c6dd 100644 --- a/packages/runtime/src/tsconfig.esm.json +++ b/packages/runtime/src/tsconfig.esm.json @@ -2,6 +2,7 @@ "extends": "../../../tsconfig.base.json", "compilerOptions": { "outDir": "../esm", - "module": "ESNext" + "module": "esnext", + "moduleResolution": "node" } } diff --git a/packages/uni-driver/test/tsconfig.json b/packages/uni-driver/test/tsconfig.json index 58c3843f3..c6a69f5b7 100644 --- a/packages/uni-driver/test/tsconfig.json +++ b/packages/uni-driver/test/tsconfig.json @@ -6,6 +6,7 @@ }, "references": [ { "path": "../../dom-test-kit/src" }, + { "path": "../../dom-test-kit/test" }, { "path": "../../core-test-kit/src" }, { "path": "../src" } ] diff --git a/tsconfig.base.json b/tsconfig.base.json index 733bb0c11..3d4978ecd 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -25,9 +25,9 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "node16", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ - "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ diff --git a/typings/externals/index.d.ts b/typings/externals/index.d.ts index 4c51f3a2b..9c5325c52 100644 --- a/typings/externals/index.d.ts +++ b/typings/externals/index.d.ts @@ -3,6 +3,12 @@ declare module 'enhanced-resolve/lib/ResolverFactory.js' { export = ResolverFactory; } +declare module 'vlq' { + // can be removed if https://github.com/Rich-Harris/vlq/pull/19 is merged/released + const VLQ: typeof import('../../node_modules/vlq/types'); + export = VLQ; +} + declare module 'is-vendor-prefixed' { function isVendorPrefixed(value: string): boolean; export = isVendorPrefixed;