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

dev: Improve prod build and monorepo scripts #165

Merged
merged 2 commits into from Aug 1, 2018

Conversation

zetlen
Copy link
Contributor

@zetlen zetlen commented Jul 22, 2018

This PR is a:

[ ] New feature
[x] Enhancement/Optimization
[ ] Refactor
[ ] Bugfix
[ ] Test for existing code
[ ] Documentation

Summary

  • Add more root-directory usability to monorepo scripts
  • Add npm run watch:venia mode that:
    • Simultaneously builds buildpack, peregrine, and venia
    • Hot reloads venia if either peregrine or venia change on disk
    • Fully restarts dev server if buildpack changes on disk
    • Interleaves and prefixes output
    • Doesn't use lerna to do this, because lerna's parallelizing console
      mode is not customizable enough
  • Fix production builds (by pegging them to very new browsers)

Additional information

Production builds will need further optimization in the future.
TODO: Adjust production build for middle tier concept
TODO: Use performance analysis to optimize chunk layout

@zetlen
Copy link
Contributor Author

zetlen commented Jul 22, 2018

Feel free to clone and run npm run watch:venia and/or npm run build && lerna run --stream --scope=theme-* build to see the new hotness.

@zetlen zetlen force-pushed the zetlen/production-build-bugfixes branch from b683cbe to df80a74 Compare July 22, 2018 20:29
@coveralls
Copy link

coveralls commented Jul 22, 2018

Pull Request Test Coverage Report for Build 378

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 25 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.1%) to 55.515%

Files with Coverage Reduction New Missed Lines %
packages/pwa-buildpack/src/WebpackTools/middlewares/StaticRootRoute.js 5 18.18%
packages/venia-concept/src/util/makeMediaPath.js 8 0.0%
packages/pwa-buildpack/src/WebpackTools/middlewares/OriginSubstitution.js 12 33.33%
Totals Coverage Status
Change from base Build 369: -0.1%
Covered Lines: 867
Relevant Lines: 1617

💛 - Coveralls

