-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwalker-package-ranger.js
251 lines (210 loc) · 9.69 KB
/
walker-package-ranger.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* eslint-disable max-depth,complexity */
import * as acorn from 'acorn';
import fs from 'fs';
import { getNodeModulesLocationForPackage } from './node-modules-utils.js';
import path from 'path';
import * as walk from 'acorn-walk';
const importMap = {};
const updateImportMap = (entry, entryPath) => {
if (path.extname(entryPath) === '') {
entryPath = `${entryPath}.js`;
}
// handle WIn v Unix-style path separators and force to /
importMap[entry.replace(/\\/g, '/')] = entryPath.replace(/\\/g, '/');
};
// handle ESM paths that have varying levels of nesting, e.g. export * from '../../something.js'
// https://github.com/ProjectEvergreen/greenwood/issues/820
async function resolveRelativeSpecifier(specifier, modulePath, dependency) {
const absoluteNodeModulesLocation = await getNodeModulesLocationForPackage(dependency);
// handle WIn v Unix-style path separators and force to /
return `${dependency}${path.join(path.dirname(modulePath), specifier).replace(/\\/g, '/').replace(absoluteNodeModulesLocation.replace(/\\/g, '/', ''), '')}`;
}
async function getPackageEntryPath(packageJson) {
let entry = packageJson.exports
? Object.keys(packageJson.exports) // first favor export maps first
: packageJson.module // next favor ESM entry points
? packageJson.module
: packageJson.main && packageJson.main !== '' // then favor main
? packageJson.main
: 'index.js'; // lastly, fallback to index.js
// use .mjs version if it exists, for packages like redux
if (!Array.isArray(entry) && fs.existsSync(`${await getNodeModulesLocationForPackage(packageJson.name)}/${entry.replace('.js', '.mjs')}`)) {
entry = entry.replace('.js', '.mjs');
}
return entry;
}
async function walkModule(modulePath, dependency) {
const moduleContents = fs.readFileSync(modulePath, 'utf-8');
walk.simple(acorn.parse(moduleContents, {
ecmaVersion: '2020',
sourceType: 'module'
}), {
async ImportDeclaration(node) {
let { value: sourceValue } = node.source;
const absoluteNodeModulesLocation = await getNodeModulesLocationForPackage(dependency);
const isBarePath = sourceValue.indexOf('http') !== 0 && sourceValue.charAt(0) !== '.' && sourceValue.charAt(0) !== path.sep;
const hasExtension = path.extname(sourceValue) !== '';
if (isBarePath && !hasExtension) {
if (!importMap[sourceValue]) {
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
}
await walkPackageJson(path.join(absoluteNodeModulesLocation, 'package.json'));
} else if (isBarePath) {
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
} else {
// walk this module for all its dependencies
sourceValue = !hasExtension
? `${sourceValue}.js`
: sourceValue;
if (fs.existsSync(path.join(absoluteNodeModulesLocation, sourceValue))) {
const entry = `/node_modules/${await resolveRelativeSpecifier(sourceValue, modulePath, dependency)}`;
await walkModule(path.join(absoluteNodeModulesLocation, sourceValue), dependency);
updateImportMap(path.join(dependency, sourceValue), entry);
}
}
},
async ExportNamedDeclaration(node) {
const sourceValue = node && node.source ? node.source.value : '';
if (sourceValue !== '' && sourceValue.indexOf('http') !== 0) {
// handle relative specifier
if (sourceValue.indexOf('.') === 0) {
const entry = `/node_modules/${await resolveRelativeSpecifier(sourceValue, modulePath, dependency)}`;
updateImportMap(path.join(dependency, sourceValue), entry);
} else {
// handle bare specifier
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
}
}
},
async ExportAllDeclaration(node) {
const sourceValue = node && node.source ? node.source.value : '';
if (sourceValue !== '' && sourceValue.indexOf('http') !== 0) {
if (sourceValue.indexOf('.') === 0) {
const entry = `/node_modules/${await resolveRelativeSpecifier(sourceValue, modulePath, dependency)}`;
updateImportMap(path.join(dependency, sourceValue), entry);
} else {
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
}
}
}
});
}
async function walkPackageJson(packageJson = {}) {
// while walking a package.json we need to find its entry point, e.g. index.js
// and then walk that for import / export statements
// and walk its package.json for its dependencies
for (const dependency of Object.keys(packageJson.dependencies || {})) {
const dependencyPackageRootPath = path.join(process.cwd(), 'node_modules', dependency);
const dependencyPackageJsonPath = path.join(dependencyPackageRootPath, 'package.json');
const dependencyPackageJson = JSON.parse(fs.readFileSync(dependencyPackageJsonPath, 'utf-8'));
const entry = await getPackageEntryPath(dependencyPackageJson);
const isJavascriptPackage = Array.isArray(entry) || typeof entry === 'string' && entry.endsWith('.js') || entry.endsWith('.mjs');
if (isJavascriptPackage) {
const absoluteNodeModulesLocation = await getNodeModulesLocationForPackage(dependency);
// https://nodejs.org/api/packages.html#packages_determining_module_system
if (Array.isArray(entry)) {
// we have an exportMap
const exportMap = entry;
for (const entry of exportMap) {
const exportMapEntry = dependencyPackageJson.exports[entry];
let packageExport;
if (Array.isArray(exportMapEntry)) {
let fallbackPath;
let esmPath;
exportMapEntry.forEach((mapItem) => {
switch (typeof mapItem) {
case 'string':
fallbackPath = mapItem;
break;
case 'object':
const entryTypes = Object.keys(mapItem);
if (entryTypes.import) {
esmPath = entryTypes.import;
} else if (entryTypes.require) {
console.error('The package you are importing needs commonjs support. Please use our commonjs plugin to fix this error.');
fallbackPath = entryTypes.require;
} else if (entryTypes.default) {
console.warn('The package you are requiring may need commonjs support. If this module is not working for you, consider adding our commonjs plugin.');
fallbackPath = entryTypes.default;
}
break;
default:
console.warn(`Sorry, we were unable to detect the module type for ${mapItem} :(. please consider opening an issue to let us know about your use case.`);
break;
}
});
packageExport = esmPath
? esmPath
: fallbackPath;
} else if (exportMapEntry.import || exportMapEntry.default) {
packageExport = exportMapEntry.import
? exportMapEntry.import
: exportMapEntry.default;
// use the dependency itself as an entry in the importMap
if (entry === '.') {
updateImportMap(dependency, `/node_modules/${path.join(dependency, packageExport)}`);
}
} else if (exportMapEntry.endsWith && (exportMapEntry.endsWith('.js') || exportMapEntry.endsWith('.mjs')) && exportMapEntry.indexOf('*') < 0) {
// is probably a file, so _not_ an export array, package.json, or wildcard export
packageExport = exportMapEntry;
}
if (packageExport) {
const packageExportLocation = path.resolve(absoluteNodeModulesLocation, packageExport);
if (packageExport.endsWith('js')) {
updateImportMap(path.join(dependency, entry), `/node_modules/${path.join(dependency, packageExport)}`);
} else if (fs.lstatSync(packageExportLocation).isDirectory()) {
fs.readdirSync(packageExportLocation)
.filter(file => file.endsWith('.js') || file.endsWith('.mjs'))
.forEach((file) => {
updateImportMap(path.join(dependency, packageExport, file), `/node_modules/${path.join(dependency, packageExport, file)}`);
});
} else {
console.warn('Warning, not able to handle export', path.join(dependency, packageExport));
}
}
}
await walkPackageJson(dependencyPackageJson);
} else {
const packageEntryPointPath = path.join(absoluteNodeModulesLocation, entry);
// sometimes a main file is actually just an empty string... :/
if (fs.existsSync(packageEntryPointPath)) {
updateImportMap(dependency, `/node_modules/${path.join(dependency, entry)}`);
await walkModule(packageEntryPointPath, dependency);
await walkPackageJson(dependencyPackageJson);
}
}
}
}
return importMap;
}
function mergeImportMap(html = '', map = {}, shouldShim = false) {
const importMapType = shouldShim ? 'importmap-shim' : 'importmap';
const hasImportMap = html.indexOf(`script type="${importMapType}"`) > 0;
const danglingComma = hasImportMap ? ',' : '';
const importMap = JSON.stringify(map, null, 2).replace('}', '').replace('{', '');
if (Object.entries(map).length === 0) {
return html;
}
if (hasImportMap) {
return html.replace('"imports": {', `
"imports": {
${importMap}${danglingComma}
`);
} else {
return html.replace('<head>', `
<head>
<script type="${importMapType}">
{
"imports": {
${importMap}
}
}
</script>
`);
}
}
export {
mergeImportMap,
walkPackageJson,
walkModule
};