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

std/node: add CommonJS require #3380

Merged
merged 1 commit into from
Nov 19, 2019
Merged

Conversation

kevinkassimo
Copy link
Contributor

@kevinkassimo kevinkassimo commented Nov 19, 2019

Basically from https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js

import { makeRequire } from "https://deno.land/std/node/require.ts";

const selfPath = window.unescape(import.meta.url.substring(7));
// Our TS compiler would try to resolve if function named "require"...
// which is BAD since this triggers
// Thus suffixing it with _ to fix this...
const require_ = makeRequire(selfPath);

const result = require_("./node/tests/cjs/cjs_a.js");
assert("helloA" in result);
// Nested require
assert("helloB" in result);
// `module.exports` is not object
assert("C" in result);
// From node_modules
assert("leftPad" in result);

Seems to also work with node_modules.

NOTICE this requires Deno.core.evalContext

This would likely be a great starting point for compat, since this requires only the entry point to explicitly create require from makeRequire. Also we could possibly inject other std/node polyfills in the require resolution steps as builtin modules, such that polyfills would work seamlessly.

ref #2644

@@ -0,0 +1,1189 @@
// Copyright Joyent, Inc. and other Node contributors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello old friend


test(function requireCycle() {
const resultA = require_("./node/tests/cjs/cjs_cycle_a");
const resultB = require_("./node/tests/cjs/cjs_cycle_b");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - very nice

@ry ry merged commit e73a82d into denoland:master Nov 19, 2019
@bartlomieju bartlomieju mentioned this pull request Nov 25, 2019
bartlomieju pushed a commit to bartlomieju/deno that referenced this pull request Dec 28, 2019
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants