Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 16, 2021
1 parent 1f6d27b commit 97a6cd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 26 additions & 4 deletions docs/999-big-list-of-options.md
Expand Up @@ -1379,7 +1379,7 @@ Default: `false`
If this option is provided, bundling will not fail if bindings are imported from a file that does not define these bindings. Instead, new variables will be created for these bindings with the value `undefined`.

#### treeshake
Type: `boolean | "smallest" | "safest" | "recommended" | { annotations?: boolean, moduleSideEffects?: ModuleSideEffectsOption, preset?: "smallest" | "safest" | "recommended", propertyReadSideEffects?: boolean | 'always', tryCatchDeoptimization?: boolean, unknownGlobalSideEffects?: boolean }`<br>
Type: `boolean | "smallest" | "safest" | "recommended" | { annotations?: boolean, correctVarValueBeforeDeclaration?: boolean, moduleSideEffects?: ModuleSideEffectsOption, preset?: "smallest" | "safest" | "recommended", propertyReadSideEffects?: boolean | 'always', tryCatchDeoptimization?: boolean, unknownGlobalSideEffects?: boolean }`<br>
CLI: `--treeshake`/`--no-treeshake`<br>
Default: `true`

Expand All @@ -1389,9 +1389,10 @@ Whether to apply tree-shaking and to fine-tune the tree-shaking process. Setting
* getters with side effects will only be retained if the return value is used (`treeshake.propertyReadSideEffects: false`)
* code from imported modules will only be retained if at least one exported value is used (`treeshake.moduleSideEffects: false`)
* you should not bundle polyfills that rely on detecting broken builtins (`treeshake.tryCatchDeoptimization: false`)
* some semantic errors may be swallowed (`treeshake.unknownGlobalSideEffects: false`)
* `"recommended"` should work well for most usage patterns. Some semantic errors may be swallowed, though (`treeshake.unknownGlobalSideEffects: false`)
* `"safest"` tries to be as spec compliant as possible while still providing some basic tree-shaking capabilities. This is currently equivalent to `true` but this may change in the next major version.
* some semantic issues may be swallowed (`treeshake.unknownGlobalSideEffects: false`, `treeshake.correctVarValueBeforeDeclaration: false`)
* `"recommended"` should work well for most usage patterns. Some semantic issues may be swallowed, though (`treeshake.unknownGlobalSideEffects: false`, `treeshake.correctVarValueBeforeDeclaration: false`)
* `"safest"` tries to be as spec compliant as possible while still providing some basic tree-shaking capabilities.
* `true` is equivalent to not specifying the option and will always choose the default value (see below).

If you discover a bug caused by the tree-shaking algorithm, please file an issue!
Setting this option to an object implies tree-shaking is enabled and grants the following additional options:
Expand All @@ -1415,6 +1416,27 @@ class Impure {
/*@__PURE__*/new Impure();
```

**treeshake.correctVarValueBeforeDeclaration**<br>
Type: `boolean`<br>
CLI: `--treeshake.correctVarValueBeforeDeclaration`/`--no-treeshake.correctVarValueBeforeDeclaration`<br>
Default: `false`

If a variable is assigned a value in its declaration and is never reassigned, Rollup assumes the value to be constant. This is not true if the variable is declared with `var`, however, as those variables can be accessed before their declaration where they will evaluate to `undefined`.
Choosing `true` will make sure Rollup does not make (wrong) assumptions about the value of such variables. Note though that this can have a noticeable negative impact on tree-shaking results.

```js
// input
if (x) console.log('not executed');
var x = true;

// output with treeshake.correctVarValueBeforeDeclaration === false
console.log('not executed');

// output with treeshake.correctVarValueBeforeDeclaration === true
if (x) console.log('not executed');
var x = true;
```

**treeshake.moduleSideEffects**<br>
Type: `boolean | "no-external" | string[] | (id: string, external: boolean) => boolean`<br>
CLI: `--treeshake.moduleSideEffects`/`--no-treeshake.moduleSideEffects`/`--treeshake.moduleSideEffects no-external`<br>
Expand Down
9 changes: 5 additions & 4 deletions src/utils/options/normalizeInputOptions.ts
Expand Up @@ -28,9 +28,10 @@ export interface CommandConfigObject {
globals: { [id: string]: string } | undefined;
}

export function normalizeInputOptions(
config: InputOptions
): { options: NormalizedInputOptions; unsetOptions: Set<string> } {
export function normalizeInputOptions(config: InputOptions): {
options: NormalizedInputOptions;
unsetOptions: Set<string>;
} {
// These are options that may trigger special warnings or behaviour later
// if the user did not select an explicit value
const unsetOptions = new Set<string>();
Expand Down Expand Up @@ -103,7 +104,7 @@ const getAcornInjectPlugins = (
): NormalizedInputOptions['acornInjectPlugins'] => ensureArray(config.acornInjectPlugins);

const getCache = (config: InputOptions): NormalizedInputOptions['cache'] =>
((config.cache as unknown) as RollupBuild)?.cache || config.cache;
(config.cache as unknown as RollupBuild)?.cache || config.cache;

const getIdMatcher = <T extends Array<any>>(
option:
Expand Down

0 comments on commit 97a6cd2

Please sign in to comment.