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(bundler): Revisions are inserted into platform.index for all bundles #935

Merged
merged 5 commits into from
Nov 14, 2018

Conversation

fragsalat
Copy link
Contributor

@fragsalat fragsalat commented Oct 11, 2018

This fixes #933 by first writing all bundles to the disk and after wards update the file name in the platform.index for each bundle. This allows users to load all bundles in parallel via script tags.

PS: There were 18 tests failing even before I started^^

@3cp
Copy link
Member

3cp commented Oct 11, 2018

Are you running nodejs v10? The test doesn't work on v10 because of lack of mock-fs support.

@3cp
Copy link
Member

3cp commented Oct 11, 2018

@JeroenVinke do you know why this PR didn't trigger circleci check? There should be 3 checks.

@fragsalat
Copy link
Contributor Author

@huochunpeng @JeroenVinke ping

@3cp
Copy link
Member

3cp commented Oct 16, 2018

@fragsalat please check the code comments.

@fragsalat
Copy link
Contributor Author

I'm actually not seeing any. Did you may forgot to submit the review?

//Replace the reference to the vendor bundle in the "index.html" file to use the correct build revision file (or remove the build revision hash);
const createSrcFileRegex = require('./utils').createSrcFileRegex;
let indexLocation = platform.index;
async writeRevToIndex(platform) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot use async/await syntax because it's only supported in nodejs v10.1.0+.
We need to support nodejs v8.9.0+.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm mdn tells me that the support in nodejs starts of 7.6.0
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/await

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it with 8.9.0 and for me it's working

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember where I got v10.1.0+. Obviously false statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem :)

@@ -232,7 +232,12 @@ exports.Bundler = class {
}

write() {
return Promise.all(this.bundles.map(x => x.write(this.project.build.targets[0])));
return Promise.all(this.bundles.map(bundle => bundle.write(this.project.build.targets[0])))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call the rev writing inside bundle.js write(), like the current implementation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use sync write to avoid promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was actually like this except that the write was asynchronous. Once the vendor bundle was reached it wrote the revisions to the html but the vendor bundle doesn't know about the revisions of other files. I don't wanted to introduce code to pass revisions of other bundles into the last bundle because it felt a bit dirty and therefore I delayed the write process.

Regarding the sync write: I started with that solution but it were a bit hard because you use a mkdir function in the async write which is based on callbacks. I don't wanted to create a sync write function without this mkdir stuff as it would mean different behavior for those functions.

@3cp
Copy link
Member

3cp commented Oct 17, 2018

My bad

@3cp
Copy link
Member

3cp commented Oct 17, 2018

The reason your PR didn't trigger circleci check is that your commit is on very old master (before circleci was turned on), please rebase to latest master.

@fragsalat
Copy link
Contributor Author

I pulled aurelia/cli@master into my branch but circle ci still don't trigger

return Promise.all(this.bundles.map(bundle => bundle.write(this.project.build.targets[0])))
.then(async() => {
for (let i = this.bundles.length; i--; ) {
await this.bundles[i].writeRevToIndex(this.project.build.targets[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised you did serial write on index file. 👍
I am not familiar with async/await, took me quite a while to understand what's doing here.

@3cp
Copy link
Member

3cp commented Oct 17, 2018

👍 looks good, and worked for me.
I didn't use async/await, the serialised await spun my head, but it's a cool usage.

} else {
logger.error(err);
// Validate rev is enabled and a hash was generated
if (platform.index && this.hash) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug, you need to write empty hash to index file.
Consider:

au run --env prod
# terminate it, then
au run

The second au run need to overwrite index file to empty hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my gosh. Thanks for pointing that out. The check for the hash was to much^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (configMatcher.test(indexFile)) {
indexFile = indexFile.replace(configMatcher, 'src="$2' + bundleLocation + '"');
await fs.writeFile(platform.index, indexFile);
logger.info(`INFO [Bundle] Updated file name for ${this.moduleId} in ${platform.index}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove "INFO [Bundle] " prefix, logger.info does that for you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got duplicated prefix.

INFO [Bundle] Writing app-bundle.js...
INFO [Bundle] Writing vendor-bundle.js...
INFO [Bundle] INFO [Bundle] Updated file name for vendor-bundle in index.html
INFO [Bundle] INFO [Bundle] Updated file name for app-bundle in index.html
Finished 'writeBundles'

}
} catch (error) {
logger.error(`ERROR [Bundle] Couldn't update file name with revision in ${platform.index}`, error);
Copy link
Member

@3cp 3cp Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove "ERRPR [Bundle] " prefix, logger.error does that for you.

@fragsalat
Copy link
Contributor Author

Fixed the logger stuff :)

@3cp
Copy link
Member

3cp commented Oct 18, 2018

@JeroenVinke could you review this? Looks good to me, not critical feature but does not hurt.

@JeroenVinke
Copy link
Collaborator

Hey @fragsalat and @huochunpeng :)

I've had a look, and had to think about the for loop for a while too 😄

Only thing I could find to complain about is the name of the function writeRevToIndex. I believe this function also updates the bundle names in index.html when rev isn't enabled. If you agree, would you mind changing the name? Then we can merge this :)

Great work @fragsalat

@Alexander-Taran
Copy link
Contributor

@JeroenVinke naming is fixed (-:

@fragsalat
Copy link
Contributor Author

Still not merged...?

@JeroenVinke
Copy link
Collaborator

Sorry about that, thanks a lot for your efforts @fragsalat

@JeroenVinke JeroenVinke merged commit e193d57 into aurelia:master Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Revisions are not inserted into index.html properly
4 participants