@parcel/packager-js 2.0.0-nightly.92 → 2.0.1
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/lib/CJSOutputFormat.js +45 -0
- package/lib/DevPackager.js +228 -0
- package/lib/ESMOutputFormat.js +117 -0
- package/lib/GlobalOutputFormat.js +24 -0
- package/lib/ScopeHoistingPackager.js +985 -0
- package/lib/{prelude.js → dev-prelude.js} +19 -10
- package/lib/helpers.js +81 -0
- package/lib/index.js +149 -0
- package/lib/utils.js +62 -0
- package/package.json +17 -10
- package/src/.eslintrc.json +13 -0
- package/src/CJSOutputFormat.js +42 -0
- package/src/DevPackager.js +230 -0
- package/src/ESMOutputFormat.js +111 -0
- package/src/GlobalOutputFormat.js +24 -0
- package/src/ScopeHoistingPackager.js +1157 -0
- package/src/{prelude.js → dev-prelude.js} +19 -10
- package/src/helpers.js +75 -0
- package/src/index.js +102 -0
- package/src/utils.js +57 -0
- package/lib/JSPackager.js +0 -174
- package/src/JSPackager.js +0 -190
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {
|
|
3
|
+
ScopeHoistingPackager,
|
|
4
|
+
OutputFormat,
|
|
5
|
+
} from './ScopeHoistingPackager';
|
|
6
|
+
|
|
7
|
+
export class ESMOutputFormat implements OutputFormat {
|
|
8
|
+
packager: ScopeHoistingPackager;
|
|
9
|
+
|
|
10
|
+
constructor(packager: ScopeHoistingPackager) {
|
|
11
|
+
this.packager = packager;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
buildBundlePrelude(): [string, number] {
|
|
15
|
+
let res = '';
|
|
16
|
+
let lines = 0;
|
|
17
|
+
for (let [source, specifiers] of this.packager.externals) {
|
|
18
|
+
let defaultSpecifier = null;
|
|
19
|
+
let namespaceSpecifier = null;
|
|
20
|
+
let namedSpecifiers = [];
|
|
21
|
+
for (let [imported, symbol] of specifiers) {
|
|
22
|
+
if (imported === 'default' /* || isCommonJS*/) {
|
|
23
|
+
defaultSpecifier = symbol;
|
|
24
|
+
} else if (imported === '*') {
|
|
25
|
+
namespaceSpecifier = `* as ${symbol}`;
|
|
26
|
+
} else {
|
|
27
|
+
let specifier = imported;
|
|
28
|
+
if (symbol !== imported) {
|
|
29
|
+
specifier += ` as ${symbol}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
namedSpecifiers.push(specifier);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
|
|
37
|
+
|
|
38
|
+
let imported = '';
|
|
39
|
+
if (namespaceSpecifier) {
|
|
40
|
+
let s = namespaceSpecifier;
|
|
41
|
+
if (defaultSpecifier) {
|
|
42
|
+
s = `${defaultSpecifier}, ${namespaceSpecifier}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
res += `import ${s} from ${JSON.stringify(source)};\n`;
|
|
46
|
+
lines++;
|
|
47
|
+
} else if (defaultSpecifier) {
|
|
48
|
+
imported = defaultSpecifier;
|
|
49
|
+
if (namedSpecifiers.length > 0) {
|
|
50
|
+
imported += `, {${namedSpecifiers.join(', ')}}`;
|
|
51
|
+
}
|
|
52
|
+
} else if (namedSpecifiers.length > 0) {
|
|
53
|
+
imported = `{${namedSpecifiers.join(', ')}}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (imported.length > 0) {
|
|
57
|
+
res += `import ${imported} from ${JSON.stringify(source)};\n`;
|
|
58
|
+
lines++;
|
|
59
|
+
} else if (!namespaceSpecifier) {
|
|
60
|
+
res += `import ${JSON.stringify(source)};\n`;
|
|
61
|
+
lines++;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (res.length > 0) {
|
|
66
|
+
res += '\n';
|
|
67
|
+
lines++;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return [res, lines];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
buildBundlePostlude(): [string, number] {
|
|
74
|
+
let res = '';
|
|
75
|
+
let lines = 0;
|
|
76
|
+
let exportSpecifiers = [];
|
|
77
|
+
for (let {
|
|
78
|
+
asset,
|
|
79
|
+
exportSymbol,
|
|
80
|
+
local,
|
|
81
|
+
exportAs,
|
|
82
|
+
} of this.packager.exportedSymbols.values()) {
|
|
83
|
+
if (this.packager.wrappedAssets.has(asset.id)) {
|
|
84
|
+
let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(
|
|
85
|
+
asset,
|
|
86
|
+
)}")`;
|
|
87
|
+
res += `\nvar ${local} = ${this.packager.getPropertyAccess(
|
|
88
|
+
obj,
|
|
89
|
+
exportSymbol,
|
|
90
|
+
)};`;
|
|
91
|
+
lines++;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (let as of exportAs) {
|
|
95
|
+
let specifier = local;
|
|
96
|
+
if (exportAs !== local) {
|
|
97
|
+
specifier += ` as ${as}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
exportSpecifiers.push(specifier);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (exportSpecifiers.length > 0) {
|
|
105
|
+
res += `\nexport {${exportSpecifiers.join(', ')}};`;
|
|
106
|
+
lines++;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return [res, lines];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {
|
|
3
|
+
ScopeHoistingPackager,
|
|
4
|
+
OutputFormat,
|
|
5
|
+
} from './ScopeHoistingPackager';
|
|
6
|
+
|
|
7
|
+
export class GlobalOutputFormat implements OutputFormat {
|
|
8
|
+
packager: ScopeHoistingPackager;
|
|
9
|
+
|
|
10
|
+
constructor(packager: ScopeHoistingPackager) {
|
|
11
|
+
this.packager = packager;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
buildBundlePrelude(): [string, number] {
|
|
15
|
+
let prelude = this.packager.bundle.env.supports('arrow-functions', true)
|
|
16
|
+
? '(() => {\n'
|
|
17
|
+
: '(function () {\n';
|
|
18
|
+
return [prelude, 1];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
buildBundlePostlude(): [string, number] {
|
|
22
|
+
return ['})();', 0];
|
|
23
|
+
}
|
|
24
|
+
}
|