Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 21, 2020
1 parent a3b89e5 commit 786909a
Show file tree
Hide file tree
Showing 25 changed files with 170 additions and 81 deletions.
21 changes: 9 additions & 12 deletions src/Chunk.ts
Expand Up @@ -895,8 +895,7 @@ export default class Chunk {
} else {
const variable = this.exportsByName![exportName];
if (variable instanceof SyntheticNamedExportVariable) continue;
const module = variable.module;
if (!module) continue;
const module = variable.module!;
if (module instanceof Module) {
exportChunk = this.chunkByModule.get(module)!;
if (exportChunk === this) continue;
Expand Down Expand Up @@ -1146,16 +1145,14 @@ export default class Chunk {
if (variable instanceof SyntheticNamedExportVariable) {
variable = variable.getBaseVariable();
}
if (variable.module) {
const chunk = this.chunkByModule.get(variable.module as Module);
if (chunk !== this) {
this.imports.add(variable);
if (
!(variable instanceof NamespaceVariable && this.outputOptions.preserveModules) &&
variable.module instanceof Module
) {
chunk!.exports.add(variable);
}
const chunk = this.chunkByModule.get(variable.module as Module);
if (chunk !== this) {
this.imports.add(variable);
if (
!(variable instanceof NamespaceVariable && this.outputOptions.preserveModules) &&
variable.module instanceof Module
) {
chunk!.exports.add(variable);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Module.ts
Expand Up @@ -846,9 +846,11 @@ export default class Module {
private addLocationToLogProps(props: RollupLogProps, pos: number): void {
props.id = this.id;
props.pos = pos;
let { column, line } = locate(this.code, pos, { offsetLine: 1 });
let code = this.code;
let { column, line } = locate(code, pos, { offsetLine: 1 });
try {
({ column, line } = getOriginalLocation(this.sourcemapChain, { column, line }));
code = this.originalCode;
} catch (e) {
this.options.onwarn({
code: 'SOURCEMAP_ERROR',
Expand All @@ -862,7 +864,7 @@ export default class Module {
pos
});
}
augmentCodeLocation(props, { column, line }, this.originalCode, this.id);
augmentCodeLocation(props, { column, line }, code, this.id);
}

private addModulesToImportDescriptions(importDescription: {
Expand Down
28 changes: 9 additions & 19 deletions src/utils/chunkAssignment.ts
Expand Up @@ -21,10 +21,7 @@ export function getChunkAssignments(
}

const assignedEntryPointsByModule: DependentModuleMap = new Map();
const { dependentEntryPointsByModule, dynamicEntryModules } = analyzeModuleGraph(
entryModules,
modulesInManualChunks
);
const { dependentEntryPointsByModule, dynamicEntryModules } = analyzeModuleGraph(entryModules);
const dynamicallyDependentEntryPointsByDynamicEntry: DependentModuleMap = getDynamicDependentEntryPoints(
dependentEntryPointsByModule,
dynamicEntryModules
Expand Down Expand Up @@ -59,16 +56,16 @@ export function getChunkAssignments(

function areEntryPointsContainedOrDynamicallyDependent(
entryPoints: Set<Module>,
superSet: Set<Module>
containedIn: Set<Module>
): boolean {
const entriesToCheck = new Set(entryPoints);
for (const entry of entriesToCheck) {
if (!superSet.has(entry)) {
if (!containedIn.has(entry)) {
if (staticEntries.has(entry)) return false;
const dynamicDependentEntryPoints = dynamicallyDependentEntryPointsByDynamicEntry.get(
const dynamicallyDependentEntryPoints = dynamicallyDependentEntryPointsByDynamicEntry.get(
entry
)!;
for (const dependentEntry of dynamicDependentEntryPoints) {
for (const dependentEntry of dynamicallyDependentEntryPoints) {
entriesToCheck.add(dependentEntry);
}
}
Expand Down Expand Up @@ -115,8 +112,7 @@ function addStaticDependenciesToManualChunk(
}

function analyzeModuleGraph(
entryModules: Module[],
modulesInManualChunks: Set<Module>
entryModules: Module[]
): {
dependentEntryPointsByModule: DependentModuleMap;
dynamicEntryModules: Set<Module>;
Expand All @@ -134,20 +130,14 @@ function analyzeModuleGraph(
}
}
for (const { resolution } of module.dynamicImports) {
if (
resolution instanceof Module &&
resolution.includedDynamicImporters.length > 0 &&
!modulesInManualChunks.has(resolution)
) {
if (resolution instanceof Module && resolution.includedDynamicImporters.length > 0) {
dynamicEntryModules.add(resolution);
entriesToHandle.add(resolution);
}
}
for (const dependency of module.implicitlyLoadedBefore) {
if (!modulesInManualChunks.has(dependency)) {
dynamicEntryModules.add(dependency);
entriesToHandle.add(dependency);
}
dynamicEntryModules.add(dependency);
entriesToHandle.add(dependency);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion test/form/samples/custom-module-context-function/_config.js
Expand Up @@ -3,7 +3,12 @@ module.exports = {
expectedWarnings: ['THIS_IS_UNDEFINED'],
options: {
moduleContext(id) {
return /main\.js$/.test(id) ? 'lolwut' : 'undefined';
if (id.endsWith('main.js')) {
return 'window';
}
if (id.endsWith('foo.js')) {
return 'global';
}
}
}
};
5 changes: 5 additions & 0 deletions test/form/samples/custom-module-context-function/_expected.js
@@ -0,0 +1,5 @@
global.prop = 'foo';

undefined.prop = 'bar';

window.prop = 'main';

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions test/form/samples/custom-module-context-function/_expected/umd.js

This file was deleted.

1 change: 1 addition & 0 deletions test/form/samples/custom-module-context-function/bar.js
@@ -0,0 +1 @@
this.prop = 'bar';
2 changes: 1 addition & 1 deletion test/form/samples/custom-module-context-function/foo.js
@@ -1 +1 @@
this.prop = 'nope';
this.prop = 'foo';
3 changes: 2 additions & 1 deletion test/form/samples/custom-module-context-function/main.js
@@ -1,2 +1,3 @@
import './foo.js';
this.prop = '???';
import './bar.js';
this.prop = 'main';
28 changes: 28 additions & 0 deletions test/form/samples/side-effects-internal-modules/_config.js
@@ -0,0 +1,28 @@
const assert = require('assert');
const VIRTUAL_ID = '\0virtual';

module.exports = {
description: 'does not pass internal modules to moduleSideEffects',
expectedWarnings: ['EMPTY_BUNDLE'],
options: {
treeshake: {
moduleSideEffects: id => {
assert.notStrictEqual(id, VIRTUAL_ID);
return false;
}
},
plugins: {
name: 'test-plugin',
resolveId(id) {
if (id === 'virtual') {
return VIRTUAL_ID;
}
},
load(id) {
if (id === VIRTUAL_ID) {
return "console.log('effect')";
}
}
}
}
};
3 changes: 3 additions & 0 deletions test/form/samples/side-effects-internal-modules/_expected.js
@@ -0,0 +1,3 @@
console.log('effect');

console.log('main');
2 changes: 2 additions & 0 deletions test/form/samples/side-effects-internal-modules/main.js
@@ -0,0 +1,2 @@
import 'virtual';
console.log('main');
45 changes: 45 additions & 0 deletions test/function/samples/cannot-resolve-sourcemap-warning/_config.js
@@ -0,0 +1,45 @@
const path = require('path');
const ID_MAIN = path.join(__dirname, 'main.js');

module.exports = {
description: 'handles when a sourcemap cannot be resolved in a warning',
options: {
plugins: {
name: 'test-plugin',
transform() {
return { code: 'export default this', map: { mappings: 'X' } };
}
}
},
warnings: [
{
code: 'SOURCEMAP_ERROR',
id: ID_MAIN,
loc: {
column: 15,
file: ID_MAIN,
line: 1
},
message:
"Error when using sourcemap for reporting an error: Can't resolve original location of error.",
pos: 15
},
{
code: 'THIS_IS_UNDEFINED',
frame: `
1: export default this
^
`,
id: ID_MAIN,
loc: {
column: 15,
file: ID_MAIN,
line: 1
},
message:
"The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten",
pos: 15,
url: 'https://rollupjs.org/guide/en/#error-this-is-undefined'
}
]
};
@@ -0,0 +1 @@
throw new Error('Not executed');
@@ -0,0 +1,25 @@
let chunkId;

module.exports = {
description: 'Throws when accessing the filename before it has been generated in buildEnd',
options: {
input: 'main.js',
plugins: {
name: 'test-plugin',
buildStart() {
chunkId = this.emitFile({ type: 'chunk', id: 'chunk.js' });
},
buildEnd() {
this.getFileName(chunkId);
}
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildEnd',
message:
'Plugin error - Unable to get file name for chunk "chunk.js". Ensure that generate is called first.',
plugin: 'test-plugin',
pluginCode: 'CHUNK_NOT_GENERATED'
}
};
@@ -0,0 +1 @@
console.log('chunk');
@@ -0,0 +1 @@
console.log('main');
@@ -0,0 +1,25 @@
let chunkId;

module.exports = {
description: 'Throws when accessing the filename before it has been generated in renderStart',
options: {
input: 'main.js',
plugins: {
name: 'test-plugin',
buildStart() {
chunkId = this.emitFile({ type: 'chunk', id: 'chunk.js' });
},
renderStart() {
this.getFileName(chunkId);
}
}
},
generateError: {
code: 'PLUGIN_ERROR',
hook: 'renderStart',
message:
'Plugin error - Unable to get file name for chunk "chunk.js". Ensure that generate is called first.',
plugin: 'test-plugin',
pluginCode: 'CHUNK_NOT_GENERATED'
}
};
@@ -0,0 +1 @@
console.log('chunk');
@@ -0,0 +1 @@
console.log('main');

0 comments on commit 786909a

Please sign in to comment.