Skip to content

Commit

Permalink
Skip calculating SRI for assets from the public/ folder.
Browse files Browse the repository at this point in the history
Saves on average ~2ms per file in one app, which adds up when there are
hundreds. SRI is currently only supported for js and css files, and Meteor
only uses it for the main bundles.
  • Loading branch information
zodern authored and Ben Newman committed Feb 28, 2019
1 parent 367572f commit 79d6435
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 9 additions & 2 deletions tools/isobuild/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ class File {
// disk).
this.sourcePath = options.sourcePath;

// Allows not calculating sri when the file doesn't support it
this._skipSri = options.skipSri

// info is just for help with debugging the tool; it isn't written to disk or
// anything.
this.info = options.info || '?';
Expand Down Expand Up @@ -619,7 +622,9 @@ class File {
hashes.push(this._inputHash);
}

hashes.push(this.sri());
if (!this._skipSri) {
hashes.push(this.sri());
}

this._hash = watch.sha1(...hashes);
}
Expand All @@ -628,7 +633,7 @@ class File {
}

sri() {
if (! this._sri) {
if (! this._sri && !this._skipSri) {
this._sri = watch.sha512(this.contents());
}

Expand Down Expand Up @@ -1160,6 +1165,8 @@ class Target {
data: resource.data,
cacheable: false,
hash: resource.hash,
sourcePath: resource.sourcePath,
skipSri: !!resource.sourcePath && resource.hash
};

const file = new File(fileOptions);
Expand Down
7 changes: 4 additions & 3 deletions tools/isobuild/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ var compileUnibuild = Profile(function (options) {

// This function needs to be factored out to support legacy handlers later on
// in the compilation process
function addAsset(contents, relPath, hash) {
function addAsset(contents, relPath, absPath, hash) {
// XXX hack to strip out private and public directory names from app asset
// paths
if (! inputSourceArch.pkg.name) {
Expand All @@ -470,7 +470,8 @@ var compileUnibuild = Profile(function (options) {
path: relPath,
servePath: colonConverter.convert(
files.pathJoin(inputSourceArch.pkg.serveRoot, relPath)),
hash: hash
hash: hash,
sourcePath: absPath
});
}

Expand All @@ -482,7 +483,7 @@ var compileUnibuild = Profile(function (options) {
const hash = optimisticHashOrNull(absPath)
const contents = optimisticReadFile(absPath)

addAsset(contents, relPath, hash);
addAsset(contents, relPath, absPath, hash);
});

// Add and compile all source files
Expand Down

0 comments on commit 79d6435

Please sign in to comment.