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

Convert source to Buffer when getting asset's hash #1966

Merged
merged 3 commits into from
Mar 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"sinon": "^6.3.4",
"tempy": "^0.2.1",
"url-search-params": "^1.1.0",
"webpack": "^4.19.1"
"webpack": "^4.19.1",
"worker-plugin": "^3.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/workbox-webpack-plugin/src/lib/get-asset-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ const crypto = require('crypto');
*/
module.exports = (asset) => {
return crypto.createHash('md5')
.update(asset.source(), 'utf8')
.update(Buffer.from(asset.source()))
.digest('hex');
};
48 changes: 48 additions & 0 deletions test/workbox-webpack-plugin/node/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WorkerPlugin = require('worker-plugin');
const expect = require('chai').expect;
const fse = require('fs-extra');
const glob = require('glob');
Expand Down Expand Up @@ -1646,4 +1647,51 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
});
});
});

describe(`[workbox-webpack-plugin] WASM Code`, function() {
// See https://github.com/GoogleChrome/workbox/issues/1916
it(`should support projects that bundle WASM code`, function(done) {
const indexFilename = 'index.js';
const outputDir = tempy.directory();
const srcDir = path.join(__dirname, '..', 'static', 'wasm-project');
const config = {
mode: 'production',
entry: {
index: path.join(srcDir, indexFilename),
},
output: {
filename: '[name].js',
globalObject: 'self',
path: outputDir,
},
plugins: [
new WorkerPlugin(),
new GenerateSW(),
],
};

const compiler = webpack(config);
compiler.run(async (webpackError, stats) => {
if (webpackError) {
return done(webpackError);
}

try {
const statsJson = stats.toJson('verbose');
expect(statsJson.warnings).to.have.lengthOf(0);

// Bundling WASM into a Worker seems to lead to different hashes in
// different environments. Instead of hardcoding hash checks, just
// confirm that we output the expected number of files, which will
// only be true if the build was successful.
const files = glob.sync(`${outputDir}/*`);
expect(files).to.have.length(6);

done();
} catch (error) {
done(error);
}
});
});
});
});
Binary file not shown.
9 changes: 9 additions & 0 deletions test/workbox-webpack-plugin/static/wasm-project/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Copyright 2018 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.
*/

new Worker('./worker.js', {type: 'module'});
11 changes: 11 additions & 0 deletions test/workbox-webpack-plugin/static/wasm-project/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright 2018 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.
*/

import('./add.wasm').then(({add}) => {
return add(10, 20);
});