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

Calypso Build Package: Add calypso-build bin shorthand #32295

Merged
merged 25 commits into from
Apr 23, 2019

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Apr 16, 2019

Changes proposed in this Pull Request

  • Alias bin/build-packages as transpile bin. This will allow us to add more bin scripts.
  • Add calypso-build alias for webpack --config=webpack.config.js

Relevant docs: https://docs.npmjs.com/files/package.json#bin

Testing instructions

npm run distclean && npm ci

Verify that npm run build-packages still transpiles packages code and writes it to packages/*/dist/cjs and packages/*/dist/esm, respectively.

npx lerna run build

Verify that apps/ are built successfully.

@matticbot
Copy link
Contributor

@ockham ockham self-assigned this Apr 16, 2019
@matticbot
Copy link
Contributor

matticbot commented Apr 16, 2019

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Webpack Runtime
webpack runtime for loading modules. It is included in the HTML page as an inline script. Is downloaded and parsed every time the app is loaded.

name      parsed_size    gzip_size
manifest         +0 B         +2 B  (+0.0%)

App Entrypoints
Common code that is always downloaded and parsed every time the app is loaded, no matter which route is used.

name   parsed_size           gzip_size
build        +16 B  (+0.0%)      +32 B  (+0.0%)

Sections
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

name         parsed_size           gzip_size
post-editor      +1110 B  (+0.1%)     +354 B  (+0.1%)
zoninator         +786 B  (+0.7%)     +261 B  (+0.9%)
reader            +324 B  (+0.1%)      +91 B  (+0.1%)
purchases         +324 B  (+0.0%)     +128 B  (+0.1%)

Async-loaded Components
React components that are loaded lazily, when a certain part of UI is displayed for the first time.

name                                         parsed_size           gzip_size
async-load-design-blocks                         +1110 B  (+0.0%)     +354 B  (+0.1%)
async-load-components-web-preview-component       +324 B  (+0.1%)      +90 B  (+0.1%)

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

docs/monorepo.md Outdated
@@ -1,5 +1,4 @@
Publishing Packages with the Monorepo
=====================================
# Publishing Packages with the Monorepo
Copy link
Member

Choose a reason for hiding this comment

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

We could change this to just "Publishing with the Monorepo" now that we also have /apps

Copy link
Member

Choose a reason for hiding this comment

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

@ockham
Copy link
Contributor Author

ockham commented Apr 16, 2019

  • Add cb alias for webpack --config=webpack.config.js

I'm starting to wonder if we can easily do this 🤔

package.json's bin field (unlike scripts) is meant for files shipped with the package to be copied to the importing package's node_modules/.bin/, from where npm will find them when called.

However (unlike the script that we use to transpile JS), our webpack call is just that: a call to webpack, with an argument provided, and accepting other arguments. And due to our monorepo setup, we can't simply point a bin to ./node_modules/.bin/webpack, since it's not installed there, even though webpack is a dependency of calypso-build.

This leaves us with the following options:

  • Add our webpack invocation to scripts rather than bin. But then again, how will other packages access that?
  • Write a JS file to wrap around the Webpack call. This seems kind of absurd:
  • If we do it JS, we need to pass through all args we get. Not sure if this is easily possible with execSync? Also, simply having to use execSync is kinda gross here.
  • Maybe we can add a (shell-agnostic) shell script that invokes webpack? Is passing args any easier there?

@simison
Copy link
Member

simison commented Apr 17, 2019

cc @gziolo & @mkaz since this seems related to comment in https://make.wordpress.org/core/2019/03/25/building-javascript/#comment-35581

@gziolo
Copy link
Member

gziolo commented Apr 17, 2019

  • Write a JS file to wrap around the Webpack call. This seems kind of absurd:
  • If we do it JS, we need to pass through all args we get. Not sure if this is easily possible with execSync? Also, simply having to use execSync is kinda gross here.
  • Maybe we can add a (shell-agnostic) shell script that invokes webpack? Is passing args any easier there?

Well, this is what we do in @wordpress/scripts:

https://github.com/WordPress/gutenberg/blob/master/packages/scripts/bin/wp-scripts.js
https://github.com/WordPress/gutenberg/blob/master/packages/scripts/scripts/build.js

It's the same technique that create-react-app or ksc-scripts are using.

@ockham ockham requested a review from a team as a code owner April 17, 2019 16:30
@sirreal
Copy link
Member

sirreal commented Apr 17, 2019

I think we can get cb 😁

I needed to distclean and install to get the monorepo package bins to be picked up.

At that point, I have node_modules/.bin/cb available, meaning it's also accessible to package scripts 💥

Through some… ehem… fanciness… we essentially get a pass-through to webpack's CLI with a default --config pointing to the @automattic/calypso-build/webpack.config.js if not provided 🎉

@@ -29,6 +29,6 @@
"scripts": {
"clean": "npx rimraf dist",
"prepublish": "npm run clean",
"prepare": "npx @automattic/calypso-build"
"prepare": "../calypso-build/bin/transpile.js"
Copy link
Member

@sirreal sirreal Apr 17, 2019

Choose a reason for hiding this comment

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

Ugly but effective 😅
The npx --package @automattic/calypso-build transpile version was broken 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How was that broken? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Fine to keep the ugly-but-effective version to be able to ship this soon.)

Copy link
Member

Choose a reason for hiding this comment

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

I may have been wrong and "fixed" this before doing the distclean dance to get the monorepo package bins properly installed.

