requirejs-esm 3.1.1 → 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/README.md +13 -1
- package/dist/api.js +9445 -272
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +9456 -267
- package/dist/plugin.js.map +1 -1
- package/package.json +12 -13
- package/src/api.d.ts +38 -4
- package/src/plugin.js +18 -2
- package/src/transform.js +23 -2
- package/src/transformer/amd.js +5 -0
- package/src/transformer/esm.js +24 -3
- package/src/transformer/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "requirejs-esm",
|
|
3
|
-
"version": "
|
|
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",
|
|
@@ -33,7 +33,6 @@
|
|
|
33
33
|
"src"
|
|
34
34
|
],
|
|
35
35
|
"scripts": {
|
|
36
|
-
"prepare": "npm run build && npm run demo",
|
|
37
36
|
"build": "node build",
|
|
38
37
|
"demo": "npm run demo:extern && npm run demo:local",
|
|
39
38
|
"demo:extern": "npm run optimize:extern && npm run minify:extern",
|
|
@@ -69,30 +68,30 @@
|
|
|
69
68
|
"statements": 45
|
|
70
69
|
},
|
|
71
70
|
"dependencies": {
|
|
72
|
-
"@prantlf/convert-source-map": "^
|
|
71
|
+
"@prantlf/convert-source-map": "^3.0.2",
|
|
73
72
|
"astring": "^1.9.0",
|
|
74
|
-
"charcodes": "^0.2.
|
|
75
|
-
"commander": "^
|
|
76
|
-
"meriyah": "^
|
|
73
|
+
"charcodes": "^0.2.1",
|
|
74
|
+
"commander": "^14.0.2",
|
|
75
|
+
"meriyah": "^7.0.0",
|
|
77
76
|
"punycode": "^2.3.1",
|
|
78
77
|
"source-map": "^0.8.0-beta.0",
|
|
79
78
|
"tiny-glob": "^0.2.9"
|
|
80
79
|
},
|
|
81
80
|
"devDependencies": {
|
|
82
|
-
"@eslint/js": "^9.
|
|
81
|
+
"@eslint/js": "^9.39.1",
|
|
83
82
|
"@prantlf/requirejs": "^3.3.1",
|
|
84
|
-
"@rollup/plugin-commonjs": "^
|
|
83
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
85
84
|
"@rollup/plugin-json": "^6.1.0",
|
|
86
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
85
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
87
86
|
"c8": "^10.1.3",
|
|
88
|
-
"eslint": "^9.
|
|
89
|
-
"globals": "^16.
|
|
87
|
+
"eslint": "^9.39.1",
|
|
88
|
+
"globals": "^16.5.0",
|
|
90
89
|
"lit-html": "^1",
|
|
91
|
-
"rollup": "^4.
|
|
90
|
+
"rollup": "^4.53.3",
|
|
92
91
|
"tehanu": "^1.0.1",
|
|
93
92
|
"tehanu-repo-coco": "^1.0.1",
|
|
94
93
|
"tehanu-teru": "^1.0.1",
|
|
95
|
-
"terser": "^5.
|
|
94
|
+
"terser": "^5.44.1"
|
|
96
95
|
},
|
|
97
96
|
"keywords": [
|
|
98
97
|
"requirejs-plugin",
|
package/src/api.d.ts
CHANGED
|
@@ -7,13 +7,47 @@ 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
|
|
25
|
+
|
|
26
|
+
interface TransformAstOptions {
|
|
27
|
+
pluginName?: string /*= 'esm'' */
|
|
28
|
+
resolvePath?: ResolvePath
|
|
29
|
+
useStrict?: boolean /*= true */
|
|
30
|
+
onBeforeTransform?: OnBeforeTransform
|
|
31
|
+
onAfterTransform?: OnAfterTransform
|
|
32
|
+
onBeforeUpdate?: OnBeforeUpdate
|
|
33
|
+
onAfterUpdate?: OnAfterUpdate
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface TransformOptions extends TransformAstOptions {
|
|
37
|
+
sourceMap?: boolean /*= true */
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface OnBeforeTransformOptions extends TransformAstOptions {
|
|
41
|
+
program: Record<string, unknown>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface OnAfterTransformOptions extends OnBeforeTransformOptions {
|
|
45
|
+
callbackBody: Record<string, unknown>[]
|
|
46
|
+
}
|
|
11
47
|
|
|
12
|
-
declare function transform(contents: string, path: string, options?:
|
|
13
|
-
pluginName?: string /*= 'esm'' */, resolvePath?: ResolvePath,
|
|
14
|
-
sourceMap?: boolean /*= true */ }): string
|
|
48
|
+
declare function transform(contents: string, path: string, options?: TransformOptions): string
|
|
15
49
|
|
|
16
|
-
declare function transformAst(ast: object): { amd?: true, updated?: true }
|
|
50
|
+
declare function transformAst(ast: object, options?: TransformOptions): { amd?: true, updated?: true }
|
|
17
51
|
|
|
18
52
|
declare function detectDefinesOrRequires(ast: object): {
|
|
19
53
|
namespace?: object
|
package/src/plugin.js
CHANGED
|
@@ -20,12 +20,21 @@ const {
|
|
|
20
20
|
// name with `esm!`, above all.
|
|
21
21
|
resolvePath,
|
|
22
22
|
// ecmaVersion,
|
|
23
|
+
// Do not insert `"use strict"` expression to the AMD modules. You'd set it
|
|
24
|
+
// to `false` if your bundler inserts `"use strict"` to the outer scope.
|
|
25
|
+
useStrict,
|
|
23
26
|
// Boolean or object with booleans { inline, content }.
|
|
24
27
|
sourceMap,
|
|
25
28
|
// Enable console logging.
|
|
26
29
|
verbose,
|
|
27
30
|
// Directory to save a copy of the transformed modules.
|
|
28
|
-
debugDir
|
|
31
|
+
debugDir,
|
|
32
|
+
// ESM transformation callbacks.
|
|
33
|
+
onBeforeTransform,
|
|
34
|
+
onAfterTransform,
|
|
35
|
+
// AMD update callbacks.
|
|
36
|
+
onBeforeUpdate,
|
|
37
|
+
onAfterUpdate
|
|
29
38
|
} = typeof module !== 'undefined' && module.config && module.config() || {}
|
|
30
39
|
|
|
31
40
|
const buildMap = {}
|
|
@@ -102,10 +111,17 @@ export default {
|
|
|
102
111
|
pluginName,
|
|
103
112
|
resolvePath,
|
|
104
113
|
/*ecmaVersion,*/
|
|
114
|
+
useStrict,
|
|
105
115
|
// Always produce the source maps when transpiling in the browser, otherwise
|
|
106
116
|
// the debugging would me impossible. When building and bundling, check if
|
|
107
117
|
// the source maps were enabled for the output.
|
|
108
|
-
sourceMap: sourceMap || !isBuild
|
|
118
|
+
sourceMap: sourceMap || !isBuild,
|
|
119
|
+
// ESM transformation callbacks.
|
|
120
|
+
onBeforeTransform,
|
|
121
|
+
onAfterTransform,
|
|
122
|
+
// AMD update callbacks.
|
|
123
|
+
onBeforeUpdate,
|
|
124
|
+
onAfterUpdate
|
|
109
125
|
}))
|
|
110
126
|
if (!updated) {
|
|
111
127
|
verbose && console.log('esm: retaining', name)
|
package/src/transform.js
CHANGED
|
@@ -13,12 +13,33 @@ export default function transform(text, file, {
|
|
|
13
13
|
// name with `esm!`, above all.
|
|
14
14
|
resolvePath = originalResolvePath,
|
|
15
15
|
// ecmaVersion = 2020,
|
|
16
|
-
|
|
16
|
+
// Do not insert `"use strict"` expression to the AMD modules. You'd set it
|
|
17
|
+
// to `false` if your bundler inserts `"use strict"` to the outer scope.
|
|
18
|
+
useStrict,
|
|
19
|
+
// Enable source maps, can be an object with booleans { inline, content }.
|
|
20
|
+
// If set to true, the object will be set to { inline: true, content: true }.
|
|
21
|
+
sourceMap,
|
|
22
|
+
// ESM transformation callbacks.
|
|
23
|
+
onBeforeTransform,
|
|
24
|
+
onAfterTransform,
|
|
25
|
+
// AMD update callbacks.
|
|
26
|
+
onBeforeUpdate,
|
|
27
|
+
onAfterUpdate
|
|
17
28
|
} = {}) {
|
|
18
29
|
// const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
|
|
19
30
|
let ast = parseModule(text, { next: true, loc: true })
|
|
20
31
|
|
|
21
|
-
const options = {
|
|
32
|
+
const options = {
|
|
33
|
+
sourceFileName: file,
|
|
34
|
+
pluginName,
|
|
35
|
+
resolvePath,
|
|
36
|
+
originalResolvePath,
|
|
37
|
+
useStrict,
|
|
38
|
+
onBeforeTransform,
|
|
39
|
+
onAfterTransform,
|
|
40
|
+
onBeforeUpdate,
|
|
41
|
+
onAfterUpdate
|
|
42
|
+
}
|
|
22
43
|
const { updated } = transformAst(ast, options)
|
|
23
44
|
|
|
24
45
|
let code, map
|
package/src/transformer/amd.js
CHANGED
|
@@ -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
|
}
|
package/src/transformer/esm.js
CHANGED
|
@@ -2,7 +2,8 @@ import { isAnonymousImport, isImportDefault, isImportAllAs } from './validators'
|
|
|
2
2
|
import {
|
|
3
3
|
identifier, memberExpression, objectExpression, arrayExpression, blockStatement,
|
|
4
4
|
variableDeclaration, variableDeclarator, returnStatement, expressionStatement,
|
|
5
|
-
forInStatement, assignmentExpression, callExpression, functionExpression
|
|
5
|
+
forInStatement, assignmentExpression, callExpression, functionExpression,
|
|
6
|
+
literal
|
|
6
7
|
} from './factories'
|
|
7
8
|
import { toExpression, replaceLiteral, exportStatement } from './converters'
|
|
8
9
|
import { generateUid, generateUidIdentifier } from './generate-id'
|
|
@@ -81,6 +82,11 @@ export function detectImportsAndExports(program) {
|
|
|
81
82
|
|
|
82
83
|
// Transforms the module format from ESM to AMD.
|
|
83
84
|
export function transformEsmToAmd(program, options) {
|
|
85
|
+
options.onBeforeTransform?.({
|
|
86
|
+
...options,
|
|
87
|
+
program
|
|
88
|
+
})
|
|
89
|
+
|
|
84
90
|
const { body } = program
|
|
85
91
|
let { length } = body
|
|
86
92
|
|
|
@@ -342,6 +348,12 @@ export function transformEsmToAmd(program, options) {
|
|
|
342
348
|
|
|
343
349
|
buildAmdModule(program, options, importPaths, importVars, namedImports)
|
|
344
350
|
|
|
351
|
+
options.onAfterTransform?.({
|
|
352
|
+
...options,
|
|
353
|
+
program,
|
|
354
|
+
callbackBody: body
|
|
355
|
+
})
|
|
356
|
+
|
|
345
357
|
function addExportStatement({ exported, local }) {
|
|
346
358
|
const asName = exported.name
|
|
347
359
|
if (asName !== 'default') {
|
|
@@ -385,13 +397,22 @@ function exportCopyLoop(exportsVar, importVar) {
|
|
|
385
397
|
|
|
386
398
|
// Wraps a program body of statements into an AMD module.
|
|
387
399
|
function buildAmdModule(program, options, importPaths, importVars, namedImports) {
|
|
400
|
+
const body = []
|
|
401
|
+
if (options.useStrict !== false) {
|
|
402
|
+
body.push(expressionStatement(literal('use strict')))
|
|
403
|
+
}
|
|
404
|
+
if (namedImports.length) {
|
|
405
|
+
body.push(...namedImports)
|
|
406
|
+
}
|
|
407
|
+
body.push(...program.body)
|
|
408
|
+
const bodyStatement = blockStatement(body)
|
|
388
409
|
program.body = [
|
|
389
410
|
expressionStatement(callExpression(
|
|
390
411
|
identifier('define'), importPaths.length ? [
|
|
391
412
|
prepareImportPaths(importPaths, options),
|
|
392
|
-
functionExpression(importVars,
|
|
413
|
+
functionExpression(importVars, bodyStatement)
|
|
393
414
|
] : [
|
|
394
|
-
functionExpression([],
|
|
415
|
+
functionExpression([], bodyStatement)
|
|
395
416
|
]))
|
|
396
417
|
]
|
|
397
418
|
}
|
package/src/transformer/index.js
CHANGED