1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389# Moduloze: ESM Conversion Guide
This guide builds [on the Overview Example from the top-level conversion guide](conversion-guide.md#overview-example).
## Conversion Overview Example: ESM
`./src/test.js`:
```js
var Whatever = require("./src/whatever.js");
var { Something } = require("./src/something.js");
var anotherVal = require("./src/another.js").another();
module.exports.whatever = Whatever();
Object.assign(module.exports,{
Something,
Another: anotherVal,
});
```
### Converted Module
```js
import Whatever from "./src/whatever.mjs";
import { Something } from "./src/something.js";
import _imp from "./src/another.mjs";
let anotherVal = _imp();
let _exp = Whatever();
export { _exp as whatever };
let _exp2 = {};
Object.assign(_exp2, {
Something,
Another: anotherVal
});
export default _exp2;
```
Let's break that conversion down.
#### Converted `import`s
By default, `var Whatever = require(..)` becomes `import Whatever from ".."`. If you set the [`namespaceImport` configuration](README.md#configuration-settings) to `true`, it would have been `import * as Whatever from ".."`. The difference is, do you want Moduloze to assume that your modules always export a single default export (so use a default `import ..` form), or that your modules always export one or more named exports (so use a namespace `import * as ..` form, to collect all named imports under the single namespace).
You should typically design all your modules to follow one or the other of those two strategies, and not mix-n-match, and then set the configuration flag to use the appropriate `import` form. Otherwise, the `import` forms Moduloze produces may very well not work as expected. Moduloze *does not* analyze the export patterns to automatically select the appropriate `import` form to use.
The import form `var { Something } = require(..)` -- or alternately, `var xyz = require(..).Something` -- signals a single named import: `import { Something } from ".."`.
If `require(..)` otherwise shows up as part of some expression, such as `require(..).another()`, then the module is first default imported (or namespace imported, depending on the `namespaceImport` config) to bind to an auto-generated identifier (`_imp`, in this case), and then the rest of the expression is computed with that result (`var anotherVal = _imp()`).
#### Converted `export`s
The named export `module.exports.whatever = ..` is an expression, which can't be directly exported. So first the computation of the export is assigned to an auto-generated variable (`_exp` in this case), and then exported by name with `export { _exp as whatever }`.
When `module.exports` shows up in any other expression besides a direct or named assignment, the results are actually computed first and assigned to an auto-generated variable (`_exp2` in this case), and then exported. The assumption is that any such operations performed against `module.exports` (via the intermediary `_exp2`) should have their final computed result exported as a default export (`export default _exp2`).
## Other Conversion Variations
Let's also explore a variety of other forms of import and export conversion.
### Import Forms
```js
require("..");
// converts to:
import "..";
```
```js
var x = require("..");
// converts to:
import x from "..";
// or, if "importNamespace" config is set:
import * as x from "..";
```
```js
// var x = ..
x = require("..");
// converts to:
import { default as _imp } from "..";
x = _imp;
// or, if "importNamespace" config is set:
import * as _imp from "..";
x = _imp;
```
```js
var x = require("..").something;
// converts to:
import { something as x } from "..";
```
```js
var x = require("..").default;
// converts to:
import x from "..";
```
```js
// var x = ..
x = require("..").something;
// converts to:
import { something as _imp } from "..";
x = _imp;
```
```js
// var x = ..
x = require("..").default;
// converts to:
import _imp from "..";
x = _imp;
```
```js
var { something } = require("..");
// converts to:
import { something } from "..";
```
```js
var { something: x } = require("..");
// converts to:
import { something as x } from "..";
```
```js
// var something = ..
({ something } = require(".."));
// converts to:
import { something as _imp } from "..";
something = _imp;
```
```js
// var x = ..
({ something: x } = require(".."));
// converts to:
import { something as imp } from "..";
x = _imp;
```
```js
var x = require("..").something(42);
// converts to:
import _imp from "..";
var x = _imp.something(42);
// or, if "namespaceImport" config is set:
import * as _imp from "..";
var x = _imp.something(42);
```
```js
// var x = ..
x = require("..").something(42);
// converts to:
import _imp from "..";
x = _imp.something(42);
// or, if "namespaceImport" config is set:
import * as _imp from "..";
x = _imp.something(42);
```
```js
var x = require("..")(42);
// converts to:
import _imp from "..";
var x = _imp(42);
// or, if "namespaceImport" config is set:
import * as _imp from "..";
var x = _imp(42);
```
```js
// var x = ..
x = require("..")(42);
// converts to:
import _imp from "..";
x = _imp(42);
// or, if "namespaceImport" config is set:
import * as _imp from "..";
x = _imp(42);
```
```js
something( require("..") );
// converts to:
import _imp from "..";
something( _imp );
// or, if "namespaceImport" config is set:
import * as _imp from "..";
something( _imp );
```
### Export Forms
In all the following forms, `module.exports` is recognized the same as just `exports`, so they're interchangeable in the conversion.
ESM modules can only have a single "default export" (like `export default ..` or `export { something as default }`), so Moduloze will throw an exception if more than one export conversion results in such a statement.
```js
module.exports = something;
// converts to:
export default something;
```
```js
module.exports = 42;
// converts to:
export default 42;
```
```js
module.exports = something(42);
// converts to:
export default something(42);
```
```js
module.exports = function something() { .. };
// converts to:
export default function something() { .. };
```
```js
module.exports.x = something;
// converts to:
export { something as x };
```
```js
module.exports.x = something.y;
// converts to:
export var { y: x } = something;
```
```js
module.exports.x = 42;
// converts to:
let _exp = 42;
export { _exp as x };
```
```js
module.exports.x = function something() { .. };
// converts to:
let _exp = function something() { .. };
export { _exp as x };
```
```js
Object.assign(module.exports,{
something() { .. },
x: 42
});
// converts to:
let _exp = {};
Object.assign(_exp,{
something() { .. },
x: 42
});
export default _exp;
```
```js
something(module.exports);
// converts to:
let _exp = {};
something(_exp);
export default _exp;
```
### Import + Export Forms
The following are recognized combined forms that are effectively import and re-export in the same statement. In some cases, this can take advantage of the `export .. from ..` combined form.
```js
module.exports = require("..");
// converts to:
import _imp from "..";
export default _imp;
// or, if "namespaceImport" config is set:
import * as _imp from "..";
export default _imp;
```
```js
module.exports = require("..").x;
// converts to:
export { x as default } from "..";
```
```js
module.exports.something = require("..").x;
// converts to:
export { x as something } from "..";
```
```js
module.exports.x = require("..");
// converts to:
import _imp from "..";
export { _imp as x };
// or, if "namespaceImport" config is set:
import * as _imp from "..";
export { _imp as x };
```