Skip to content
Permalink
Browse files
early exportMode detection, remove default indirection
  • Loading branch information
guybedford authored and lukastaegert committed May 15, 2018
1 parent c084d4a commit 32913d0cc21352860d1e5b7a612d60d711ef8cf7
Showing 12 changed files with 27 additions and 38 deletions.
@@ -2,7 +2,6 @@ import { timeEnd, timeStart } from './utils/timers';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import Module from './Module';
import finalisers from './finalisers/index';
import getExportMode from './utils/getExportMode';
import getIndentString from './utils/getIndentString';
import transformBundle from './utils/transformBundle';
import collapseSourcemaps from './utils/collapseSourcemaps';
@@ -84,7 +83,7 @@ function getGlobalName(
export default class Chunk {
hasDynamicImport: boolean = false;
indentString: string = undefined;
namedExportsMode: boolean = true;
exportMode: string = 'named';
usedModules: Module[] = undefined;
id: string = undefined;
name: string;
@@ -505,7 +504,9 @@ export default class Chunk {
safeName = getSafeName(variable.name);
toDeshadow.add(safeName);
} else {
safeName = `${(<Module>module).chunk.name}.${module.chunk.getVariableExportName(variable)}`;
const chunk = (<Module>module).chunk;
if (chunk.exportMode === 'default') safeName = chunk.name;
else safeName = `${chunk.name}.${module.chunk.getVariableExportName(variable)}`;
}
if (safeName) variable.setSafeName(safeName);
});
@@ -800,12 +801,9 @@ export default class Chunk {
});
}

const exportMode = this.isEntryModuleFacade && getExportMode(this, options);
this.namedExportsMode = exportMode !== 'default';

this.renderedDeclarations = {
dependencies: this.getChunkDependencyDeclarations(options, inputBase),
exports: exportMode === 'none' ? [] : this.getChunkExportDeclarations()
exports: this.exportMode === 'none' ? [] : this.getChunkExportDeclarations()
};

timeEnd('render modules', 3);
@@ -992,7 +990,7 @@ export default class Chunk {
let relPath = this.id ? normalize(relative(dirname(this.id), depId)) : depId;
if (!relPath.startsWith('../')) relPath = './' + relPath;

if (dep instanceof Chunk) renderedDependency.namedExportsMode = dep.namedExportsMode;
if (dep instanceof Chunk) renderedDependency.namedExportsMode = dep.exportMode !== 'default';
renderedDependency.id = relPath;
}

@@ -1009,7 +1007,7 @@ export default class Chunk {
this.renderedSource,
{
indentString: this.indentString,
namedExportsMode: this.namedExportsMode,
namedExportsMode: this.exportMode !== 'default',
hasExports,
intro: addons.intro,
outro: addons.outro,
@@ -45,11 +45,7 @@ export default function cjs(
return `require('${id}');`;
}

if (!namedExportsMode) {
return `${varOrConst} ${name} = { default: require('${id}') };`;
}

if (!interop || isChunk || !exportsDefault) {
if (!interop || isChunk || !exportsDefault || !namedExportsMode) {
return `${varOrConst} ${name} = require('${id}');`;
}

@@ -22,7 +22,7 @@ export default function getExportBlock(
if (!dep.reexports) return false;
return dep.reexports.some(expt => {
if (expt.reexported === 'default') {
local = `${dep.name}.${expt.imported}`;
local = dep.namedExportsMode ? `${dep.name}.${expt.imported}` : dep.name;
return true;
}
return false;
@@ -8,7 +8,7 @@ export default function getInteropBlock(
) {
return dependencies
.map(({ name, exportsNames, exportsDefault, namedExportsMode }) => {
if (!namedExportsMode) return `${name} = { default: ${name} };`;
if (!namedExportsMode) return;

if (!exportsDefault || options.interop === false) return null;

@@ -21,6 +21,7 @@ import {
Plugin,
ModuleJSON
} from './types';
import getExportMode from '../utils/getExportMode';

export const VERSION = '<@VERSION@>';

@@ -169,6 +170,7 @@ export default function rollup(
.then(addons => {
chunk.generateInternalExports(outputOptions);
const inputBase = dirname(resolve(inputOptions.input));
chunk.exportMode = getExportMode(chunk, outputOptions);
chunk.preRender(outputOptions, inputBase);
chunk.id = basename(inputOptions.input);
return chunk.render(outputOptions, addons);
@@ -336,10 +338,11 @@ export default function rollup(
return (
Promise.resolve()
.then(() => {
if (!inputOptions.experimentalPreserveModules) {
for (let chunk of chunks) {
for (let chunk of chunks) {
if (!inputOptions.experimentalPreserveModules)
chunk.generateInternalExports(outputOptions);
}
if (chunk.isEntryModuleFacade)
chunk.exportMode = getExportMode(chunk, outputOptions);
}
for (let chunk of chunks) {
chunk.preRender(outputOptions, inputBase);
@@ -1,7 +1,5 @@
define(['./main2.js'], function (main2) { 'use strict';

main2 = { default: main2 };

main2.default();
main2();

});
@@ -1,5 +1,5 @@
'use strict';

var main2 = { default: require('./main2.js') };
var main2 = require('./main2.js');

main2.default();
main2();
@@ -1,9 +1,7 @@
define(['./first.js'], function (first) { 'use strict';

first = { default: first };



return first.default;
return first;

});
@@ -1,8 +1,6 @@
define(['./first.js'], function (first) { 'use strict';

first = { default: first };

console.log(first.default);
console.log(first.default);
console.log(first);
console.log(first);

});
@@ -1,7 +1,5 @@
define(['./first.js'], function (first) { 'use strict';

first = { default: first };



});
@@ -1,7 +1,7 @@
'use strict';

var first = { default: require('./first.js') };
var first = require('./first.js');



module.exports = first.default;
module.exports = first;
@@ -1,6 +1,6 @@
'use strict';

var first = { default: require('./first.js') };
var first = require('./first.js');

console.log(first.default);
console.log(first.default);
console.log(first);
console.log(first);

0 comments on commit 32913d0

Please sign in to comment.