@zetlen zetlen force-pushed the zetlen/production-build-bugfixes branch from df80a74 to 7465bed Compare July 30, 2018 13:41
@@ -12,7 +12,7 @@
"presets": [
["env", {
"targets": {
"browsers": ["last 2 versions", "ie 11"]
"browsers": ["> 5%"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This should slim down the build. 👍

package.json Outdated
"watch:buildpack": "cd packages/pwa-buildpack && npm run -s watch",
"watch:peregrine": "cd packages/peregrine && npm run -s watch",
"watch:dev-server": "nodemon --exec npm --cwd packages/venia-concept start",
"watch:venia": "concurrently -c yellow.dim,green.dim,cyan.dim -n ␢,℗,☄︎ 'npm run watch:buildpack' 'npm run watch:peregrine' 'npm run watch:dev-server'",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really the plan? Watch all of these at the same time? I'd rather just watch venia, personally. Can we make this one watch:all or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is definitely what you want. This has the exact same functionality that npm start in venia does today, with three key improvements:

  1. If you change a Peregrine source file (e.g. moving a concept from Venia to Peregrine) it will hot reload and stay up to date, instead of requiring a manual Peregrine rebuild
  2. If you change a Buildpack source file (e.g. changing the behavior of a plugin or DevServer), it will shut down the dev server, rebuild Buildpack, and restart the dev server to reflect the new changes.
  3. You can run it from project root.

If you don't touch Peregrine or Buildpack, it's functionally the same.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't touch peregrine or buildpack, is it more expensive to watch them? Does it bog your system down more? That's my only concern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jimbo Peregrine and Buildpack have many fewer files than Venia, so I don't think it's a major concern. However: I could just change this so that "watch:venia" just runs "cd packages/venia-concept && npm start; cd -", and the command currently named "watch:venia" could be renamed "watch:all". Would that help?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think that would help. 👍

// In production mode, it may be a pathname, which makes it unsafe
// to use as an API base. Normalize it as a full path using a DOM node
// as a native URL parser.
const parser = document.createElement('a');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate? What does this workaround do that URL doesn't?

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's guaranteed to produce an absolute URL from a possibly-relative URL. The webpackPublicPath argument is relative in production and absolute in dev, so we need something that can handle both. URL objects cannot:

// URL constructors deal in absolute URLs.
try {
  new URL('/hermano')
} catch(e) {
  console.warn(e.message, '/hermano'); // Failed to construct 'URL': Invalid URL
}

// They can use relative URLs if you supply a "base URL" as a second argument.
const absoluteBase = 'https://github.com/magento-research/pwa-studio/';
const url = new URL('pulls/165', absoluteBase);
console.log(url)
// Url { protocol: "https:" hostname: "github.com", pathname: "/magento-research/pwa-studio/pulls/165" }

// If the base URL is relative, they throw.
try {
  new URL('pulls/165', '/magento-research/pwa-studio/');
} catch(e) {
  console.warn(e.message, '/magento-research/pwa-studio/');
}

// Anchor tags will accept absolute or relative 'href' attributes:
const parserTag = document.createElement('a');
parserTag.setAttribute('href','/magento-research/pwa-studio');
// But their properties are always absolute:
console.log('parserTag.getAttribute("href")', parserTag.getAttribute("href"));
console.log('parserTag.href', parserTag.href); // "https://github.com/magento-research/pwa-studio/";

// So you can guarantee an absolute URL built with URL semantics
// if you can guarantee an absolute base, which we do with our parser.
// Which is a lot safer than string concatenation.

function appendPathToUrl(path, base) {
   const parser = document.createElement('a');
   parser.setAttribute('href', base);
   return new URL(path, parser.href).toString()
}

console.log(
  'true or false: using an anchor tag as a parser normalizes absolute URLs:'
  appendPathToUrl('pulls/165', '/magento-research/pwa-studio/') === 
  appendPathToUrl('pulls/165', 'https://github.com/magento-research/pwa-studio/')
);
// true

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, makes perfect sense to me now. Thanks for the explanation. 👍

}
browsers: ['> 5%']
},
modules: false
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a bug fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! For Webpack tree shaking.

@zetlen zetlen force-pushed the zetlen/production-build-bugfixes branch from 7465bed to 26c5c0b Compare July 31, 2018 22:56
@zetlen
Copy link
Contributor Author

zetlen commented Jul 31, 2018

@jimbo Made changes based on your feedback.

@zetlen zetlen force-pushed the zetlen/production-build-bugfixes branch from 26c5c0b to c20fc36 Compare August 1, 2018 12:18
@zetlen
Copy link
Contributor Author

zetlen commented Aug 1, 2018

@jimbo Build is fixed. We had a misconfiguration in CI where the test suite wouldn't run anymore, which is how the Greenkeeper update got merged without noticing test failure.

  • We don't run our tests directly in CI; we run Danger and it runs the tests from the Dangerfile.
  • Danger is evidently incompatible with a CircleCI setting which builds all pushed branches. It can't tell if it's in a PR anymore.
  • I had enabled that setting while configuring Greenkeeper, but I don't think we need it anymore.
  • Disabled that setting and Danger runs now.
  • TODO: run tests directly, store artifacts, and run Danger on those artifacts.

 - Add more root-directory usability to monorepo scripts
 - Add `npm run watch:all` mode that:
   - Simultaneously builds buildpack, peregrine, and venia
   - Hot reloads venia if either peregrine or venia change on disk
   - Fully restarts dev server if buildpack changes on disk
   - Interleaves and prefixes output
   - Doesn't use lerna to do this, because lerna's parallelizing console
   mode is not customizable enough
 - Fix production builds (by pegging them to very new browsers)
@zetlen zetlen force-pushed the zetlen/production-build-bugfixes branch from c20fc36 to e26a97c Compare August 1, 2018 12:46
Copy link
Contributor

@jimbo jimbo left a comment

Choose a reason for hiding this comment

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

@zetlen Thanks for making changes. Looks good to me now.

For what it's worth, watch:all is very slow compared to watch:venia. It makes my machine's fans spin up to high, and it takes a long time to hot reload. That can be optimized, so I don't think it blocks this, but it's something we'll need to improve or remove.

@zetlen zetlen merged commit 4ad172a into master Aug 1, 2018
@zetlen zetlen deleted the zetlen/production-build-bugfixes branch August 1, 2018 20:24
jimbo added a commit that referenced this pull request Aug 2, 2018
jimbo added a commit that referenced this pull request Aug 2, 2018
@jimbo jimbo restored the zetlen/production-build-bugfixes branch August 6, 2018 21:09
zetlen added a commit that referenced this pull request Aug 11, 2018
- Add more root-directory usability to monorepo scripts
- Add `npm run watch:all` mode that:
  - Simultaneously builds buildpack, peregrine, and venia
  - Hot reloads venia if either peregrine or venia change on disk
  - Fully restarts dev server if buildpack changes on disk
  - Interleaves and prefixes output
  - Doesn't use lerna to do this, because lerna's parallelizing console
  mode is not customizable enough
- Fix production builds (by pegging them to very new browsers)
zetlen added a commit that referenced this pull request Sep 18, 2018
- Add more root-directory usability to monorepo scripts
- Add `npm run watch:all` mode that:
  - Simultaneously builds buildpack, peregrine, and venia
  - Hot reloads venia if either peregrine or venia change on disk
  - Fully restarts dev server if buildpack changes on disk
  - Interleaves and prefixes output
  - Doesn't use lerna to do this, because lerna's parallelizing console
  mode is not customizable enough
- Fix production builds (by pegging them to very new browsers)

fix: improve dev mode watch and log
zetlen added a commit that referenced this pull request Sep 18, 2018
- Add more root-directory usability to monorepo scripts
- Add `npm run watch:all` mode that:
  - Simultaneously builds buildpack, peregrine, and venia
  - Hot reloads venia if either peregrine or venia change on disk
  - Fully restarts dev server if buildpack changes on disk
  - Interleaves and prefixes output
  - Doesn't use lerna to do this, because lerna's parallelizing console
  mode is not customizable enough
- Fix production builds (by pegging them to very new browsers)

fix: improve dev mode watch and log

chore: remove greenkeeper for now

chore: flush CircleCI cache by revving cache key

chore: reorg dependencies for lerna

fix: lerna dedupe and hoist

fix: use npm ci command to ensure deps

fix: populate .env file in ci

fix: clean after build for CI

fix: only clean dist, leave node_modules

fix: help eslint detect root modules
zetlen added a commit that referenced this pull request Sep 18, 2018
- Add more root-directory usability to monorepo scripts
- Add `npm run watch:all` mode that:
  - Simultaneously builds buildpack, peregrine, and venia
  - Hot reloads venia if either peregrine or venia change on disk
  - Fully restarts dev server if buildpack changes on disk
  - Interleaves and prefixes output
  - Doesn't use lerna to do this, because lerna's parallelizing console
  mode is not customizable enough
- Fix production builds (by pegging them to very new browsers)

fix: improve dev mode watch and log

chore: remove greenkeeper for now

chore: flush CircleCI cache by revving cache key

chore: reorg dependencies for lerna

fix: lerna dedupe and hoist

fix: use npm ci command to ensure deps

fix: populate .env file in ci

fix: clean after build for CI

fix: only clean dist, leave node_modules

fix: help eslint detect root modules
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.

None yet

3 participants