Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 26, 2020
1 parent 90c5613 commit 7948eb6
Show file tree
Hide file tree
Showing 31 changed files with 149 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
GetManualChunk,
HasModuleSideEffects,
NormalizedInputOptions,
NormalizedTreeshakingOptions,
ResolvedId,
ResolveIdResult,
SourceDescription
Expand Down Expand Up @@ -51,8 +50,9 @@ export class ModuleLoader {
private readonly options: NormalizedInputOptions,
private readonly pluginDriver: PluginDriver
) {
this.hasModuleSideEffects =
(options.treeshake as NormalizedTreeshakingOptions)?.moduleSideEffects || (() => true);
this.hasModuleSideEffects = options.treeshake
? options.treeshake.moduleSideEffects
: () => true;
}

async addEntryModules(
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/CallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
if (argument.hasEffects(context)) return true;
}
if (
(this.context.options.treeshake as NormalizedTreeshakingOptions)?.annotations &&
(this.context.options.treeshake as NormalizedTreeshakingOptions).annotations &&
this.annotatedPure
)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class Identifier extends NodeBase implements PatternNode {

hasEffects(): boolean {
return (
(this.context.options.treeshake as NormalizedTreeshakingOptions)?.unknownGlobalSideEffects &&
(this.context.options.treeshake as NormalizedTreeshakingOptions).unknownGlobalSideEffects &&
this.variable instanceof GlobalVariable &&
this.variable.hasEffectsWhenAccessedAtPath(EMPTY_PATH)
);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/MemberExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
return (
this.property.hasEffects(context) ||
this.object.hasEffects(context) ||
((this.context.options.treeshake as NormalizedTreeshakingOptions)?.propertyReadSideEffects &&
((this.context.options.treeshake as NormalizedTreeshakingOptions).propertyReadSideEffects &&
this.object.hasEffectsWhenAccessedAtPath([this.propertyKey!], context))
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/NewExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class NewExpression extends NodeBase {
if (argument.hasEffects(context)) return true;
}
if (
(this.context.options.treeshake as NormalizedTreeshakingOptions)?.annotations &&
(this.context.options.treeshake as NormalizedTreeshakingOptions).annotations &&
this.annotatedPure
)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/TryStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class TryStatement extends StatementBase {

hasEffects(context: HasEffectsContext): boolean {
return (
((this.context.options.treeshake as NormalizedTreeshakingOptions)?.tryCatchDeoptimization
((this.context.options.treeshake as NormalizedTreeshakingOptions).tryCatchDeoptimization
? this.block.body.length > 0
: this.block.hasEffects(context)) ||
(this.finalizer !== null && this.finalizer.hasEffects(context))
Expand Down
6 changes: 2 additions & 4 deletions src/finalisers/umd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ export default function umd(
factoryArgs.unshift('exports');
}

const amdOptions = options.amd || {};

const amdParams =
(amdOptions.id ? `'${amdOptions.id}',${_}` : ``) +
(options.amd.id ? `'${options.amd.id}',${_}` : ``) +
(amdDeps.length ? `[${amdDeps.join(`,${_}`)}],${_}` : ``);

const define = amdOptions.define;
const define = options.amd.define;
const cjsExport = !namedExportsMode && hasExports ? `module.exports${_}=${_}` : ``;
const useStrict = options.strict ? `${_}'use strict';${n}` : ``;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/options/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type CompleteInputOptions<U extends keyof InputOptions> = {

function mergeInputOptions(
config: GenericConfigObject,
overrides: CommandConfigObject = { external: [], globals: undefined },
overrides: CommandConfigObject,
defaultOnWarnHandler: WarningHandler
): InputOptions {
const getOption = (name: string): any => overrides[name] ?? config[name];
Expand Down Expand Up @@ -179,7 +179,7 @@ type CompleteOutputOptions<U extends keyof OutputOptions> = {

function mergeOutputOptions(
config: GenericConfigObject,
overrides: GenericConfigObject = {},
overrides: GenericConfigObject,
warn: WarningHandler
): OutputOptions {
const getOption = (name: string): any => overrides[name] ?? config[name];
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/amd-id/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
description: 'allows to declare an AMD id',
options: { output: { amd: { id: 'my-id' } } }
};
5 changes: 5 additions & 0 deletions test/form/samples/amd-id/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define('my-id', function () { 'use strict';

console.log(42);

});
3 changes: 3 additions & 0 deletions test/form/samples/amd-id/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log(42);
1 change: 1 addition & 0 deletions test/form/samples/amd-id/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(42);
6 changes: 6 additions & 0 deletions test/form/samples/amd-id/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function () {
'use strict';

console.log(42);

}());
10 changes: 10 additions & 0 deletions test/form/samples/amd-id/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
System.register([], function () {
'use strict';
return {
execute: function () {

console.log(42);

}
};
});
8 changes: 8 additions & 0 deletions test/form/samples/amd-id/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define('my-id', factory) :
factory();
}((function () { 'use strict';

console.log(42);

})));
1 change: 1 addition & 0 deletions test/form/samples/amd-id/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(42);
10 changes: 10 additions & 0 deletions test/form/samples/globals-function/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
description: 'Externals aliases with deshadowing',
options: {
external: ['a', 'b'],
output: {
globals: id => `thisIs${id.toUpperCase()}`,
name: 'myBundle'
}
}
};
8 changes: 8 additions & 0 deletions test/form/samples/globals-function/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define(['a', 'b'], function (a, b) { 'use strict';

a = a && Object.prototype.hasOwnProperty.call(a, 'default') ? a['default'] : a;
b = b && Object.prototype.hasOwnProperty.call(b, 'default') ? b['default'] : b;

console.log(a, b);

});
8 changes: 8 additions & 0 deletions test/form/samples/globals-function/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var a = _interopDefault(require('a'));
var b = _interopDefault(require('b'));

console.log(a, b);
4 changes: 4 additions & 0 deletions test/form/samples/globals-function/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import a from 'a';
import b from 'b';

console.log(a, b);
9 changes: 9 additions & 0 deletions test/form/samples/globals-function/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function (a, b) {
'use strict';

a = a && Object.prototype.hasOwnProperty.call(a, 'default') ? a['default'] : a;
b = b && Object.prototype.hasOwnProperty.call(b, 'default') ? b['default'] : b;

console.log(a, b);

}(thisIsA, thisIsB));
16 changes: 16 additions & 0 deletions test/form/samples/globals-function/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
System.register('myBundle', ['a', 'b'], function () {
'use strict';
var a, b;
return {
setters: [function (module) {
a = module.default;
}, function (module) {
b = module.default;
}],
execute: function () {

console.log(a, b);

}
};
});
12 changes: 12 additions & 0 deletions test/form/samples/globals-function/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('a'), require('b')) :
typeof define === 'function' && define.amd ? define(['a', 'b'], factory) :
(global = global || self, factory(global.thisIsA, global.thisIsB));
}(this, (function (a, b) { 'use strict';

a = a && Object.prototype.hasOwnProperty.call(a, 'default') ? a['default'] : a;
b = b && Object.prototype.hasOwnProperty.call(b, 'default') ? b['default'] : b;

console.log(a, b);

})));
4 changes: 4 additions & 0 deletions test/form/samples/globals-function/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import a from 'a';
import b from 'b';

console.log(a, b);
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ define(['exports', 'external'], function (exports, external) { 'use strict';
}
});

try {
const x = 1;
} catch {}

exports.create = create;
exports.getPrototypeOf = getPrototypeOf;
exports.quux = quux;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ test({
}
});

try {
const x = 1;
} catch {}

exports.create = create;
exports.getPrototypeOf = getPrototypeOf;
exports.quux = quux;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ test({
}
});

try {
const x = 1;
} catch {}

export { create, getPrototypeOf, quux, quux as strange };
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var stirred = (function (exports, external) {
}
});

try {
const x = 1;
} catch {}

exports.create = create;
exports.getPrototypeOf = getPrototypeOf;
exports.quux = quux;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ System.register('stirred', ['external'], function (exports) {
}
});

try {
const x = 1;
} catch {}

}
};
});
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
}
});

try {
const x = 1;
} catch {}

exports.create = create;
exports.getPrototypeOf = getPrototypeOf;
exports.quux = quux;
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/no-treeshake/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ test({
var unused = 1;
}
});

try {
const x = 1;
} catch {}

0 comments on commit 7948eb6

Please sign in to comment.