mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
std/node: require_ -> require (#4828)
This commit is contained in:
parent
6e5f3453f8
commit
f72f045de5
3 changed files with 19 additions and 19 deletions
|
@ -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");
|
||||
```
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue