requirejs-esm 2.1.0 → 2.3.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 +1 -1
- package/dist/api.js +5748 -5564
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +5673 -5564
- package/dist/plugin.js.map +1 -1
- package/package.json +12 -12
- package/src/api.d.ts +28 -0
- package/src/api.js +1 -0
- package/src/transform.js +2 -3
- package/src/transformer/amd.js +86 -8
- package/src/transformer/esm.js +72 -0
- package/src/transformer/index.js +9 -5
- package/src/transformer/keywords.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "requirejs-esm",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.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",
|
|
@@ -84,9 +84,9 @@
|
|
|
84
84
|
},
|
|
85
85
|
"dependencies": {
|
|
86
86
|
"@prantlf/convert-source-map": "^2.0.0",
|
|
87
|
-
"astring": "^1.8.
|
|
87
|
+
"astring": "^1.8.3",
|
|
88
88
|
"charcodes": "^0.2.0",
|
|
89
|
-
"commander": "^9.
|
|
89
|
+
"commander": "^9.3.0",
|
|
90
90
|
"meriyah": "^4.2.1",
|
|
91
91
|
"punycode": "^2.1.1",
|
|
92
92
|
"source-map": "^0.8.0-beta.0",
|
|
@@ -94,19 +94,19 @@
|
|
|
94
94
|
},
|
|
95
95
|
"devDependencies": {
|
|
96
96
|
"@prantlf/requirejs": "^3.0.0",
|
|
97
|
-
"@rollup/plugin-commonjs": "^
|
|
97
|
+
"@rollup/plugin-commonjs": "^22.0.1",
|
|
98
98
|
"@rollup/plugin-json": "^4.1.0",
|
|
99
|
-
"@rollup/plugin-node-resolve": "^13.
|
|
99
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
100
100
|
"@semantic-release/changelog": "^6.0.1",
|
|
101
101
|
"@semantic-release/git": "^10.0.1",
|
|
102
|
-
"c8": "^7.11.
|
|
103
|
-
"eslint": "^8.
|
|
102
|
+
"c8": "^7.11.3",
|
|
103
|
+
"eslint": "^8.19.0",
|
|
104
104
|
"lit-html": "^1",
|
|
105
|
-
"rollup": "^2.
|
|
106
|
-
"tehanu": "^0.
|
|
107
|
-
"tehanu-repo-coco": "^0.0
|
|
108
|
-
"tehanu-teru": "^0.
|
|
109
|
-
"terser": "^5.
|
|
105
|
+
"rollup": "^2.76.0",
|
|
106
|
+
"tehanu": "^1.0.1",
|
|
107
|
+
"tehanu-repo-coco": "^1.0.0",
|
|
108
|
+
"tehanu-teru": "^1.0.0",
|
|
109
|
+
"terser": "^5.14.1"
|
|
110
110
|
},
|
|
111
111
|
"keywords": [
|
|
112
112
|
"requirejs-plugin",
|
package/src/api.d.ts
CHANGED
|
@@ -12,3 +12,31 @@ type ResolvePath = ((sourcePath: string, currentFile: string, options?: ResolveO
|
|
|
12
12
|
declare function transform(contents: string, path: string, options?: {
|
|
13
13
|
pluginName?: string /*= 'esm'' */, resolvePath?: ResolvePath,
|
|
14
14
|
sourceMap?: boolean /*= true */ }): string
|
|
15
|
+
|
|
16
|
+
declare function transformAst(ast: object): { amd?: true, updated?: true }
|
|
17
|
+
|
|
18
|
+
declare function detectDefinesOrRequires(ast: object): {
|
|
19
|
+
namespace?: object
|
|
20
|
+
func: object
|
|
21
|
+
name?: object
|
|
22
|
+
deps?: object
|
|
23
|
+
params?: object[]
|
|
24
|
+
factory?: object
|
|
25
|
+
output?: object
|
|
26
|
+
body?: object
|
|
27
|
+
}[]
|
|
28
|
+
|
|
29
|
+
declare function detectImportsAndExports(ast: object): {
|
|
30
|
+
imports: {
|
|
31
|
+
node: object
|
|
32
|
+
source: string
|
|
33
|
+
local?: string
|
|
34
|
+
specifiers?: { imported: string, local: string }[]
|
|
35
|
+
export?: true
|
|
36
|
+
}[]
|
|
37
|
+
exports: {
|
|
38
|
+
node: object,
|
|
39
|
+
default?: true
|
|
40
|
+
import?: true
|
|
41
|
+
}[]
|
|
42
|
+
}
|
package/src/api.js
CHANGED
package/src/transform.js
CHANGED
|
@@ -4,7 +4,7 @@ import { parseModule } from 'meriyah'
|
|
|
4
4
|
import { generate } from 'astring'
|
|
5
5
|
import { SourceMapGenerator } from 'source-map'
|
|
6
6
|
import convert from '@prantlf/convert-source-map'
|
|
7
|
-
import
|
|
7
|
+
import { transformAst } from './transformer'
|
|
8
8
|
|
|
9
9
|
export default function transform(text, file, {
|
|
10
10
|
// Allow using a different plugin alias than `esm` in the source code.
|
|
@@ -19,9 +19,8 @@ export default function transform(text, file, {
|
|
|
19
19
|
let ast = parseModule(text, { next: true, loc: true })
|
|
20
20
|
|
|
21
21
|
const options = { sourceFileName: file, pluginName, resolvePath, originalResolvePath }
|
|
22
|
-
|
|
22
|
+
const { updated } = transformAst(ast, options)
|
|
23
23
|
|
|
24
|
-
const { updated } = options
|
|
25
24
|
let code, map
|
|
26
25
|
if (updated) {
|
|
27
26
|
if (sourceMap === true) {
|
package/src/transformer/amd.js
CHANGED
|
@@ -10,11 +10,12 @@ function detectDefineOrRequireCall(expr) {
|
|
|
10
10
|
if (length === 0) return false
|
|
11
11
|
|
|
12
12
|
const { callee } = expr
|
|
13
|
-
let func
|
|
13
|
+
let namespace, func
|
|
14
14
|
// namespace.define(...)
|
|
15
15
|
if (callee.type === 'MemberExpression') {
|
|
16
16
|
const { object } = callee
|
|
17
17
|
if (object.type !== 'Identifier') return false
|
|
18
|
+
namespace = object
|
|
18
19
|
func = callee.property
|
|
19
20
|
} else {
|
|
20
21
|
func = callee
|
|
@@ -25,9 +26,10 @@ function detectDefineOrRequireCall(expr) {
|
|
|
25
26
|
if (func.name === 'define') {
|
|
26
27
|
let index = 0
|
|
27
28
|
let arg = args[index]
|
|
28
|
-
let deps
|
|
29
|
+
let name, deps, params
|
|
29
30
|
if (arg.type === 'Literal') {
|
|
30
31
|
if (length <= ++index || typeof arg.value !== 'string') return false
|
|
32
|
+
name = arg
|
|
31
33
|
arg = args[index]
|
|
32
34
|
}
|
|
33
35
|
if (arg.type === 'ArrayExpression') {
|
|
@@ -35,15 +37,91 @@ function detectDefineOrRequireCall(expr) {
|
|
|
35
37
|
if (length <= ++index) return false
|
|
36
38
|
arg = args[index]
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
|
|
41
|
+
if (!deps) {
|
|
42
|
+
({ params, deps } = detectCjsDeps(arg) || {})
|
|
43
|
+
} else {
|
|
44
|
+
params = arg.params
|
|
45
|
+
}
|
|
46
|
+
return { namespace, func, name, deps, params, factory: arg }
|
|
47
|
+
}
|
|
48
|
+
return arg.type === 'ObjectExpression' && { namespace, func, name, deps, output: arg }
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
// require([deps], success, error)
|
|
51
|
+
// require([deps], success, [error])
|
|
43
52
|
if (func.name === 'require') {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
const arg = args[0]
|
|
54
|
+
if (arg.type === 'ArrayExpression') {
|
|
55
|
+
if (length < 2) return false
|
|
56
|
+
const body = args[1]
|
|
57
|
+
if (body.type === 'FunctionExpression' || body.type === 'ArrowFunctionExpression') {
|
|
58
|
+
return { func, deps: arg, params: body.params, body }
|
|
59
|
+
}
|
|
60
|
+
} else if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
|
|
61
|
+
const { params, deps } = detectCjsDeps(arg) || {}
|
|
62
|
+
return { func, params, deps, body: arg }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Checks if the expression parameters can support CJS-like require calls
|
|
68
|
+
// in the expression body.
|
|
69
|
+
function isCjsExpression(expr) {
|
|
70
|
+
const { params } = expr;
|
|
71
|
+
if (!params.length) return false
|
|
72
|
+
const param = params[0]
|
|
73
|
+
return param.type === 'Identifier' && param.name === 'require'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Detects variable declarations with CJS-like require calls and such require
|
|
77
|
+
// calls on the first level of the expression body.
|
|
78
|
+
function detectCjsDeps(expr) {
|
|
79
|
+
if (!isCjsExpression(expr)) return
|
|
80
|
+
const params = []
|
|
81
|
+
const namedDeps = []
|
|
82
|
+
const unnamedDeps = []
|
|
83
|
+
for (const statement of expr.body.body || []) {
|
|
84
|
+
const { type } = statement
|
|
85
|
+
if (type === 'VariableDeclaration') {
|
|
86
|
+
for (const declarator of statement.declarations) {
|
|
87
|
+
const { id } = declarator
|
|
88
|
+
if (id && id.type === 'Identifier') {
|
|
89
|
+
const { init } = declarator
|
|
90
|
+
if (init && init.type === 'CallExpression') {
|
|
91
|
+
const { callee } = init
|
|
92
|
+
if (callee.type === 'Identifier' && callee.name === 'require') {
|
|
93
|
+
const { arguments: args } = init
|
|
94
|
+
if (args.length === 1) {
|
|
95
|
+
const arg = args[0]
|
|
96
|
+
if (arg.type === 'Literal') {
|
|
97
|
+
params.push(id)
|
|
98
|
+
namedDeps.push(arg)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} else if (type === 'ExpressionStatement') {
|
|
106
|
+
const { expression } = statement
|
|
107
|
+
if (expression.type === 'CallExpression') {
|
|
108
|
+
const { callee } = expression
|
|
109
|
+
if (callee.type === 'Identifier' && callee.name === 'require') {
|
|
110
|
+
const { arguments: args } = expression
|
|
111
|
+
if (args.length === 1) {
|
|
112
|
+
const arg = args[0]
|
|
113
|
+
if (arg.type === 'Literal') {
|
|
114
|
+
unnamedDeps.push(arg)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
let deps = [...namedDeps, ...unnamedDeps]
|
|
122
|
+
if (deps.length) {
|
|
123
|
+
deps = { type: 'ArrayExpression', elements: deps }
|
|
124
|
+
return { params, deps }
|
|
47
125
|
}
|
|
48
126
|
}
|
|
49
127
|
|
package/src/transformer/esm.js
CHANGED
|
@@ -7,6 +7,78 @@ import {
|
|
|
7
7
|
import { toExpression, replaceLiteral, exportStatement } from './converters'
|
|
8
8
|
import { generateUid, generateUidIdentifier } from './generate-id'
|
|
9
9
|
|
|
10
|
+
// Detects if a program contains import statements.
|
|
11
|
+
// Returns information about the import statemets [{ node, source, specifiers|local }, ...] or [].
|
|
12
|
+
export function detectImportsAndExports(program) {
|
|
13
|
+
const { body } = program
|
|
14
|
+
const { length } = body
|
|
15
|
+
|
|
16
|
+
const imports = []
|
|
17
|
+
const exports = []
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < length; ++i) {
|
|
20
|
+
const node = body[i]
|
|
21
|
+
|
|
22
|
+
// import
|
|
23
|
+
if (node.type === 'ImportDeclaration') {
|
|
24
|
+
const { source } = node
|
|
25
|
+
|
|
26
|
+
// import "some"
|
|
27
|
+
if (isAnonymousImport(node)) {
|
|
28
|
+
imports.push({ node, source })
|
|
29
|
+
}
|
|
30
|
+
// import some from "some"
|
|
31
|
+
// import * as some from "some"
|
|
32
|
+
else if (isImportDefault(node) || isImportAllAs(node)) {
|
|
33
|
+
const { local } = node.specifiers[0]
|
|
34
|
+
imports.push({ node, source, local })
|
|
35
|
+
}
|
|
36
|
+
// import {x, y, z} from "xyz"
|
|
37
|
+
else {
|
|
38
|
+
const specifiers = node.specifiers.map(({ imported, local }) => ({ imported, local }))
|
|
39
|
+
imports.push({ node, source, specifiers })
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// export default
|
|
44
|
+
else if (node.type === 'ExportDefaultDeclaration') {
|
|
45
|
+
exports.push({ node, default: true })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// export {x as y}
|
|
49
|
+
// export var a = 1
|
|
50
|
+
// export function test() {}
|
|
51
|
+
// export class Test {}
|
|
52
|
+
else if (node.type === 'ExportNamedDeclaration') {
|
|
53
|
+
const { specifiers } = node
|
|
54
|
+
|
|
55
|
+
// export var a = 1
|
|
56
|
+
if (!specifiers.length) {
|
|
57
|
+
exports.push({ node })
|
|
58
|
+
} else { // export {x as y}
|
|
59
|
+
// export { ... } from "module"
|
|
60
|
+
const { source } = node
|
|
61
|
+
if (source) {
|
|
62
|
+
exports.push({ node, source, import: true })
|
|
63
|
+
const specifiers = node.specifiers.map(({ exported: imported, local }) => ({ imported, local }))
|
|
64
|
+
imports.push({ node, source, specifiers, export: true })
|
|
65
|
+
} else {
|
|
66
|
+
exports.push({ node })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// export * from "module"
|
|
72
|
+
if (node.type === 'ExportAllDeclaration') {
|
|
73
|
+
exports.push({ node, import: true })
|
|
74
|
+
const { source } = node
|
|
75
|
+
imports.push({ node, source, export: true })
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { imports, exports }
|
|
80
|
+
}
|
|
81
|
+
|
|
10
82
|
// Transforms the module format from ESM to AMD.
|
|
11
83
|
export function transformEsmToAmd(program, options) {
|
|
12
84
|
const { body } = program
|
package/src/transformer/index.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { detectDefinesOrRequires, updateAmdDeps } from './amd'
|
|
2
|
-
import { transformEsmToAmd } from './esm'
|
|
2
|
+
import { detectImportsAndExports, transformEsmToAmd } from './esm'
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export function transformAst(program, options = {}) {
|
|
5
5
|
const amds = detectDefinesOrRequires(program)
|
|
6
6
|
const { length } = amds
|
|
7
|
+
const result = {}
|
|
7
8
|
if (length) {
|
|
8
|
-
|
|
9
|
+
result.amd = true
|
|
9
10
|
if (options.resolvePath) {
|
|
10
11
|
for (const amd of amds) {
|
|
11
|
-
|
|
12
|
+
result.updated |= updateAmdDeps(amd, options)
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
} else {
|
|
15
16
|
transformEsmToAmd(program, options)
|
|
16
|
-
|
|
17
|
+
result.updated = true
|
|
17
18
|
}
|
|
19
|
+
return result
|
|
18
20
|
}
|
|
21
|
+
|
|
22
|
+
export { detectDefinesOrRequires, detectImportsAndExports }
|
|
@@ -54,7 +54,7 @@ const reservedWordsStrictSet = new Set(reservedWords.strict)
|
|
|
54
54
|
const reservedWordsStrictBindSet = new Set(reservedWords.strictBind)
|
|
55
55
|
|
|
56
56
|
export function isReservedWord(word, inModule) {
|
|
57
|
-
return inModule && word === 'await' || word === 'enum'
|
|
57
|
+
return inModule && word === 'await' || word === 'enum'
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export function isStrictReservedWord(word, inModule) {
|