requirejs-esm 4.0.0 → 4.1.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.0.0",
3
+ "version": "4.1.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",
@@ -72,7 +72,7 @@
72
72
  "astring": "^1.9.0",
73
73
  "charcodes": "^0.2.1",
74
74
  "commander": "^14.0.2",
75
- "meriyah": "^6.1.4",
75
+ "meriyah": "^7.0.0",
76
76
  "punycode": "^2.3.1",
77
77
  "source-map": "^0.8.0-beta.0",
78
78
  "tiny-glob": "^0.2.9"
@@ -87,7 +87,7 @@
87
87
  "eslint": "^9.39.1",
88
88
  "globals": "^16.5.0",
89
89
  "lit-html": "^1",
90
- "rollup": "^4.53.1",
90
+ "rollup": "^4.53.3",
91
91
  "tehanu": "^1.0.1",
92
92
  "tehanu-repo-coco": "^1.0.1",
93
93
  "tehanu-teru": "^1.0.1",
package/src/api.d.ts CHANGED
@@ -7,18 +7,44 @@ interface ResolveOptions {
7
7
 
8
8
  declare function resolvePath(sourcePath: string, currentFile: string, options?: ResolveOptions): string
9
9
 
10
+ interface AmdOptions {
11
+ namespace?: Record<string, unknown>
12
+ func: Record<string, unknown>
13
+ name: string
14
+ deps?: string[]
15
+ params?: string[]
16
+ factory?: Record<string, unknown>
17
+ output?: Record<string, unknown>
18
+ }
19
+
10
20
  type ResolvePath = ((sourcePath: string, currentFile: string, options?: ResolveOptions) => string) | false
21
+ type OnBeforeTransform = (options: OnBeforeTransformOptions) => void
22
+ type OnAfterTransform = (options: OnAfterTransformOptions) => void
23
+ type OnBeforeUpdate = (options: AmdOptions) => boolean
24
+ type OnAfterUpdate = (options: AmdOptions) => boolean
11
25
 
12
26
  interface TransformAstOptions {
13
27
  pluginName?: string /*= 'esm'' */
14
28
  resolvePath?: ResolvePath
15
29
  useStrict?: boolean /*= true */
30
+ onBeforeTransform?: OnBeforeTransform
31
+ onAfterTransform?: OnAfterTransform
32
+ onBeforeUpdate?: OnBeforeUpdate
33
+ onAfterUpdate?: OnAfterUpdate
16
34
  }
17
35
 
18
36
  interface TransformOptions extends TransformAstOptions {
19
37
  sourceMap?: boolean /*= true */
20
38
  }
21
39
 
40
+ interface OnBeforeTransformOptions extends TransformAstOptions {
41
+ program: Record<string, unknown>
42
+ }
43
+
44
+ interface OnAfterTransformOptions extends OnBeforeTransformOptions {
45
+ callbackBody: Record<string, unknown>[]
46
+ }
47
+
22
48
  declare function transform(contents: string, path: string, options?: TransformOptions): string
23
49
 
24
50
  declare function transformAst(ast: object, options?: TransformOptions): { amd?: true, updated?: true }
package/src/plugin.js CHANGED
@@ -28,7 +28,13 @@ const {
28
28
  // Enable console logging.
29
29
  verbose,
30
30
  // Directory to save a copy of the transformed modules.
31
- debugDir
31
+ debugDir,
32
+ // ESM transformation callbacks.
33
+ onBeforeTransform,
34
+ onAfterTransform,
35
+ // AMD update callbacks.
36
+ onBeforeUpdate,
37
+ onAfterUpdate
32
38
  } = typeof module !== 'undefined' && module.config && module.config() || {}
33
39
 
34
40
  const buildMap = {}
@@ -109,7 +115,13 @@ export default {
109
115
  // Always produce the source maps when transpiling in the browser, otherwise
110
116
  // the debugging would me impossible. When building and bundling, check if
111
117
  // the source maps were enabled for the output.
112
- sourceMap: sourceMap || !isBuild
118
+ sourceMap: sourceMap || !isBuild,
119
+ // ESM transformation callbacks.
120
+ onBeforeTransform,
121
+ onAfterTransform,
122
+ // AMD update callbacks.
123
+ onBeforeUpdate,
124
+ onAfterUpdate
113
125
  }))
114
126
  if (!updated) {
115
127
  verbose && console.log('esm: retaining', name)
package/src/transform.js CHANGED
@@ -18,12 +18,28 @@ export default function transform(text, file, {
18
18
  useStrict,
19
19
  // Enable source maps, can be an object with booleans { inline, content }.
20
20
  // If set to true, the object will be set to { inline: true, content: true }.
21
- sourceMap
21
+ sourceMap,
22
+ // ESM transformation callbacks.
23
+ onBeforeTransform,
24
+ onAfterTransform,
25
+ // AMD update callbacks.
26
+ onBeforeUpdate,
27
+ onAfterUpdate
22
28
  } = {}) {
23
29
  // const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
24
30
  let ast = parseModule(text, { next: true, loc: true })
25
31
 
26
- const options = { sourceFileName: file, pluginName, resolvePath, originalResolvePath, useStrict }
32
+ const options = {
33
+ sourceFileName: file,
34
+ pluginName,
35
+ resolvePath,
36
+ originalResolvePath,
37
+ useStrict,
38
+ onBeforeTransform,
39
+ onAfterTransform,
40
+ onBeforeUpdate,
41
+ onAfterUpdate
42
+ }
27
43
  const { updated } = transformAst(ast, options)
28
44
 
29
45
  let code, map
@@ -153,6 +153,8 @@ export function detectDefinesOrRequires(program) {
153
153
 
154
154
  // Updates dependency paths to be prefixed by `esm!` or otherwise updated.
155
155
  export function updateAmdDeps(amd, options) {
156
+ options.onBeforeUpdate?.(amd)
157
+
156
158
  const { deps } = amd
157
159
  if (!deps) return
158
160
 
@@ -172,5 +174,8 @@ export function updateAmdDeps(amd, options) {
172
174
  }
173
175
  }
174
176
  }
177
+
178
+ updated ||= options.onAfterUpdate?.(amd)
179
+
175
180
  return updated
176
181
  }
@@ -82,6 +82,11 @@ export function detectImportsAndExports(program) {
82
82
 
83
83
  // Transforms the module format from ESM to AMD.
84
84
  export function transformEsmToAmd(program, options) {
85
+ options.onBeforeTransform?.({
86
+ ...options,
87
+ program
88
+ })
89
+
85
90
  const { body } = program
86
91
  let { length } = body
87
92
 
@@ -343,6 +348,12 @@ export function transformEsmToAmd(program, options) {
343
348
 
344
349
  buildAmdModule(program, options, importPaths, importVars, namedImports)
345
350
 
351
+ options.onAfterTransform?.({
352
+ ...options,
353
+ program,
354
+ callbackBody: body
355
+ })
356
+
346
357
  function addExportStatement({ exported, local }) {
347
358
  const asName = exported.name
348
359
  if (asName !== 'default') {
@@ -9,7 +9,7 @@ export function transformAst(program, options = {}) {
9
9
  result.amd = true
10
10
  if (options.resolvePath) {
11
11
  for (const amd of amds) {
12
- result.updated |= updateAmdDeps(amd, options)
12
+ result.updated ||= updateAmdDeps(amd, options)
13
13
  }
14
14
  }
15
15
  } else {