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