-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: add hook for global preload code
Backport-PR-URL: #32610 PR-URL: #32068 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
- Loading branch information
1 parent
0e513b2
commit 62b5bd4
Showing
6 changed files
with
154 additions
and
1 deletion.
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
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,32 @@ | ||
// Flags: --experimental-modules --experimental-loader ./test/fixtures/es-module-loaders/loader-side-effect.mjs --require ./test/fixtures/es-module-loaders/loader-side-effect-require-preload.js | ||
import { allowGlobals, mustCall } from '../common/index.mjs'; | ||
import assert from 'assert'; | ||
import { fileURLToPath } from 'url'; | ||
import { Worker, isMainThread, parentPort } from 'worker_threads'; | ||
|
||
/* global implicitGlobalProperty */ | ||
assert.strictEqual(globalThis.implicitGlobalProperty, 42); | ||
allowGlobals(implicitGlobalProperty); | ||
|
||
/* global implicitGlobalConst */ | ||
assert.strictEqual(implicitGlobalConst, 42 * 42); | ||
allowGlobals(implicitGlobalConst); | ||
|
||
/* global explicitGlobalProperty */ | ||
assert.strictEqual(globalThis.explicitGlobalProperty, 42 * 42 * 42); | ||
allowGlobals(explicitGlobalProperty); | ||
|
||
/* global preloadOrder */ | ||
assert.deepStrictEqual(globalThis.preloadOrder, ['--require', 'loader']); | ||
allowGlobals(preloadOrder); | ||
|
||
if (isMainThread) { | ||
const worker = new Worker(fileURLToPath(import.meta.url)); | ||
const promise = new Promise((resolve, reject) => { | ||
worker.on('message', resolve); | ||
worker.on('error', reject); | ||
}); | ||
promise.then(mustCall()); | ||
} else { | ||
parentPort.postMessage('worker done'); | ||
} |
6 changes: 6 additions & 0 deletions
6
test/fixtures/es-module-loaders/loader-side-effect-require-preload.js
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,6 @@ | ||
/** | ||
* This file is combined with `loader-side-effect.mjs` via `--require`. Its | ||
* purpose is to test execution order of the two kinds of preload code. | ||
*/ | ||
|
||
(globalThis.preloadOrder || (globalThis.preloadOrder = [])).push('--require'); |
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,32 @@ | ||
// Arrow function so it closes over the this-value of the preload scope. | ||
const globalPreload = () => { | ||
/* global getBuiltin */ | ||
const assert = getBuiltin('assert'); | ||
const vm = getBuiltin('vm'); | ||
|
||
assert.strictEqual(typeof require, 'undefined'); | ||
assert.strictEqual(typeof module, 'undefined'); | ||
assert.strictEqual(typeof exports, 'undefined'); | ||
assert.strictEqual(typeof __filename, 'undefined'); | ||
assert.strictEqual(typeof __dirname, 'undefined'); | ||
|
||
assert.strictEqual(this, globalThis); | ||
(globalThis.preloadOrder || (globalThis.preloadOrder = [])).push('loader'); | ||
|
||
vm.runInThisContext(`\ | ||
var implicitGlobalProperty = 42; | ||
const implicitGlobalConst = 42 * 42; | ||
`); | ||
|
||
assert.strictEqual(globalThis.implicitGlobalProperty, 42); | ||
(implicitGlobalProperty).foo = 'bar'; // assert: not strict mode | ||
|
||
globalThis.explicitGlobalProperty = 42 * 42 * 42; | ||
} | ||
|
||
export function getGlobalPreloadCode() { | ||
return `\ | ||
<!-- assert: inside of script goal --> | ||
(${globalPreload.toString()})(); | ||
`; | ||
} |