requirejs-esm 4.0.0 → 4.2.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.2.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,15 +153,25 @@ 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
+ let updated = options.onBeforeUpdate?.({
157
+ ...options,
158
+ amd
159
+ })
160
+
156
161
  const { deps } = amd
157
- if (!deps) return
162
+ if (!deps) {
163
+ afterUpdate()
164
+ return updated
165
+ }
158
166
 
159
167
  const { sourceFileName: parentName } = options
160
168
  const { elements } = deps
161
- if (!elements.length) return
169
+ if (!elements.length) {
170
+ afterUpdate()
171
+ return updated
172
+ }
162
173
 
163
174
  const { resolvePath } = options
164
- let updated
165
175
  for (const element of elements) {
166
176
  if (element.type === 'Literal') {
167
177
  const moduleName = element.value
@@ -172,5 +182,26 @@ export function updateAmdDeps(amd, options) {
172
182
  }
173
183
  }
174
184
  }
185
+
186
+ afterUpdate()
187
+
175
188
  return updated
189
+
190
+ function afterUpdate() {
191
+ const updatedNow = options.onAfterUpdate?.({
192
+ ...options,
193
+ amd
194
+ })
195
+ updated ||= updatedNow
196
+ }
197
+ }
198
+
199
+ export function callAmdUpdateHooks(amd, options) {
200
+ options = {
201
+ ...options,
202
+ amd
203
+ }
204
+ const updatedBefore = options.onBeforeUpdate?.(options)
205
+ const updatedAfter = options.onAfterUpdate?.(options)
206
+ return updatedBefore || updatedAfter
176
207
  }
@@ -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') {
@@ -1,4 +1,4 @@
1
- import { detectDefinesOrRequires, updateAmdDeps } from './amd'
1
+ import { detectDefinesOrRequires, updateAmdDeps, callAmdUpdateHooks } from './amd'
2
2
  import { detectImportsAndExports, transformEsmToAmd } from './esm'
3
3
 
4
4
  export function transformAst(program, options = {}) {
@@ -7,10 +7,11 @@ export function transformAst(program, options = {}) {
7
7
  const result = {}
8
8
  if (length) {
9
9
  result.amd = true
10
- if (options.resolvePath) {
11
- for (const amd of amds) {
12
- result.updated |= updateAmdDeps(amd, options)
13
- }
10
+ for (const amd of amds) {
11
+ const updated = options.resolvePath
12
+ ? updateAmdDeps(amd, options)
13
+ : callAmdUpdateHooks(amd, options)
14
+ result.updated ||= updated
14
15
  }
15
16
  } else {
16
17
  transformEsmToAmd(program, options)