fix(lazyBundleIdPlugin): fixed the issue with creation of orphaned '.map' files in the distribution directory #6455
+47
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Orphaned Sourcemap Files in Browser Builds
The bug is introduced between v4.27.1 and v4.38.3
(I only have these two versions locally and it is not there in v4.27.1, but it is there in v4.38.3)
Problem
When
sourceMap: trueis enabled instencil.config.ts, the build process generates orphaned.mapfiles in the browser build output directory (e.g.,/dist/my-library/). These are sourcemap files that exist without their corresponding JavaScript files.Example of Orphaned Files
While the actual JavaScript files exist with different names:
Root Cause
The issue was caused by bugs in the
lazyBundleIdPluginand file writing logic:1. Orphaned Sourcemaps After File Renaming
When
lazyBundleIdPluginrenames files frommy-component.esm.jstop-667b042f.entry.js(when hashing is enabled), Rollup has already created the sourcemap filemy-component.esm.js.mapin the bundle. The plugin wasn't deleting the old sourcemap, leaving it orphaned.2. Loader Files Not Written for Browser Builds
The
writeLazyEntry()function skips writing loader files for browser builds (returns early at line 268-270), but their sourcemaps remained in the Rollup bundle, creating orphaned files likeloader.esm.js.mapandp-xxx.system.js.map.3. Race Conditions in File Writes
The
writeLazyModule(),writeLazyEntry(), andwriteLazyChunk()functions were not properly awaiting both JS and sourcemap file writes atomically, which could lead to incomplete writes in edge cases.Solution
Changes Made
1.
src/compiler/output-targets/dist-lazy/lazy-bundleid-plugin.tsPurpose: Clean up orphaned sourcemaps during file renaming
Changes:
my-component.esm.js→p-667b042f.entry.js), it now deletes the old sourcemap file (my-component.esm.js.map) from the Rollup bundle to prevent orphaned fileswriteLazyEntry()skips writing loader files for browser builds, their sourcemaps must be removed from the bundle to prevent orphaned filesnewFileNameto a variable for clarity and to enable the orphaned sourcemap check2.
src/compiler/output-targets/dist-lazy/write-lazy-entry-module.tsPurpose: Fix race condition in file writes
Changes:
Promise.all(): Ensures both JS and sourcemap files are written atomically before the function completes, preventing potential race conditions3.
src/compiler/output-targets/dist-lazy/generate-lazy-module.tsPurpose: Fix race conditions in file writes
Changes:
writeLazyChunk(): Properly returnPromise.all()for atomic writes of JS and sourcemap fileswriteLazyEntry(): Properly returnPromise.all()for atomic writes of JS and sourcemap filesTesting
To verify the fix:
Enable sourcemaps in your
stencil.config.ts:Build the project:
Check for orphaned sourcemap files:
Expected result: No orphaned files should be reported
Impact