@parcel/packager-js 2.8.3 → 2.9.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/lib/CJSOutputFormat.js +0 -9
- package/lib/DevPackager.js +7 -52
- package/lib/ESMOutputFormat.js +3 -26
- package/lib/GlobalOutputFormat.js +0 -5
- package/lib/ScopeHoistingPackager.js +120 -250
- package/lib/helpers.js +0 -2
- package/lib/index.js +2 -31
- package/lib/utils.js +0 -11
- package/package.json +7 -7
- package/src/ScopeHoistingPackager.js +9 -10
package/lib/CJSOutputFormat.js
CHANGED
|
@@ -4,21 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.CJSOutputFormat = void 0;
|
|
7
|
-
|
|
8
7
|
class CJSOutputFormat {
|
|
9
8
|
constructor(packager) {
|
|
10
9
|
this.packager = packager;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
buildBundlePrelude() {
|
|
14
12
|
let res = '';
|
|
15
13
|
let lines = 0;
|
|
16
|
-
|
|
17
14
|
for (let [source, specifiers] of this.packager.externals) {
|
|
18
15
|
// CJS only supports the namespace symbol. This ensures that all accesses
|
|
19
16
|
// are live and the `this` binding is correct.
|
|
20
17
|
let namespace = specifiers.get('*');
|
|
21
|
-
|
|
22
18
|
if (namespace) {
|
|
23
19
|
res += `var ${namespace} = require(${JSON.stringify(source)});\n`;
|
|
24
20
|
lines++;
|
|
@@ -27,19 +23,14 @@ class CJSOutputFormat {
|
|
|
27
23
|
lines++;
|
|
28
24
|
}
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
if (res.length > 0) {
|
|
32
27
|
res += '\n';
|
|
33
28
|
lines++;
|
|
34
29
|
}
|
|
35
|
-
|
|
36
30
|
return [res, lines];
|
|
37
31
|
}
|
|
38
|
-
|
|
39
32
|
buildBundlePostlude() {
|
|
40
33
|
return ['', 0];
|
|
41
34
|
}
|
|
42
|
-
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
exports.CJSOutputFormat = CJSOutputFormat;
|
package/lib/DevPackager.js
CHANGED
|
@@ -4,63 +4,44 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.DevPackager = void 0;
|
|
7
|
-
|
|
8
7
|
function _utils() {
|
|
9
8
|
const data = require("@parcel/utils");
|
|
10
|
-
|
|
11
9
|
_utils = function () {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
function _sourceMap() {
|
|
19
15
|
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
20
|
-
|
|
21
16
|
_sourceMap = function () {
|
|
22
17
|
return data;
|
|
23
18
|
};
|
|
24
|
-
|
|
25
19
|
return data;
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
function _assert() {
|
|
29
22
|
const data = _interopRequireDefault(require("assert"));
|
|
30
|
-
|
|
31
23
|
_assert = function () {
|
|
32
24
|
return data;
|
|
33
25
|
};
|
|
34
|
-
|
|
35
26
|
return data;
|
|
36
27
|
}
|
|
37
|
-
|
|
38
28
|
function _path() {
|
|
39
29
|
const data = _interopRequireDefault(require("path"));
|
|
40
|
-
|
|
41
30
|
_path = function () {
|
|
42
31
|
return data;
|
|
43
32
|
};
|
|
44
|
-
|
|
45
33
|
return data;
|
|
46
34
|
}
|
|
47
|
-
|
|
48
35
|
function _fs() {
|
|
49
36
|
const data = _interopRequireDefault(require("fs"));
|
|
50
|
-
|
|
51
37
|
_fs = function () {
|
|
52
38
|
return data;
|
|
53
39
|
};
|
|
54
|
-
|
|
55
40
|
return data;
|
|
56
41
|
}
|
|
57
|
-
|
|
58
42
|
var _utils2 = require("./utils");
|
|
59
|
-
|
|
60
43
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
61
|
-
|
|
62
44
|
const PRELUDE = _fs().default.readFileSync(_path().default.join(__dirname, 'dev-prelude.js'), 'utf8').trim().replace(/;$/, '');
|
|
63
|
-
|
|
64
45
|
class DevPackager {
|
|
65
46
|
constructor(options, bundleGraph, bundle, parcelRequireName) {
|
|
66
47
|
this.options = options;
|
|
@@ -68,7 +49,6 @@ class DevPackager {
|
|
|
68
49
|
this.bundle = bundle;
|
|
69
50
|
this.parcelRequireName = parcelRequireName;
|
|
70
51
|
}
|
|
71
|
-
|
|
72
52
|
async package() {
|
|
73
53
|
// Load assets
|
|
74
54
|
let queue = new (_utils().PromiseQueue)({
|
|
@@ -93,10 +73,8 @@ class DevPackager {
|
|
|
93
73
|
let script = null;
|
|
94
74
|
this.bundle.traverse(node => {
|
|
95
75
|
let wrapped = first ? '' : ',';
|
|
96
|
-
|
|
97
76
|
if (node.type === 'dependency') {
|
|
98
77
|
let resolved = this.bundleGraph.getResolvedAsset(node.value, this.bundle);
|
|
99
|
-
|
|
100
78
|
if (resolved && resolved.type !== 'js') {
|
|
101
79
|
// if this is a reference to another javascript asset, we should not include
|
|
102
80
|
// its output, as its contents should already be loaded.
|
|
@@ -106,23 +84,20 @@ class DevPackager {
|
|
|
106
84
|
return;
|
|
107
85
|
}
|
|
108
86
|
}
|
|
109
|
-
|
|
110
87
|
if (node.type === 'asset') {
|
|
111
88
|
let asset = node.value;
|
|
112
|
-
(0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
|
|
113
|
-
// outside the bundle wrapper function so that its variables are exposed as globals.
|
|
89
|
+
(0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
|
|
114
90
|
|
|
91
|
+
// If this is the main entry of a script rather than a module, we need to hoist it
|
|
92
|
+
// outside the bundle wrapper function so that its variables are exposed as globals.
|
|
115
93
|
if (this.bundle.env.sourceType === 'script' && asset === this.bundle.getMainEntry()) {
|
|
116
94
|
script = results[i++];
|
|
117
95
|
return;
|
|
118
96
|
}
|
|
119
|
-
|
|
120
97
|
let deps = {};
|
|
121
98
|
let dependencies = this.bundleGraph.getDependencies(asset);
|
|
122
|
-
|
|
123
99
|
for (let dep of dependencies) {
|
|
124
100
|
let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
|
|
125
|
-
|
|
126
101
|
if (this.bundleGraph.isDependencySkipped(dep)) {
|
|
127
102
|
deps[(0, _utils2.getSpecifier)(dep)] = false;
|
|
128
103
|
} else if (resolved) {
|
|
@@ -132,7 +107,6 @@ class DevPackager {
|
|
|
132
107
|
deps[(0, _utils2.getSpecifier)(dep)] = dep.specifier;
|
|
133
108
|
}
|
|
134
109
|
}
|
|
135
|
-
|
|
136
110
|
let {
|
|
137
111
|
code,
|
|
138
112
|
mapBuffer
|
|
@@ -141,97 +115,78 @@ class DevPackager {
|
|
|
141
115
|
wrapped += JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) + ':[function(require,module,exports) {\n' + output + '\n},';
|
|
142
116
|
wrapped += JSON.stringify(deps);
|
|
143
117
|
wrapped += ']';
|
|
144
|
-
|
|
145
118
|
if (this.bundle.env.isNode() && asset.meta.has_node_replacements === true) {
|
|
146
119
|
const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
|
|
147
120
|
wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
|
|
148
121
|
wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
|
|
149
122
|
}
|
|
150
|
-
|
|
151
123
|
if (this.bundle.env.sourceMap) {
|
|
152
124
|
if (mapBuffer) {
|
|
153
125
|
map.addBuffer(mapBuffer, lineOffset);
|
|
154
126
|
} else {
|
|
155
127
|
map.addEmptyMap(_path().default.relative(this.options.projectRoot, asset.filePath).replace(/\\+/g, '/'), output, lineOffset);
|
|
156
128
|
}
|
|
157
|
-
|
|
158
129
|
lineOffset += (0, _utils().countLines)(output) + 1;
|
|
159
130
|
}
|
|
160
|
-
|
|
161
131
|
i++;
|
|
162
132
|
}
|
|
163
|
-
|
|
164
133
|
assets += wrapped;
|
|
165
134
|
first = false;
|
|
166
135
|
});
|
|
167
136
|
let entries = this.bundle.getEntryAssets();
|
|
168
137
|
let mainEntry = this.bundle.getMainEntry();
|
|
169
|
-
|
|
170
138
|
if (!this.isEntry() && this.bundle.env.outputFormat === 'global' || this.bundle.env.sourceType === 'script') {
|
|
171
139
|
// In async bundles we don't want the main entry to execute until we require it
|
|
172
140
|
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
173
141
|
entries = entries.filter(a => {
|
|
174
142
|
var _mainEntry;
|
|
175
|
-
|
|
176
143
|
return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
|
|
177
144
|
});
|
|
178
145
|
mainEntry = null;
|
|
179
146
|
}
|
|
147
|
+
let contents = prefix + '({' + assets + '},' + JSON.stringify(entries.map(asset => this.bundleGraph.getAssetPublicId(asset))) + ', ' + JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) + ', ' + JSON.stringify(this.parcelRequireName) + ')' + '\n';
|
|
180
148
|
|
|
181
|
-
|
|
149
|
+
// The entry asset of a script bundle gets hoisted outside the bundle wrapper function
|
|
182
150
|
// so that its variables become globals. We need to replace any require calls for
|
|
183
151
|
// runtimes with a parcelRequire call.
|
|
184
|
-
|
|
185
152
|
if (this.bundle.env.sourceType === 'script' && script) {
|
|
186
153
|
let entryMap;
|
|
187
154
|
let mapBuffer = script.mapBuffer;
|
|
188
|
-
|
|
189
155
|
if (mapBuffer) {
|
|
190
156
|
entryMap = new (_sourceMap().default)(this.options.projectRoot, mapBuffer);
|
|
191
157
|
}
|
|
192
|
-
|
|
193
158
|
contents += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, script.code, entryMap, this.parcelRequireName);
|
|
194
|
-
|
|
195
159
|
if (this.bundle.env.sourceMap && entryMap) {
|
|
196
160
|
map.addSourceMap(entryMap, lineOffset);
|
|
197
161
|
}
|
|
198
162
|
}
|
|
199
|
-
|
|
200
163
|
return {
|
|
201
164
|
contents,
|
|
202
165
|
map
|
|
203
166
|
};
|
|
204
167
|
}
|
|
205
|
-
|
|
206
168
|
getPrefix() {
|
|
207
169
|
let interpreter;
|
|
208
170
|
let mainEntry = this.bundle.getMainEntry();
|
|
209
|
-
|
|
210
171
|
if (mainEntry && this.isEntry() && !this.bundle.target.env.isBrowser()) {
|
|
211
172
|
let _interpreter = mainEntry.meta.interpreter;
|
|
212
173
|
(0, _assert().default)(_interpreter == null || typeof _interpreter === 'string');
|
|
213
174
|
interpreter = _interpreter;
|
|
214
175
|
}
|
|
215
|
-
|
|
216
176
|
let importScripts = '';
|
|
217
|
-
|
|
218
177
|
if (this.bundle.env.isWorker()) {
|
|
219
178
|
let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
|
|
220
|
-
|
|
221
179
|
for (let b of bundles) {
|
|
222
180
|
importScripts += `importScripts("${(0, _utils().relativeBundlePath)(this.bundle, b)}");\n`;
|
|
223
181
|
}
|
|
224
182
|
}
|
|
225
|
-
|
|
226
|
-
|
|
183
|
+
return (
|
|
184
|
+
// If the entry asset included a hashbang, repeat it at the top of the bundle
|
|
227
185
|
(interpreter != null ? `#!${interpreter}\n` : '') + importScripts + PRELUDE
|
|
228
186
|
);
|
|
229
187
|
}
|
|
230
|
-
|
|
231
188
|
isEntry() {
|
|
232
189
|
return !this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated';
|
|
233
190
|
}
|
|
234
|
-
|
|
235
191
|
}
|
|
236
|
-
|
|
237
192
|
exports.DevPackager = DevPackager;
|
package/lib/ESMOutputFormat.js
CHANGED
|
@@ -4,61 +4,49 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.ESMOutputFormat = void 0;
|
|
7
|
-
|
|
8
7
|
class ESMOutputFormat {
|
|
9
8
|
constructor(packager) {
|
|
10
9
|
this.packager = packager;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
buildBundlePrelude() {
|
|
14
12
|
let res = '';
|
|
15
13
|
let lines = 0;
|
|
16
|
-
|
|
17
14
|
for (let [source, specifiers] of this.packager.externals) {
|
|
18
15
|
let defaultSpecifier = null;
|
|
19
16
|
let namespaceSpecifier = null;
|
|
20
17
|
let namedSpecifiers = [];
|
|
21
|
-
|
|
22
18
|
for (let [imported, symbol] of specifiers) {
|
|
23
|
-
if (imported === 'default'
|
|
24
|
-
/* || isCommonJS*/
|
|
25
|
-
) {
|
|
19
|
+
if (imported === 'default' /* || isCommonJS*/) {
|
|
26
20
|
defaultSpecifier = symbol;
|
|
27
21
|
} else if (imported === '*') {
|
|
28
22
|
namespaceSpecifier = `* as ${symbol}`;
|
|
29
23
|
} else {
|
|
30
24
|
let specifier = imported;
|
|
31
|
-
|
|
32
25
|
if (symbol !== imported) {
|
|
33
26
|
specifier += ` as ${symbol}`;
|
|
34
27
|
}
|
|
35
|
-
|
|
36
28
|
namedSpecifiers.push(specifier);
|
|
37
29
|
}
|
|
38
|
-
}
|
|
30
|
+
}
|
|
39
31
|
|
|
32
|
+
// ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
|
|
40
33
|
|
|
41
34
|
let imported = '';
|
|
42
|
-
|
|
43
35
|
if (namespaceSpecifier) {
|
|
44
36
|
let s = namespaceSpecifier;
|
|
45
|
-
|
|
46
37
|
if (defaultSpecifier) {
|
|
47
38
|
s = `${defaultSpecifier}, ${namespaceSpecifier}`;
|
|
48
39
|
}
|
|
49
|
-
|
|
50
40
|
res += `import ${s} from ${JSON.stringify(source)};\n`;
|
|
51
41
|
lines++;
|
|
52
42
|
} else if (defaultSpecifier) {
|
|
53
43
|
imported = defaultSpecifier;
|
|
54
|
-
|
|
55
44
|
if (namedSpecifiers.length > 0) {
|
|
56
45
|
imported += `, {${namedSpecifiers.join(', ')}}`;
|
|
57
46
|
}
|
|
58
47
|
} else if (namedSpecifiers.length > 0) {
|
|
59
48
|
imported = `{${namedSpecifiers.join(', ')}}`;
|
|
60
49
|
}
|
|
61
|
-
|
|
62
50
|
if (imported.length > 0) {
|
|
63
51
|
res += `import ${imported} from ${JSON.stringify(source)};\n`;
|
|
64
52
|
lines++;
|
|
@@ -67,20 +55,16 @@ class ESMOutputFormat {
|
|
|
67
55
|
lines++;
|
|
68
56
|
}
|
|
69
57
|
}
|
|
70
|
-
|
|
71
58
|
if (res.length > 0) {
|
|
72
59
|
res += '\n';
|
|
73
60
|
lines++;
|
|
74
61
|
}
|
|
75
|
-
|
|
76
62
|
return [res, lines];
|
|
77
63
|
}
|
|
78
|
-
|
|
79
64
|
buildBundlePostlude() {
|
|
80
65
|
let res = '';
|
|
81
66
|
let lines = 0;
|
|
82
67
|
let exportSpecifiers = [];
|
|
83
|
-
|
|
84
68
|
for (let {
|
|
85
69
|
asset,
|
|
86
70
|
exportSymbol,
|
|
@@ -92,26 +76,19 @@ class ESMOutputFormat {
|
|
|
92
76
|
res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
|
|
93
77
|
lines++;
|
|
94
78
|
}
|
|
95
|
-
|
|
96
79
|
for (let as of exportAs) {
|
|
97
80
|
let specifier = local;
|
|
98
|
-
|
|
99
81
|
if (exportAs !== local) {
|
|
100
82
|
specifier += ` as ${as}`;
|
|
101
83
|
}
|
|
102
|
-
|
|
103
84
|
exportSpecifiers.push(specifier);
|
|
104
85
|
}
|
|
105
86
|
}
|
|
106
|
-
|
|
107
87
|
if (exportSpecifiers.length > 0) {
|
|
108
88
|
res += `\nexport {${exportSpecifiers.join(', ')}};`;
|
|
109
89
|
lines++;
|
|
110
90
|
}
|
|
111
|
-
|
|
112
91
|
return [res, lines];
|
|
113
92
|
}
|
|
114
|
-
|
|
115
93
|
}
|
|
116
|
-
|
|
117
94
|
exports.ESMOutputFormat = ESMOutputFormat;
|
|
@@ -4,21 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.GlobalOutputFormat = void 0;
|
|
7
|
-
|
|
8
7
|
class GlobalOutputFormat {
|
|
9
8
|
constructor(packager) {
|
|
10
9
|
this.packager = packager;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
buildBundlePrelude() {
|
|
14
12
|
let prelude = this.packager.bundle.env.supports('arrow-functions', true) ? '(() => {\n' : '(function () {\n';
|
|
15
13
|
return [prelude, 1];
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
buildBundlePostlude() {
|
|
19
16
|
return ['})();', 0];
|
|
20
17
|
}
|
|
21
|
-
|
|
22
18
|
}
|
|
23
|
-
|
|
24
19
|
exports.GlobalOutputFormat = GlobalOutputFormat;
|