-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy path_old_getMain.js.bak
More file actions
152 lines (138 loc) · 4.69 KB
/
_old_getMain.js.bak
File metadata and controls
152 lines (138 loc) · 4.69 KB
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
function replaceName(filename, name) {
return resolve(
dirname(filename),
name + basename(filename).replace(/^[^.]+/, ''),
);
}
/**
* @param {any} exports - package.json "exports" field value
* @param {string} exportPath - the export to look up (eg: '.', './a', './b/c')
* @param {string[]} conditions - conditional export keys to use (note: unlike in resolution, order here *does* define precedence!)
* @param {RegExp|string} [defaultPattern] - only use (resolved) default export filenames that match this pattern
* @param {string} [condition] - (internal) the nearest condition key on the stack
*/
function walk(exports, exportPath, conditions, defaultPattern, condition) {
if (!exports) return;
if (typeof exports === 'string') {
if (
condition === 'default' &&
defaultPattern &&
!exports.match(defaultPattern)
) {
return;
}
return exports;
}
if (Array.isArray(exports)) {
for (const map of exports) {
const r = walk(map, exportPath, conditions, defaultPattern, condition);
if (r) return r;
}
return;
}
const map = exports[exportPath];
if (map) {
const r = walk(map, exportPath, conditions, defaultPattern, condition);
if (r) return r;
}
for (const condition of conditions) {
const map = exports[condition];
if (!map) continue;
const r = walk(map, exportPath, conditions, defaultPattern, condition);
if (r) return r;
}
// return walk(exports['.'] || exports.import || exports.module);
}
function getMain({ options, entry, format }) {
const { pkg } = options;
const pkgMain = options['pkg-main'];
if (!pkgMain) {
return options.output;
}
// package.json export name (see https://nodejs.org/api/packages.html#packages_subpath_exports)
let exportPath = '.';
let mainNoExtension = options.output;
if (options.multipleEntries) {
const commonDir = options.entries
.reduce((acc, entry) => {
entry = dirname(entry).split(sep);
if (!acc) return entry;
if (entry.length < acc.length) acc.length = entry.length;
let last = entry.length - 1;
while (entry[last] !== acc[last]) {
last--;
acc.pop();
}
return acc;
}, undefined)
.join(sep);
console.log('>>>>>', { first: options.entries[0], commonDir });
const isMainEntry = entry.match(
/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/,
);
let name = isMainEntry ? mainNoExtension : entry;
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
if (!isMainEntry) {
exportPath =
'./' +
posix.relative(commonDir, entry.replace(/\.([mc]js|[tj]sx?)$/g, ''));
}
}
mainNoExtension = mainNoExtension.replace(
/(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/,
'',
);
const mainsByFormat = {};
const MJS = pkg.type === 'module' ? /\.m?js$/i : /\.mjs$/i;
const CJS = pkg.type === 'module' ? /\.cjs$/i : /\.js$/i;
const UMD = /[.-]umd\.c?js$/i;
const CONDITIONS_MJS = ['import', 'module', 'default'];
const CONDITIONS_MODERN = ['modern', 'esmodules', ...CONDITIONS_MJS];
const CONDITIONS_CJS = ['require', 'default'];
const CONDITIONS_UMD = ['umd', 'default'];
mainsByFormat.modern =
walk(pkg.exports, exportPath, CONDITIONS_MODERN, MJS) ||
(pkg.syntax && pkg.syntax.esmodules) ||
pkg.esmodule ||
replaceName('x.modern.js', mainNoExtension);
mainsByFormat.es = walk(pkg.exports, exportPath, CONDITIONS_MJS, MJS);
if (!mainsByFormat.es || mainsByFormat.es === mainsByFormat.modern) {
mainsByFormat.es =
pkg.module && !pkg.module.match(/src\//)
? pkg.module
: pkg['jsnext:main'] || replaceName('x.esm.js', mainNoExtension);
}
mainsByFormat.umd =
walk(pkg.exports, exportPath, CONDITIONS_UMD, UMD) ||
pkg['umd:main'] ||
pkg.unpkg ||
replaceName('x.umd.js', mainNoExtension);
mainsByFormat.cjs = walk(pkg.exports, exportPath, CONDITIONS_CJS, CJS);
if (!mainsByFormat.cjs || mainsByFormat.cjs === mainsByFormat.umd) {
mainsByFormat.cjs =
pkg['cjs:main'] ||
replaceName(pkg.type === 'module' ? 'x.cjs' : 'x.js', mainNoExtension);
}
if (pkg.type === 'module') {
let errors = [];
let filenames = [];
if (mainsByFormat.cjs.endsWith('.js')) {
errors.push('CommonJS');
filenames.push(` "cjs:main": "${mainsByFormat.cjs}",`);
}
if (mainsByFormat.umd.endsWith('.js')) {
errors.push('CommonJS');
const field = pkg['umd:main'] ? 'umd:main' : 'unpkg';
filenames.push(` "${field}": "${mainsByFormat.umd}",`);
}
if (errors.length) {
const warning =
`Warning: A package.json with {"type":"module"} should use .cjs file extensions for` +
` ${errors.join(' and ')} filename${errors.length == 1 ? '' : 's'}:` +
`\n${filenames.join('\n')}`;
stderr(yellow(warning));
}
}
console.log('>> MAIN: ', format, entry, exportPath, mainsByFormat[format]);
return mainsByFormat[format] || mainsByFormat.cjs;
}