Please double check me, we may be able to use npx --package @automattic/calypso-build transpile, although I suspect the change will be painful for some folks as they'll likely experience the same problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wondering if #32383 might've been the actual reason for this. Seems like a stretch, but maybe some silent error when trying to build a local package along the dependency chain?

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 reverted the commit since it seems to work in my testing 🤔 I'll leave it to reviewers/testers to correct me there...

packages/calypso-build/bin/cb.js Outdated Show resolved Hide resolved
@ockham ockham changed the title Calypso Build Package: Alias bin/build-packages as transpile bin Calypso Build Package: Add cb bin shorthand Apr 18, 2019
@ockham
Copy link
Contributor Author

ockham commented Apr 18, 2019

Thanks Jon, loving it! I've pushed a commit to have our apps/ use cb (which works like a charm). Going to rebase and then set to 'Needs Review'.

docs/monorepo.md Outdated Show resolved Hide resolved
@ockham ockham force-pushed the update/calypso-build-bins branch 3 times, most recently from 2a753bd to 40cc278 Compare April 18, 2019 09:50
apps/o2-blocks/package.json Outdated Show resolved Hide resolved
@ockham ockham added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Apr 18, 2019
@ockham
Copy link
Contributor Author

ockham commented Apr 18, 2019

Finall figured out the build error (--env.WP, without ='true', can confuse webpack about the entry point, if put in the wrong place).

@ockham
Copy link
Contributor Author

ockham commented Apr 18, 2019

Somewhat related: #32389 to guard against broken o2-blocks builds.

@simison
Copy link
Member

simison commented Apr 19, 2019

This can probably be switched to use cb?

"build-notifications": "webpack --config='client/notifications/webpack.config.js'",

(the whole thing should probably be in /apps but that's whole another story.)

Copy link
Member

@simison simison left a comment

Choose a reason for hiding this comment

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

Looks fantastic and works well in my testing.

Tested:

  • npm ci
  • npm start
  • npm run build-notifications:
    "build-notifications": "webpack --config='client/notifications/webpack.config.js'",
  • npm run build-notifications: by switching webpack to cb
  • npx lerna run build
  • npx lerna run prepare

Only standing note I left was about build-notifications

@ockham
Copy link
Contributor Author

ockham commented Apr 23, 2019

I'll rebase

Done

package.json Outdated Show resolved Hide resolved
packages/calypso-build/bin/cb.js Outdated Show resolved Hide resolved
@@ -16,10 +16,10 @@
},
"homepage": "https://github.com/Automattic/wp-calypso",
"scripts": {
"plugin": "webpack --source='plugin'",
"plugin": "cb --config='./webpack.config.js' --source='plugin' --env.WP",
Copy link
Member

Choose a reason for hiding this comment

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

Do you know where these --source arguments come from? I don't see them documented in webpack CLI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, they're a custom addition to argv in full-site-editing's Webpack config:

* @param {object} argv.source "plugin" or "theme"

Was also wondering if we should discourage 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Was also wondering if we should discourage 🤔

Oh, that's interesting! I'd say cb itself should be very strict about adhering to the webpack CLI, but let's not prescribe how consumers should use it (yet)…

Copy link
Member

Choose a reason for hiding this comment

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

Some feedback about naming:

  • cb feels like something very generic, like a shell command. Specialized commands should have longer names, e.g., calypso-build.
  • webpack --source: again, very generic, looks like a built-in webpack option. --calypso-something-source would communicate better that it's something custom.

Copy link
Contributor Author

@ockham ockham Apr 23, 2019

Choose a reason for hiding this comment

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

Thanks Jarda! I tend to agree with both points.

For a while, I was thinking that cb was a nice shorthand, and that we could use npx --package=@automattic/calypso-build cb to make sure we're getting the right command, but using that caused some rather annoying that I wasn't able to resolve easily.

Inclined to file a commit to s/cb/calypso-build/g, unless anyone objects? @sirreal?

Copy link
Member

Choose a reason for hiding this comment

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

  • cb feels like something very generic, like a shell command. Specialized commands should have longer names, e.g., calypso-build.

@blowery suggested this in the interest of brevity. In practice, this will likely live mostly inside package.json scripts 🤔 I'm fine either way.

  • webpack --source: again, very generic, looks like a built-in webpack option. --calypso-something-source would communicate better that it's something custom.

This isn't a "core" calypso-build option, but some configuration that a particular webpack config function is exposing which is why I'm not too concerned. It would be harmful if folks start copying the pattern and expect it to be "core" functionality… Perhaps it is something to discourage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • webpack --source: again, very generic, looks like a built-in webpack option. --calypso-something-source would communicate better that it's something custom.

Agree. We're using env (e.g. env.WP) to communicate Calypso-specific stuff, whereas full-size-editing deviates from that pattern. However, the --source arg has been merged prior to this PR and is thus arguably orthogonal to this one, so I'd like to change it with a separate PR.

apps/full-site-editing/package.json Outdated Show resolved Hide resolved
apps/full-site-editing/package.json Outdated Show resolved Hide resolved
apps/o2-blocks/package.json Outdated Show resolved Hide resolved
@ockham ockham changed the title Calypso Build Package: Add cb bin shorthand Calypso Build Package: Add calypso-build bin shorthand Apr 23, 2019
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

This is looking great!

@ockham ockham merged commit 925d1c7 into master Apr 23, 2019
@ockham ockham deleted the update/calypso-build-bins branch April 23, 2019 07:55
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants