Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit d2a2a3e

Browse files
jdaltonMylesBorins
authored andcommitted
esm: Remove --loader.
PR-URL: #6 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 517d943 commit d2a2a3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2
-472
lines changed

.eslintrc.js

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports = {
3636
files: [
3737
'doc/api/esm.md',
3838
'*.mjs',
39-
'test/es-module/test-esm-example-loader.js',
4039
],
4140
parserOptions: { sourceType: 'module' },
4241
},

doc/api/cli.md

-9
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ default) is not firewall-protected.**
174174

175175
See the [debugging security implications][] section for more information.
176176

177-
### `--loader=file`
178-
<!-- YAML
179-
added: v9.0.0
180-
-->
181-
182-
Specify the `file` of the custom [experimental ECMAScript Module][] loader.
183-
184177
### `--napi-modules`
185178
<!-- YAML
186179
added: v7.10.0
@@ -603,7 +596,6 @@ Node.js options that are allowed are:
603596
- `--inspect`
604597
- `--inspect-brk`
605598
- `--inspect-port`
606-
- `--loader`
607599
- `--napi-modules`
608600
- `--no-deprecation`
609601
- `--no-force-async-hooks-checks`
@@ -788,7 +780,6 @@ greater than `4` (its current default value). For more information, see the
788780
[debugger]: debugger.html
789781
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
790782
[emit_warning]: process.html#process_process_emitwarning_warning_type_code_ctor
791-
[experimental ECMAScript Module]: esm.html#esm_loader_hooks
792783
[libuv threadpool documentation]: http://docs.libuv.org/en/latest/threadpool.html
793784
[remote code execution]: https://www.owasp.org/index.php/Code_Injection
794785
[secureProtocol]: tls.html#tls_tls_createsecurecontext_options

doc/api/esm.md

