1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

std/node: require_ -> require (#4828)

This commit is contained in:
Nikolai Vavilov 2020-04-20 21:30:52 +03:00 committed by GitHub
parent 6e5f3453f8
commit f72f045de5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 19 deletions

View file

@ -74,11 +74,11 @@ modules.
```ts ```ts
import { createRequire } from "https://deno.land/std/node/module.ts"; import { createRequire } from "https://deno.land/std/node/module.ts";
const require_ = createRequire(import.meta.url); const require = createRequire(import.meta.url);
// Loads native module polyfill. // Loads native module polyfill.
const path = require_("path"); const path = require("path");
// Loads extensionless module. // Loads extensionless module.
const cjsModule = require_("./my_mod"); const cjsModule = require("./my_mod");
// Visits node_modules. // Visits node_modules.
const leftPad = require_("left-pad"); const leftPad = require("left-pad");
``` ```

View file

@ -510,10 +510,10 @@ class Module {
* with `node_modules` lookup and `index.js` lookup support. * with `node_modules` lookup and `index.js` lookup support.
* Also injects available Node.js builtin module polyfills. * Also injects available Node.js builtin module polyfills.
* *
* const require_ = createRequire(import.meta.url); * const require = createRequire(import.meta.url);
* const fs = require_("fs"); * const fs = require("fs");
* const leftPad = require_("left-pad"); * const leftPad = require("left-pad");
* const cjsModule = require_("./cjs_mod"); * const cjsModule = require("./cjs_mod");
* *
* @param filename path or URL to current module * @param filename path or URL to current module
* @return Require function to import CJS modules * @return Require function to import CJS modules

View file

@ -1,14 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { test } = Deno; const { test } = Deno;
import { assertEquals, assert, assertStrContains } from "../testing/asserts.ts"; import { assertEquals, assert, assertStrContains } from "../testing/asserts.ts";
import { createRequire } from "./module.ts"; import { createRequire } from "./module.ts";
// TS compiler would try to resolve if function named "require" const require = createRequire(import.meta.url);
// Thus suffixing it with require_ to fix this...
const require_ = createRequire(import.meta.url);
test(function requireSuccess() { test(function requireSuccess() {
// Relative to import.meta.url // Relative to import.meta.url
const result = require_("./tests/cjs/cjs_a.js"); const result = require("./tests/cjs/cjs_a.js");
assert("helloA" in result); assert("helloA" in result);
assert("helloB" in result); assert("helloB" in result);
assert("C" in result); assert("C" in result);
@ -20,16 +20,16 @@ test(function requireSuccess() {
}); });
test(function requireCycle() { test(function requireCycle() {
const resultA = require_("./tests/cjs/cjs_cycle_a"); const resultA = require("./tests/cjs/cjs_cycle_a");
const resultB = require_("./tests/cjs/cjs_cycle_b"); const resultB = require("./tests/cjs/cjs_cycle_b");
assert(resultA); assert(resultA);
assert(resultB); assert(resultB);
}); });
test(function requireBuiltin() { test(function requireBuiltin() {
const fs = require_("fs"); const fs = require("fs");
assert("readFileSync" in fs); assert("readFileSync" in fs);
const { readFileSync, isNull, extname } = require_("./tests/cjs/cjs_builtin"); const { readFileSync, isNull, extname } = require("./tests/cjs/cjs_builtin");
assertEquals( assertEquals(
readFileSync("./node/_fs/testdata/hello.txt", { encoding: "utf8" }), readFileSync("./node/_fs/testdata/hello.txt", { encoding: "utf8" }),
"hello world" "hello world"
@ -39,18 +39,18 @@ test(function requireBuiltin() {
}); });
test(function requireIndexJS() { test(function requireIndexJS() {
const { isIndex } = require_("./tests/cjs"); const { isIndex } = require("./tests/cjs");
assert(isIndex); assert(isIndex);
}); });
test(function requireNodeOs() { test(function requireNodeOs() {
const os = require_("os"); const os = require("os");
assert(os.arch); assert(os.arch);
assert(typeof os.arch() == "string"); assert(typeof os.arch() == "string");
}); });
test(function requireStack() { test(function requireStack() {
const { hello } = require_("./tests/cjs/cjs_throw"); const { hello } = require("./tests/cjs/cjs_throw");
try { try {
hello(); hello();
} catch (e) { } catch (e) {