diff --git a/deno.jsonc b/deno.jsonc index 2d1fed1..f87b154 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -8,7 +8,9 @@ "rules": { "exclude": [ "no-explicit-any", - "camelcase" + "camelcase", + "no-import-prefix", + "no-unversioned-import" ] } }, diff --git a/lib/package_json.ts b/lib/package_json.ts index ecdc31d..e3a6e1b 100644 --- a/lib/package_json.ts +++ b/lib/package_json.ts @@ -15,6 +15,8 @@ export interface GetPackageJsonOptions { includeTsLib: boolean | undefined; testEnabled: boolean | undefined; shims: ShimOptions; + testColor?: boolean; + testRunner?: boolean; } export function getPackageJson({ @@ -27,6 +29,8 @@ export function getPackageJson({ includeTsLib, testEnabled, shims, + testColor = true, + testRunner = true, }: GetPackageJsonOptions): Record { const finalEntryPoints = transformOutput .main.entryPoints.map((e, i) => ({ @@ -65,7 +69,7 @@ export function getPackageJson({ }; const testDevDependencies = testEnabled ? ({ - ...(!Object.keys(dependencies).includes("picocolors") + ...(testColor && !Object.keys(dependencies).includes("picocolors") ? { "picocolors": "^1.0.0", } @@ -88,7 +92,7 @@ export function getPackageJson({ // override with specified dependencies ...(packageJsonObj.devDependencies ?? {}), }; - const scripts = testEnabled + const scripts = testEnabled && testRunner ? ({ test: "node test_runner.js", // override with specified scripts diff --git a/lib/shims.ts b/lib/shims.ts index 3cd19ba..5499b74 100644 --- a/lib/shims.ts +++ b/lib/shims.ts @@ -45,6 +45,12 @@ export interface ShimOptions { weakRef?: ShimValue; /** Shim `WebSocket` with the `ws` package (https://www.npmjs.com/package/ws). */ webSocket?: boolean | "dev"; + /** Shim `globalThis` with a proxy that provides Deno globals. */ + globalThis?: ShimValue; + /** Enable colored test output using picocolors. */ + testColor?: boolean; + /** Enable test runner script in package.json. */ + testRunner?: boolean; /** Custom shims to use. */ custom?: Shim[]; /** Custom shims to use only for the test code. */ @@ -58,7 +64,11 @@ export interface DenoShimOptions { test: boolean | "dev"; } -export function shimOptionsToTransformShims(options: ShimOptions) { +export function shimOptionsToTransformShims(options: ShimOptions): { + shims: Shim[]; + testShims: Shim[]; + useGlobalThisShim: boolean; +} { const shims: Shim[] = []; const testShims: Shim[] = []; @@ -84,9 +94,12 @@ export function shimOptionsToTransformShims(options: ShimOptions) { testShims.push(...options.customDev); } + const useGlobalThisShim = options.globalThis !== false; + return { shims, testShims, + useGlobalThisShim, }; function add(option: boolean | "dev" | undefined, getShim: () => Shim) { diff --git a/lib/utils.test.ts b/lib/utils.test.ts index 7a0a0e9..dbac615 100644 --- a/lib/utils.test.ts +++ b/lib/utils.test.ts @@ -38,5 +38,6 @@ Deno.test("getDntVersion", () => { getDntVersion("https://deno.land/x/dnt@20.21.22/mod.ts"), "20.21.22", ); + assertEquals(getDntVersion("https://jsr.io/@deno/dnt/1.2.3/mod.ts"), "1.2.3"); assertEquals(getDntVersion("file:///test/mod.ts"), "dev"); }); diff --git a/lib/utils.ts b/lib/utils.ts index 356450e..17ceaa0 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -106,5 +106,6 @@ export function valueToUrl(value: string) { } export function getDntVersion(url = import.meta.url) { - return /\/dnt@([0-9]+\.[0-9]+\.[0-9]+)\//.exec(url)?.[1] ?? "dev"; + return /\/(?:dnt@|@deno\/dnt\/)([0-9]+\.[0-9]+\.[0-9]+)\//.exec(url)?.[1] ?? + "dev"; } diff --git a/mod.ts b/mod.ts index 29fc8e0..5929f23 100644 --- a/mod.ts +++ b/mod.ts @@ -539,6 +539,8 @@ export async function build(options: BuildOptions): Promise { includeDeclarations: options.declaration === "separate", includeTsLib: options.compilerOptions?.importHelpers, shims: options.shims, + testColor: options.shims.testColor ?? true, + testRunner: options.shims.testRunner ?? true, }); writeFile( path.join(options.outDir, "package.json"), @@ -574,7 +576,9 @@ export async function build(options: BuildOptions): Promise { } async function transformEntryPoints(): Promise { - const { shims, testShims } = shimOptionsToTransformShims(options.shims); + const { shims, testShims, useGlobalThisShim } = shimOptionsToTransformShims( + options.shims, + ); return transform({ entryPoints: entryPoints.map((e) => e.path), testEntryPoints: options.test @@ -591,6 +595,7 @@ export async function build(options: BuildOptions): Promise { importMap: options.importMap, configFile: options.configFile, cwd: path.toFileUrl(cwd).toString(), + useGlobalThisShim, }); } diff --git a/transform.ts b/transform.ts index 2e2d726..dcf60e8 100644 --- a/transform.ts +++ b/transform.ts @@ -76,6 +76,7 @@ export interface TransformOptions { importMap?: string; configFile?: string; cwd: string; + useGlobalThisShim?: boolean; } /** Dependency in a package.json file. */