/
Module.js
458 lines (368 loc) · 12.5 KB
/
Module.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { parse } from 'acorn';
import MagicString from 'magic-string';
import { locate } from 'locate-character';
import { timeStart, timeEnd } from './utils/flushTime.js';
import { assign, blank, keys } from './utils/object.js';
import { basename, extname } from './utils/path.js';
import { makeLegal } from './utils/identifierHelpers.js';
import getCodeFrame from './utils/getCodeFrame.js';
import { SOURCEMAPPING_URL_RE } from './utils/sourceMappingURL.js';
import error from './utils/error.js';
import relativeId from './utils/relativeId.js';
import { SyntheticNamespaceDeclaration } from './Declaration.js';
import extractNames from './ast/utils/extractNames.js';
import enhance from './ast/enhance.js';
import clone from './ast/clone.js';
import ModuleScope from './ast/scopes/ModuleScope.js';
function tryParse ( module, acornOptions ) {
try {
return parse( module.code, assign( {
ecmaVersion: 8,
sourceType: 'module',
onComment: ( block, text, start, end ) => module.comments.push( { block, text, start, end } ),
preserveParens: false
}, acornOptions ) );
} catch ( err ) {
module.error( {
code: 'PARSE_ERROR',
message: err.message.replace( / \(\d+:\d+\)$/, '' )
}, err.pos );
}
}
export default class Module {
constructor ( { id, code, originalCode, originalSourceMap, ast, sourceMapChain, resolvedIds, resolvedExternalIds, bundle } ) {
this.code = code;
this.id = id;
this.bundle = bundle;
this.originalCode = originalCode;
this.originalSourceMap = originalSourceMap;
this.sourceMapChain = sourceMapChain;
this.comments = [];
timeStart( 'ast' );
if ( ast ) {
// prevent mutating the provided AST, as it may be reused on
// subsequent incremental rebuilds
this.ast = clone( ast );
this.astClone = ast;
} else {
this.ast = tryParse( this, bundle.acornOptions ); // TODO what happens to comments if AST is provided?
this.astClone = clone( this.ast );
}
timeEnd( 'ast' );
this.excludeFromSourcemap = /\0/.test( id );
this.context = bundle.getModuleContext( id );
// all dependencies
this.sources = [];
this.dependencies = [];
this.resolvedIds = resolvedIds || blank();
this.resolvedExternalIds = resolvedExternalIds || blank();
// imports and exports, indexed by local name
this.imports = blank();
this.exports = blank();
this.exportsAll = blank();
this.reexports = blank();
this.exportAllSources = [];
this.exportAllModules = null;
// By default, `id` is the filename. Custom resolvers and loaders
// can change that, but it makes sense to use it for the source filename
this.magicString = new MagicString( code, {
filename: this.excludeFromSourcemap ? null : id, // don't include plugin helpers in sourcemap
indentExclusionRanges: []
} );
// remove existing sourceMappingURL comments
this.comments = this.comments.filter( comment => {
//only one line comment can contain source maps
const isSourceMapComment = !comment.block && SOURCEMAPPING_URL_RE.test( comment.text );
if ( isSourceMapComment ) {
this.magicString.remove( comment.start, comment.end );
}
return !isSourceMapComment;
} );
this.declarations = blank();
this.type = 'Module'; // TODO only necessary so that Scope knows this should be treated as a function scope... messy
this.scope = new ModuleScope( this );
timeStart( 'analyse' );
this.analyse();
timeEnd( 'analyse' );
this.strongDependencies = [];
}
addExport ( node ) {
const source = node.source && node.source.value;
// export { name } from './other.js'
if ( source ) {
if ( !~this.sources.indexOf( source ) ) this.sources.push( source );
if ( node.type === 'ExportAllDeclaration' ) {
// Store `export * from '...'` statements in an array of delegates.
// When an unknown import is encountered, we see if one of them can satisfy it.
this.exportAllSources.push( source );
}
else {
node.specifiers.forEach( specifier => {
const name = specifier.exported.name;
if ( this.exports[ name ] || this.reexports[ name ] ) {
this.error( {
code: 'DUPLICATE_EXPORT',
message: `A module cannot have multiple exports with the same name ('${name}')`
}, specifier.start );
}
this.reexports[ name ] = {
start: specifier.start,
source,
localName: specifier.local.name,
module: null // filled in later
};
} );
}
}
// export default function foo () {}
// export default foo;
// export default 42;
else if ( node.type === 'ExportDefaultDeclaration' ) {
const identifier = ( node.declaration.id && node.declaration.id.name ) || node.declaration.name;
if ( this.exports.default ) {
this.error( {
code: 'DUPLICATE_EXPORT',
message: `A module can only have one default export`
}, node.start );
}
this.exports.default = {
localName: 'default',
identifier
};
// create a synthetic declaration
//this.declarations.default = new SyntheticDefaultDeclaration( node, identifier || this.basename() );
}
// export var { foo, bar } = ...
// export var foo = 42;
// export var a = 1, b = 2, c = 3;
// export function foo () {}
else if ( node.declaration ) {
const declaration = node.declaration;
if ( declaration.type === 'VariableDeclaration' ) {
declaration.declarations.forEach( decl => {
extractNames( decl.id ).forEach( localName => {
this.exports[ localName ] = { localName };
} );
} );
} else {
// export function foo () {}
const localName = declaration.id.name;
this.exports[ localName ] = { localName };
}
}
// export { foo, bar, baz }
else {
node.specifiers.forEach( specifier => {
const localName = specifier.local.name;
const exportedName = specifier.exported.name;
if ( this.exports[ exportedName ] || this.reexports[ exportedName ] ) {
this.error({
code: 'DUPLICATE_EXPORT',
message: `A module cannot have multiple exports with the same name ('${exportedName}')`
}, specifier.start );
}
this.exports[ exportedName ] = { localName };
});
}
}
addImport ( node ) {
const source = node.source.value;
if ( !~this.sources.indexOf( source ) ) this.sources.push( source );
node.specifiers.forEach( specifier => {
const localName = specifier.local.name;
if ( this.imports[ localName ] ) {
this.error( {
code: 'DUPLICATE_IMPORT',
message: `Duplicated import '${localName}'`
}, specifier.start );
}
const isDefault = specifier.type === 'ImportDefaultSpecifier';
const isNamespace = specifier.type === 'ImportNamespaceSpecifier';
const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name;
this.imports[ localName ] = { source, specifier, name, module: null };
} );
}
analyse () {
enhance( this.ast, this, this.comments );
// discover this module's imports and exports
let lastNode;
for ( const node of this.ast.body ) {
if ( node.isImportDeclaration ) {
this.addImport( node );
} else if ( node.isExportDeclaration ) {
this.addExport( node );
}
if ( lastNode ) lastNode.next = node.leadingCommentStart || node.start;
lastNode = node;
}
}
basename () {
const base = basename( this.id );
const ext = extname( this.id );
return makeLegal( ext ? base.slice( 0, -ext.length ) : base );
}
bindImportSpecifiers () {
[ this.imports, this.reexports ].forEach( specifiers => {
keys( specifiers ).forEach( name => {
const specifier = specifiers[ name ];
const id = this.resolvedIds[ specifier.source ] || this.resolvedExternalIds[ specifier.source ];
specifier.module = this.bundle.moduleById.get( id );
} );
} );
this.exportAllModules = this.exportAllSources.map( source => {
const id = this.resolvedIds[ source ] || this.resolvedExternalIds[ source ];
return this.bundle.moduleById.get( id );
} );
this.sources.forEach( source => {
const id = this.resolvedIds[ source ];
if ( id ) {
const module = this.bundle.moduleById.get( id );
this.dependencies.push( module );
}
} );
}
bindReferences () {
for ( const node of this.ast.body ) {
node.bind();
}
// if ( this.declarations.default ) {
// if ( this.exports.default.identifier ) {
// const declaration = this.trace( this.exports.default.identifier );
// if ( declaration ) this.declarations.default.bind( declaration );
// }
// }
}
error ( props, pos ) {
if ( pos !== undefined ) {
props.pos = pos;
const { line, column } = locate( this.code, pos, { offsetLine: 1 } ); // TODO trace sourcemaps
props.loc = { file: this.id, line, column };
props.frame = getCodeFrame( this.code, line, column );
}
error( props );
}
findParent () {
// TODO what does it mean if we're here?
return null;
}
getExports () {
const exports = blank();
keys( this.exports ).forEach( name => {
exports[ name ] = true;
} );
keys( this.reexports ).forEach( name => {
exports[ name ] = true;
} );
this.exportAllModules.forEach( module => {
if ( module.isExternal ) {
exports[ `*${module.id}` ] = true;
return;
}
module.getExports().forEach( name => {
if ( name !== 'default' ) exports[ name ] = true;
} );
} );
return keys( exports );
}
namespace () {
if ( !this.declarations[ '*' ] ) {
this.declarations[ '*' ] = new SyntheticNamespaceDeclaration( this );
}
return this.declarations[ '*' ];
}
render ( es, legacy ) {
const magicString = this.magicString.clone();
for ( const node of this.ast.body ) {
node.render( magicString, es );
}
if ( this.namespace().needsNamespaceBlock ) {
magicString.append( '\n\n' + this.namespace().renderBlock( es, legacy, '\t' ) ); // TODO use correct indentation
}
return magicString.trim();
}
run () {
for ( const node of this.ast.body ) {
if ( node.hasEffects() ) {
node.run();
}
}
}
toJSON () {
return {
id: this.id,
dependencies: this.dependencies.map( module => module.id ),
code: this.code,
originalCode: this.originalCode,
originalSourceMap: this.originalSourceMap,
ast: this.astClone,
sourceMapChain: this.sourceMapChain,
resolvedIds: this.resolvedIds,
resolvedExternalIds: this.resolvedExternalIds
};
}
trace ( name ) {
// TODO this is slightly circular
if ( name in this.scope.declarations ) {
return this.scope.declarations[ name ];
}
if ( name in this.imports ) {
const importDeclaration = this.imports[ name ];
const otherModule = importDeclaration.module;
if ( importDeclaration.name === '*' && !otherModule.isExternal ) {
return otherModule.namespace();
}
const declaration = otherModule.traceExport( importDeclaration.name );
if ( !declaration ) {
this.error( {
code: 'MISSING_EXPORT',
message: `'${importDeclaration.name}' is not exported by ${relativeId( otherModule.id )}`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
}, importDeclaration.specifier.start );
}
return declaration;
}
return null;
}
traceExport ( name ) {
// export * from 'external'
if ( name[ 0 ] === '*' ) {
const module = this.bundle.moduleById.get( name.slice( 1 ) );
return module.traceExport( '*' );
}
// export { foo } from './other.js'
const reexportDeclaration = this.reexports[ name ];
if ( reexportDeclaration ) {
const declaration = reexportDeclaration.module.traceExport( reexportDeclaration.localName );
if ( !declaration ) {
this.error( {
code: 'MISSING_EXPORT',
message: `'${reexportDeclaration.localName}' is not exported by ${relativeId( reexportDeclaration.module.id )}`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
}, reexportDeclaration.start );
}
return declaration;
}
const exportDeclaration = this.exports[ name ];
if ( exportDeclaration ) {
const name = exportDeclaration.localName;
const declaration = this.trace( name );
return declaration || this.bundle.scope.findDeclaration( name );
}
if ( name === 'default' ) return;
for ( let i = 0; i < this.exportAllModules.length; i += 1 ) {
const module = this.exportAllModules[ i ];
const declaration = module.traceExport( name );
if ( declaration ) return declaration;
}
}
warn ( warning, pos ) {
if ( pos !== undefined ) {
warning.pos = pos;
const { line, column } = locate( this.code, pos, { offsetLine: 1 } ); // TODO trace sourcemaps
warning.loc = { file: this.id, line, column };
warning.frame = getCodeFrame( this.code, line, column );
}
warning.id = this.id;
this.bundle.warn( warning );
}
}