Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"rules": {
"exclude": [
"no-explicit-any",
"camelcase"
"camelcase",
"no-import-prefix",
"no-unversioned-import"
]
}
},
Expand Down
8 changes: 6 additions & 2 deletions lib/package_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface GetPackageJsonOptions {
includeTsLib: boolean | undefined;
testEnabled: boolean | undefined;
shims: ShimOptions;
testColor?: boolean;
testRunner?: boolean;
}

export function getPackageJson({
Expand All @@ -27,6 +29,8 @@ export function getPackageJson({
includeTsLib,
testEnabled,
shims,
testColor = true,
testRunner = true,
}: GetPackageJsonOptions): Record<string, unknown> {
const finalEntryPoints = transformOutput
.main.entryPoints.map((e, i) => ({
Expand Down Expand Up @@ -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",
}
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lib/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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[] = [];

Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
3 changes: 2 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
7 changes: 6 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ export async function build(options: BuildOptions): Promise<void> {
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"),
Expand Down Expand Up @@ -574,7 +576,9 @@ export async function build(options: BuildOptions): Promise<void> {
}

async function transformEntryPoints(): Promise<TransformOutput> {
const { shims, testShims } = shimOptionsToTransformShims(options.shims);
const { shims, testShims, useGlobalThisShim } = shimOptionsToTransformShims(
options.shims,
);
return transform({
entryPoints: entryPoints.map((e) => e.path),
testEntryPoints: options.test
Expand All @@ -591,6 +595,7 @@ export async function build(options: BuildOptions): Promise<void> {
importMap: options.importMap,
configFile: options.configFile,
cwd: path.toFileUrl(cwd).toString(),
useGlobalThisShim,
});
}

Expand Down
1 change: 1 addition & 0 deletions transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface TransformOptions {
importMap?: string;
configFile?: string;
cwd: string;
useGlobalThisShim?: boolean;
}

/** Dependency in a package.json file. */
Expand Down