@atlaspack/packager-js 2.23.1 → 2.23.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @atlaspack/packager-js
2
2
 
3
+ ## 2.23.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#796](https://github.com/atlassian-labs/atlaspack/pull/796) [`0e967b0`](https://github.com/atlassian-labs/atlaspack/commit/0e967b0cf0fbd885588b81476ec1f25a507ffa94) Thanks [@benjervis](https://github.com/benjervis)! - Minor refactor of the internals of the scope hoisting packager. Some of the internal objects are now keyed on Assets rather than string asset IDs.
8
+
9
+ - [#801](https://github.com/atlassian-labs/atlaspack/pull/801) [`b104fd8`](https://github.com/atlassian-labs/atlaspack/commit/b104fd846ea17c583e68ddc6b309c5e1edee6938) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Fix incorrect output with skipped assets with scope hoisting improvements
10
+
11
+ - Updated dependencies [[`0207171`](https://github.com/atlassian-labs/atlaspack/commit/0207171e59e985c51e105aec2e0a99de174374bd), [`10ee3fa`](https://github.com/atlassian-labs/atlaspack/commit/10ee3fa5b75a92acde8973673d9b3c5b6f3958e5), [`bbe4e28`](https://github.com/atlassian-labs/atlaspack/commit/bbe4e28c89e5e9284bd730075d4d6f2a3cb37b21), [`1180103`](https://github.com/atlassian-labs/atlaspack/commit/118010351ed444f8178988afb3f77807154dd933), [`2bc93b1`](https://github.com/atlassian-labs/atlaspack/commit/2bc93b17cea07fd7cbb68acec84d8471345a22b4)]:
12
+ - @atlaspack/feature-flags@2.25.2
13
+ - @atlaspack/rust@3.8.2
14
+ - @atlaspack/utils@3.0.0
15
+ - @atlaspack/plugin@2.14.32
16
+ - @atlaspack/types@2.15.22
17
+
3
18
  ## 2.23.1
4
19
 
5
20
  ### Patch Changes
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CJSOutputFormat = void 0;
4
+ class CJSOutputFormat {
5
+ constructor(packager) {
6
+ this.packager = packager;
7
+ }
8
+ buildBundlePrelude() {
9
+ let res = '';
10
+ let lines = 0;
11
+ for (let [source, specifiers] of this.packager.externals) {
12
+ // CJS only supports the namespace symbol. This ensures that all accesses
13
+ // are live and the `this` binding is correct.
14
+ let namespace = specifiers.get('*');
15
+ if (namespace) {
16
+ res += `var ${namespace} = require(${JSON.stringify(source)});\n`;
17
+ lines++;
18
+ }
19
+ else {
20
+ res += `require(${JSON.stringify(source)});\n`;
21
+ lines++;
22
+ }
23
+ }
24
+ if (res.length > 0) {
25
+ res += '\n';
26
+ lines++;
27
+ }
28
+ return [res, lines];
29
+ }
30
+ buildBundlePostlude() {
31
+ return ['', 0];
32
+ }
33
+ }
34
+ exports.CJSOutputFormat = CJSOutputFormat;
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DevPackager = void 0;
7
+ const utils_1 = require("@atlaspack/utils");
8
+ const source_map_1 = __importDefault(require("@parcel/source-map"));
9
+ const feature_flags_1 = require("@atlaspack/feature-flags");
10
+ const assert_1 = __importDefault(require("assert"));
11
+ const path_1 = __importDefault(require("path"));
12
+ const fs_1 = __importDefault(require("fs"));
13
+ const utils_2 = require("./utils");
14
+ const PRELUDE = fs_1.default
15
+ .readFileSync(path_1.default.join(__dirname, 'dev-prelude.js'), 'utf8')
16
+ .trim()
17
+ .replace(/;$/, '');
18
+ class DevPackager {
19
+ constructor(options, bundleGraph, bundle, parcelRequireName) {
20
+ this.options = options;
21
+ this.bundleGraph = bundleGraph;
22
+ this.bundle = bundle;
23
+ this.parcelRequireName = parcelRequireName;
24
+ }
25
+ async package() {
26
+ // Load assets
27
+ let queue = new utils_1.PromiseQueue({ maxConcurrent: 32 });
28
+ this.bundle.traverseAssets((asset) => {
29
+ queue.add(async () => {
30
+ let [code, mapBuffer] = await Promise.all([
31
+ asset.getCode(),
32
+ this.bundle.env.sourceMap && asset.getMapBuffer(),
33
+ ]);
34
+ return { code, mapBuffer };
35
+ });
36
+ });
37
+ let results = await queue.run();
38
+ let assets = '';
39
+ let i = 0;
40
+ let first = true;
41
+ let map = new source_map_1.default(this.options.projectRoot);
42
+ let prefix = this.getPrefix();
43
+ let lineOffset = (0, utils_1.countLines)(prefix);
44
+ let script = null;
45
+ this.bundle.traverse((node) => {
46
+ let wrapped = first ? '' : ',';
47
+ if (node.type === 'dependency') {
48
+ let resolved = this.bundleGraph.getResolvedAsset(node.value, this.bundle);
49
+ if (resolved && resolved.type !== 'js') {
50
+ // if this is a reference to another javascript asset, we should not include
51
+ // its output, as its contents should already be loaded.
52
+ (0, assert_1.default)(!this.bundle.hasAsset(resolved));
53
+ wrapped +=
54
+ JSON.stringify(this.bundleGraph.getAssetPublicId(resolved)) +
55
+ ':[function() {},{}]';
56
+ }
57
+ else {
58
+ return;
59
+ }
60
+ }
61
+ if (node.type === 'asset') {
62
+ let asset = node.value;
63
+ (0, assert_1.default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
64
+ // If this is the main entry of a script rather than a module, we need to hoist it
65
+ // outside the bundle wrapper function so that its variables are exposed as globals.
66
+ if (this.bundle.env.sourceType === 'script' &&
67
+ asset === this.bundle.getMainEntry()) {
68
+ // @ts-expect-error TS2322
69
+ script = results[i++];
70
+ return;
71
+ }
72
+ let deps = {};
73
+ let dependencies = this.bundleGraph.getDependencies(asset);
74
+ for (let dep of dependencies) {
75
+ let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
76
+ let specifier = (0, utils_2.getSpecifier)(dep);
77
+ if (this.bundleGraph.isDependencySkipped(dep)) {
78
+ deps[specifier] = false;
79
+ }
80
+ else if (resolved) {
81
+ deps[specifier] = this.bundleGraph.getAssetPublicId(resolved);
82
+ }
83
+ else {
84
+ // An external module - map placeholder to original specifier.
85
+ deps[specifier] = dep.specifier;
86
+ }
87
+ }
88
+ if ((0, feature_flags_1.getFeatureFlag)('hmrImprovements')) {
89
+ // Add dependencies for parcelRequire calls added by runtimes
90
+ // so that the HMR runtime can correctly traverse parents.
91
+ let hmrDeps = asset.meta.hmrDeps;
92
+ if (this.options.hmrOptions && Array.isArray(hmrDeps)) {
93
+ for (let id of hmrDeps) {
94
+ (0, assert_1.default)(typeof id === 'string');
95
+ deps[id] = id;
96
+ }
97
+ }
98
+ }
99
+ // @ts-expect-error TS2339
100
+ let { code, mapBuffer } = results[i];
101
+ let output = code || '';
102
+ wrapped +=
103
+ JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) +
104
+ ':[function(require,module,exports,__globalThis) {\n' +
105
+ output +
106
+ '\n},';
107
+ wrapped += JSON.stringify(deps);
108
+ wrapped += ']';
109
+ if (this.bundle.env.isNode() &&
110
+ asset.meta.has_node_replacements === true) {
111
+ const relPath = (0, utils_1.normalizeSeparators)(path_1.default.relative(this.bundle.target.distDir, path_1.default.dirname(asset.filePath)));
112
+ wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
113
+ wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
114
+ }
115
+ if (this.bundle.env.sourceMap) {
116
+ if (mapBuffer) {
117
+ map.addBuffer(mapBuffer, lineOffset);
118
+ }
119
+ else {
120
+ map.addEmptyMap(path_1.default
121
+ .relative(this.options.projectRoot, asset.filePath)
122
+ .replace(/\\+/g, '/'), output, lineOffset);
123
+ }
124
+ lineOffset += (0, utils_1.countLines)(output) + 1;
125
+ }
126
+ i++;
127
+ }
128
+ assets += wrapped;
129
+ first = false;
130
+ });
131
+ let entries = this.bundle.getEntryAssets();
132
+ let mainEntry = this.bundle.getMainEntry();
133
+ if ((!this.isEntry() && this.bundle.env.outputFormat === 'global') ||
134
+ this.bundle.env.sourceType === 'script') {
135
+ // In async bundles we don't want the main entry to execute until we require it
136
+ // as there might be dependencies in a sibling bundle that hasn't loaded yet.
137
+ entries = entries.filter((a) => a.id !== mainEntry?.id);
138
+ mainEntry = null;
139
+ }
140
+ let contents = prefix +
141
+ '({' +
142
+ assets +
143
+ '},' +
144
+ JSON.stringify(entries.map((asset) => this.bundleGraph.getAssetPublicId(asset))) +
145
+ ', ' +
146
+ JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) +
147
+ ', ' +
148
+ JSON.stringify(this.parcelRequireName) +
149
+ ')' +
150
+ '\n';
151
+ // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
152
+ // so that its variables become globals. We need to replace any require calls for
153
+ // runtimes with a parcelRequire call.
154
+ if (this.bundle.env.sourceType === 'script' && script) {
155
+ let entryMap;
156
+ // @ts-expect-error TS2339
157
+ let mapBuffer = script.mapBuffer;
158
+ if (mapBuffer) {
159
+ entryMap = new source_map_1.default(this.options.projectRoot, mapBuffer);
160
+ }
161
+ contents += (0, utils_2.replaceScriptDependencies)(this.bundleGraph, this.bundle,
162
+ // @ts-expect-error TS2339
163
+ script.code, entryMap, this.parcelRequireName);
164
+ if (this.bundle.env.sourceMap && entryMap) {
165
+ // @ts-expect-error TS2551
166
+ map.addSourceMap(entryMap, lineOffset);
167
+ }
168
+ }
169
+ return {
170
+ contents,
171
+ map,
172
+ };
173
+ }
174
+ getPrefix() {
175
+ let interpreter;
176
+ let mainEntry = this.bundle.getMainEntry();
177
+ if (mainEntry && this.isEntry() && !this.bundle.target.env.isBrowser()) {
178
+ let _interpreter = mainEntry.meta.interpreter;
179
+ (0, assert_1.default)(_interpreter == null || typeof _interpreter === 'string');
180
+ interpreter = _interpreter;
181
+ }
182
+ let importScripts = '';
183
+ if (this.bundle.env.isWorker() || this.bundle.env.isTesseract()) {
184
+ let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
185
+ for (let b of bundles) {
186
+ importScripts += `importScripts("${(0, utils_1.relativeBundlePath)(this.bundle, b)}");\n`;
187
+ }
188
+ }
189
+ return (
190
+ // If the entry asset included a hashbang, repeat it at the top of the bundle
191
+ (interpreter != null ? `#!${interpreter}\n` : '') +
192
+ importScripts +
193
+ PRELUDE);
194
+ }
195
+ isEntry() {
196
+ return (!this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') ||
197
+ this.bundle.env.isIsolated() ||
198
+ this.bundle.bundleBehavior === 'isolated' ||
199
+ this.bundle.bundleBehavior === 'inlineIsolated');
200
+ }
201
+ }
202
+ exports.DevPackager = DevPackager;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ESMOutputFormat = void 0;
4
+ const utils_1 = require("./utils");
5
+ class ESMOutputFormat {
6
+ constructor(packager) {
7
+ this.packager = packager;
8
+ }
9
+ buildBundlePrelude() {
10
+ let res = '';
11
+ let lines = 0;
12
+ for (let [source, specifiers] of this.packager.externals) {
13
+ let defaultSpecifier = null;
14
+ let namespaceSpecifier = null;
15
+ let namedSpecifiers = [];
16
+ for (let [imported, symbol] of specifiers) {
17
+ if (imported === 'default' /* || isCommonJS*/) {
18
+ defaultSpecifier = symbol;
19
+ }
20
+ else if (imported === '*') {
21
+ namespaceSpecifier = `* as ${symbol}`;
22
+ }
23
+ else {
24
+ let specifier = imported;
25
+ if (!(0, utils_1.isValidIdentifier)(specifier)) {
26
+ specifier = JSON.stringify(specifier);
27
+ }
28
+ if (symbol !== imported) {
29
+ specifier += ` as ${symbol}`;
30
+ }
31
+ namedSpecifiers.push(specifier);
32
+ }
33
+ }
34
+ // ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
35
+ let imported = '';
36
+ if (namespaceSpecifier) {
37
+ let s = namespaceSpecifier;
38
+ if (defaultSpecifier) {
39
+ s = `${defaultSpecifier}, ${namespaceSpecifier}`;
40
+ }
41
+ res += `import ${s} from ${JSON.stringify(source)};\n`;
42
+ lines++;
43
+ }
44
+ else if (defaultSpecifier) {
45
+ imported = defaultSpecifier;
46
+ if (namedSpecifiers.length > 0) {
47
+ imported += `, {${namedSpecifiers.join(', ')}}`;
48
+ }
49
+ }
50
+ else if (namedSpecifiers.length > 0) {
51
+ imported = `{${namedSpecifiers.join(', ')}}`;
52
+ }
53
+ if (imported.length > 0) {
54
+ res += `import ${imported} from ${JSON.stringify(source)};\n`;
55
+ lines++;
56
+ }
57
+ else if (!namespaceSpecifier) {
58
+ res += `import ${JSON.stringify(source)};\n`;
59
+ lines++;
60
+ }
61
+ }
62
+ if (res.length > 0) {
63
+ res += '\n';
64
+ lines++;
65
+ }
66
+ return [res, lines];
67
+ }
68
+ buildBundlePostlude() {
69
+ let res = '';
70
+ let lines = 0;
71
+ let exportSpecifiers = [];
72
+ for (let { asset, exportSymbol, local, exportAs, } of this.packager.exportedSymbols.values()) {
73
+ if (this.packager.wrappedAssets.has(asset)) {
74
+ let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
75
+ res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
76
+ lines++;
77
+ }
78
+ for (let as of exportAs) {
79
+ let specifier = local;
80
+ if (as !== local) {
81
+ if (!(0, utils_1.isValidIdentifier)(as)) {
82
+ as = JSON.stringify(as);
83
+ }
84
+ specifier += ` as ${as}`;
85
+ }
86
+ exportSpecifiers.push(specifier);
87
+ }
88
+ }
89
+ if (exportSpecifiers.length > 0) {
90
+ res += `\nexport {${exportSpecifiers.join(', ')}};`;
91
+ lines++;
92
+ }
93
+ if (this.packager.needsPrelude &&
94
+ this.packager.shouldBundleQueue(this.packager.bundle)) {
95
+ // Should be last thing the bundle executes on intial eval
96
+ res += `\n$parcel$global.rlb(${JSON.stringify(this.packager.bundle.publicId)})`;
97
+ lines++;
98
+ }
99
+ return [res, lines];
100
+ }
101
+ }
102
+ exports.ESMOutputFormat = ESMOutputFormat;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GlobalOutputFormat = void 0;
4
+ class GlobalOutputFormat {
5
+ constructor(packager) {
6
+ this.packager = packager;
7
+ }
8
+ buildBundlePrelude() {
9
+ let prelude = this.packager.bundle.env.supports('arrow-functions', true)
10
+ ? '(() => {\n'
11
+ : '(function () {\n';
12
+ return [prelude, 1];
13
+ }
14
+ buildBundlePostlude() {
15
+ return ['})();', 0];
16
+ }
17
+ }
18
+ exports.GlobalOutputFormat = GlobalOutputFormat;