1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -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
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.
const path = require_("path");
const path = require("path");
// Loads extensionless module.
const cjsModule = require_("./my_mod");
const cjsModule = require("./my_mod");
// 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.
* Also injects available Node.js builtin module polyfills.
*
* const require_ = createRequire(import.meta.url);
* const fs = require_("fs");
* const leftPad = require_("left-pad");
* const cjsModule = require_("./cjs_mod");
* const require = createRequire(import.meta.url);
* const fs = require("fs");
* const leftPad = require("left-pad");
* const cjsModule = require("./cjs_mod");
*
* @param filename path or URL to current module
* @return Require function to import CJS modules

View file

@ -1,14 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { test } = Deno;
import { assertEquals, assert, assertStrContains } 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);
const require = createRequire(import.meta.url);
test(function requireSuccess() {
// 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("helloB" in result);
assert("C" in result);
@ -20,16 +20,16 @@ test(function requireSuccess() {
});
test(function requireCycle() {
const resultA = require_("./tests/cjs/cjs_cycle_a");
const resultB = require_("./tests/cjs/cjs_cycle_b");
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");
const fs = require("fs");
assert("readFileSync" in fs);
const { readFileSync, isNull, extname } = require_("./tests/cjs/cjs_builtin");
const { readFileSync, isNull, extname } = require("./tests/cjs/cjs_builtin");
assertEquals(
readFileSync("./node/_fs/testdata/hello.txt", { encoding: "utf8" }),
"hello world"
@ -39,18 +39,18 @@ test(function requireBuiltin() {
});
test(function requireIndexJS() {
const { isIndex } = require_("./tests/cjs");
const { isIndex } = require("./tests/cjs");
assert(isIndex);
});
test(function requireNodeOs() {
const os = require_("os");
const os = require("os");
assert(os.arch);
assert(typeof os.arch() == "string");
});
test(function requireStack() {
const { hello } = require_("./tests/cjs/cjs_throw");
const { hello } = require("./tests/cjs/cjs_throw");
try {
hello();
} catch (e) {