Skip to content
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

Fix sourcemap in projects with code splitting #113

Merged
merged 4 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/sourcemap-code-splitting/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
chunk: 1,
};
3 changes: 3 additions & 0 deletions examples/sourcemap-code-splitting/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
chunk: 2,
};
3 changes: 3 additions & 0 deletions examples/sourcemap-code-splitting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sourcemap and code splitting

Test case for sourcemap and code splitting
3 changes: 3 additions & 0 deletions examples/sourcemap-code-splitting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import('./1.js');
import('./2.js');
console.log('ok');
22 changes: 22 additions & 0 deletions examples/sourcemap-code-splitting/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var expect = require('expect');
var fs = require('fs');
var path = require('path');

var webpackVersion = Number(require('webpack/package.json').version.split('.')[0]);

module.exports.skip = function skip() {
return webpackVersion < 2;
};

module.exports.check = function check() {
var findAndStripSriHashString = function(filePath, pattern, offset) {
var fileContent = fs.readFileSync(path.join(__dirname, filePath), 'utf-8');
var string = fileContent.substring(fileContent.indexOf(pattern) + (offset || 0))
.match(/\{(.*?)\}/)[0].replace(/\\/g, '').replace(/\"/g, '');
return string;
}

var sriHashesInSource = findAndStripSriHashString('dist/index.js', 'sha256-', -10);
var sriHashesInMap = findAndStripSriHashString('dist/index.js.map', 'var sriHashes = ');
expect(sriHashesInSource.length).toEqual(sriHashesInMap.length);
};
19 changes: 19 additions & 0 deletions examples/sourcemap-code-splitting/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var SriPlugin = require('webpack-subresource-integrity');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: {
index: './index.js'
},
output: {
crossOriginLoading: 'anonymous'
},
devtool: 'source-map',
plugins: [
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: true
}),
new HtmlWebpackPlugin()
]
};
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ SubresourceIntegrityPlugin.prototype.replaceAsset = function replaceAsset(
var newAsset;
var magicMarker;
var magicMarkerPos;
var hashFuncNames = this.options.hashFuncNames;

newAsset = new ReplaceSource(assets[chunkFile]);

Array.from(hashByChunkId.entries()).forEach(function replaceMagicMarkers(idAndHash) {
magicMarker = util.makePlaceholder(idAndHash[0]);
magicMarker = util.makePlaceholder(hashFuncNames, idAndHash[0]);
magicMarkerPos = oldSource.indexOf(magicMarker);
if (magicMarkerPos >= 0) {
newAsset.replace(
Expand All @@ -189,7 +190,7 @@ SubresourceIntegrityPlugin.prototype.replaceAsset = function replaceAsset(
// eslint-disable-next-line no-param-reassign
assets[chunkFile] = newAsset;

newAsset.integrity = util.computeIntegrity(this.options.hashFuncNames, newAsset.source());
newAsset.integrity = util.computeIntegrity(hashFuncNames, newAsset.source());
return newAsset;
};

Expand Down
3 changes: 2 additions & 1 deletion jmtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
function addSriHashes(mainTemplate, source, chunk) {
var allChunks = util.findChunks(chunk);
var includedChunks = chunk.getChunkMaps().hash;
var hashFuncNames = this.sriPlugin.options.hashFuncNames;

if (allChunks.size > 0) {
return (Template.asString || mainTemplate.asString)([
Expand All @@ -29,7 +30,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
) {
if (includedChunks[depChunk.id.toString()]) {
// eslint-disable-next-line no-param-reassign
sriHashes[depChunk.id] = util.makePlaceholder(depChunk.id);
sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id);
}
return sriHashes;
},
Expand Down
5 changes: 3 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ function isRuntimeChunk(chunk) {
return "hasRuntime" in chunk ? chunk.hasRuntime() : chunk.entry;
}

function makePlaceholder(id) {
return "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
function makePlaceholder(hashFuncNames, id) {
var placeholder = "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
return computeIntegrity(hashFuncNames, placeholder);
}

module.exports.computeIntegrity = computeIntegrity;
Expand Down