@@ -12,7 +12,12 @@ import SourceMap from '@parcel/source-map';
12
12
import invariant from 'assert' ;
13
13
import path from 'path' ;
14
14
import fs from 'fs' ;
15
- import { replaceScriptDependencies , getSpecifier } from './utils' ;
15
+ import {
16
+ replaceScriptDependencies ,
17
+ getSpecifier ,
18
+ makeValidIdentifier ,
19
+ isValidIdentifier ,
20
+ } from './utils' ;
16
21
import { helpers } from './helpers' ;
17
22
18
23
const PRELUDE = fs
@@ -61,6 +66,7 @@ export class DevPackager {
61
66
let prefix = this . getPrefix ( ) ;
62
67
let lineOffset = countLines ( prefix ) ;
63
68
let script : ?{ | code : string , mapBuffer : ?Buffer | } = null ;
69
+ let externals = new Set ( ) ;
64
70
65
71
let usedHelpers = 0 ;
66
72
this . bundle . traverse ( node => {
@@ -116,6 +122,9 @@ export class DevPackager {
116
122
} else {
117
123
// An external module - map placeholder to original specifier.
118
124
deps [ specifier ] = dep . specifier ;
125
+ if ( this . bundle . env . outputFormat === 'esmodule' ) {
126
+ externals . add ( dep . specifier ) ;
127
+ }
119
128
}
120
129
}
121
130
@@ -209,7 +218,20 @@ export class DevPackager {
209
218
prefix = prefix . replace ( '// INSERT_LOAD_HERE' , load . replace ( / \n / g, '' ) ) ;
210
219
}
211
220
221
+ let externalImports = '' ;
222
+ let externalMap = '{' ;
223
+ let e = 0 ;
224
+ for ( let external of externals ) {
225
+ let name = `__parcelExternal${ e ++ } ` ;
226
+ externalImports += `import * as ${ name } from ${ JSON . stringify (
227
+ external ,
228
+ ) } ;\n`;
229
+ externalMap += `${ JSON . stringify ( external ) } : ${ name } ,` ;
230
+ }
231
+ externalMap += '}' ;
232
+
212
233
let contents =
234
+ externalImports +
213
235
prefix +
214
236
'({' +
215
237
assets +
@@ -222,7 +244,9 @@ export class DevPackager {
222
244
mainEntry ? this . bundleGraph . getAssetPublicId ( mainEntry ) : null ,
223
245
) +
224
246
', ' +
225
- JSON . stringify ( this . parcelRequireName ) ;
247
+ JSON . stringify ( this . parcelRequireName ) +
248
+ ', ' +
249
+ externalMap ;
226
250
227
251
if ( usedHelpers & 1 ) {
228
252
// Generate a relative path from this bundle to the root of the dist dir.
@@ -258,6 +282,70 @@ export class DevPackager {
258
282
259
283
contents += ')\n' ;
260
284
285
+ // Add ES module exports from the entry asset.
286
+ if (
287
+ this . bundle . env . outputFormat === 'esmodule' &&
288
+ mainEntry &&
289
+ ( this . bundle . env . isLibrary || ! this . bundle . env . isBrowser ( ) )
290
+ ) {
291
+ let hasNamespace = mainEntry . symbols . hasExportSymbol ( '*' ) ;
292
+ let importedSymbols = new Map ( ) ;
293
+ let exportedSymbols = new Map ( ) ;
294
+ for ( let {
295
+ exportAs,
296
+ symbol,
297
+ exportSymbol,
298
+ } of this . bundleGraph . getExportedSymbols ( mainEntry ) ) {
299
+ if ( typeof symbol === 'string' ) {
300
+ if ( hasNamespace && exportAs !== '*' ) {
301
+ continue ;
302
+ }
303
+
304
+ if ( exportAs === '*' ) {
305
+ exportAs = 'default' ;
306
+ }
307
+
308
+ let id = makeValidIdentifier ( exportSymbol ) ;
309
+ if ( id === 'default' ) {
310
+ id = '_default' ;
311
+ }
312
+ importedSymbols . set ( exportSymbol , id ) ;
313
+ exportedSymbols . set ( exportAs , id ) ;
314
+ }
315
+ }
316
+
317
+ contents += 'let {' ;
318
+ for ( let [ key , value ] of importedSymbols ) {
319
+ contents += isValidIdentifier ( key ) ? key : JSON . stringify ( key ) ;
320
+ if ( value !== key ) {
321
+ contents += ': ' ;
322
+ contents += value ;
323
+ }
324
+ contents += ', ' ;
325
+ }
326
+
327
+ contents +=
328
+ '} = ' +
329
+ this . parcelRequireName +
330
+ '(' +
331
+ JSON . stringify ( this . bundleGraph . getAssetPublicId ( mainEntry ) ) +
332
+ ');\n' ;
333
+ contents += 'export {' ;
334
+
335
+ for ( let [ exportAs , ident ] of exportedSymbols ) {
336
+ contents += ident ;
337
+ if ( exportAs !== ident ) {
338
+ contents += ' as ' ;
339
+ contents += isValidIdentifier ( exportAs )
340
+ ? exportAs
341
+ : JSON . stringify ( exportAs ) ;
342
+ }
343
+ contents += ', ' ;
344
+ }
345
+
346
+ contents += '};\n' ;
347
+ }
348
+
261
349
// The entry asset of a script bundle gets hoisted outside the bundle wrapper function
262
350
// so that its variables become globals. We need to replace any require calls for
263
351
// runtimes with a parcelRequire call.
0 commit comments