Skip to content
Permalink
Browse files
Completely omit files that do not have any included statements (#2940)
  • Loading branch information
lukastaegert committed Jun 14, 2019
1 parent dd00ff5 commit b2160230822aa5afa947d4b37381bf677ab81554
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 79 deletions.
@@ -152,7 +152,7 @@ export default class Chunk {
exports: ChunkExports;
} = undefined as any;
private renderedHash: string = undefined as any;
private renderedModuleSources: MagicString[] = undefined as any;
private renderedModuleSources = new Map<Module, MagicString>();
private renderedSource: MagicStringBundle | null = null;
private renderedSourceLength: number = undefined as any;
private sortedExportNames: string[] | null = null;
@@ -522,36 +522,34 @@ export default class Chunk {
this.prepareDynamicImports();

let hoistedSource = '';

const renderedModules = (this.renderedModules = Object.create(null));
this.renderedModuleSources = [];

for (let i = 0; i < this.orderedModules.length; i++) {
const module = this.orderedModules[i];
const source = module.render(renderOptions);
source.trim();
if (options.compact && source.lastLine().indexOf('//') !== -1) source.append('\n');
this.renderedModuleSources.push(source);

for (const module of this.orderedModules) {
let renderedLength = 0;
if (module.isIncluded()) {
const source = module.render(renderOptions).trim();
if (options.compact && source.lastLine().indexOf('//') !== -1) source.append('\n');
const namespace = module.getOrCreateNamespace();
if (namespace.included || source.length() > 0) {
renderedLength = source.length();
this.renderedModuleSources.set(module, source);
magicString.addSource(source);
this.usedModules.push(module);

if (namespace.included && !this.graph.preserveModules) {
const rendered = namespace.renderBlock(renderOptions);
if (namespace.renderFirst()) hoistedSource += n + rendered;
else magicString.addSource(new MagicString(rendered));
}
}
}
const { renderedExports, removedExports } = module.getRenderedExports();
renderedModules[module.id] = {
originalLength: module.originalCode.length,
removedExports,
renderedExports,
renderedLength: source.length()
renderedLength
};

const namespace = module.getOrCreateNamespace();
if (namespace.included || !source.isEmpty()) {
magicString.addSource(source);
this.usedModules.push(module);

if (namespace.included && !this.graph.preserveModules) {
const rendered = namespace.renderBlock(renderOptions);
if (namespace.renderFirst()) hoistedSource += n + rendered;
else magicString.addSource(new MagicString(rendered));
}
}
}

if (hoistedSource) magicString.prepend(hoistedSource + n + n);
@@ -813,9 +811,7 @@ export default class Chunk {
}

private finaliseDynamicImports(format: string) {
for (let i = 0; i < this.orderedModules.length; i++) {
const module = this.orderedModules[i];
const code = this.renderedModuleSources[i];
for (const [module, code] of this.renderedModuleSources) {
for (const { node, resolution } of module.dynamicImports) {
if (!resolution) continue;
if (resolution instanceof Module) {
@@ -840,9 +836,7 @@ export default class Chunk {
}

private finaliseImportMetas(format: string): void {
for (let i = 0; i < this.orderedModules.length; i++) {
const module = this.orderedModules[i];
const code = this.renderedModuleSources[i];
for (const [module, code] of this.renderedModuleSources) {
for (const importMeta of module.importMetas) {
importMeta.renderFinalMechanism(code, this.id, format, this.graph.pluginDriver);
}
@@ -0,0 +1,3 @@
module.exports = {
description: 'Also remove banner comments when completely tree-shaking files'
};
@@ -0,0 +1,4 @@
// dep included banner: included
console.log('dep included');

// dep included footer: included
@@ -0,0 +1,6 @@
// dep included banner: included

const removed = 1;
console.log('dep included');

// dep included footer: included
@@ -0,0 +1,5 @@
// dep removed banner: removed

const removed = 1;

// dep removed footer: removed
@@ -0,0 +1,6 @@
// main banner: removed

import './dep-removed';
import './dep-included';

// main footer: removed
@@ -1,13 +1,5 @@
define(function () { 'use strict';

/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -1,13 +1,5 @@
'use strict';

/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -1,11 +1,3 @@
/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -1,14 +1,6 @@
(function () {
'use strict';

/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -3,14 +3,6 @@ System.register([], function () {
return {
execute: function () {

/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -3,14 +3,6 @@
factory();
}(function () { 'use strict';

/* header 1 */

/* footer 1 */

/* header 2 */

/* footer 2 */

/* header 3 */

/* leading retained */
@@ -423,7 +423,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
input: ['input'],
plugins: [
loader({
input: 'export { renderedFn, renderedClass, renderedConst } from "code"',
input: '/* removed header */ export { renderedFn, renderedClass, renderedConst } from "code"',
code:
'export function renderedFn() {}\nexport function removedFn() {}\n' +
'export class renderedClass {}\nexport class removedClass {}\n' +
@@ -457,7 +457,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
renderedLength: 72
},
input: {
originalLength: 63,
originalLength: 84,
removedExports: [],
renderedExports: [],
renderedLength: 0

0 comments on commit b216023

Please sign in to comment.