forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesm-main-loader.mjs
39 lines (32 loc) · 1.28 KB
/
esm-main-loader.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {extname} from 'path';
import {fileURLToPath} from 'url';
import * as extensionLoader from './esm-extension-loader.mjs';
import * as nodeModuleLoader from './esm-node-module-loader.mjs';
const loaders = [extensionLoader, nodeModuleLoader];
export async function resolve(initialSpecifier, initialCtx, defaultResolve) {
let nextFn = (i) => (s, c) => {
if (i === loaders.length) {
return defaultResolve(s, c, defaultResolve);
}
return loaders[i].resolve(s, c, nextFn(i + 1));
};
return nextFn(0)(initialSpecifier, initialCtx);
}
export async function load(url, context, defaultLoad) {
// Using `--loader` causes non-ESM extension-less files like
// for `typescript/bin/tsc` to be considered as ESM. This is a bug
// via: https://github.com/nodejs/node/issues/33226.
// Workaround is to load such extension-less files as CommonJS. Similar
// to how they are loaded without `--loader` being specified.
if (url.startsWith('file://') && extname(fileURLToPath(url)) === '') {
context.format = 'commonjs';
}
return defaultLoad(url, context, defaultLoad);
}