-119
Original file line numberDiff line numberDiff line change
@@ -146,125 +146,6 @@ fs.readFileSync = () => Buffer.from('Hello, ESM');
146146
fs.readFileSync === readFileSync;
147147
```
148148
149-
## Loader hooks
150-
151-
<!-- type=misc -->
152-
153-
To customize the default module resolution, loader hooks can optionally be
154-
provided via a `--loader ./loader-name.mjs` argument to Node.js.
155-
156-
When hooks are used they only apply to ES module loading and not to any
157-
CommonJS modules loaded.
158-
159-
### Resolve hook
160-
161-
The resolve hook returns the resolved file URL and module format for a
162-
given module specifier and parent file URL:
163-
164-
```js
165-
const baseURL = new URL('file://');
166-
baseURL.pathname = `${process.cwd()}/`;
167-
168-
export async function resolve(specifier,
169-
parentModuleURL = baseURL,
170-
defaultResolver) {
171-
return {
172-
url: new URL(specifier, parentModuleURL).href,
173-
format: 'esm'
174-
};
175-
}
176-
```
177-
178-
The `parentModuleURL` is provided as `undefined` when performing main Node.js
179-
load itself.
180-
181-
The default Node.js ES module resolution function is provided as a third
182-
argument to the resolver for easy compatibility workflows.
183-
184-
In addition to returning the resolved file URL value, the resolve hook also
185-
returns a `format` property specifying the module format of the resolved
186-
module. This can be one of the following:
187-
188-
| `format` | Description |
189-
| --- | --- |
190-
| `'esm'` | Load a standard JavaScript module |
191-
| `'builtin'` | Load a node builtin CommonJS module |
192-
| `'dynamic'` | Use a [dynamic instantiate hook][] |
193-
194-
For example, a dummy loader to load JavaScript restricted to browser resolution
195-
rules with only JS file extension and Node.js builtin modules support could
196-
be written:
197-
198-
```js
199-
import path from 'path';
200-
import process from 'process';
201-
import Module from 'module';
202-
203-
const builtins = Module.builtinModules;
204-
const JS_EXTENSIONS = new Set(['.js', '.mjs']);
205-
206-
const baseURL = new URL('file://');
207-
baseURL.pathname = `${process.cwd()}/`;
208-
209-
export function resolve(specifier, parentModuleURL = baseURL, defaultResolve) {
210-
if (builtins.includes(specifier)) {
211-
return {
212-
url: specifier,
213-
format: 'builtin'
214-
};
215-
}
216-
if (/^\.{0,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
217-
// For node_modules support:
218-
// return defaultResolve(specifier, parentModuleURL);
219-
throw new Error(
220-
`imports must begin with '/', './', or '../'; '${specifier}' does not`);
221-
}
222-
const resolved = new URL(specifier, parentModuleURL);
223-
const ext = path.extname(resolved.pathname);
224-
if (!JS_EXTENSIONS.has(ext)) {
225-
throw new Error(
226-
`Cannot load file with non-JavaScript file extension ${ext}.`);
227-
}
228-
return {
229-
url: resolved.href,
230-
format: 'esm'
231-
};
232-
}
233-
```
234-
235-
With this loader, running:
236-
237-
```console
238-
NODE_OPTIONS='--experimental-modules --loader ./custom-loader.mjs' node x.js
239-
```
240-
241-
would load the module `x.js` as an ES module with relative resolution support
242-
(with `node_modules` loading skipped in this example).
243-
244-
### Dynamic instantiate hook
245-
246-
To create a custom dynamic module that doesn't correspond to one of the
247-
existing `format` interpretations, the `dynamicInstantiate` hook can be used.
248-
This hook is called only for modules that return `format: 'dynamic'` from
249-
the `resolve` hook.
250-
251-
```js
252-
export async function dynamicInstantiate(url) {
253-
return {
254-
exports: ['customExportName'],
255-
execute: (exports) => {
256-
// get and set functions provided for pre-allocated export names
257-
exports.customExportName.set('value');
258-
}
259-
};
260-
}
261-
```
262-
263-
With the list of module exports provided upfront, the `execute` function will
264-
then be called at the exact point of module evaluation order for that module
265-
in the import tree.
266-
267149
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
268-
[dynamic instantiate hook]: #esm_dynamic_instantiate_hook
269150
[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename
270151
[ESM Minimal Kernel]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md

lib/internal/modules/cjs/loader.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const { getOptionValue } = require('internal/options');
4545
const preserveSymlinks = getOptionValue('--preserve-symlinks');
4646
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4747
const experimentalModules = getOptionValue('--experimental-modules');
48-
const hasLoader = getOptionValue('--loader');
4948

5049
const {
5150
ERR_INVALID_ARG_TYPE,
@@ -766,7 +765,7 @@ Module.runMain = function() {
766765
const ext = path.extname(base);
767766
const isESM = ext === '.mjs';
768767

769-
if (experimentalModules && (isESM || hasLoader)) {
768+
if (experimentalModules && isESM) {
770769
if (asyncESM === undefined) lazyLoadESM();
771770
asyncESM.loaderPromise.then((loader) => {
772771
return loader.import(pathToFileURL(process.argv[1]).pathname);

lib/internal/process/esm_loader.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
callbackMap,
77
} = internalBinding('module_wrap');
88

9-
const { pathToFileURL } = require('internal/url');
109
const Loader = require('internal/modules/esm/loader');
1110
const {
1211
wrapToModuleMap,
@@ -46,16 +45,8 @@ exports.loaderPromise = new Promise((resolve, reject) => {
4645
exports.ESMLoader = undefined;
4746

4847
exports.setup = function() {
49-
let ESMLoader = new Loader();
48+
const ESMLoader = new Loader();
5049
const loaderPromise = (async () => {
51-
const userLoader = require('internal/options').getOptionValue('--loader');
52-
if (userLoader) {
53-
const hooks = await ESMLoader.import(
54-
userLoader, pathToFileURL(`${process.cwd()}/`).href);
55-
ESMLoader = new Loader();
56-
ESMLoader.hook(hooks);
57-
exports.ESMLoader = ESMLoader;
58-
}
5950
return ESMLoader;
6051
})();
6152
loaderResolve(loaderPromise);

src/node_config.cc

-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ static void Initialize(Local<Object> target,
8989

9090
if (env->options()->experimental_modules) {
9191
READONLY_BOOLEAN_PROPERTY("experimentalModules");
92-
const std::string& userland_loader = env->options()->userland_loader;
93-
if (!userland_loader.empty()) {
94-
READONLY_STRING_PROPERTY(target, "userLoader", userland_loader);
95-
}
9692
}
9793

9894
if (env->options()->experimental_vm_modules)

src/node_options.cc

-9
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
3232
}
3333

3434
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
35-
if (!userland_loader.empty() && !experimental_modules) {
36-
errors->push_back("--loader requires --experimental-modules be enabled");
37-
}
38-
3935
if (syntax_check_only && has_eval_string) {
4036
errors->push_back("either --check or --eval can be used, not both");
4137
}
@@ -102,11 +98,6 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
10298
&EnvironmentOptions::experimental_worker,
10399
kAllowedInEnvironment);
104100
AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals);
105-
AddOption("--loader",
106-
"(with --experimental-modules) use the specified file as a "
107-
"custom loader",
108-
&EnvironmentOptions::userland_loader,
109-
kAllowedInEnvironment);
110101
AddOption("--no-deprecation",
111102
"silence deprecation warnings",
112103
&EnvironmentOptions::no_deprecation,

src/node_options.h

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class EnvironmentOptions : public Options {
8484
bool trace_deprecation = false;
8585
bool trace_sync_io = false;
8686
bool trace_warnings = false;
87-
std::string userland_loader;
8887

8988
bool syntax_check_only = false;
9089
bool has_eval_string = false;

test/es-module/test-esm-example-loader.js

-6
This file was deleted.

test/es-module/test-esm-loader-dependency.mjs

-5
This file was deleted.

test/es-module/test-esm-loader-invalid-format.mjs

-12
This file was deleted.

test/es-module/test-esm-loader-invalid-url.mjs

-14
This file was deleted.

test/es-module/test-esm-loader-missing-dynamic-instantiate-hook.mjs

-10
This file was deleted.

test/es-module/test-esm-named-exports.mjs

-9
This file was deleted.

test/es-module/test-esm-preserve-symlinks-not-found-plain.mjs

-3
This file was deleted.

test/es-module/test-esm-preserve-symlinks-not-found.mjs

-3
This file was deleted.

test/es-module/test-esm-resolve-hook.mjs

-8
This file was deleted.

test/es-module/test-esm-shared-loader-dep.mjs

-11
This file was deleted.

test/es-module/test-esm-throw-undefined.mjs

-16
This file was deleted.

test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs

-29
This file was deleted.

0 commit comments

Comments
 (0)