requirejs-esm 2.4.1 → 3.0.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": "2.4.1",
3
+ "version": "3.0.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",
@@ -19,7 +19,7 @@
19
19
  "url": "https://github.com/prantlf/requirejs-esm/issues"
20
20
  },
21
21
  "engines": {
22
- "node": ">=14"
22
+ "node": ">=18"
23
23
  },
24
24
  "main": "dist/api.js",
25
25
  "module": "src/api.js",
@@ -42,7 +42,8 @@
42
42
  "demo:local": "npm run optimize:local && npm run minify:local",
43
43
  "optimize:local": "r.js -o demo-local/build.config.js",
44
44
  "minify:local": "terser -cm --ecma 2018 --comments false --source-map content=demo-local/main-built.js.map --source-map includeSources --source-map url=main-built.min.js.map -o demo-local/main-built.min.js demo-local/main-built.js",
45
- "lint": "eslint -c .eslintrc.browser.yml src && eslint -c .eslintrc.node.yml perf/*.js bin 'test/*.js'",
45
+ "lint": "eslint src bin perf/*.js bin test/*.js",
46
+ "fix": "npm run lint -- --fix",
46
47
  "check": "tehanu test/*.js",
47
48
  "cover": "c8 node test && c8 --no-clean ./bin/esm2requirejs.js && c8 --no-clean ./bin/esm2requirejs.js test/input/esm-import-all.js && c8 --no-clean ./bin/esm2requirejs.js dummy || c8 report -r text -r lcov | grep -Ev '(\\.\\.\\.)|(index.mjs)|(source-map.js)|(base64-vlq.js)|(base64.js)|(binary-search.js)|(mapping-list.js)|(read-wasm-browser.js)|(url-browser.js)|(wasm.js)|(regexes.js)|(public-api.js)|(All files)' && c8 check-coverage",
48
49
  "test": "npm run lint && npm run cover",
@@ -67,46 +68,31 @@
67
68
  "functions": 20,
68
69
  "statements": 45
69
70
  },
70
- "release": {
71
- "plugins": [
72
- "@semantic-release/commit-analyzer",
73
- "@semantic-release/release-notes-generator",
74
- "@semantic-release/changelog",
75
- "@semantic-release/npm",
76
- [
77
- "@semantic-release/github",
78
- {
79
- "failComment": false
80
- }
81
- ],
82
- "@semantic-release/git"
83
- ]
84
- },
85
71
  "dependencies": {
86
72
  "@prantlf/convert-source-map": "^2.0.0",
87
- "astring": "^1.8.4",
73
+ "astring": "^1.8.6",
88
74
  "charcodes": "^0.2.0",
89
- "commander": "^10.0.1",
90
- "meriyah": "^4.3.5",
91
- "punycode": "^2.3.0",
75
+ "commander": "^12.1.0",
76
+ "meriyah": "^5.0.0",
77
+ "punycode": "^2.3.1",
92
78
  "source-map": "^0.8.0-beta.0",
93
79
  "tiny-glob": "^0.2.9"
94
80
  },
95
81
  "devDependencies": {
96
- "@prantlf/requirejs": "^3.0.2",
97
- "@rollup/plugin-commonjs": "^24.1.0",
98
- "@rollup/plugin-json": "^6.0.0",
99
- "@rollup/plugin-node-resolve": "^15.0.2",
100
- "@semantic-release/changelog": "^6.0.3",
101
- "@semantic-release/git": "^10.0.1",
102
- "c8": "^7.13.0",
103
- "eslint": "^8.40.0",
82
+ "@eslint/js": "^9.8.0",
83
+ "@prantlf/requirejs": "^3.2.0",
84
+ "@rollup/plugin-commonjs": "^26.0.1",
85
+ "@rollup/plugin-json": "^6.1.0",
86
+ "@rollup/plugin-node-resolve": "^15.2.3",
87
+ "c8": "^10.1.2",
88
+ "eslint": "^9.8.0",
89
+ "globals": "^15.8.0",
104
90
  "lit-html": "^1",
105
- "rollup": "^3.21.5",
91
+ "rollup": "^4.19.1",
106
92
  "tehanu": "^1.0.1",
107
- "tehanu-repo-coco": "^1.0.0",
108
- "tehanu-teru": "^1.0.0",
109
- "terser": "^5.17.1"
93
+ "tehanu-repo-coco": "^1.0.1",
94
+ "tehanu-teru": "^1.0.1",
95
+ "terser": "^5.31.3"
110
96
  },
