requirejs-esm 4.1.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.1.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",
@@ -153,17 +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
- options.onBeforeUpdate?.(amd)
156
+ let updated = options.onBeforeUpdate?.({
157
+ ...options,
158
+ amd
159
+ })
157
160
 
158
161
  const { deps } = amd
159
- if (!deps) return
162
+ if (!deps) {
163
+ afterUpdate()
164
+ return updated
165
+ }
160
166
 
161
167
  const { sourceFileName: parentName } = options
162
168
  const { elements } = deps
163
- if (!elements.length) return
169
+ if (!elements.length) {
170
+ afterUpdate()
171
+ return updated
172
+ }
164
173
 
165
174
  const { resolvePath } = options
166
- let updated
167
175
  for (const element of elements) {
168
176
  if (element.type === 'Literal') {
169
177
  const moduleName = element.value
@@ -175,7 +183,25 @@ export function updateAmdDeps(amd, options) {
175
183
  }
176
184
  }
177
185
 
178
- updated ||= options.onAfterUpdate?.(amd)
186
+ afterUpdate()
179
187
 
180
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
181
207
  }
@@ -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)