/
node-cjs-loader-utils.js
118 lines (106 loc) · 3.7 KB
/
node-cjs-loader-utils.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// copied from https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js
const path = require('path');
const fs = require('fs');
module.exports.assertScriptCanLoadAsCJSImpl = assertScriptCanLoadAsCJSImpl;
// copied from Module._extensions['.js']
function assertScriptCanLoadAsCJSImpl(filename) {
const pkg = readPackageScope(filename);
// Function require shouldn't be used in ES modules.
if (pkg && pkg.data && pkg.data.type === 'module') {
const parentPath = module.parent && module.parent.filename;
const packageJsonPath = path.resolve(pkg.path, 'package.json');
throw createErrRequireEsm(filename, parentPath, packageJsonPath);
}
}
function readPackageScope(checkPath) {
const rootSeparatorIndex = checkPath.indexOf(path.sep);
let separatorIndex;
while (
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
) {
checkPath = checkPath.slice(0, separatorIndex);
if (checkPath.endsWith(path.sep + 'node_modules'))
return false;
const pjson = readPackage(checkPath);
if (pjson) return {
path: checkPath,
data: pjson
};
}
return false;
}
const packageJsonCache = new Map();
function readPackage(requestPath) {
const jsonPath = path.resolve(requestPath, 'package.json');
const existing = packageJsonCache.get(jsonPath);
if (existing !== undefined) return existing;
const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath));
if (json === undefined) {
packageJsonCache.set(jsonPath, false);
return false;
}
// TODO Related to `--experimental-policy`? Disabling for now
// if (manifest) {
// const jsonURL = pathToFileURL(jsonPath);
// manifest.assertIntegrity(jsonURL, json);
// }
try {
const parsed = JSON.parse(json);
const filtered = {
name: parsed.name,
main: parsed.main,
exports: parsed.exports,
type: parsed.type
};
packageJsonCache.set(jsonPath, filtered);
return filtered;
} catch (e) {
e.path = jsonPath;
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
throw e;
}
}
function internalModuleReadJSON(path) {
try {
return fs.readFileSync(path, 'utf8')
} catch (e) {
if (e.code === 'ENOENT') return undefined
throw e
}
}
function createErrRequireEsm(filename, parentPath, packageJsonPath) {
// Attempt to create an error object that matches node's native error close enough
const code = 'ERR_REQUIRE_ESM'
const err = new Error(getMessage(filename, parentPath, packageJsonPath))
err.name = `Error [${ code }]`
err.stack
Object.defineProperty(err, 'name', {
value: 'Error',
enumerable: false,
writable: true,
configurable: true
})
err.code = code
return err
// copy-pasted from https://github.com/nodejs/node/blob/master/lib/internal/errors.js#L1294-L1311
function getMessage(filename, parentPath = null, packageJsonPath = null) {
const ext = path.extname(filename)
let msg = `Must use import to load ES Module: ${filename}`;
if (parentPath && packageJsonPath) {
const path = require('path');
const basename = path.basename(filename) === path.basename(parentPath) ?
filename : path.basename(filename);
msg +=
'\nrequire() of ES modules is not supported.\nrequire() of ' +
`${filename} ${parentPath ? `from ${parentPath} ` : ''}` +
`is an ES module file as it is a ${ext} file whose nearest parent ` +
`package.json contains "type": "module" which defines all ${ext} ` +
'files in that package scope as ES modules.\nInstead ' +
'change the requiring code to use ' +
'import(), or remove "type": "module" from ' +
`${packageJsonPath}.\n`;
return msg;
}
return msg;
}
}