This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
index.js
198 lines (172 loc) · 6.59 KB
/
index.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
import {dirname, extname, normalize, resolve, sep} from 'path';
import builtins from 'builtin-modules';
import resolveId from 'resolve';
import isModule from 'is-module';
import fs from 'fs';
const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
// It is important that .mjs occur before .js so that Rollup will interpret npm modules
// which deploy both ESM .mjs and CommonJS .js files as ESM.
const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];
let readFileCache = {};
const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));
function cachedReadFile (file, cb) {
if (file in readFileCache === false) {
readFileCache[file] = readFileAsync(file).catch(err => {
delete readFileCache[file];
throw err;
});
}
readFileCache[file].then(contents => cb(null, contents), cb);
}
let isFileCache = {};
function cachedIsFile (file, cb) {
if (file in isFileCache === false) {
isFileCache[file] = statAsync(file)
.then(
stat => stat.isFile(),
err => {
if (err.code == 'ENOENT') return false;
delete isFileCache[file];
throw err;
});
}
isFileCache[file].then(contents => cb(null, contents), cb);
}
const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents)));
export default function nodeResolve ( options = {} ) {
const useModule = options.module !== false;
const useMain = options.main !== false;
const useJsnext = options.jsnext === true;
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
const jail = options.jail;
const only = Array.isArray(options.only)
? options.only.map(o => o instanceof RegExp
? o
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
)
: null;
const browserMapCache = {};
if ( options.skip ) {
throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' );
}
if ( !useModule && !useMain && !useJsnext ) {
throw new Error( `At least one of options.module, options.main or options.jsnext must be true` );
}
let preserveSymlinks;
return {
name: 'node-resolve',
options ( options ) {
preserveSymlinks = options.preserveSymlinks;
},
generateBundle () {
isFileCache = {};
readFileCache = {};
},
resolveId ( importee, importer ) {
if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins
const basedir = importer ? dirname( importer ) : process.cwd();
if (options.browser && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
const browser = browserMapCache[importer];
if (browser[importee] === false || browser[resolvedImportee] === false) {
return ES6_BROWSER_EMPTY;
}
if (browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json']) {
importee = browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json'];
}
}
const parts = importee.split( /[/\\]/ );
let id = parts.shift();
if ( id[0] === '@' && parts.length ) {
// scoped packages
id += `/${parts.shift()}`;
} else if ( id[0] === '.' ) {
// an import relative to the parent dir of the importer
id = resolve( basedir, importee );
}
if (only && !only.some(pattern => pattern.test(id))) return null;
let disregardResult = false;
let packageBrowserField = false;
const extensions = options.extensions || DEFAULT_EXTS;
const resolveOptions = {
basedir,
packageFilter ( pkg, pkgPath ) {
const pkgRoot = dirname( pkgPath );
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
browser[ key ] = resolved;
if ( key[0] === '.' ) {
const absoluteKey = resolve( pkgRoot, key );
browser[ absoluteKey ] = resolved;
if ( !extname(key) ) {
extensions.reduce( ( browser, ext ) => {
browser[ absoluteKey + ext ] = browser[ key ];
return browser;
}, browser );
}
}
return browser;
}, {});
}
if (options.browser && typeof pkg[ 'browser' ] === 'string') {
pkg[ 'main' ] = pkg[ 'browser' ];
} else if ( useModule && pkg[ 'module' ] ) {
pkg[ 'main' ] = pkg[ 'module' ];
} else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
pkg[ 'main' ] = pkg[ 'jsnext:main' ];
} else if ( ( useJsnext || useModule ) && !useMain ) {
disregardResult = true;
}
return pkg;
},
readFile: cachedReadFile,
isFile: cachedIsFile,
extensions: extensions
};
if (preserveSymlinks !== undefined) {
resolveOptions.preserveSymlinks = preserveSymlinks;
}
return resolveIdAsync(
importee,
Object.assign( resolveOptions, customResolveOptions )
)
.catch(() => false)
.then(resolved => {
if (options.browser && packageBrowserField) {
if (packageBrowserField[ resolved ]) {
resolved = packageBrowserField[ resolved ];
}
browserMapCache[resolved] = packageBrowserField;
}
if ( !disregardResult && resolved !== false ) {
if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) {
resolved = fs.realpathSync( resolved );
}
if ( ~builtins.indexOf( resolved ) ) {
return null;
} else if ( ~builtins.indexOf( importee ) && preferBuiltins ) {
if ( !isPreferBuiltinsSet ) {
this.warn(
`preferring built-in module '${importee}' over local alternative ` +
`at '${resolved}', pass 'preferBuiltins: false' to disable this ` +
`behavior or 'preferBuiltins: true' to disable this warning`
);
}
return null;
} else if ( jail && resolved.indexOf( normalize( jail.trim( sep ) ) ) !== 0 ) {
return null;
}
}
if ( resolved && options.modulesOnly ) {
return readFileAsync( resolved, 'utf-8').then(code => isModule( code ) ? resolved : null);
} else {
return resolved === false ? null : resolved;
}
});
}
};
}