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 116
/
Copy pathindex.js
243 lines (187 loc) · 7.61 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
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
import { statSync } from 'fs';
import { basename, dirname, extname, resolve, sep } from 'path';
import { sync as nodeResolveSync } from 'resolve';
import acorn from 'acorn';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { attachScopes, createFilter, makeLegalIdentifier } from 'rollup-pluginutils';
import { flatten, isReference } from './ast-utils.js';
var firstpass = /\b(?:require|module|exports|global)\b/;
var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
const reserved = 'abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield'.split( ' ' );
var blacklistedExports = { __esModule: true };
reserved.forEach( word => blacklistedExports[ word ] = true );
function getName ( id ) {
const base = basename( id );
const ext = extname( base );
return makeLegalIdentifier( ext.length ? base.slice( 0, -ext.length ) : base );
}
function getCandidatesForExtension ( resolved, extension ) {
return [
resolved + extension,
resolved + `${sep}index${extension}`
];
}
function getCandidates ( resolved, extensions ) {
return extensions.reduce(
( paths, extension ) => paths.concat( getCandidatesForExtension ( resolved, extension ) ),
[resolved]
);
}
export default function commonjs ( options = {} ) {
const extensions = options.extensions || ['.js'];
const filter = createFilter( options.include, options.exclude );
let bundleUsesGlobal = false;
let bundleRequiresWrappers = false;
const sourceMap = options.sourceMap !== false;
let customNamedExports = {};
if ( options.namedExports ) {
Object.keys( options.namedExports ).forEach( id => {
let resolvedId;
try {
resolvedId = nodeResolveSync( id, { basedir: process.cwd() });
} catch ( err ) {
resolvedId = resolve( id );
}
customNamedExports[ resolvedId ] = options.namedExports[ id ];
});
}
return {
resolveId ( importee, importer ) {
if ( importee[0] !== '.' ) return; // not our problem
const resolved = resolve( dirname( importer ), importee );
const candidates = getCandidates( resolved, extensions );
for ( let i = 0; i < candidates.length; i += 1 ) {
try {
const stats = statSync( candidates[i] );
if ( stats.isFile() ) return candidates[i];
} catch ( err ) { /* noop */ }
}
},
transform ( code, id ) {
if ( !filter( id ) ) return null;
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;
if ( !firstpass.test( code ) ) return null;
let ast;
try {
ast = acorn.parse( code, {
ecmaVersion: 6,
sourceType: 'module'
});
} catch ( err ) {
err.message += ` in ${id}`;
throw err;
}
const magicString = new MagicString( code );
let required = {};
let uid = 0;
let scope = attachScopes( ast, 'scope' );
let uses = { module: false, exports: false, global: false };
let namedExports = {};
if ( customNamedExports[ id ] ) {
customNamedExports[ id ].forEach( name => namedExports[ name ] = true );
}
let scopeDepth = 0;
walk( ast, {
enter ( node, parent ) {
if ( node.scope ) scope = node.scope;
if ( /^Function/.test( node.type ) ) scopeDepth += 1;
if ( sourceMap ) {
magicString.addSourcemapLocation( node.start );
magicString.addSourcemapLocation( node.end );
}
// Is this an assignment to exports or module.exports?
if ( node.type === 'AssignmentExpression' ) {
if ( node.left.type !== 'MemberExpression' ) return;
const flattened = flatten( node.left );
if ( !flattened ) return;
if ( scope.contains( flattened.name ) ) return;
const match = exportsPattern.exec( flattened.keypath );
if ( !match || flattened.keypath === 'exports' ) return;
if ( flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression' ) {
return node.right.properties.forEach( prop => {
if ( prop.computed || prop.key.type !== 'Identifier' ) return;
const name = prop.key.name;
if ( name === makeLegalIdentifier( name ) ) namedExports[ name ] = true;
});
}
if ( match[1] ) namedExports[ match[1] ] = true;
return;
}
if ( node.type === 'Identifier' ) {
if ( ( node.name in uses && !uses[ node.name ] ) && isReference( node, parent ) && !scope.contains( node.name ) ) uses[ node.name ] = true;
return;
}
if ( node.type === 'ThisExpression' && scopeDepth === 0 ) {
uses.global = true;
magicString.overwrite( node.start, node.end, `__commonjs_global`, true );
return;
}
if ( node.type !== 'CallExpression' ) return;
if ( node.callee.name !== 'require' || scope.contains( 'require' ) ) return;
if ( node.arguments.length !== 1 || node.arguments[0].type !== 'Literal' ) return; // TODO handle these weird cases?
const source = node.arguments[0].value;
let existing = required[ source ];
let name;
if ( !existing ) {
name = `require$$${uid++}`;
required[ source ] = { source, name, importsDefault: false };
} else {
name = required[ source ].name;
}
if ( parent.type !== 'ExpressionStatement' ) {
required[ source ].importsDefault = true;
magicString.overwrite( node.start, node.end, name );
} else {
// is a bare import, e.g. `require('foo');`
magicString.remove( parent.start, parent.end );
}
},
leave ( node ) {
if ( node.scope ) scope = scope.parent;
if ( /^Function/.test( node.type ) ) scopeDepth -= 1;
}
});
const sources = Object.keys( required );
if ( !sources.length && !uses.module && !uses.exports && !uses.global ) {
if ( Object.keys( customNamedExports ).length ) {
throw new Error( `Custom named exports were specified for ${id} but it does not appear to be a CommonJS module` );
}
return null; // not a CommonJS module
}
bundleRequiresWrappers = true;
const name = getName( id );
const importBlock = sources.length ?
sources.map( source => {
const { name, importsDefault } = required[ source ];
return `import ${importsDefault ? `${name} from ` : ``}'${source}';`;
}).join( '\n' ) :
'';
const args = `module${uses.exports || uses.global ? ', exports' : ''}${uses.global ? ', global' : ''}`;
const intro = `\n\nvar ${name} = __commonjs(function (${args}) {\n`;
let outro = `\n});\n\nexport default (${name} && typeof ${name} === 'object' && 'default' in ${name} ? ${name}['default'] : ${name});\n`;
outro += Object.keys( namedExports )
.filter( key => !blacklistedExports[ key ] )
.map( x => `export var ${x} = ${name}.${x};` )
.join( '\n' );
magicString.trim()
.prepend( importBlock + intro )
.trim()
.append( outro );
code = magicString.toString();
const map = sourceMap ? magicString.generateMap() : null;
if ( uses.global ) bundleUsesGlobal = true;
return { code, map };
},
intro () {
var intros = [];
if ( bundleUsesGlobal ) {
intros.push( `var __commonjs_global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this;` );
}
if ( bundleRequiresWrappers ) {
intros.push( `function __commonjs(fn, module) { return module = { exports: {} }, fn(module, module.exports${bundleUsesGlobal ? ', __commonjs_global' : ''}), module.exports; }\n` );
}
return intros.join( '\n' );
}
};
}