-
-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathutil.js
166 lines (142 loc) · 4.56 KB
/
util.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
import { dirname, extname, resolve } from 'path';
import { createFilter } from '@rollup/pluginutils';
import { realpathSync } from './fs';
// returns the imported package name for bare module imports
export function getPackageName(id) {
if (id.startsWith('.') || id.startsWith('/')) {
return null;
}
const split = id.split('/');
// @my-scope/my-package/foo.js -> @my-scope/my-package
// @my-scope/my-package -> @my-scope/my-package
if (split[0][0] === '@') {
return `${split[0]}/${split[1]}`;
}
// my-package/foo.js -> my-package
// my-package -> my-package
return split[0];
}
export function getMainFields(options) {
let mainFields;
if (options.mainFields) {
({ mainFields } = options);
} else {
mainFields = ['module', 'main'];
}
if (options.browser && mainFields.indexOf('browser') === -1) {
return ['browser'].concat(mainFields);
}
if (!mainFields.length) {
throw new Error('Please ensure at least one `mainFields` value is specified');
}
return mainFields;
}
export function getPackageInfo(options) {
const {
cache,
extensions,
pkg,
mainFields,
preserveSymlinks,
useBrowserOverrides,
rootDir,
ignoreSideEffectsForRoot
} = options;
let { pkgPath } = options;
if (cache.has(pkgPath)) {
return cache.get(pkgPath);
}
// browserify/resolve doesn't realpath paths returned in its packageFilter callback
if (!preserveSymlinks) {
pkgPath = realpathSync(pkgPath);
}
const pkgRoot = dirname(pkgPath);
const packageInfo = {
// copy as we are about to munge the `main` field of `pkg`.
packageJson: { ...pkg },
// path to package.json file
packageJsonPath: pkgPath,
// directory containing the package.json
root: pkgRoot,
// which main field was used during resolution of this module (main, module, or browser)
resolvedMainField: 'main',
// whether the browser map was used to resolve the entry point to this module
browserMappedMain: false,
// the entry point of the module with respect to the selected main field and any
// relevant browser mappings.
resolvedEntryPoint: ''
};
let overriddenMain = false;
for (let i = 0; i < mainFields.length; i++) {
const field = mainFields[i];
if (typeof pkg[field] === 'string') {
pkg.main = pkg[field];
packageInfo.resolvedMainField = field;
overriddenMain = true;
break;
}
}
const internalPackageInfo = {
cachedPkg: pkg,
hasModuleSideEffects: () => null,
hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
packageBrowserField:
useBrowserOverrides &&
typeof pkg.browser === 'object' &&
Object.keys(pkg.browser).reduce((browser, key) => {
let resolved = pkg.browser[key];
if (resolved && resolved[0] === '.') {
resolved = resolve(pkgRoot, resolved);
}
/* eslint-disable no-param-reassign */
browser[key] = resolved;
if (key[0] === '.') {
const absoluteKey = resolve(pkgRoot, key);
browser[absoluteKey] = resolved;
if (!extname(key)) {
extensions.reduce((subBrowser, ext) => {
subBrowser[absoluteKey + ext] = subBrowser[key];
return subBrowser;
}, browser);
}
}
return browser;
}, {}),
packageInfo
};
const browserMap = internalPackageInfo.packageBrowserField;
if (
useBrowserOverrides &&
typeof pkg.browser === 'object' &&
// eslint-disable-next-line no-prototype-builtins
browserMap.hasOwnProperty(pkg.main)
) {
packageInfo.resolvedEntryPoint = browserMap[pkg.main];
packageInfo.browserMappedMain = true;
} else {
// index.node is technically a valid default entrypoint as well...
packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js');
packageInfo.browserMappedMain = false;
}
if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
const packageSideEffects = pkg.sideEffects;
if (typeof packageSideEffects === 'boolean') {
internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
} else if (Array.isArray(packageSideEffects)) {
internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
resolve: pkgRoot
});
}
}
cache.set(pkgPath, internalPackageInfo);
return internalPackageInfo;
}
export function normalizeInput(input) {
if (Array.isArray(input)) {
return input;
} else if (typeof input === 'object') {
return Object.values(input);
}
// otherwise it's a string
return [input];
}