@buxlabs/es6-to-amd 1.1.2 → 1.2.2
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/CHANGELOG.md +5 -0
- package/README.md +1 -1
- package/package.json +5 -7
- package/src/class/Module.js +52 -59
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buxlabs/es6-to-amd",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "es6 to amd converter",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,14 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/buxlabs/es6-to-amd#readme",
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"ava": "^
|
|
29
|
-
"nyc": "^
|
|
30
|
-
"standard": "^14.3.4"
|
|
28
|
+
"ava": "^6.4.1",
|
|
29
|
+
"nyc": "^17.1.0"
|
|
31
30
|
},
|
|
32
31
|
"dependencies": {
|
|
33
|
-
"abstract-syntax-tree": "^2.
|
|
34
|
-
"pure-utilities": "^1.
|
|
35
|
-
"underscore": "^1.10.2"
|
|
32
|
+
"abstract-syntax-tree": "^2.22.0",
|
|
33
|
+
"pure-utilities": "^1.4.6"
|
|
36
34
|
},
|
|
37
35
|
"standard": {
|
|
38
36
|
"ignore": [
|
package/src/class/Module.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { unique } = require('underscore')
|
|
4
3
|
const AbstractSyntaxTree = require('abstract-syntax-tree')
|
|
5
|
-
const { identifier } = require('pure-utilities/array')
|
|
4
|
+
const { identifier, unique } = require('pure-utilities/array')
|
|
6
5
|
const { flatten } = require('pure-utilities/collection')
|
|
7
6
|
|
|
7
|
+
const {
|
|
8
|
+
BlockStatement,
|
|
9
|
+
CallExpression,
|
|
10
|
+
ExpressionStatement,
|
|
11
|
+
FunctionExpression,
|
|
12
|
+
Identifier,
|
|
13
|
+
Literal,
|
|
14
|
+
NewExpression,
|
|
15
|
+
ReturnStatement
|
|
16
|
+
} = AbstractSyntaxTree
|
|
17
|
+
|
|
8
18
|
class Module extends AbstractSyntaxTree {
|
|
9
19
|
convert () {
|
|
10
20
|
if (this.has('ImportDeclaration') || this.has('ImportExpression')) {
|
|
@@ -17,7 +27,7 @@ class Module extends AbstractSyntaxTree {
|
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
convertCodeWithImportDeclarations () {
|
|
20
|
-
|
|
30
|
+
const pairs = this.getDependencyPairs()
|
|
21
31
|
if (this.has('ImportExpression')) {
|
|
22
32
|
pairs.unshift({ element: 'require', param: 'require' })
|
|
23
33
|
this.convertImportExpressions()
|
|
@@ -34,13 +44,9 @@ class Module extends AbstractSyntaxTree {
|
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
prependUseStrictLiteral () {
|
|
37
|
-
this.prepend({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
type: 'Literal',
|
|
41
|
-
value: 'use strict'
|
|
42
|
-
}
|
|
43
|
-
})
|
|
47
|
+
this.prepend(new ExpressionStatement({
|
|
48
|
+
expression: new Literal({ value: 'use strict' })
|
|
49
|
+
}))
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
isSideEffectImportDeclaration (node) {
|
|
@@ -48,10 +54,10 @@ class Module extends AbstractSyntaxTree {
|
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
getDependencyPairs () {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
const dependencyToIdentifierMap = {}
|
|
58
|
+
const imports = this.find('ImportDeclaration')
|
|
59
|
+
const ids = unique(imports.map(item => item.name))
|
|
60
|
+
const result = flatten(imports.map(node => {
|
|
55
61
|
if (this.isSideEffectImportDeclaration(node)) {
|
|
56
62
|
return {
|
|
57
63
|
element: node.source.value
|
|
@@ -62,14 +68,14 @@ class Module extends AbstractSyntaxTree {
|
|
|
62
68
|
return this.getLocalSpecifier(node, specifier)
|
|
63
69
|
}
|
|
64
70
|
if (specifier.type === 'ImportSpecifier') {
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
let param
|
|
72
|
+
const value = node.source.value
|
|
67
73
|
if (specifier.imported.name !== specifier.local.name) {
|
|
68
74
|
return this.getLocalSpecifier(node, specifier)
|
|
69
75
|
} else if (Object.prototype.hasOwnProperty.call(dependencyToIdentifierMap, value)) {
|
|
70
76
|
param = dependencyToIdentifierMap[value]
|
|
71
77
|
} else {
|
|
72
|
-
|
|
78
|
+
const identifiers = unique(flatten(ids)).concat(Object.values(dependencyToIdentifierMap))
|
|
73
79
|
param = identifier(identifiers)
|
|
74
80
|
dependencyToIdentifierMap[value] = param
|
|
75
81
|
}
|
|
@@ -100,12 +106,12 @@ class Module extends AbstractSyntaxTree {
|
|
|
100
106
|
if (node.type !== 'ImportExpression') {
|
|
101
107
|
return node
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
const resolve = { type: 'Identifier', name: 'resolve' }
|
|
110
|
+
const reject = { type: 'Identifier', name: 'reject' }
|
|
111
|
+
const defaultIdentifier = { type: 'Identifier', name: 'default' }
|
|
112
|
+
const value = { type: 'Identifier', name: 'value' }
|
|
113
|
+
const module = { type: 'Identifier', name: 'module' }
|
|
114
|
+
const enumerable = { type: 'Identifier', name: 'enumerable' }
|
|
109
115
|
return this.getNewExpression('Promise', [
|
|
110
116
|
this.getFunctionExpression([resolve, reject], [
|
|
111
117
|
this.getCallExpression('require', [
|
|
@@ -114,17 +120,17 @@ class Module extends AbstractSyntaxTree {
|
|
|
114
120
|
this.getCallExpression('resolve', [
|
|
115
121
|
this.getConditionalExpression(
|
|
116
122
|
this.getLogicalExpression(
|
|
117
|
-
this.getBinaryExpression(this.getTypeof(module), '!==',
|
|
123
|
+
this.getBinaryExpression(this.getTypeof(module), '!==', new Literal({ value: 'object' })),
|
|
118
124
|
'||',
|
|
119
|
-
this.getBinaryExpression(
|
|
125
|
+
this.getBinaryExpression(new Literal({ value: 'default' }), 'in', module)
|
|
120
126
|
),
|
|
121
127
|
this.getObjectExpression([this.getProperty(defaultIdentifier, module)]),
|
|
122
128
|
this.getCallExpression(this.getMemberExpression('Object', 'defineProperty'), [
|
|
123
129
|
module,
|
|
124
|
-
|
|
130
|
+
new Literal({ value: 'default' }),
|
|
125
131
|
this.getObjectExpression([
|
|
126
132
|
this.getProperty(value, module),
|
|
127
|
-
this.getProperty(enumerable,
|
|
133
|
+
this.getProperty(enumerable, new Literal({ value: false }))
|
|
128
134
|
])
|
|
129
135
|
])
|
|
130
136
|
)
|
|
@@ -138,13 +144,12 @@ class Module extends AbstractSyntaxTree {
|
|
|
138
144
|
}
|
|
139
145
|
|
|
140
146
|
convertExportNamedDeclarations () {
|
|
141
|
-
|
|
147
|
+
const declarations = this.find('ExportNamedDeclaration')
|
|
142
148
|
this.convertExportNamedDeclarationToDeclaration()
|
|
143
149
|
this.remove({ type: 'ExportNamedDeclaration' })
|
|
144
|
-
this.append({
|
|
145
|
-
type: 'ReturnStatement',
|
|
150
|
+
this.append(new ReturnStatement({
|
|
146
151
|
argument: this.getObjectExpressionForDeclarations(declarations)
|
|
147
|
-
})
|
|
152
|
+
}))
|
|
148
153
|
}
|
|
149
154
|
|
|
150
155
|
convertExportNamedDeclarationToDeclaration () {
|
|
@@ -182,14 +187,12 @@ class Module extends AbstractSyntaxTree {
|
|
|
182
187
|
}
|
|
183
188
|
|
|
184
189
|
getDefine (nodes) {
|
|
185
|
-
return {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
type: 'CallExpression',
|
|
189
|
-
callee: { type: 'Identifier', name: 'define' },
|
|
190
|
+
return new ExpressionStatement({
|
|
191
|
+
expression: new CallExpression({
|
|
192
|
+
callee: new Identifier({ name: 'define' }),
|
|
190
193
|
arguments: nodes
|
|
191
|
-
}
|
|
192
|
-
}
|
|
194
|
+
})
|
|
195
|
+
})
|
|
193
196
|
}
|
|
194
197
|
|
|
195
198
|
convertExportNamedDeclarationToDefine () {
|
|
@@ -201,30 +204,24 @@ class Module extends AbstractSyntaxTree {
|
|
|
201
204
|
}
|
|
202
205
|
|
|
203
206
|
getFunctionExpression (params, body) {
|
|
204
|
-
return {
|
|
205
|
-
type: 'FunctionExpression',
|
|
207
|
+
return new FunctionExpression({
|
|
206
208
|
params: params,
|
|
207
|
-
body: {
|
|
208
|
-
|
|
209
|
-
body: body
|
|
210
|
-
}
|
|
211
|
-
}
|
|
209
|
+
body: new BlockStatement({ body })
|
|
210
|
+
})
|
|
212
211
|
}
|
|
213
212
|
|
|
214
213
|
getNewExpression (name, args) {
|
|
215
|
-
return {
|
|
216
|
-
|
|
217
|
-
callee: { type: 'Identifier', name: name },
|
|
214
|
+
return new NewExpression({
|
|
215
|
+
callee: new Identifier({ name }),
|
|
218
216
|
arguments: args
|
|
219
|
-
}
|
|
217
|
+
})
|
|
220
218
|
}
|
|
221
219
|
|
|
222
220
|
getCallExpression (name, args) {
|
|
223
|
-
return {
|
|
224
|
-
|
|
225
|
-
callee: typeof name === 'string' ? { type: 'Identifier', name: name } : name,
|
|
221
|
+
return new CallExpression({
|
|
222
|
+
callee: typeof name === 'string' ? new Identifier({ name }) : name,
|
|
226
223
|
arguments: args
|
|
227
|
-
}
|
|
224
|
+
})
|
|
228
225
|
}
|
|
229
226
|
|
|
230
227
|
getMemberExpression (object, member) {
|
|
@@ -268,10 +265,6 @@ class Module extends AbstractSyntaxTree {
|
|
|
268
265
|
}
|
|
269
266
|
}
|
|
270
267
|
|
|
271
|
-
getLiteral (value) {
|
|
272
|
-
return { type: 'Literal', value: value }
|
|
273
|
-
}
|
|
274
|
-
|
|
275
268
|
getTypeof (argument) {
|
|
276
269
|
return {
|
|
277
270
|
type: 'UnaryExpression',
|
|
@@ -357,11 +350,11 @@ class Module extends AbstractSyntaxTree {
|
|
|
357
350
|
|
|
358
351
|
wrapWithDefineWithArrayExpression (pairs) {
|
|
359
352
|
pairs = unique(pairs, item => item.element + item.param)
|
|
360
|
-
|
|
353
|
+
const elements = pairs.map(pair => pair.element)
|
|
361
354
|
.map(function (element) {
|
|
362
355
|
return { type: 'Literal', value: element }
|
|
363
356
|
})
|
|
364
|
-
|
|
357
|
+
const params = pairs.filter(pair => pair.param).map(pair => pair.param)
|
|
365
358
|
.map(function (param) {
|
|
366
359
|
return { type: 'Identifier', name: param }
|
|
367
360
|
})
|