rollup 3.20.6 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin/rollup CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  /*
4
4
  @license
5
- Rollup.js v3.20.6
6
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
5
+ Rollup.js v3.21.0
6
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
7
7
 
8
8
  https://github.com/rollup/rollup
9
9
 
package/dist/es/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -16,7 +16,7 @@ import { lstat, realpath, readdir, readFile, mkdir, writeFile } from 'node:fs/pr
16
16
  import { EventEmitter } from 'node:events';
17
17
  import * as tty from 'tty';
18
18
 
19
- var version$1 = "3.20.6";
19
+ var version$1 = "3.21.0";
20
20
 
21
21
  const comma = ','.charCodeAt(0);
22
22
  const semicolon = ';'.charCodeAt(0);
@@ -10342,9 +10342,9 @@ class FunctionDeclaration extends FunctionNode {
10342
10342
  function getDeclarationStart(code, start) {
10343
10343
  return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
10344
10344
  }
10345
- function getIdInsertPosition(code, declarationKeyword, endMarker, start) {
10346
- const declarationEnd = findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
10347
- code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd));
10345
+ function getFunctionIdInsertPosition(code, start) {
10346
+ const declarationEnd = findFirstOccurrenceOutsideComment(code, 'function', start) + 'function'.length;
10347
+ code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '(', declarationEnd));
10348
10348
  const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
10349
10349
  if (generatorStarPos === -1) {
10350
10350
  return declarationEnd;
@@ -10369,10 +10369,14 @@ class ExportDefaultDeclaration extends NodeBase {
10369
10369
  const { start, end } = nodeRenderOptions;
10370
10370
  const declarationStart = getDeclarationStart(code.original, this.start);
10371
10371
  if (this.declaration instanceof FunctionDeclaration) {
10372
- this.renderNamedDeclaration(code, declarationStart, 'function', '(', this.declaration.id === null, options);
10372
+ this.renderNamedDeclaration(code, declarationStart, this.declaration.id === null
10373
+ ? getFunctionIdInsertPosition(code.original, declarationStart)
10374
+ : null, options);
10373
10375
  }
10374
10376
  else if (this.declaration instanceof ClassDeclaration) {
10375
- this.renderNamedDeclaration(code, declarationStart, 'class', '{', this.declaration.id === null, options);
10377
+ this.renderNamedDeclaration(code, declarationStart, this.declaration.id === null
10378
+ ? findFirstOccurrenceOutsideComment(code.original, 'class', start) + 'class'.length
10379
+ : null, options);
10376
10380
  }
10377
10381
  else if (this.variable.getOriginalVariable() !== this.variable) {
10378
10382
  // Remove altogether to prevent re-declaring the same variable
@@ -10395,13 +10399,13 @@ class ExportDefaultDeclaration extends NodeBase {
10395
10399
  this.declaration.render(code, options);
10396
10400
  }
10397
10401
  applyDeoptimizations() { }
10398
- renderNamedDeclaration(code, declarationStart, declarationKeyword, endMarker, needsId, options) {
10402
+ renderNamedDeclaration(code, declarationStart, idInsertPosition, options) {
10399
10403
  const { exportNamesByVariable, format, snippets: { getPropertyAccess } } = options;
10400
10404
  const name = this.variable.getName(getPropertyAccess);
10401
10405
  // Remove `export default`
10402
10406
  code.remove(this.start, declarationStart);
10403
- if (needsId) {
10404
- code.appendLeft(getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart), ` ${name}`);
10407
+ if (idInsertPosition !== null) {
10408
+ code.appendLeft(idInsertPosition, ` ${name}`);
10405
10409
  }
10406
10410
  if (format === 'system' &&
10407
10411
  this.declaration instanceof ClassDeclaration &&
@@ -10529,6 +10533,7 @@ class ForOfStatement extends NodeBase {
10529
10533
  applyDeoptimizations() {
10530
10534
  this.deoptimized = true;
10531
10535
  this.left.deoptimizePath(EMPTY_PATH);
10536
+ this.right.deoptimizePath(UNKNOWN_PATH);
10532
10537
  this.context.requestTreeshakingPass();
10533
10538
  }
10534
10539
  }
@@ -10980,6 +10985,74 @@ function getToStringTagValue(getObject) {
10980
10985
  });
10981
10986
  }
10982
10987
 
10988
+ function isReassignedExportsMember(variable, exportNamesByVariable) {
10989
+ return (variable.renderBaseName !== null && exportNamesByVariable.has(variable) && variable.isReassigned);
10990
+ }
10991
+
10992
+ class VariableDeclarator extends NodeBase {
10993
+ declareDeclarator(kind) {
10994
+ this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
10995
+ }
10996
+ deoptimizePath(path) {
10997
+ this.id.deoptimizePath(path);
10998
+ }
10999
+ hasEffects(context) {
11000
+ if (!this.deoptimized)
11001
+ this.applyDeoptimizations();
11002
+ const initEffect = this.init?.hasEffects(context);
11003
+ this.id.markDeclarationReached();
11004
+ return initEffect || this.id.hasEffects(context);
11005
+ }
11006
+ include(context, includeChildrenRecursively) {
11007
+ const { deoptimized, id, init } = this;
11008
+ if (!deoptimized)
11009
+ this.applyDeoptimizations();
11010
+ this.included = true;
11011
+ init?.include(context, includeChildrenRecursively);
11012
+ id.markDeclarationReached();
11013
+ if (includeChildrenRecursively || id.shouldBeIncluded(context)) {
11014
+ id.include(context, includeChildrenRecursively);
11015
+ }
11016
+ }
11017
+ render(code, options) {
11018
+ const { exportNamesByVariable, snippets: { _, getPropertyAccess } } = options;
11019
+ const { end, id, init, start } = this;
11020
+ const renderId = id.included;
11021
+ if (renderId) {
11022
+ id.render(code, options);
11023
+ }
11024
+ else {
11025
+ const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', id.end);
11026
+ code.remove(start, findNonWhiteSpace(code.original, operatorPos + 1));
11027
+ }
11028
+ if (init) {
11029
+ if (id instanceof Identifier && init instanceof ClassExpression && !init.id) {
11030
+ const renderedVariable = id.variable.getName(getPropertyAccess);
11031
+ if (renderedVariable !== id.name) {
11032
+ code.appendLeft(init.start + 5, ` ${id.name}`);
11033
+ }
11034
+ }
11035
+ init.render(code, options, renderId ? BLANK : { renderedSurroundingElement: ExpressionStatement$1 });
11036
+ }
11037
+ else if (id instanceof Identifier &&
11038
+ isReassignedExportsMember(id.variable, exportNamesByVariable)) {
11039
+ code.appendLeft(end, `${_}=${_}void 0`);
11040
+ }
11041
+ }
11042
+ applyDeoptimizations() {
11043
+ this.deoptimized = true;
11044
+ const { id, init } = this;
11045
+ if (init && id instanceof Identifier && init instanceof ClassExpression && !init.id) {
11046
+ const { name, variable } = id;
11047
+ for (const accessedVariable of init.scope.accessedOutsideVariables.values()) {
11048
+ if (accessedVariable !== variable) {
11049
+ accessedVariable.forbidName(name);
11050
+ }
11051
+ }
11052
+ }
11053
+ }
11054
+ }
11055
+
10983
11056
  // TODO once ImportExpression follows official ESTree specs with "null" as
10984
11057
  // default, keys.ts should be updated
10985
11058
  class ImportExpression extends NodeBase {
@@ -10996,6 +11069,79 @@ class ImportExpression extends NodeBase {
10996
11069
  bind() {
10997
11070
  this.source.bind();
10998
11071
  }
11072
+ /**
11073
+ * Get imported variables for deterministic usage, valid cases are:
11074
+ *
11075
+ * - `const { foo } = await import('bar')`.
11076
+ * - `(await import('bar')).foo`
11077
+ * - `import('bar').then(({ foo }) => {})`
11078
+ *
11079
+ * Returns empty array if it's side-effect only import.
11080
+ * Returns undefined if it's not fully deterministic.
11081
+ */
11082
+ getDeterministicImportedNames() {
11083
+ const parent1 = this.parent;
11084
+ // Side-effect only: import('bar')
11085
+ if (parent1 instanceof ExpressionStatement) {
11086
+ return EMPTY_ARRAY;
11087
+ }
11088
+ if (parent1 instanceof AwaitExpression) {
11089
+ const parent2 = parent1.parent;
11090
+ // Side-effect only: await import('bar')
11091
+ if (parent2 instanceof ExpressionStatement) {
11092
+ return EMPTY_ARRAY;
11093
+ }
11094
+ // Case 1: const { foo } = await import('bar')
11095
+ if (parent2 instanceof VariableDeclarator) {
11096
+ const declaration = parent2.id;
11097
+ return declaration instanceof ObjectPattern
11098
+ ? getDeterministicObjectDestructure(declaration)
11099
+ : undefined;
11100
+ }
11101
+ // Case 2: (await import('bar')).foo
11102
+ if (parent2 instanceof MemberExpression) {
11103
+ const id = parent2.property;
11104
+ if (!parent2.computed && id instanceof Identifier) {
11105
+ return [id.name];
11106
+ }
11107
+ }
11108
+ return;
11109
+ }
11110
+ // Case 3: import('bar').then(({ foo }) => {})
11111
+ if (parent1 instanceof MemberExpression) {
11112
+ const callExpression = parent1.parent;
11113
+ const property = parent1.property;
11114
+ if (!(callExpression instanceof CallExpression) || !(property instanceof Identifier)) {
11115
+ return;
11116
+ }
11117
+ const memberName = property.name;
11118
+ // side-effect only, when only chaining .catch or .finally
11119
+ if (callExpression.parent instanceof ExpressionStatement &&
11120
+ ['catch', 'finally'].includes(memberName)) {
11121
+ return EMPTY_ARRAY;
11122
+ }
11123
+ if (memberName !== 'then')
11124
+ return;
11125
+ // Side-effect only: import('bar').then()
11126
+ if (callExpression.arguments.length === 0) {
11127
+ return EMPTY_ARRAY;
11128
+ }
11129
+ const argument = callExpression.arguments[0];
11130
+ if (callExpression.arguments.length !== 1 ||
11131
+ !(argument instanceof ArrowFunctionExpression || argument instanceof FunctionExpression)) {
11132
+ return;
11133
+ }
11134
+ // Side-effect only: import('bar').then(() => {})
11135
+ if (argument.params.length === 0) {
11136
+ return EMPTY_ARRAY;
11137
+ }
11138
+ const declaration = argument.params[0];
11139
+ if (argument.params.length === 1 && declaration instanceof ObjectPattern) {
11140
+ return getDeterministicObjectDestructure(declaration);
11141
+ }
11142
+ return;
11143
+ }
11144
+ }
10999
11145
  hasEffects() {
11000
11146
  return true;
11001
11147
  }
