requirejs-esm 4.3.0 → 4.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "requirejs-esm",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "A RequireJS plugin converting JavaScript modules from ESM to AMD.",
5
5
  "author": "Ferdinand Prantl <prantlf@gmail.com>",
6
6
  "license": "MIT",
@@ -78,18 +78,18 @@
78
78
  "tiny-glob": "^0.2.9"
79
79
  },
80
80
  "devDependencies": {
81
- "@biomejs/biome": "^2.4.14",
82
- "@prantlf/requirejs": "^3.3.1",
83
- "@rollup/plugin-commonjs": "^29.0.2",
81
+ "@biomejs/biome": "^2.4.16",
82
+ "@prantlf/requirejs": "^3.4.0",
83
+ "@rollup/plugin-commonjs": "^29.0.3",
84
84
  "@rollup/plugin-json": "^6.1.0",
85
85
  "@rollup/plugin-node-resolve": "^16.0.3",
86
86
  "c8": "^11.0.0",
87
87
  "lit-html": "^1",
88
- "rollup": "^4.60.2",
88
+ "rollup": "^4.61.1",
89
89
  "tehanu": "^1.0.1",
90
90
  "tehanu-repo-coco": "^1.0.1",
91
91
  "tehanu-teru": "^1.0.1",
92
- "terser": "^5.46.2"
92
+ "terser": "^5.48.0"
93
93
  },
94
94
  "keywords": [
95
95
  "requirejs-plugin",
package/src/api.d.ts CHANGED
@@ -28,6 +28,7 @@ interface TransformAstOptions {
28
28
  resolvePath?: ResolvePath
29
29
  skipIfNoImportExport?: boolean /*= false */
30
30
  useStrict?: boolean /*= true */
31
+ splitDefaultNamedDeclarations?: boolean /*= false */
31
32
  sourceMap?: boolean /*= false */
32
33
  onBeforeTransform?: OnBeforeTransform
33
34
  onAfterTransform?: OnAfterTransform
package/src/plugin.js CHANGED
@@ -25,6 +25,9 @@ const {
25
25
  // Do not insert `"use strict"` expression to the AMD modules. You'd set it
26
26
  // to `false` if your bundler inserts `"use strict"` to the outer scope.
27
27
  useStrict,
28
+ // Split `export default class A {}` to `class A {}; export default A`
29
+ // to trade easier AST manipulation for 100% code compatibility.
30
+ splitDefaultNamedDeclarations,
28
31
  // Boolean or object with booleans { inline, content }.
29
32
  sourceMap,
30
33
  // Enable console logging.
@@ -115,6 +118,7 @@ export default {
115
118
  /*ecmaVersion,*/
116
119
  useStrict,
117
120
  skipIfNoImportExport,
121
+ splitDefaultNamedDeclarations,
118
122
  // Always produce the source maps when transpiling in the browser, otherwise
119
123
  // the debugging would me impossible. When building and bundling, check if
120
124
  // the source maps were enabled for the output.
package/src/transform.js CHANGED
@@ -18,6 +18,9 @@ export default function transform(text, file, {
18
18
  // Do not insert `"use strict"` expression to the AMD modules. You'd set it
19
19
  // to `false` if your bundler inserts `"use strict"` to the outer scope.
20
20
  useStrict,
21
+ // Split `export default class A {}` to `class A {}; export default A`
22
+ // to trade easier AST manipulation for 100% code compatibility.
23
+ splitDefaultNamedDeclarations,
21
24
  // Enable source maps, can be an object with booleans { inline, content }.
22
25
  // If set to true, the object will be set to { inline: true, content: true }.
23
26
  sourceMap,
@@ -45,6 +48,7 @@ export default function transform(text, file, {
45
48
  originalResolvePath,
46
49
  useStrict,
47
50
  skipIfNoImportExport,
51
+ splitDefaultNamedDeclarations,
48
52
  onBeforeTransform,
49
53
  onAfterTransform,
50
54
  onBeforeUpdate,
@@ -87,6 +87,7 @@ export function transformEsmToAmd(program, options) {
87
87
  program
88
88
  })
89
89
 
90
+ const { splitDefaultNamedDeclarations } = options
90
91
  const { body } = program
91
92
  let { length } = body
92
93
 
@@ -148,47 +149,52 @@ export function transformEsmToAmd(program, options) {
148
149
 
149
150
  // expression after keyword default
150
151
  const { declaration } = statement
152
+ // export default X
151
153
  let exportValue = declaration
152
- const needExportExpression = true
153
-
154
- if (declaration.type === 'FunctionDeclaration') {
155
- exportValue = toExpression(exportValue)
156
- }
157
- if (declaration.type === 'ClassDeclaration') {
158
- exportValue = toExpression(exportValue)
159
- // const classNode = exportValue
160
-
161
- // if (classNode.id) {
162
- // body[i] = classNode
163
-
164
- // const className = identifier(classNode.id.name)
165
- // let exportStat
166
- // if (i + 1 === length && isOnlyDefaultExport) {
167
- // exportStat = returnStatement(identifier(className))
168
- // needReturnExport = false
169
- // } else {
170
- // exportStat = exportStatement(exportsVar, 'default', className)
171
- // }
172
-
173
- // program.pushContainer('body', [exportStat])
174
- // needExportExpression = false
175
- // } else {
176
- // exportValue = toExpression(classNode)
177
- // }
154
+ let keepDeclaration
155
+
156
+ if (declaration.type === 'FunctionDeclaration' ||
157
+ declaration.type === 'ClassDeclaration') {
158
+ const { id } = declaration
159
+ if (id && splitDefaultNamedDeclarations) {
160
+ // export default function X() {}
161
+ // export default class X {}
162
+ exportValue = identifier(declaration.id.name)
163
+ keepDeclaration = true
164
+ } else {
165
+ // export default function () {}
166
+ // export default class {}
167
+ exportValue = toExpression(declaration)
168
+ }
178
169
  }
179
170
 
180
- if (needExportExpression) {
181
- let exportStat
171
+ let exportStat
182
172
 
183
- if (i + 1 === length && isOnlyDefaultExport) {
184
- exportStat = returnStatement(exportValue)
185
- needReturnExport = false
186
- } else {
187
- exportStat = exportStatement(exportsVar, 'default', exportValue)
188
- }
173
+ if (i + 1 === length && isOnlyDefaultExport) {
174
+ exportStat = returnStatement(exportValue)
175
+ needReturnExport = false
176
+ } else {
177
+ exportStat = exportStatement(exportsVar, 'default', exportValue)
178
+ }
189
179
 
190
- body[i] = exportStat
180
+ // This changes the original code by putting the name of the exported
181
+ // function or class to the module scope. Being able to access the class
182
+ // by name simplifies other AST manipulations, which would have to be more
183
+ // complicated. Or the developer would have to help the manipulator
184
+ // by separating the export expression to a declaration and an export
185
+ // of an identifier. Use it if it doesn't break your code.
186
+ //
187
+ // // original, the name X is not in the module scope
188
+ // export default class X {}
189
+ //
190
+ // // converted, the name X is in the module scope
191
+ // class X {}
192
+ // export default X
193
+ if (keepDeclaration) {
194
+ body.splice(i++, 0, declaration)
195
+ ++length
191
196
  }
197
+ body[i] = exportStat
192
198
  }
193
199
 
194
200
  // export {x as y}