111
97
  "keywords": [
112
98
  "requirejs-plugin",
@@ -93,6 +93,7 @@ export function transformEsmToAmd(program, options) {
93
93
  let hasExport = false
94
94
  let needReturnExport = false
95
95
  let isOnlyDefaultExport = true
96
+ let defaultExportVar
96
97
 
97
98
  for (let i = 0; i < length; ++i) {
98
99
  const statement = body[i]
@@ -188,6 +189,7 @@ export function transformEsmToAmd(program, options) {
188
189
  // export var a = 1
189
190
  // export function test() {}
190
191
  // export class Test {}
192
+ // export {default as A} from "module"
191
193
  if (statement.type === 'ExportNamedDeclaration') {
192
194
  hasExport = true
193
195
  needReturnExport = true
@@ -246,13 +248,45 @@ export function transformEsmToAmd(program, options) {
246
248
  addExportStatement({ exported, local: identifier(localName) })
247
249
  } else {
248
250
  localName = name
249
- addExportStatement(specifier)
251
+ if (localName === 'default') {
252
+ if (exported.name === 'default') {
253
+ // export {default} from "module"
254
+ defaultExportVar = importVar
255
+ } else {
256
+ // export {default as A} from "module"
257
+ addExportDefaultStatement(exported, importVar)
258
+ }
259
+ } else if (exported.name === 'default') {
260
+ // export {A as default} from "module"
261
+ defaultExportVar = importVar
262
+ } else {
263
+ // export {A as B} from "module"
264
+ addExportStatement(specifier)
265
+ }
266
+ }
267
+ if (localName !== 'default' && !defaultExportVar) {
268
+ // _exports.B = A;
269
+ namedImports.push(declareImport(localName, importVar.name, name))
250
270
  }
251
- namedImports.push(declareImport(localName, importVar.name, name))
252
271
  }
253
272
  } else {
254
273
  for (const specifier of specifiers) {
255
- addExportStatement(specifier)
274
+ const { exported, local } = specifier
275
+ if (local.name === 'default') {
276
+ if (exported.name === 'default') {
277
+ // export {default}
278
+ throw new Error('Expression `export { default }` is not supported.')
279
+ } else {
280
+ // export {default as A}
281
+ throw new Error(`Expression \`export { default as ${exported.name} }\` is not supported.`)
282
+ }
283
+ } else if (exported.name === 'default') {
284
+ // export {A as default}
285
+ defaultExportVar = local
286
+ } else {
287
+ // export {A as B}
288
+ addExportStatement(specifier)
289
+ }
256
290
  }
257
291
  }
258
292
 
@@ -280,22 +314,27 @@ export function transformEsmToAmd(program, options) {
280
314
 
281
315
  // adding define wrapper
282
316
  if (hasExport && needReturnExport) {
283
- // var _exports = {}
284
- body.unshift(
285
- variableDeclaration('let', [
286
- variableDeclarator(exportsVar, objectExpression([]))
287
- ])
288
- )
289
-
290
- // return <expression>
291
317
  let returnStat
292
- if (isOnlyDefaultExport) {
293
- // return _exports.default
294
- returnStat = returnStatement(memberExpression(exportsVar, identifier('default')))
295
- }
296
- else {
297
- // return _exports
298
- returnStat = returnStatement(exportsVar)
318
+ if (defaultExportVar) {
319
+ // return _import
320
+ returnStat = returnStatement(defaultExportVar)
321
+ } else {
322
+ // var _exports = {}
323
+ body.unshift(
324
+ variableDeclaration('let', [
325
+ variableDeclarator(exportsVar, objectExpression([]))
326
+ ])
327
+ )
328
+
329
+ // return <expression>
330
+ if (isOnlyDefaultExport) {
331
+ // return _exports.default
332
+ returnStat = returnStatement(memberExpression(exportsVar, identifier('default')))
333
+ }
334
+ else {
335
+ // return _exports
336
+ returnStat = returnStatement(exportsVar)
337
+ }
299
338
  }
300
339
 
301
340
  body.push(returnStat)
@@ -311,6 +350,15 @@ export function transformEsmToAmd(program, options) {
311
350
  const exportStat = exportStatement(exportsVar, asName, local)
312
351
  body.push(exportStat)
313
352
  }
353
+
354
+ function addExportDefaultStatement(exported, imported) {
355
+ const asName = exported.name
356
+ if (asName !== 'default') {
357
+ isOnlyDefaultExport = false
358
+ }
359
+ const exportStat = exportStatement(exportsVar, asName, imported)
360
+ body.push(exportStat)
361
+ }
314
362
  }
315
363
 
316
364
  function declareImport(varName, object, property) {