Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uses of deno.land/std/node/{}.ts with native node:{} #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
9 changes: 4 additions & 5 deletions polyfills/global-for-deno.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "virtual:deno-std-node-global";
export const process = globalThis.process;
export const Buffer = globalThis.Buffer;
export const setImmediate = globalThis.setImmediate;
export const clearImmediate = globalThis.clearImmediate;
import * as process from "virtual:node:process";
export { process };
export { Buffer } from "virtual:node:buffer";
export { setImmediate, clearImmediate } from "virtual:node:timers";
41 changes: 19 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ export function polyfillNode(options: PolyfillNodeOptions = {}): Plugin {
}

export interface PolyfillNodeForDenoOptions {
stdVersion?: string;
globals?: boolean;
polyfills?: {
assert?: boolean | "empty";
Expand Down Expand Up @@ -258,7 +257,7 @@ export interface PolyfillNodeForDenoOptions {
export function polyfillNodeForDeno(
options: PolyfillNodeForDenoOptions = {},
): Plugin {
const { stdVersion = "0.177.0", globals = true, polyfills = {} } = options;
const { globals = true, polyfills = {} } = options;

const moduleNames = [...new Set([...denoPolyfills])];

Expand All @@ -268,15 +267,12 @@ export function polyfillNodeForDeno(
name: "node-polyfills",

setup(build) {
build.onResolve(
{
filter: /^virtual:deno-std-node-global$/,
},
() => ({
path: `https://deno.land/std@${stdVersion}/node/global.ts`,
build.onResolve({ filter: /^virtual:node:.*/ }, async ({ path }) => {
return {
path: path.replace(/^virtual:node:/, "node:"),
external: true,
}),
);
};
});

build.onResolve({ filter }, async ({ path }) => {
const [, , moduleName] = path.match(filter)!;
Expand All @@ -296,15 +292,18 @@ export function polyfillNodeForDeno(
}

return {
path: `https://deno.land/std@${stdVersion}/node/${moduleName}.ts`,
external: true,
path: path,
namespace: "polyfillNodePackage",
};
}
});

build.onLoad({ namespace: "polyfillNodeForDeno", filter: /.*/ }, () => ({
contents: denoGlobalsContents(stdVersion),
}));
build.onLoad({ namespace: "polyfillNodePackage", filter: /.*/ }, args => {
console.log('Loading polyfill', args.path)
return {
contents: denoNodePackage(args.path),
}
});

if (globals) {
build.initialOptions.footer;
Expand Down Expand Up @@ -373,11 +372,12 @@ const jspmPolyiflls = new Set([
const denoPolyfills = new Set([
"assert",
"assert/strict",
"async_hooks",
"buffer",
"child_process",
"console",
"constants",
"crypto",
"child_process",
"dns",
"events",
"fs",
Expand Down Expand Up @@ -412,12 +412,9 @@ async function resolveImport(specifier: string) {
return fileURLToPath(resolved);
}

function denoGlobalsContents(stdVersion: string) {
function denoNodePackage(moduleName: string) {
return `
import "https://deno.land/std@${stdVersion}/node/global.ts";
export const process = globalThis.process;
export const Buffer = globalThis.Buffer;
export const setImmediate = globalThis.setImmediate;
export const clearImmediate = globalThis.clearImmediate;
export * from "virtual:node:${moduleName}";
export { default } from "virtual:node:${moduleName}";
`;
}