@atlaspack/packager-js 2.25.9 → 2.25.11-dev-fix-sourcemaps-50acc1acf.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaspack/packager-js
2
2
 
3
+ ## 2.25.10
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1107](https://github.com/atlassian-labs/atlaspack/pull/1107) [`6fde8ea`](https://github.com/atlassian-labs/atlaspack/commit/6fde8ea2e32d79b7f99008ca6645128663de09aa) Thanks [@at-nathan](https://github.com/at-nathan)! - Fix scope hoisting source map for CSS modules
8
+
3
9
  ## 2.25.9
4
10
 
5
11
  ### Patch Changes
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2024 Atlassian US., Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.ScopeHoistingPackager = void 0;
40
+ exports.appendHoistedValues = appendHoistedValues;
40
41
  const utils_1 = require("@atlaspack/utils");
41
42
  const source_map_1 = __importDefault(require("@atlaspack/source-map"));
42
43
  const nullthrows_1 = __importDefault(require("nullthrows"));
@@ -57,6 +58,25 @@ const utils_2 = require("./utils");
57
58
  const REPLACEMENT_RE_CONDITIONAL = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
58
59
  const REPLACEMENT_RE = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
59
60
  const BUILTINS = Object.keys(globals_1.default.builtin);
61
+ /**
62
+ * Joins filtered hoisted parcelRequire values into the bundle output,
63
+ * returning the text to append and the number of newlines it adds.
64
+ *
65
+ * Returns `{text: '', lineCount: 0}` when there are no values, to avoid
66
+ * emitting a stray leading `\n` (which would over-count lineCount and
67
+ * desynchronise the bundle source-map offset bookkeeping).
68
+ *
69
+ * Exported for direct unit testing of the lineCount bookkeeping.
70
+ */
71
+ function appendHoistedValues(values) {
72
+ if (values.length === 0) {
73
+ return { text: '', lineCount: 0 };
74
+ }
75
+ return {
76
+ text: '\n' + values.join('\n'),
77
+ lineCount: values.length,
78
+ };
79
+ }
60
80
  const GLOBALS_BY_CONTEXT = {
61
81
  browser: new Set([...BUILTINS, ...Object.keys(globals_1.default.browser)]),
62
82
  'web-worker': new Set([...BUILTINS, ...Object.keys(globals_1.default.worker)]),
@@ -136,10 +156,14 @@ class ScopeHoistingPackager {
136
156
  this.seenHoistedRequires.clear();
137
157
  let [content, map, lines] = this.visitAsset(asset);
138
158
  if (sourceMap && map) {
159
+ this.addAssetBoundaryMarker(sourceMap, asset, lineCount);
139
160
  sourceMap.addSourceMap(map, lineCount);
140
161
  }
141
162
  else if (this.bundle.env.sourceMap) {
142
163
  sourceMap = map;
164
+ if (sourceMap) {
165
+ this.addAssetBoundaryMarker(sourceMap, asset, lineCount);
166
+ }
143
167
  }
144
168
  res += content + '\n';
145
169
  lineCount += lines + 1;
@@ -240,6 +264,7 @@ class ScopeHoistingPackager {
240
264
  }
241
265
  res += (0, utils_2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
242
266
  if (sourceMap && map) {
267
+ this.addAssetBoundaryMarker(sourceMap, mainEntry, lineCount);
243
268
  // @ts-expect-error TS2339 - addSourceMap method exists but missing from @parcel/source-map type definitions
244
269
  sourceMap.addSourceMap(map, lineCount);
245
270
  }
@@ -516,6 +541,30 @@ class ScopeHoistingPackager {
516
541
  let { code, map } = (0, nullthrows_1.default)(this.assetOutputs.get(asset));
517
542
  return this.buildAsset(asset, code, map);
518
543
  }
544
+ /**
545
+ * Insert a single "boundary marker" mapping at the start of the line where
546
+ * `asset`'s source map is about to be appended. Without this marker, source
547
+ * map consumers (and downstream optimizers such as swc that need to compose
548
+ * via nearest-neighbour lookups) will attribute the columns at the start of
549
+ * this asset's region to the *previous* asset's last mapping, because there
550
+ * is no mapping anchoring the start of the new asset. This is especially
551
+ * visible for assets with sparse maps (e.g. codegen'd `.graphql.ts` files
552
+ * from Relay) whose first mapping may be hundreds of columns into the line.
553
+ *
554
+ * The marker points at `(asset.filePath, 1, 0)`. Any mapping the asset's
555
+ * own map provides at the same generated position will override this one
556
+ * (last-write-wins inside `parcel_sourcemap`).
557
+ */
558
+ addAssetBoundaryMarker(sourceMap, asset, lineOffset) {
559
+ if (!sourceMap)
560
+ return;
561
+ let source = this.getAssetFilePath(asset);
562
+ sourceMap.addIndexedMapping({
563
+ generated: { line: lineOffset + 1, column: 0 },
564
+ original: { line: 1, column: 0 },
565
+ source,
566
+ });
567
+ }
519
568
  getAssetFilePath(asset) {
520
569
  return path_1.default.relative(this.options.projectRoot, asset.filePath);
521
570
  }
@@ -525,6 +574,13 @@ class ScopeHoistingPackager {
525
574
  let sourceMap = this.bundle.env.sourceMap && map
526
575
  ? new source_map_1.default(this.options.projectRoot, map)
527
576
  : null;
577
+ // Anchor the start of this asset's region with a boundary marker so the
578
+ // first columns of the asset don't fall back to a previous asset's
579
+ // mapping during downstream source-map composition. See the
580
+ // addAssetBoundaryMarker JSDoc for the full rationale.
581
+ if (sourceMap) {
582
+ this.addAssetBoundaryMarker(sourceMap, asset, 0);
583
+ }
528
584
  // If this asset is skipped, just add dependencies and not the asset's content.
529
585
  if (this.shouldSkipAsset(asset)) {
530
586
  let depCode = '';
@@ -558,6 +614,7 @@ class ScopeHoistingPackager {
558
614
  let [code, map, lines] = this.visitAsset(resolved);
559
615
  depCode += code + '\n';
560
616
  if (sourceMap && map) {
617
+ this.addAssetBoundaryMarker(sourceMap, resolved, lineCount);
561
618
  sourceMap.addSourceMap(map, lineCount);
562
619
  }
563
620
  lineCount += lines + 1;
@@ -656,7 +713,13 @@ class ScopeHoistingPackager {
656
713
  }
657
714
  else {
658
715
  if (shouldWrap) {
659
- depContent.push(this.visitAsset(resolved));
716
+ let visited = this.visitAsset(resolved);
717
+ depContent.push([
718
+ visited[0],
719
+ visited[1],
720
+ visited[2],
721
+ resolved,
722
+ ]);
660
723
  }
661
724
  else {
662
725
  let [depCode, depMap, depLines] = this.visitAsset(resolved);
@@ -680,6 +743,7 @@ class ScopeHoistingPackager {
680
743
  sourceMap.offsetLines(lineCount + 1, lines);
681
744
  }
682
745
  if (map) {
746
+ this.addAssetBoundaryMarker(sourceMap, resolved, lineCount);
683
747
  sourceMap.addSourceMap(map, lineCount);
684
748
  }
685
749
  }
@@ -719,11 +783,14 @@ ${code}
719
783
  code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
720
784
  lineCount += 1;
721
785
  }
722
- for (let [depCode, map, lines] of depContent) {
786
+ for (let [depCode, map, lines, depAsset] of depContent) {
723
787
  if (!depCode)
724
788
  continue;
725
789
  code += depCode + '\n';
726
790
  if (sourceMap && map) {
791
+ if (depAsset) {
792
+ this.addAssetBoundaryMarker(sourceMap, depAsset, lineCount);
793
+ }
727
794
  sourceMap.addSourceMap(map, lineCount);
728
795
  }
729
796
  lineCount += lines + 1;
@@ -1089,8 +1156,13 @@ ${code}
1089
1156
  for (let val of hoistedValues) {
1090
1157
  this.seenHoistedRequires.add(val);
1091
1158
  }
1092
- res += '\n' + hoistedValues.join('\n');
1093
- lineCount += hoisted.size;
1159
+ // Only emit the leading `\n` and bump the line count when we
1160
+ // actually have values to write. Otherwise `res += '\n' + ''` would
1161
+ // emit a stray blank line and the line count would over-count by
1162
+ // `hoisted.size` (which includes the now-filtered entries).
1163
+ let appended = appendHoistedValues(hoistedValues);
1164
+ res += appended.text;
1165
+ lineCount += appended.lineCount;
1094
1166
  }
1095
1167
  else {
1096
1168
  res += '\n' + [...hoisted.values()].join('\n');
@@ -1268,7 +1340,7 @@ ${code}
1268
1340
  : '';
1269
1341
  prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(exp)}, ${get}${set});\n`;
1270
1342
  this.usedHelpers.add('$parcel$export');
1271
- prependLineCount += 1 + usedExports.length;
1343
+ prependLineCount += 1;
1272
1344
  }
1273
1345
  }
1274
1346
  }
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ScopeHoistingPackager = void 0;
7
+ exports.appendHoistedValues = appendHoistedValues;
7
8
  function _utils() {
8
9
  const data = require("@atlaspack/utils");
9
10
  _utils = function () {
@@ -80,6 +81,29 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
80
81
  const REPLACEMENT_RE_CONDITIONAL = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
81
82
  const REPLACEMENT_RE = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
82
83
  const BUILTINS = Object.keys(_globals().default.builtin);
84
+
85
+ /**
86
+ * Joins filtered hoisted parcelRequire values into the bundle output,
87
+ * returning the text to append and the number of newlines it adds.
88
+ *
89
+ * Returns `{text: '', lineCount: 0}` when there are no values, to avoid
90
+ * emitting a stray leading `\n` (which would over-count lineCount and
91
+ * desynchronise the bundle source-map offset bookkeeping).
92
+ *
93
+ * Exported for direct unit testing of the lineCount bookkeeping.
94
+ */
95
+ function appendHoistedValues(values) {
96
+ if (values.length === 0) {
97
+ return {
98
+ text: '',
99
+ lineCount: 0
100
+ };
101
+ }
102
+ return {
103
+ text: '\n' + values.join('\n'),
104
+ lineCount: values.length
105
+ };
106
+ }
83
107
  const GLOBALS_BY_CONTEXT = {
84
108
  browser: new Set([...BUILTINS, ...Object.keys(_globals().default.browser)]),
85
109
  'web-worker': new Set([...BUILTINS, ...Object.keys(_globals().default.worker)]),
@@ -147,9 +171,13 @@ class ScopeHoistingPackager {
147
171
  this.seenHoistedRequires.clear();
148
172
  let [content, map, lines] = this.visitAsset(asset);
149
173
  if (sourceMap && map) {
174
+ this.addAssetBoundaryMarker(sourceMap, asset, lineCount);
150
175
  sourceMap.addSourceMap(map, lineCount);
151
176
  } else if (this.bundle.env.sourceMap) {
152
177
  sourceMap = map;
178
+ if (sourceMap) {
179
+ this.addAssetBoundaryMarker(sourceMap, asset, lineCount);
180
+ }
153
181
  }
154
182
  res += content + '\n';
155
183
  lineCount += lines + 1;
@@ -257,6 +285,7 @@ class ScopeHoistingPackager {
257
285
  }
258
286
  res += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
259
287
  if (sourceMap && map) {
288
+ this.addAssetBoundaryMarker(sourceMap, mainEntry, lineCount);
260
289
  // @ts-expect-error TS2339 - addSourceMap method exists but missing from @parcel/source-map type definitions
261
290
  sourceMap.addSourceMap(map, lineCount);
262
291
  }
@@ -513,6 +542,36 @@ class ScopeHoistingPackager {
513
542
  } = (0, _nullthrows().default)(this.assetOutputs.get(asset));
514
543
  return this.buildAsset(asset, code, map);
515
544
  }
545
+
546
+ /**
547
+ * Insert a single "boundary marker" mapping at the start of the line where
548
+ * `asset`'s source map is about to be appended. Without this marker, source
549
+ * map consumers (and downstream optimizers such as swc that need to compose
550
+ * via nearest-neighbour lookups) will attribute the columns at the start of
551
+ * this asset's region to the *previous* asset's last mapping, because there
552
+ * is no mapping anchoring the start of the new asset. This is especially
553
+ * visible for assets with sparse maps (e.g. codegen'd `.graphql.ts` files
554
+ * from Relay) whose first mapping may be hundreds of columns into the line.
555
+ *
556
+ * The marker points at `(asset.filePath, 1, 0)`. Any mapping the asset's
557
+ * own map provides at the same generated position will override this one
558
+ * (last-write-wins inside `parcel_sourcemap`).
559
+ */
560
+ addAssetBoundaryMarker(sourceMap, asset, lineOffset) {
561
+ if (!sourceMap) return;
562
+ let source = this.getAssetFilePath(asset);
563
+ sourceMap.addIndexedMapping({
564
+ generated: {
565
+ line: lineOffset + 1,
566
+ column: 0
567
+ },
568
+ original: {
569
+ line: 1,
570
+ column: 0
571
+ },
572
+ source
573
+ });
574
+ }
516
575
  getAssetFilePath(asset) {
517
576
  return _path().default.relative(this.options.projectRoot, asset.filePath);
518
577
  }
@@ -520,6 +579,13 @@ class ScopeHoistingPackager {
520
579
  let shouldWrap = this.wrappedAssets.has(asset);
521
580
  let deps = this.bundleGraph.getDependencies(asset);
522
581
  let sourceMap = this.bundle.env.sourceMap && map ? new (_sourceMap2().default)(this.options.projectRoot, map) : null;
582
+ // Anchor the start of this asset's region with a boundary marker so the
583
+ // first columns of the asset don't fall back to a previous asset's
584
+ // mapping during downstream source-map composition. See the
585
+ // addAssetBoundaryMarker JSDoc for the full rationale.
586
+ if (sourceMap) {
587
+ this.addAssetBoundaryMarker(sourceMap, asset, 0);
588
+ }
523
589
 
524
590
  // If this asset is skipped, just add dependencies and not the asset's content.
525
591
  if (this.shouldSkipAsset(asset)) {
@@ -553,6 +619,7 @@ class ScopeHoistingPackager {
553
619
  let [code, map, lines] = this.visitAsset(resolved);
554
620
  depCode += code + '\n';
555
621
  if (sourceMap && map) {
622
+ this.addAssetBoundaryMarker(sourceMap, resolved, lineCount);
556
623
  sourceMap.addSourceMap(map, lineCount);
557
624
  }
558
625
  lineCount += lines + 1;
@@ -647,7 +714,8 @@ class ScopeHoistingPackager {
647
714
  }
648
715
  } else {
649
716
  if (shouldWrap) {
650
- depContent.push(this.visitAsset(resolved));
717
+ let visited = this.visitAsset(resolved);
718
+ depContent.push([visited[0], visited[1], visited[2], resolved]);
651
719
  } else {
652
720
  let [depCode, depMap, depLines] = this.visitAsset(resolved);
653
721
  res = depCode + '\n' + res;
@@ -671,6 +739,7 @@ class ScopeHoistingPackager {
671
739
  sourceMap.offsetLines(lineCount + 1, lines);
672
740
  }
673
741
  if (map) {
742
+ this.addAssetBoundaryMarker(sourceMap, resolved, lineCount);
674
743
  sourceMap.addSourceMap(map, lineCount);
675
744
  }
676
745
  }
@@ -712,10 +781,13 @@ ${code}
712
781
  code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
713
782
  lineCount += 1;
714
783
  }
715
- for (let [depCode, map, lines] of depContent) {
784
+ for (let [depCode, map, lines, depAsset] of depContent) {
716
785
  if (!depCode) continue;
717
786
  code += depCode + '\n';
718
787
  if (sourceMap && map) {
788
+ if (depAsset) {
789
+ this.addAssetBoundaryMarker(sourceMap, depAsset, lineCount);
790
+ }
719
791
  sourceMap.addSourceMap(map, lineCount);
720
792
  }
721
793
  lineCount += lines + 1;
@@ -1034,8 +1106,14 @@ ${code}
1034
1106
  for (let val of hoistedValues) {
1035
1107
  this.seenHoistedRequires.add(val);
1036
1108
  }
1037
- res += '\n' + hoistedValues.join('\n');
1038
- lineCount += hoisted.size;
1109
+
1110
+ // Only emit the leading `\n` and bump the line count when we
1111
+ // actually have values to write. Otherwise `res += '\n' + ''` would
1112
+ // emit a stray blank line and the line count would over-count by
1113
+ // `hoisted.size` (which includes the now-filtered entries).
1114
+ let appended = appendHoistedValues(hoistedValues);
1115
+ res += appended.text;
1116
+ lineCount += appended.lineCount;
1039
1117
  } else {
1040
1118
  res += '\n' + [...hoisted.values()].join('\n');
1041
1119
  lineCount += hoisted.size;
@@ -1192,7 +1270,7 @@ ${code}
1192
1270
  let set = !isEsmExport && asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`) : '';
1193
1271
  prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(exp)}, ${get}${set});\n`;
1194
1272
  this.usedHelpers.add('$parcel$export');
1195
- prependLineCount += 1 + usedExports.length;
1273
+ prependLineCount += 1;
1196
1274
  }
1197
1275
  }
1198
1276
  }
@@ -1,5 +1,19 @@
1
1
  import type { Asset, BundleGraph, Dependency, PluginOptions, NamedBundle, PluginLogger } from '@atlaspack/types';
2
2
  import SourceMap from '@atlaspack/source-map';
3
+ /**
4
+ * Joins filtered hoisted parcelRequire values into the bundle output,
5
+ * returning the text to append and the number of newlines it adds.
6
+ *
7
+ * Returns `{text: '', lineCount: 0}` when there are no values, to avoid
8
+ * emitting a stray leading `\n` (which would over-count lineCount and
9
+ * desynchronise the bundle source-map offset bookkeeping).
10
+ *
11
+ * Exported for direct unit testing of the lineCount bookkeeping.
12
+ */
13
+ export declare function appendHoistedValues(values: ReadonlyArray<string>): {
14
+ text: string;
15
+ lineCount: number;
16
+ };
3
17
  export interface OutputFormat {
4
18
  buildBundlePrelude(): [string, number];
5
19
  buildBundlePostlude(): [string, number];
@@ -56,6 +70,21 @@ export declare class ScopeHoistingPackager {
56
70
  getTopLevelName(name: string): string;
57
71
  getPropertyAccess(obj: string, property: string): string;
58
72
  visitAsset(asset: Asset): [string, SourceMap | null | undefined, number];
73
+ /**
74
+ * Insert a single "boundary marker" mapping at the start of the line where
75
+ * `asset`'s source map is about to be appended. Without this marker, source
76
+ * map consumers (and downstream optimizers such as swc that need to compose
77
+ * via nearest-neighbour lookups) will attribute the columns at the start of
78
+ * this asset's region to the *previous* asset's last mapping, because there
79
+ * is no mapping anchoring the start of the new asset. This is especially
80
+ * visible for assets with sparse maps (e.g. codegen'd `.graphql.ts` files
81
+ * from Relay) whose first mapping may be hundreds of columns into the line.
82
+ *
83
+ * The marker points at `(asset.filePath, 1, 0)`. Any mapping the asset's
84
+ * own map provides at the same generated position will override this one
85
+ * (last-write-wins inside `parcel_sourcemap`).
86
+ */
87
+ addAssetBoundaryMarker(sourceMap: SourceMap, asset: Asset, lineOffset: number): void;
59
88
  getAssetFilePath(asset: Asset): string;
60
89
  buildAsset(asset: Asset, code: string, map?: Buffer | null): [string, SourceMap | null | undefined, number];
61
90
  buildReplacements(asset: Asset, deps: Array<Dependency>): [Map<string, Array<Dependency>>, Map<string, string>];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/packager-js",
3
- "version": "2.25.9",
3
+ "version": "2.25.11-dev-fix-sourcemaps-50acc1acf.0",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -16,20 +16,23 @@
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/diagnostic": "2.14.4",
20
- "@atlaspack/feature-flags": "2.31.3",
21
- "@atlaspack/logger": "2.14.55",
22
- "@atlaspack/plugin": "2.14.63",
23
- "@atlaspack/rust": "3.29.2",
24
- "@atlaspack/source-map": "3.3.7",
25
- "@atlaspack/types": "2.15.53",
26
- "@atlaspack/utils": "3.4.5",
19
+ "@atlaspack/diagnostic": "2.14.5-dev-fix-sourcemaps-50acc1acf.0",
20
+ "@atlaspack/feature-flags": "2.31.4-dev-fix-sourcemaps-50acc1acf.0",
21
+ "@atlaspack/logger": "2.14.56-dev-fix-sourcemaps-50acc1acf.0",
22
+ "@atlaspack/plugin": "2.14.64-dev-fix-sourcemaps-50acc1acf.0",
23
+ "@atlaspack/rust": "3.29.3-dev-fix-sourcemaps-50acc1acf.0",
24
+ "@atlaspack/source-map": "3.3.8-dev-fix-sourcemaps-50acc1acf.0",
25
+ "@atlaspack/types": "2.15.54-dev-fix-sourcemaps-50acc1acf.0",
26
+ "@atlaspack/utils": "3.4.6-dev-fix-sourcemaps-50acc1acf.0",
27
27
  "globals": "^15.15.0",
28
28
  "nullthrows": "^1.1.1",
29
29
  "outdent": "^0.7.1"
30
30
  },
31
31
  "type": "commonjs",
32
32
  "scripts": {
33
+ "test": "mocha",
34
+ "test-ci": "mocha",
33
35
  "build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
34
- }
36
+ },
37
+ "gitHead": "50acc1acf31d5c8415a8154841a8ed09264e665a"
35
38
  }