requirejs-esm 2.1.0 → 2.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": "2.1.0",
3
+ "version": "2.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",
@@ -84,9 +84,9 @@
84
84
  },
85
85
  "dependencies": {
86
86
  "@prantlf/convert-source-map": "^2.0.0",
87
- "astring": "^1.8.1",
87
+ "astring": "^1.8.3",
88
88
  "charcodes": "^0.2.0",
89
- "commander": "^9.1.0",
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": "^21.0.3",
97
+ "@rollup/plugin-commonjs": "^22.0.0",
98
98
  "@rollup/plugin-json": "^4.1.0",
99
- "@rollup/plugin-node-resolve": "^13.1.3",
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.0",
103
- "eslint": "^8.13.0",
102
+ "c8": "^7.11.3",
103
+ "eslint": "^8.17.0",
104
104
  "lit-html": "^1",
105
- "rollup": "^2.70.1",
106
- "tehanu": "^0.2.2",
107
- "tehanu-repo-coco": "^0.0.2",
108
- "tehanu-teru": "^0.2.2",
109
- "terser": "^5.12.1"
105
+ "rollup": "^2.75.6",
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,27 @@ 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
+ }[]
24
+
25
+ declare function detectImportsAndExports(ast: object): {
26
+ imports: {
27
+ node: object
28
+ source: string
29
+ local?: string
30
+ specifiers?: { imported: string, local: string }[]
31
+ export?: true
32
+ }[]
33
+ exports: {
34
+ node: object,
35
+ default?: true
36
+ import?: true
37
+ }[]
38
+ }
package/src/api.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { resolvePath } from './resolve-path'
2
2
  export { default as transform } from './transform'
3
+ export { transformAst, detectDefinesOrRequires, detectImportsAndExports } from './transformer'
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 transformModules from './transformer'
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
- transformModules(ast, options)
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) {
@@ -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
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,16 @@ function detectDefineOrRequireCall(expr) {
35
37
  if (length <= ++index) return false
36
38
  arg = args[index]
37
39
  }
38
- return (arg.type === 'FunctionExpression' || arg.type === 'ObjectExpression') &&
39
- { deps: deps }
40
+ return (arg.type === 'FunctionExpression' ||
41
+ arg.type === 'ArrowFunctionExpression' ||
42
+ arg.type === 'ObjectExpression') && { namespace, func, name, deps }
40
43
  }
41
44
 
42
45
  // require([deps], success, error)
43
46
  if (func.name === 'require') {
44
47
  const deps = args[0]
45
48
  return deps.type === 'ArrayExpression' && length >= 2 &&
46
- args[1].type === 'FunctionExpression' && { deps: deps }
49
+ args[1].type === 'FunctionExpression' && { func, deps }
47
50
  }
48
51
  }
49
52
 
@@ -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
@@ -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 default function transformModules(program, options) {
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
- options.amd = true
9
+ result.amd = true
9
10
  if (options.resolvePath) {
10
11
  for (const amd of amds) {
11
- options.updated |= updateAmdDeps(amd, options)
12
+ result.updated |= updateAmdDeps(amd, options)
12
13
  }
13
14
  }
14
15
  } else {
15
16
  transformEsmToAmd(program, options)
16
- options.updated = true
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) {