@parcel/packager-js 2.0.0-nightly.151 → 2.0.0-nightly.1514
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 +36 -0
- package/lib/DevPackager.js +192 -0
- package/lib/ESMOutputFormat.js +99 -0
- package/lib/GlobalOutputFormat.js +19 -0
- package/lib/ScopeHoistingPackager.js +990 -0
- package/lib/{prelude.js → dev-prelude.js} +23 -12
- package/lib/helpers.js +129 -0
- package/lib/index.js +146 -0
- package/lib/utils.js +51 -0
- package/package.json +18 -10
- package/src/.eslintrc.json +13 -0
- package/src/CJSOutputFormat.js +42 -0
- package/src/DevPackager.js +252 -0
- package/src/ESMOutputFormat.js +122 -0
- package/src/GlobalOutputFormat.js +24 -0
- package/src/ScopeHoistingPackager.js +1349 -0
- package/src/{prelude.js → dev-prelude.js} +23 -12
- package/src/helpers.js +169 -0
- package/src/index.js +145 -0
- package/src/utils.js +57 -0
- package/lib/JSPackager.js +0 -174
- package/src/JSPackager.js +0 -190
package/src/JSPackager.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
|
-
|
|
3
|
-
import invariant from 'assert';
|
|
4
|
-
import {Packager} from '@parcel/plugin';
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import {concat, link, generate} from '@parcel/scope-hoisting';
|
|
7
|
-
import SourceMap from '@parcel/source-map';
|
|
8
|
-
import {
|
|
9
|
-
countLines,
|
|
10
|
-
PromiseQueue,
|
|
11
|
-
relativeBundlePath,
|
|
12
|
-
replaceInlineReferences,
|
|
13
|
-
} from '@parcel/utils';
|
|
14
|
-
import path from 'path';
|
|
15
|
-
|
|
16
|
-
const PRELUDE = fs
|
|
17
|
-
.readFileSync(path.join(__dirname, 'prelude.js'), 'utf8')
|
|
18
|
-
.trim()
|
|
19
|
-
.replace(/;$/, '');
|
|
20
|
-
|
|
21
|
-
export default new Packager({
|
|
22
|
-
async package({
|
|
23
|
-
bundle,
|
|
24
|
-
bundleGraph,
|
|
25
|
-
getInlineBundleContents,
|
|
26
|
-
getSourceMapReference,
|
|
27
|
-
options,
|
|
28
|
-
}) {
|
|
29
|
-
function replaceReferences({contents, map}) {
|
|
30
|
-
return replaceInlineReferences({
|
|
31
|
-
bundle,
|
|
32
|
-
bundleGraph,
|
|
33
|
-
contents,
|
|
34
|
-
getInlineReplacement: (dependency, inlineType, content) => ({
|
|
35
|
-
from: `"${dependency.id}"`,
|
|
36
|
-
to: inlineType === 'string' ? JSON.stringify(content) : content,
|
|
37
|
-
}),
|
|
38
|
-
getInlineBundleContents,
|
|
39
|
-
map,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// If scope hoisting is enabled, we use a different code path.
|
|
44
|
-
if (bundle.env.scopeHoist) {
|
|
45
|
-
let ast = await concat(bundle, bundleGraph);
|
|
46
|
-
ast = link({bundle, bundleGraph, ast, options});
|
|
47
|
-
return replaceReferences({
|
|
48
|
-
contents: generate(bundleGraph, bundle, ast).contents,
|
|
49
|
-
map: null,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (bundle.env.outputFormat === 'esmodule') {
|
|
54
|
-
throw new Error(
|
|
55
|
-
`esmodule output is not supported without scope hoisting.`,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// For development, we just concatenate all of the code together
|
|
60
|
-
// rather then enabling scope hoisting, which would be too slow.
|
|
61
|
-
let codeQueue = new PromiseQueue({maxConcurrent: 32});
|
|
62
|
-
let mapQueue = new PromiseQueue({maxConcurrent: 32});
|
|
63
|
-
bundle.traverse(node => {
|
|
64
|
-
if (node.type === 'asset') {
|
|
65
|
-
codeQueue.add(() => node.value.getCode());
|
|
66
|
-
mapQueue.add(() => node.value.getMap());
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
let [code, maps] = await Promise.all([codeQueue.run(), mapQueue.run()]);
|
|
71
|
-
|
|
72
|
-
let assets = '';
|
|
73
|
-
let i = 0;
|
|
74
|
-
let first = true;
|
|
75
|
-
let map = new SourceMap();
|
|
76
|
-
let lineOffset = countLines(PRELUDE);
|
|
77
|
-
|
|
78
|
-
let stubsWritten = new Set();
|
|
79
|
-
bundle.traverse(node => {
|
|
80
|
-
let wrapped = first ? '' : ',';
|
|
81
|
-
|
|
82
|
-
if (node.type === 'dependency') {
|
|
83
|
-
let resolved = bundleGraph.getDependencyResolution(node.value);
|
|
84
|
-
if (
|
|
85
|
-
resolved &&
|
|
86
|
-
resolved.type !== 'js' &&
|
|
87
|
-
!stubsWritten.has(resolved.id)
|
|
88
|
-
) {
|
|
89
|
-
// if this is a reference to another javascript asset, we should not include
|
|
90
|
-
// its output, as its contents should already be loaded.
|
|
91
|
-
invariant(!bundle.hasAsset(resolved));
|
|
92
|
-
wrapped += JSON.stringify(resolved.id) + ':[function() {},{}]';
|
|
93
|
-
} else {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (node.type === 'asset') {
|
|
99
|
-
let asset = node.value;
|
|
100
|
-
invariant(
|
|
101
|
-
asset.type === 'js',
|
|
102
|
-
'all assets in a js bundle must be js assets',
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
let deps = {};
|
|
106
|
-
let dependencies = bundleGraph.getDependencies(asset);
|
|
107
|
-
for (let dep of dependencies) {
|
|
108
|
-
let resolved = bundleGraph.getDependencyResolution(dep);
|
|
109
|
-
if (resolved) {
|
|
110
|
-
deps[dep.moduleSpecifier] = resolved.id;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let output = code[i] || '';
|
|
115
|
-
wrapped +=
|
|
116
|
-
JSON.stringify(asset.id) +
|
|
117
|
-
':[function(require,module,exports) {\n' +
|
|
118
|
-
output +
|
|
119
|
-
'\n},';
|
|
120
|
-
wrapped += JSON.stringify(deps);
|
|
121
|
-
wrapped += ']';
|
|
122
|
-
|
|
123
|
-
if (options.sourceMaps) {
|
|
124
|
-
let assetMap =
|
|
125
|
-
maps[i] ??
|
|
126
|
-
SourceMap.generateEmptyMap(
|
|
127
|
-
path
|
|
128
|
-
.relative(options.projectRoot, asset.filePath)
|
|
129
|
-
.replace(/\\+/g, '/'),
|
|
130
|
-
output,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
map.addMap(assetMap, lineOffset);
|
|
134
|
-
lineOffset += countLines(output) + 1;
|
|
135
|
-
}
|
|
136
|
-
i++;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
assets += wrapped;
|
|
140
|
-
first = false;
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
let entries = bundle.getEntryAssets();
|
|
144
|
-
let interpreter: ?string = null;
|
|
145
|
-
|
|
146
|
-
let isEntry =
|
|
147
|
-
!bundleGraph.hasParentBundleOfType(bundle, 'js') ||
|
|
148
|
-
bundle.env.isIsolated();
|
|
149
|
-
if (isEntry) {
|
|
150
|
-
let entryAsset = entries[entries.length - 1];
|
|
151
|
-
// $FlowFixMe
|
|
152
|
-
interpreter = bundle.target.env.isBrowser()
|
|
153
|
-
? null
|
|
154
|
-
: entryAsset.meta.interpreter;
|
|
155
|
-
} else if (bundle.env.outputFormat === 'global') {
|
|
156
|
-
// The last entry is the main entry, but in async bundles we don't want it to execute until we require it
|
|
157
|
-
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
158
|
-
entries.pop();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
let importScripts = '';
|
|
162
|
-
if (bundle.env.isWorker()) {
|
|
163
|
-
let bundles = bundleGraph.getSiblingBundles(bundle);
|
|
164
|
-
for (let b of bundles) {
|
|
165
|
-
importScripts += `importScripts("${relativeBundlePath(bundle, b)}");\n`;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
let sourceMapReference = await getSourceMapReference(map);
|
|
170
|
-
|
|
171
|
-
return replaceReferences({
|
|
172
|
-
contents:
|
|
173
|
-
// If the entry asset included a hashbang, repeat it at the top of the bundle
|
|
174
|
-
(interpreter != null ? `#!${interpreter}\n` : '') +
|
|
175
|
-
importScripts +
|
|
176
|
-
(PRELUDE +
|
|
177
|
-
'({' +
|
|
178
|
-
assets +
|
|
179
|
-
'},{},' +
|
|
180
|
-
JSON.stringify(entries.map(asset => asset.id)) +
|
|
181
|
-
', ' +
|
|
182
|
-
'null' +
|
|
183
|
-
')\n\n' +
|
|
184
|
-
'//# sourceMappingURL=' +
|
|
185
|
-
sourceMapReference +
|
|
186
|
-
'\n'),
|
|
187
|
-
map,
|
|
188
|
-
});
|
|
189
|
-
},
|
|
190
|
-
});
|