-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Account for injectManifest replacements in sourcemaps #2239
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/workbox-build/src/lib/replace-and-update-source-map.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2019 Google LLC | ||
|
||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
const {SourceMapConsumer, SourceMapGenerator} = require('source-map'); | ||
|
||
/** | ||
* Adapted from https://github.com/nsams/sourcemap-aware-replace, with modern | ||
* JavaScript updates, along with additional properties copied from originalMap. | ||
* | ||
* @param {Object} options | ||
* @param {string} options.jsFilename The name for the file whose contents | ||
* correspond to originalSource. | ||
* @param {Object} options.originalMap The sourcemap for originalSource, | ||
* prior to any replacements. | ||
* @param {string} options.originalSource The source code, prior to any | ||
* replacements. | ||
* @param {string} options.replaceString A string to swap in for searchString. | ||
* @param {string} options.searchString A string in originalSource to replace. | ||
* Only the first occurrence will be replaced. | ||
* @return {{source: string, map: string}} An object containing both | ||
* originalSource with the replacement applied, and the modified originalMap. | ||
* | ||
* @private | ||
*/ | ||
async function replaceAndUpdateSourceMap({ | ||
jsFilename, | ||
originalMap, | ||
originalSource, | ||
replaceString, | ||
searchString, | ||
}) { | ||
const generator = new SourceMapGenerator({ | ||
file: jsFilename, | ||
}); | ||
|
||
const consumer = await new SourceMapConsumer(originalMap); | ||
|
||
let pos; | ||
let src = originalSource; | ||
const replacements = []; | ||
let lineNum = 0; | ||
let filePos = 0; | ||
|
||
const lines = src.split('\n'); | ||
for (let line of lines) { | ||
lineNum++; | ||
let searchPos = 0; | ||
while ((pos = line.indexOf(searchString, searchPos)) !== -1) { | ||
src = src.substring(0, filePos + pos) + replaceString + | ||
src.substring(filePos + pos + searchString.length); | ||
line = line.substring(0, pos) + replaceString + | ||
line.substring(pos + searchString.length); | ||
replacements.push({line: lineNum, column: pos}); | ||
searchPos = pos + replaceString.length; | ||
} | ||
filePos += line.length + 1; | ||
} | ||
|
||
replacements.reverse(); | ||
|
||
consumer.eachMapping((mapping) => { | ||
for (const replacement of replacements) { | ||
if (replacement.line == mapping.generatedLine && | ||
mapping.generatedColumn > replacement.column) { | ||
const offset = searchString.length - replaceString.length; | ||
mapping.generatedColumn -= offset; | ||
} | ||
} | ||
|
||
if (mapping.source) { | ||
const newMapping = { | ||
generated: { | ||
line: mapping.generatedLine, | ||
column: mapping.generatedColumn, | ||
}, | ||
original: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn, | ||
}, | ||
source: mapping.source, | ||
}; | ||
return generator.addMapping(newMapping); | ||
} | ||
|
||
return mapping; | ||
}); | ||
|
||
consumer.destroy(); | ||
|
||
const updatedSourceMap = Object.assign(JSON.parse(generator.toString()), { | ||
names: originalMap.names, | ||
sourceRoot: originalMap.sourceRoot, | ||
sources: originalMap.sources, | ||
sourcesContent: originalMap.sourcesContent, | ||
}); | ||
|
||
return { | ||
map: JSON.stringify(updatedSourceMap), | ||
source: src, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is returned without changing the URL of the source map to reflect the filename in |
||
}; | ||
} | ||
|
||
module.exports = replaceAndUpdateSourceMap; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will always keep the same source map filename, and if
swSrc
andswDest
are in the same folder, it'll overwrite the original source map file.Instead, this should just append ".map" to
swDest
.