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

🏗♻️ Consolidate all babel configs into babel.config.js #27576

Merged
merged 16 commits into from
Apr 9, 2020
Merged

🏗♻️ Consolidate all babel configs into babel.config.js #27576

merged 16 commits into from
Apr 9, 2020

Conversation

rsimha
Copy link
Contributor

@rsimha rsimha commented Apr 5, 2020

The compilation tasks and checks in the amphtml toolchain apply babel transforms to code in a variety of ways. Today, the configs for these transforms are peppered across build-system/, resulting in a hard-to-maintain mess of config code.

#27117 was filed to address this problem, and #27161 took a first stab at fixing this. This PR picks up from there, and does the following:

  • Moves all config code out of build-system/compile/build.conf.js and deletes the file
  • Implements configs for all babel, babelify, and gulp-babel invocations in build-system/babel-config/*-config.js
  • Adds a caller identifier to every invocation site
  • Repurposes babel.config.js to return the config that corresponds to the caller

With this PR, all babel configs will live in build-system/babel-config/, while the resulting runtime binaries remain unchanged.

Fixes #27117
Closes #27161

@rsimha
Copy link
Contributor Author

rsimha commented Apr 5, 2020

@jridgewell @erwinmombay @kristoferbaxter babel.config.js has now been fully refactored. However, it was not easy to figure out which portions of build.conf.js overrode / supplemented the original babel.config.js.

I tried reverse engineering this by printing the configurations before / after refactor, but there were too many files for which to inspect the diffs.

It will be significantly easier if you folks took a look at this file and pointed out things that need to be changed.

Copy link
Contributor

@rcebulko rcebulko left a comment

Choose a reason for hiding this comment

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

Given how long I spent trying to parse a lot of this code, I'm impressed at how much you were able to consolidate. That said, is there any good way for use to validate these changes to ensure we didn't miss some small thing? For instance, if we were to run each of the build commands/babel configs, would we be able to diff the outputs and verify that we got the exact same results, or are the changes expected to impact the output in some way?

@rsimha
Copy link
Contributor Author

rsimha commented Apr 6, 2020

would we be able to diff the outputs and verify that we got the exact same results

See #27576 (comment) for how we can make sure the same babel transforms are applied. Also, re: code output, the stated goal (in the description) is that nothing should change. We'll eventually get there, after multiple rounds of review and testing.

Copy link
Contributor

@kristoferbaxter kristoferbaxter left a comment

Choose a reason for hiding this comment

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

I'm not certain I get this PR yet.

Everything related to Babel is in one place, but the callers are strings that execute other functions, which return static Object literals?

Would it make more sense to make the babel.config.js file lean (contains imports for these callers from a subdirectory inside babel or similar) and delegate the functionality with a prescribed lifecycle?

OWNERS Show resolved Hide resolved
build-system/compile/post-closure-babel.js Outdated Show resolved Hide resolved
build-system/compile/pre-closure-babel.js Outdated Show resolved Hide resolved
build-system/compile/single-pass.js Show resolved Hide resolved
build-system/tasks/helpers.js Show resolved Hide resolved
build-system/tasks/helpers.js Show resolved Hide resolved
build-system/tasks/karma.conf.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
@rsimha
Copy link
Contributor Author

rsimha commented Apr 6, 2020

Everything related to Babel is in one place, but the callers are strings that execute other functions, which return static Object literals?

No, all the caller objects are merely signatures that help babel.config.js determine where a babel transform invocation originated, and consequently, which config to return. See https://babeljs.io/docs/en/options#caller.

Would it make more sense to make the babel.config.js file lean (contains imports for these callers from a subdirectory inside babel or similar) and delegate the functionality with a prescribed lifecycle?

Having babel.config.js import code from elsewhere is against the original stated goals of the effort to centralize all configurations in one place. For more context, see #27117 and #27161 (review).

@rsimha
Copy link
Contributor Author

rsimha commented Apr 6, 2020

It will be significantly easier if you folks took a look at this file and pointed out things that need to be changed.

@kristoferbaxter Can you take a look at the current WIP babel.config.js and point out what the intended set of transforms are for each use case? (It's really difficult to reverse engineer this stuff because there are hundreds of invocations with different configs across all our use cases). Once I get initial input from you, @jridgewell, and @erwinmombay, I can do some testing to make sure we're generating the exact same compiled output after this refactor.

Copy link
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

🙌

@kristoferbaxter
Copy link
Contributor

On the nature of a single file versus many, I'm not too strongly positioned however it might be worth considering this structure.

babel.config.js
-- babel-configurations
-- - single-pass.js
-- - dist.js
-- - dist-esm.js
-- - build.js
-- - test.js

Each of these files can export an object for the different lifecycles they support.
{ standalone?: () => {}, preClosure?: () => {}, postClosure?: () => {} }

These lifecycle names and meanings can be defined in a class that all exports used by babel.config.js as ConfigOptions use, and can be enforced via static typing (either TypeScript or Closure types in our case).

Additionally, the names of the "caller" is a nice contract for storing them in a Map inside babel.config.js.

note: this is pseudo-code.

import {singlePassConfiguration} from 'babel-configurations/single-pass';

const configOptions = new Map({
  'single-pass': singlePassConfiguration,
  ...
})

export function execute(caller) {
  if (configOptions.has(caller)) {
    const configuration = configOptions.get(caller);
    ...
  }
}

Copy link
Contributor

@kristoferbaxter kristoferbaxter left a comment

Choose a reason for hiding this comment

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

Configs look like they should be accurate for esm and dist afaik.

babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
babel.config.js Outdated Show resolved Hide resolved
@kristoferbaxter
Copy link
Contributor

@rsimha – A heads up on an additional transform added today. Will need to add this to the post-closure-babel order.

#27613

@rsimha rsimha marked this pull request as ready for review April 7, 2020 23:08
@rsimha
Copy link
Contributor Author

rsimha commented Apr 7, 2020

@kristoferbaxter Thanks for the excellent PR feedback! I've incorporated all your comments and marked this PR as ready for review.

Now begins the task of running "before" and "after" builds of all kinds and ironing out config diffs. I'll post an update once this is done, and report if I find any problems.

@kristoferbaxter
Copy link
Contributor

Awesome, I should be able to take another look at this first thing tomorrow.

@amp-owners-bot
Copy link

amp-owners-bot bot commented Apr 8, 2020

Hey @estherkim, these files were changed:

build-system/tasks/e2e/index.js
build-system/tasks/runtime-test/runtime-test-base.js

Copy link
Contributor

@kristoferbaxter kristoferbaxter 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 a significant improvement to readability. Thank you for doing this!

build-system/babel-config/dev-dependencies.js Outdated Show resolved Hide resolved
build-system/babel-config/index.js Show resolved Hide resolved
build-system/babel-config/pre-closure-config.js Outdated Show resolved Hide resolved
build-system/compile/post-closure-babel.js Show resolved Hide resolved
@rsimha
Copy link
Contributor Author

rsimha commented Apr 9, 2020

Thanks for the patient reviews, everyone. We are now in a working state with an all-green Travis build.

I did a before/after comparison of the entire dist/ folder output for gulp dist (the variant shipped to stable channel). All minified JS binaries are byte-for-byte identical. I also spot-checked the main AMP binary for a few other variants like gulp dist --fortesting, gulp --compiled, and gulp build and found them to be byte-for-byte identical.

image

For the remaining callers of babel transforms like gulp dep-check, gulp check-types, gulp dist --single_pass, and gulp dist --esm, I added debug logging to the validate() function in node_modules/@babel/core/lib/config/validation/options.js to print out the final version of the config used by babel / babelify / gulp-babel, and made sure we're using the same config as before in all places.

This PR is now ready to merge.

@rsimha rsimha changed the title 🏗♻️ Consolidate all babel configuration into babel.config.js 🏗♻️ Consolidate all babel configs into babel.config.js Apr 9, 2020
@rsimha rsimha merged commit f801a93 into ampproject:master Apr 9, 2020
@rsimha rsimha deleted the 2020-04-03-BabelConfigRefactor branch April 9, 2020 07:47
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Apr 9, 2020
erwinmombay added a commit that referenced this pull request Apr 9, 2020
* Revert "🏗🐛 Fix babel plugin tests and run them for babel config chan… (#27664)"

This reverts commit 39dac8e.

* Revert "🏗♻️ Consolidate all `babel` configs into `babel.config.js` (#27576)"

This reverts commit f801a93.
metarag pushed a commit to metarag/amphtml that referenced this pull request Apr 9, 2020
* Revert "🏗🐛 Fix babel plugin tests and run them for babel config chan… (ampproject#27664)"

This reverts commit 39dac8e.

* Revert "🏗♻️ Consolidate all `babel` configs into `babel.config.js` (ampproject#27576)"

This reverts commit f801a93.
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Apr 10, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Apr 13, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Apr 14, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request May 6, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request May 12, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Jun 16, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Sep 16, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Sep 22, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Oct 20, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Oct 21, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Oct 29, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Nov 2, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Nov 2, 2020
erwinmombay added a commit to erwinmombay/amphtml that referenced this pull request Nov 2, 2020
erwinmombay added a commit that referenced this pull request Nov 2, 2020
* temp

* temp

* add conditional to the css removal

* add travis entry

* add esm-tests.js file

* minor fix to add coverage

* remove compiled to test

* add compiled back in

* Revert "🏗♻️ Consolidate all `babel` configs into `babel.config.js` (#27576)"

This reverts commit f801a93.

* temp

* temp

* reset this to be close to master

* temp

* temp

* temp

* temp

* for non dev builds (compiled, rtv) take into account the module build for regex replace

* temp

* remove debuggers

* temp

* apply recs

* remove runner.js

* apply recs

* add --esm flag to integration run

* download dist output as we need f.js/integration.js from dist nomodule

* allow cors explicitly for mjs files GET requests

* add flag to overwrite when unzipping

* fix test type inference

* apply recs
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* temp

* temp

* add conditional to the css removal

* add travis entry

* add esm-tests.js file

* minor fix to add coverage

* remove compiled to test

* add compiled back in

* Revert "🏗♻️ Consolidate all `babel` configs into `babel.config.js` (ampproject#27576)"

This reverts commit f801a93.

* temp

* temp

* reset this to be close to master

* temp

* temp

* temp

* temp

* for non dev builds (compiled, rtv) take into account the module build for regex replace

* temp

* remove debuggers

* temp

* apply recs

* remove runner.js

* apply recs

* add --esm flag to integration run

* download dist output as we need f.js/integration.js from dist nomodule

* allow cors explicitly for mjs files GET requests

* add flag to overwrite when unzipping

* fix test type inference

* apply recs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove build.conf.js
6 participants