@@ -11187,6 +11333,15 @@ const accessedImportGlobals = {
11187
11333
  cjs: ['require'],
11188
11334
  system: ['module']
11189
11335
  };
11336
+ function getDeterministicObjectDestructure(objectPattern) {
11337
+ const variables = [];
11338
+ for (const property of objectPattern.properties) {
11339
+ if (property.type === 'RestElement' || property.computed || property.key.type !== 'Identifier')
11340
+ return;
11341
+ variables.push(property.key.name);
11342
+ }
11343
+ return variables;
11344
+ }
11190
11345
 
11191
11346
  class ImportNamespaceSpecifier extends NodeBase {
11192
11347
  applyDeoptimizations() { }
@@ -12431,10 +12586,6 @@ class UpdateExpression extends NodeBase {
12431
12586
  }
12432
12587
  }
12433
12588
 
12434
- function isReassignedExportsMember(variable, exportNamesByVariable) {
12435
- return (variable.renderBaseName !== null && exportNamesByVariable.has(variable) && variable.isReassigned);
12436
- }
12437
-
12438
12589
  function areAllDeclarationsIncludedAndNotExported(declarations, exportNamesByVariable) {
12439
12590
  for (const declarator of declarations) {
12440
12591
  if (!declarator.id.included)
@@ -12611,70 +12762,6 @@ function gatherSystemExportsAndGetSingleExport(separatedNodes, options, aggregat
12611
12762
  return singleSystemExport;
12612
12763
  }
12613
12764
 
12614
- class VariableDeclarator extends NodeBase {
12615
- declareDeclarator(kind) {
12616
- this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
12617
- }
12618
- deoptimizePath(path) {
12619
- this.id.deoptimizePath(path);
12620
- }
12621
- hasEffects(context) {
12622
- if (!this.deoptimized)
12623
- this.applyDeoptimizations();
12624
- const initEffect = this.init?.hasEffects(context);
12625
- this.id.markDeclarationReached();
12626
- return initEffect || this.id.hasEffects(context);
12627
- }
12628
- include(context, includeChildrenRecursively) {
12629
- const { deoptimized, id, init } = this;
12630
- if (!deoptimized)
12631
- this.applyDeoptimizations();
12632
- this.included = true;
12633
- init?.include(context, includeChildrenRecursively);
12634
- id.markDeclarationReached();
12635
- if (includeChildrenRecursively || id.shouldBeIncluded(context)) {
12636
- id.include(context, includeChildrenRecursively);
12637
- }
12638
- }
12639
- render(code, options) {
12640
- const { exportNamesByVariable, snippets: { _, getPropertyAccess } } = options;
12641
- const { end, id, init, start } = this;
12642
- const renderId = id.included;
12643
- if (renderId) {
12644
- id.render(code, options);
12645
- }
12646
- else {
12647
- const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', id.end);
12648
- code.remove(start, findNonWhiteSpace(code.original, operatorPos + 1));
12649
- }
12650
- if (init) {
12651
- if (id instanceof Identifier && init instanceof ClassExpression && !init.id) {
12652
- const renderedVariable = id.variable.getName(getPropertyAccess);
12653
- if (renderedVariable !== id.name) {
12654
- code.appendLeft(init.start + 5, ` ${id.name}`);
12655
- }
12656
- }
12657
- init.render(code, options, renderId ? BLANK : { renderedSurroundingElement: ExpressionStatement$1 });
12658
- }
12659
- else if (id instanceof Identifier &&
12660
- isReassignedExportsMember(id.variable, exportNamesByVariable)) {
12661
- code.appendLeft(end, `${_}=${_}void 0`);
12662
- }
12663
- }
12664
- applyDeoptimizations() {
12665
- this.deoptimized = true;
12666
- const { id, init } = this;
12667
- if (init && id instanceof Identifier && init instanceof ClassExpression && !init.id) {
12668
- const { name, variable } = id;
12669
- for (const accessedVariable of init.scope.accessedOutsideVariables.values()) {
12670
- if (accessedVariable !== variable) {
12671
- accessedVariable.forbidName(name);
12672
- }
12673
- }
12674
- }
12675
- }
12676
- }
12677
-
12678
12765
  class WhileStatement extends NodeBase {
12679
12766
  hasEffects(context) {
12680
12767
  if (this.test.hasEffects(context))
@@ -12875,7 +12962,9 @@ class NamespaceVariable extends Variable {
12875
12962
  renderBlock(options) {
12876
12963
  const { exportNamesByVariable, format, freeze, indent: t, namespaceToStringTag, snippets: { _, cnst, getObject, getPropertyAccess, n, s } } = options;
12877
12964
  const memberVariables = this.getMemberVariables();
12878
- const members = Object.entries(memberVariables).map(([name, original]) => {
12965
+ const members = Object.entries(memberVariables)
12966
+ .filter(([_, variable]) => variable.included)
12967
+ .map(([name, original]) => {
12879
12968
  if (this.referencedEarly || original.isReassigned) {
12880
12969
  return [
12881
12970
  null,
@@ -13577,6 +13666,28 @@ class Module {
13577
13666
  this.ast.include(createInclusionContext(), true);
13578
13667
  this.includeAllExports(false);
13579
13668
  }
13669
+ includeExportsByNames(names) {
13670
+ if (!this.isExecuted) {
13671
+ markModuleAndImpureDependenciesAsExecuted(this);
13672
+ this.graph.needsTreeshakingPass = true;
13673
+ }
13674
+ let includeNamespaceMembers = false;
13675
+ for (const name of names) {
13676
+ const variable = this.getVariableForExportName(name)[0];
13677
+ if (variable) {
13678
+ variable.deoptimizePath(UNKNOWN_PATH);
13679
+ if (!variable.included) {
13680
+ this.includeVariable(variable);
13681
+ }
13682
+ }
13683
+ if (!this.exports.has(name) && !this.reexportDescriptions.has(name)) {
13684
+ includeNamespaceMembers = true;
13685
+ }
13686
+ }
13687
+ if (includeNamespaceMembers) {
13688
+ this.namespace.setMergedNamespaces(this.includeAndGetAdditionalMergedNamespaces());
13689
+ }
13690
+ }
13580
13691
  isIncluded() {
13581
13692
  // Modules where this.ast is missing have been loaded via this.load and are
13582
13693
  // not yet fully processed, hence they cannot be included.
@@ -13969,7 +14080,15 @@ class Module {
13969
14080
  const resolution = this.dynamicImports.find(dynamicImport => dynamicImport.node === node).resolution;
13970
14081
  if (resolution instanceof Module) {
13971
14082
  resolution.includedDynamicImporters.push(this);
13972
- resolution.includeAllExports(true);
14083
+ const importedNames = this.options.treeshake
14084
+ ? node.getDeterministicImportedNames()
14085
+ : undefined;
14086
+ if (importedNames) {
14087
+ resolution.includeExportsByNames(importedNames);
14088
+ }
14089
+ else {
14090
+ resolution.includeAllExports(true);
14091
+ }
13973
14092
  }
13974
14093
  }
13975
14094
  includeVariable(variable) {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -31,7 +31,7 @@ function _interopNamespaceDefault(e) {
31
31
 
32
32
  const tty__namespace = /*#__PURE__*/_interopNamespaceDefault(tty);
33
33
 
34
- var version$1 = "3.20.6";
34
+ var version$1 = "3.21.0";
35
35
 
36
36
  function ensureArray$1(items) {
37
37
  if (Array.isArray(items)) {
@@ -10840,9 +10840,9 @@ class FunctionDeclaration extends FunctionNode {
10840
10840
  function getDeclarationStart(code, start) {
10841
10841
  return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
10842
10842
  }
10843
- function getIdInsertPosition(code, declarationKeyword, endMarker, start) {
10844
- const declarationEnd = findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
10845
- code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd));
10843
+ function getFunctionIdInsertPosition(code, start) {
10844
+ const declarationEnd = findFirstOccurrenceOutsideComment(code, 'function', start) + 'function'.length;
10845
+ code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '(', declarationEnd));
10846
10846
  const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
10847
10847
  if (generatorStarPos === -1) {
10848
10848
  return declarationEnd;
@@ -10867,10 +10867,14 @@ class ExportDefaultDeclaration extends NodeBase {
10867
10867
  const { start, end } = nodeRenderOptions;
10868
10868
  const declarationStart = getDeclarationStart(code.original, this.start);
10869
10869
  if (this.declaration instanceof FunctionDeclaration) {
10870
- this.renderNamedDeclaration(code, declarationStart, 'function', '(', this.declaration.id === null, options);
10870
+ this.renderNamedDeclaration(code, declarationStart, this.declaration.id === null
10871
+ ? getFunctionIdInsertPosition(code.original, declarationStart)
10872
+ : null, options);
10871
10873
  }
10872
10874
  else if (this.declaration instanceof ClassDeclaration) {
10873
- this.renderNamedDeclaration(code, declarationStart, 'class', '{', this.declaration.id === null, options);
10875
+ this.renderNamedDeclaration(code, declarationStart, this.declaration.id === null
10876
+ ? findFirstOccurrenceOutsideComment(code.original, 'class', start) + 'class'.length
10877
+ : null, options);
10874
10878
  }
10875
10879
  else if (this.variable.getOriginalVariable() !== this.variable) {
10876
10880
  // Remove altogether to prevent re-declaring the same variable
@@ -10893,13 +10897,13 @@ class ExportDefaultDeclaration extends NodeBase {
10893
10897
  this.declaration.render(code, options);
10894
10898
  }
10895
10899
  applyDeoptimizations() { }
10896
- renderNamedDeclaration(code, declarationStart, declarationKeyword, endMarker, needsId, options) {
10900
+ renderNamedDeclaration(code, declarationStart, idInsertPosition, options) {
10897
10901
  const { exportNamesByVariable, format, snippets: { getPropertyAccess } } = options;
10898
10902
  const name = this.variable.getName(getPropertyAccess);
10899
10903
  // Remove `export default`
10900
10904
  code.remove(this.start, declarationStart);
10901
- if (needsId) {
10902
- code.appendLeft(getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart), ` ${name}`);
10905
+ if (idInsertPosition !== null) {
10906
+ code.appendLeft(idInsertPosition, ` ${name}`);
10903
10907
  }
10904
10908
  if (format === 'system' &&
10905
10909
  this.declaration instanceof ClassDeclaration &&
@@ -11027,6 +11031,7 @@ class ForOfStatement extends NodeBase {
11027
11031
  applyDeoptimizations() {
11028
11032
  this.deoptimized = true;
11029
11033
  this.left.deoptimizePath(EMPTY_PATH);
11034
+ this.right.deoptimizePath(UNKNOWN_PATH);
11030
11035
  this.context.requestTreeshakingPass();
11031
11036
  }
11032
11037
  }
@@ -11478,6 +11483,74 @@ function getToStringTagValue(getObject) {
11478
11483
  });
11479
11484
  }
11480
11485
 
11486
+ function isReassignedExportsMember(variable, exportNamesByVariable) {
11487
+ return (variable.renderBaseName !== null && exportNamesByVariable.has(variable) && variable.isReassigned);
11488
+ }
11489
+
11490
+ class VariableDeclarator extends NodeBase {
11491
+ declareDeclarator(kind) {
11492
+ this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
11493
+ }
11494
+ deoptimizePath(path) {
11495
+ this.id.deoptimizePath(path);
11496
+ }
11497
+ hasEffects(context) {
11498
+ if (!this.deoptimized)
11499
+ this.applyDeoptimizations();
11500
+ const initEffect = this.init?.hasEffects(context);
11501
+ this.id.markDeclarationReached();
11502
+ return initEffect || this.id.hasEffects(context);
11503
+ }
11504
+ include(context, includeChildrenRecursively) {
11505
+ const { deoptimized, id, init } = this;
11506
+ if (!deoptimized)
11507
+ this.applyDeoptimizations();
11508
+ this.included = true;
11509
+ init?.include(context, includeChildrenRecursively);
11510
+ id.markDeclarationReached();
11511
+ if (includeChildrenRecursively || id.shouldBeIncluded(context)) {
11512
+ id.include(context, includeChildrenRecursively);
11513
+ }
11514
+ }
11515
+ render(code, options) {
11516
+ const { exportNamesByVariable, snippets: { _, getPropertyAccess } } = options;
11517
+ const { end, id, init, start } = this;
11518
+ const renderId = id.included;
11519
+ if (renderId) {
11520
+ id.render(code, options);
11521
+ }
11522
+ else {
11523
+ const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', id.end);
11524
+ code.remove(start, findNonWhiteSpace(code.original, operatorPos + 1));
11525
+ }
11526
+ if (init) {
11527
+ if (id instanceof Identifier && init instanceof ClassExpression && !init.id) {
11528
+ const renderedVariable = id.variable.getName(getPropertyAccess);
11529
+ if (renderedVariable !== id.name) {
11530
+ code.appendLeft(init.start + 5, ` ${id.name}`);
11531
+ }
11532
+ }
11533
+ init.render(code, options, renderId ? BLANK : { renderedSurroundingElement: ExpressionStatement$1 });
11534
+ }
11535
+ else if (id instanceof Identifier &&
11536
+ isReassignedExportsMember(id.variable, exportNamesByVariable)) {
11537
+ code.appendLeft(end, `${_}=${_}void 0`);
11538
+ }
11539
+ }
11540
+ applyDeoptimizations() {
11541
+ this.deoptimized = true;
11542
+ const { id, init } = this;
11543
+ if (init && id instanceof Identifier && init instanceof ClassExpression && !init.id) {
11544
+ const { name, variable } = id;
11545
+ for (const accessedVariable of init.scope.accessedOutsideVariables.values()) {
11546
+ if (accessedVariable !== variable) {
11547
+ accessedVariable.forbidName(name);
11548
+ }
11549
+ }
11550
+ }
11551
+ }
11552
+ }
11553
+
11481
11554
  // TODO once ImportExpression follows official ESTree specs with "null" as
11482
11555
  // default, keys.ts should be updated
11483
11556
  class ImportExpression extends NodeBase {
@@ -11494,6 +11567,79 @@ class ImportExpression extends NodeBase {
11494
11567
  bind() {
11495
11568
  this.source.bind();
11496
11569
  }
11570
+ /**
11571
+ * Get imported variables for deterministic usage, valid cases are:
11572
+ *
11573
+ * - `const { foo } = await import('bar')`.
11574
+ * - `(await import('bar')).foo`
11575
+ * - `import('bar').then(({ foo }) => {})`
11576
+ *
11577
+ * Returns empty array if it's side-effect only import.
11578
+ * Returns undefined if it's not fully deterministic.
11579
+ */
11580
+ getDeterministicImportedNames() {
11581
+ const parent1 = this.parent;
11582
+ // Side-effect only: import('bar')
11583
+ if (parent1 instanceof ExpressionStatement) {
11584
+ return EMPTY_ARRAY;
11585
+ }
11586
+ if (parent1 instanceof AwaitExpression) {
11587
+ const parent2 = parent1.parent;
11588
+ // Side-effect only: await import('bar')
11589
+ if (parent2 instanceof ExpressionStatement) {
11590
+ return EMPTY_ARRAY;
11591
+ }
11592
+ // Case 1: const { foo } = await import('bar')
11593
+ if (parent2 instanceof VariableDeclarator) {
11594
+ const declaration = parent2.id;
11595
+ return declaration instanceof ObjectPattern
11596
+ ? getDeterministicObjectDestructure(declaration)
11597
+ : undefined;
11598
+ }
11599
+ // Case 2: (await import('bar')).foo
11600
+ if (parent2 instanceof MemberExpression) {
11601
+ const id = parent2.property;
11602
+ if (!parent2.computed && id instanceof Identifier) {
11603
+ return [id.name];
11604
+ }
11605
+ }
11606
+ return;
11607
+ }
11608
+ // Case 3: import('bar').then(({ foo }) => {})
11609
+ if (parent1 instanceof MemberExpression) {
11610
+ const callExpression = parent1.parent;
11611
+ const property = parent1.property;
11612
+ if (!(callExpression instanceof CallExpression) || !(property instanceof Identifier)) {
11613
+ return;
11614
+ }
11615
+ const memberName = property.name;
11616
+ // side-effect only, when only chaining .catch or .finally
11617
+ if (callExpression.parent instanceof ExpressionStatement &&
11618
+ ['catch', 'finally'].includes(memberName)) {
11619
+ return EMPTY_ARRAY;
11620
+ }
11621
+ if (memberName !== 'then')
11622
+ return;
11623
+ // Side-effect only: import('bar').then()
11624
+ if (callExpression.arguments.length === 0) {
11625
+ return EMPTY_ARRAY;
11626
+ }
11627
+ const argument = callExpression.arguments[0];
11628
+ if (callExpression.arguments.length !== 1 ||
11629
+ !(argument instanceof ArrowFunctionExpression || argument instanceof FunctionExpression)) {
11630
+ return;
11631
+ }
11632
+ // Side-effect only: import('bar').then(() => {})
11633
+ if (argument.params.length === 0) {
11634
+ return EMPTY_ARRAY;
11635
+ }
11636
+ const declaration = argument.params[0];
11637
+ if (argument.params.length === 1 && declaration instanceof ObjectPattern) {
11638
+ return getDeterministicObjectDestructure(declaration);
11639
+ }
11640
+ return;
11641
+ }
11642
+ }
11497
11643
  hasEffects() {
11498
11644
  return true;
11499
11645
  }
@@ -11685,6 +11831,15 @@ const accessedImportGlobals = {
11685
11831
  cjs: ['require'],
11686
11832
  system: ['module']
11687
11833
  };
11834
+ function getDeterministicObjectDestructure(objectPattern) {
11835
+ const variables = [];
11836
+ for (const property of objectPattern.properties) {
11837
+ if (property.type === 'RestElement' || property.computed || property.key.type !== 'Identifier')
11838
+ return;
11839
+ variables.push(property.key.name);
11840
+ }
11841
+ return variables;
11842
+ }
11688
11843
 
11689
11844
  class ImportNamespaceSpecifier extends NodeBase {
11690
11845
  applyDeoptimizations() { }
@@ -12929,10 +13084,6 @@ class UpdateExpression extends NodeBase {
12929
13084
  }
12930
13085
  }
12931
13086
 
12932
- function isReassignedExportsMember(variable, exportNamesByVariable) {
12933
- return (variable.renderBaseName !== null && exportNamesByVariable.has(variable) && variable.isReassigned);
12934
- }
12935
-
12936
13087
  function areAllDeclarationsIncludedAndNotExported(declarations, exportNamesByVariable) {
12937
13088
  for (const declarator of declarations) {
12938
13089
  if (!declarator.id.included)
@@ -13109,70 +13260,6 @@ function gatherSystemExportsAndGetSingleExport(separatedNodes, options, aggregat
13109
13260
  return singleSystemExport;
13110
13261
  }
13111
13262
 
13112
- class VariableDeclarator extends NodeBase {
13113
- declareDeclarator(kind) {
13114
- this.id.declare(kind, this.init || UNDEFINED_EXPRESSION);
13115
- }
13116
- deoptimizePath(path) {
13117
- this.id.deoptimizePath(path);
13118
- }
13119
- hasEffects(context) {
13120
- if (!this.deoptimized)
13121
- this.applyDeoptimizations();
13122
- const initEffect = this.init?.hasEffects(context);
13123
- this.id.markDeclarationReached();
13124
- return initEffect || this.id.hasEffects(context);
13125
- }
13126
- include(context, includeChildrenRecursively) {
13127
- const { deoptimized, id, init } = this;
13128
- if (!deoptimized)
13129
- this.applyDeoptimizations();
13130
- this.included = true;
13131
- init?.include(context, includeChildrenRecursively);
13132
- id.markDeclarationReached();
13133
- if (includeChildrenRecursively || id.shouldBeIncluded(context)) {
13134
- id.include(context, includeChildrenRecursively);
13135
- }
13136
- }
13137
- render(code, options) {
13138
- const { exportNamesByVariable, snippets: { _, getPropertyAccess } } = options;
13139
- const { end, id, init, start } = this;
13140
- const renderId = id.included;
13141
- if (renderId) {
13142
- id.render(code, options);
13143
- }
13144
- else {
13145
- const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', id.end);
13146
- code.remove(start, findNonWhiteSpace(code.original, operatorPos + 1));
13147
- }
13148
- if (init) {
13149
- if (id instanceof Identifier && init instanceof ClassExpression && !init.id) {
13150
- const renderedVariable = id.variable.getName(getPropertyAccess);
13151
- if (renderedVariable !== id.name) {
13152
- code.appendLeft(init.start + 5, ` ${id.name}`);
13153
- }
13154
- }
13155
- init.render(code, options, renderId ? BLANK : { renderedSurroundingElement: ExpressionStatement$1 });
13156
- }
13157
- else if (id instanceof Identifier &&
13158
- isReassignedExportsMember(id.variable, exportNamesByVariable)) {
13159
- code.appendLeft(end, `${_}=${_}void 0`);
13160
- }
13161
- }
13162
- applyDeoptimizations() {
13163
- this.deoptimized = true;
13164
- const { id, init } = this;
13165
- if (init && id instanceof Identifier && init instanceof ClassExpression && !init.id) {
13166
- const { name, variable } = id;
13167
- for (const accessedVariable of init.scope.accessedOutsideVariables.values()) {
13168
- if (accessedVariable !== variable) {
13169
- accessedVariable.forbidName(name);
13170
- }
13171
- }
13172
- }
13173
- }
13174
- }
13175
-
13176
13263
  class WhileStatement extends NodeBase {
13177
13264
  hasEffects(context) {
13178
13265
  if (this.test.hasEffects(context))
@@ -13373,7 +13460,9 @@ class NamespaceVariable extends Variable {
13373
13460
  renderBlock(options) {
13374
13461
  const { exportNamesByVariable, format, freeze, indent: t, namespaceToStringTag, snippets: { _, cnst, getObject, getPropertyAccess, n, s } } = options;
13375
13462
  const memberVariables = this.getMemberVariables();
13376
- const members = Object.entries(memberVariables).map(([name, original]) => {
13463
+ const members = Object.entries(memberVariables)
13464
+ .filter(([_, variable]) => variable.included)
13465
+ .map(([name, original]) => {
13377
13466
  if (this.referencedEarly || original.isReassigned) {
13378
13467
  return [
13379
13468
  null,
@@ -14075,6 +14164,28 @@ class Module {
14075
14164
  this.ast.include(createInclusionContext(), true);
14076
14165
  this.includeAllExports(false);
14077
14166
  }
14167
+ includeExportsByNames(names) {
14168
+ if (!this.isExecuted) {
14169
+ markModuleAndImpureDependenciesAsExecuted(this);
14170
+ this.graph.needsTreeshakingPass = true;
14171
+ }
14172
+ let includeNamespaceMembers = false;
14173
+ for (const name of names) {
14174
+ const variable = this.getVariableForExportName(name)[0];
14175
+ if (variable) {
14176
+ variable.deoptimizePath(UNKNOWN_PATH);
14177
+ if (!variable.included) {
14178
+ this.includeVariable(variable);
14179
+ }
14180
+ }
14181
+ if (!this.exports.has(name) && !this.reexportDescriptions.has(name)) {
14182
+ includeNamespaceMembers = true;
14183
+ }
14184
+ }
14185
+ if (includeNamespaceMembers) {
14186
+ this.namespace.setMergedNamespaces(this.includeAndGetAdditionalMergedNamespaces());
14187
+ }
14188
+ }
14078
14189
  isIncluded() {
14079
14190
  // Modules where this.ast is missing have been loaded via this.load and are
14080
14191
  // not yet fully processed, hence they cannot be included.
@@ -14467,7 +14578,15 @@ class Module {
14467
14578
  const resolution = this.dynamicImports.find(dynamicImport => dynamicImport.node === node).resolution;
14468
14579
  if (resolution instanceof Module) {
14469
14580
  resolution.includedDynamicImporters.push(this);
14470
- resolution.includeAllExports(true);
14581
+ const importedNames = this.options.treeshake
14582
+ ? node.getDeterministicImportedNames()
14583
+ : undefined;
14584
+ if (importedNames) {
14585
+ resolution.includeExportsByNames(importedNames);
14586
+ }
14587
+ else {
14588
+ resolution.includeAllExports(true);
14589
+ }
14471
14590
  }
14472
14591
  }
14473
14592
  includeVariable(variable) {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v3.20.6
4
- Tue, 18 Apr 2023 11:34:41 GMT - commit 911700bc0bc90e5b1b8db01ea77fe3040a05480d
3
+ Rollup.js v3.21.0
4
+ Sun, 23 Apr 2023 19:43:51 GMT - commit b79b73cd8bb98c10ab7eedae154bb5c1e03ced7d
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup",
3
- "version": "3.20.6",
3
+ "version": "3.21.0",
4
4
  "description": "Next-generation ES module bundler",
5
5
  "main": "dist/rollup.js",
6
6
  "module": "dist/es/rollup.js",