Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Optimize result filtration. #77

Merged
merged 1 commit into from
Dec 12, 2016
Merged

Conversation

yavorsky
Copy link
Member

@yavorsky yavorsky commented Dec 11, 2016

Why?
For now, we have excess .filter(Boolean) while processing preset's result:
Firstly, there are extra checks for modules:

const modules = [
// false
  moduleType === "commonjs" && [require("babel-plugin-transform-es2015-modules-commonjs"), { loose }],
// false
  moduleType === "systemjs" && [require("babel-plugin-transform-es2015-modules-systemjs"), { loose }],
// false
  moduleType === "amd" && [require("babel-plugin-transform-es2015-modules-amd"), { loose }],
// true
  moduleType === "umd" && [require("babel-plugin-transform-es2015-modules-umd"), { loose }],
].filter(Boolean);
// Up to 4 checks

Then we iterate all modules array to get only single matched module:

modules.filter(Boolean)

By the end, we are processing new iteration by filtering preset's result:

plugins: [
  ...modules,
  ...plugins,
  useBuiltIns === true && [transformPolyfillRequirePlugin, { polyfills, regenerator }]
].filter(Boolean)

despite:

  1. we've already checked modules array
  2. plugins can't contain anything except array(it's result of allTransformations.map which returns array only and throws error if no plugin was found).
    So, we are iterating the whole array to check only last value.

How this PR optimizes aforesaid?

  1. We already have MODULE_TRANSFORMATIONS to check and get module's plugin.
  2. Replace array filtering with particular checks.
  3. Fill up preset's result gradually.

const modulePlugin = moduleType !== false && MODULE_TRANSFORMATIONS[moduleType];
const plugins = [];

modulePlugin &&
Copy link
Member

Choose a reason for hiding this comment

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

Kinda like just doing an if check in this case?

@hzoo hzoo merged commit 07dc85b into babel:master Dec 12, 2016
@hzoo
Copy link
Member

hzoo commented Dec 12, 2016

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants