forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node: add some Node.js polyfill to require() (denoland/deno#3382)
- Loading branch information
1 parent
1a94dea
commit 20556b0
Showing
8 changed files
with
132 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window["global"] = window; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { test } from "../testing/mod.ts"; | ||
import { assertEquals, assert } from "../testing/asserts.ts"; | ||
import { createRequire } from "./module.ts"; | ||
|
||
// TS compiler would try to resolve if function named "require" | ||
// Thus suffixing it with require_ to fix this... | ||
const require_ = createRequire(import.meta.url); | ||
|
||
test(function requireSuccess() { | ||
// Relative to import.meta.url | ||
const result = require_("./tests/cjs/cjs_a.js"); | ||
assert("helloA" in result); | ||
assert("helloB" in result); | ||
assert("C" in result); | ||
assert("leftPad" in result); | ||
assertEquals(result.helloA(), "A"); | ||
assertEquals(result.helloB(), "B"); | ||
assertEquals(result.C, "C"); | ||
assertEquals(result.leftPad("pad", 4), " pad"); | ||
}); | ||
|
||
test(function requireCycle() { | ||
const resultA = require_("./tests/cjs/cjs_cycle_a"); | ||
const resultB = require_("./tests/cjs/cjs_cycle_b"); | ||
assert(resultA); | ||
assert(resultB); | ||
}); | ||
|
||
test(function requireBuiltin() { | ||
const fs = require_("fs"); | ||
assert("readFileSync" in fs); | ||
const { readFileSync, isNull, extname } = require_("./tests/cjs/cjs_builtin"); | ||
assertEquals( | ||
readFileSync("./node/testdata/hello.txt", { encoding: "utf8" }), | ||
"hello world" | ||
); | ||
assert(isNull(null)); | ||
assertEquals(extname("index.html"), ".html"); | ||
}); | ||
|
||
test(function requireIndexJS() { | ||
const { isIndex } = require_("./tests/cjs"); | ||
assert(isIndex); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "../path/mod.ts"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable */ | ||
const fs = require("fs"); | ||
const util = require("util"); | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
readFileSync: fs.readFileSync, | ||
isNull: util.isNull, | ||
extname: path.extname | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { isIndex: true }; |