Skip to content

Commit

Permalink
Add treeshake.correctVarValueBeforeDeclaration option
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 13, 2021
1 parent abb32d7 commit 046f3f6
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 34 deletions.
10 changes: 8 additions & 2 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -14,7 +14,7 @@ import LocalVariable from '../variables/LocalVariable';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import SpreadElement from './SpreadElement';
import { ExpressionEntity, LiteralValueOrUnknown } from './shared/Expression';
import { ExpressionEntity, LiteralValueOrUnknown, UNKNOWN_EXPRESSION } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';

Expand Down Expand Up @@ -45,9 +45,15 @@ export default class Identifier extends NodeBase implements PatternNode {

declare(kind: string, init: ExpressionEntity): LocalVariable[] {
let variable: LocalVariable;
const { treeshake } = this.context.options;
switch (kind) {
case 'var':
variable = this.scope.addDeclaration(this, this.context, init, true);
variable = this.scope.addDeclaration(
this,
this.context,
treeshake && treeshake.correctVarValueBeforeDeclaration ? UNKNOWN_EXPRESSION : init,
true
);
break;
case 'function':
// in strict mode, functions are only hoisted within a scope but not across block scopes
Expand Down
2 changes: 2 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -478,6 +478,7 @@ type TreeshakingPreset = 'smallest' | 'safest' | 'recommended';

export interface TreeshakingOptions {
annotations?: boolean;
correctVarValueBeforeDeclaration?: boolean;
moduleSideEffects?: ModuleSideEffectsOption;
preset?: TreeshakingPreset;
propertyReadSideEffects?: boolean | 'always';
Expand All @@ -489,6 +490,7 @@ export interface TreeshakingOptions {

export interface NormalizedTreeshakingOptions {
annotations: boolean;
correctVarValueBeforeDeclaration: boolean;
moduleSideEffects: HasModuleSideEffects;
propertyReadSideEffects: boolean | 'always';
tryCatchDeoptimization: boolean;
Expand Down
61 changes: 29 additions & 32 deletions src/utils/options/normalizeInputOptions.ts
Expand Up @@ -7,6 +7,7 @@ import {
PreserveEntrySignaturesOption,
PureModulesOption,
RollupBuild,
TreeshakingOptions,
WarningHandler
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
Expand Down Expand Up @@ -233,15 +234,21 @@ const getTreeshake = (
if (configTreeshake === false) {
return false;
}
if (!configTreeshake || configTreeshake === true) {
return {
annotations: true,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: true
};
if (typeof configTreeshake === 'string') {
const preset = treeshakePresets[configTreeshake];
if (preset) {
return preset;
}
error(
errInvalidOption(
'treeshake',
`valid values are false, true, ${printQuotedStringList(
Object.keys(treeshakePresets)
)}. You can also supply an object for more fine-grained control`
)
);
}
let configWithPreset: TreeshakingOptions = {};
if (typeof configTreeshake === 'object') {
if (typeof configTreeshake.pureExternalModules !== 'undefined') {
warnDeprecationWithOptions(
Expand All @@ -251,7 +258,7 @@ const getTreeshake = (
strictDeprecations
);
}
let configWithPreset = configTreeshake;
configWithPreset = configTreeshake;
const presetName = configTreeshake.preset;
if (presetName) {
const preset = treeshakePresets[presetName];
Expand All @@ -266,34 +273,24 @@ const getTreeshake = (
);
}
}
return {
annotations: configWithPreset.annotations !== false,
moduleSideEffects: configTreeshake.pureExternalModules
}
return {
annotations: configWithPreset.annotations !== false,
correctVarValueBeforeDeclaration: configWithPreset.correctVarValueBeforeDeclaration === true,
moduleSideEffects:
typeof configTreeshake === 'object' && configTreeshake.pureExternalModules
? getHasModuleSideEffects(
configTreeshake.moduleSideEffects,
configTreeshake.pureExternalModules
)
: getHasModuleSideEffects(configWithPreset.moduleSideEffects, undefined),
propertyReadSideEffects:
configWithPreset.propertyReadSideEffects === 'always'
? 'always'
: configWithPreset.propertyReadSideEffects !== false,
tryCatchDeoptimization: configWithPreset.tryCatchDeoptimization !== false,
unknownGlobalSideEffects: configWithPreset.unknownGlobalSideEffects !== false
};
}
const preset = treeshakePresets[configTreeshake];
if (preset) {
return preset;
}
error(
errInvalidOption(
'treeshake',
`valid values are false, true, ${printQuotedStringList(
Object.keys(treeshakePresets)
)}. You can also supply an object for more fine-grained control`
)
);
propertyReadSideEffects:
configWithPreset.propertyReadSideEffects === 'always'
? 'always'
: configWithPreset.propertyReadSideEffects !== false,
tryCatchDeoptimization: configWithPreset.tryCatchDeoptimization !== false,
unknownGlobalSideEffects: configWithPreset.unknownGlobalSideEffects !== false
};
};

const getHasModuleSideEffects = (
Expand Down
3 changes: 3 additions & 0 deletions src/utils/options/options.ts
Expand Up @@ -38,20 +38,23 @@ export const treeshakePresets: {
} = {
recommended: {
annotations: true,
correctVarValueBeforeDeclaration: false,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: false
},
safest: {
annotations: true,
correctVarValueBeforeDeclaration: true,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: true
},
smallest: {
annotations: true,
correctVarValueBeforeDeclaration: false,
moduleSideEffects: () => false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
Expand Down
3 changes: 3 additions & 0 deletions test/cli/samples/treeshake-preset-override/main.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
Expand Up @@ -11,6 +11,7 @@ module.exports = {
plugins: [
{
buildStart(options) {
assert.strictEqual(options.treeshake.correctVarValueBeforeDeclaration, false);
assert.strictEqual(options.treeshake.propertyReadSideEffects, false);
assert.strictEqual(options.treeshake.tryCatchDeoptimization, false);
assert.strictEqual(options.treeshake.unknownGlobalSideEffects, true);
Expand Down
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
1 change: 1 addition & 0 deletions test/form/samples/treeshake-presets/recommended/_config.js
Expand Up @@ -8,6 +8,7 @@ module.exports = {
plugins: [
{
buildStart(options) {
assert.strictEqual(options.treeshake.correctVarValueBeforeDeclaration, false);
assert.strictEqual(options.treeshake.propertyReadSideEffects, true);
assert.strictEqual(options.treeshake.tryCatchDeoptimization, true);
assert.strictEqual(options.treeshake.unknownGlobalSideEffects, false);
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-presets/recommended/main.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
1 change: 1 addition & 0 deletions test/form/samples/treeshake-presets/safest/_config.js
Expand Up @@ -8,6 +8,7 @@ module.exports = {
plugins: [
{
buildStart(options) {
assert.strictEqual(options.treeshake.correctVarValueBeforeDeclaration, true);
assert.strictEqual(options.treeshake.propertyReadSideEffects, true);
assert.strictEqual(options.treeshake.tryCatchDeoptimization, true);
assert.strictEqual(options.treeshake.unknownGlobalSideEffects, true);
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-presets/safest/_expected.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-presets/safest/main.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
1 change: 1 addition & 0 deletions test/form/samples/treeshake-presets/smallest/_config.js
Expand Up @@ -8,6 +8,7 @@ module.exports = {
plugins: [
{
buildStart(options) {
assert.strictEqual(options.treeshake.correctVarValueBeforeDeclaration, false);
assert.strictEqual(options.treeshake.propertyReadSideEffects, false);
assert.strictEqual(options.treeshake.tryCatchDeoptimization, false);
assert.strictEqual(options.treeshake.unknownGlobalSideEffects, false);
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-presets/smallest/main.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;
1 change: 1 addition & 0 deletions test/form/samples/treeshake-presets/true/_config.js
Expand Up @@ -8,6 +8,7 @@ module.exports = {
plugins: [
{
buildStart(options) {
assert.strictEqual(options.treeshake.correctVarValueBeforeDeclaration, false);
assert.strictEqual(options.treeshake.propertyReadSideEffects, true);
assert.strictEqual(options.treeshake.tryCatchDeoptimization, true);
assert.strictEqual(options.treeshake.unknownGlobalSideEffects, true);
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-presets/true/main.js
Expand Up @@ -13,3 +13,6 @@ try {
} catch {}

unknownGlobal;

if (!foo) console.log('effect');
var foo = true;

0 comments on commit 046f3f6

Please sign in to comment.