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

Switch to babel preset env + async/await/generator support #1668

Merged
merged 4 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/storyshots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@storybook/react": "^3.2.5",
"babel-cli": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"react": "^15.6.1",
"react-dom": "^15.6.1"
Expand Down
4 changes: 2 additions & 2 deletions app/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"babel-plugin-transform-regenerator": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-env": "^1.6.0",
"babel-preset-minify": "^0.2.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-runtime": "^6.23.0",
Expand Down
25 changes: 6 additions & 19 deletions app/react-native/src/server/config/babel.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
presets: [
// let, const, destructuring, classes, modules
require.resolve('babel-preset-es2015'),
// exponentiation
require.resolve('babel-preset-es2016'),
// JSX, Flow
[
require.resolve('babel-preset-env'),
{
modules: false,
},
],
require.resolve('babel-preset-react'),
],
plugins: [
// function x(a, b, c,) { }
require.resolve('babel-plugin-syntax-trailing-function-commas'),
// await fetch()
require.resolve('babel-plugin-syntax-async-functions'),
// class { handleClick = () => { } }
require.resolve('babel-plugin-transform-class-properties'),
// { ...todo, completed: true }
require.resolve('babel-plugin-transform-object-rest-spread'),
// function* () { yield 42; yield 43; }
require.resolve('babel-plugin-transform-regenerator'),
// Polyfills the runtime needed for async/await and generators
[
Expand Down
28 changes: 7 additions & 21 deletions app/react-native/src/server/config/babel.prod.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
presets: [
// let, const, destructuring, classes, modules
require.resolve('babel-preset-es2015'),
// exponentiation
require.resolve('babel-preset-es2016'),
// JSX, Flow
[
require.resolve('babel-preset-env'),
{
modules: false,
},
],
require.resolve('babel-preset-react'),
require.resolve('babel-preset-minify'),
],
plugins: [
// function x(a, b, c,) { }
Copy link
Member

Choose a reason for hiding this comment

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

these comments are kinda helpful to figuring out what these transforms do.

Copy link
Member Author

Choose a reason for hiding this comment

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

Mah, I find the package names pretty self-explanatory. I wonder if I can just switch this to babel-preset-stage-n?

Copy link
Member

@danielduan danielduan Aug 16, 2017

Choose a reason for hiding this comment

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

that might make everyone's lives easier.

I actually see these things as an custom babel config templates for users as well. we really don't have any documentation on what sort of plugins users would need to correctly compile storybook with their own configs.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's because we're not maintaining/documenting babel..

It's just a .babelrc. would you expect us to document/instruct people how to config that?

require.resolve('babel-plugin-syntax-trailing-function-commas'),
// await fetch()
require.resolve('babel-plugin-syntax-async-functions'),
// class { handleClick = () => { } }
require.resolve('babel-plugin-transform-class-properties'),
// { ...todo, completed: true }
require.resolve('babel-plugin-transform-object-rest-spread'),
// function* () { yield 42; yield 43; }
require.resolve('babel-plugin-transform-regenerator'),
// Polyfills the runtime needed for async/await and generators
[
Expand All @@ -38,7 +26,5 @@ module.exports = {
regenerator: true,
},
],
// Optimization: hoist JSX that never changes out of render()
require.resolve('babel-plugin-transform-react-constant-elements'),
],
};
4 changes: 2 additions & 2 deletions app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"babel-core": "^6.25.0",
"babel-loader": "^7.0.0",
"babel-plugin-react-docgen": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-env": "^1.6.0",
"babel-preset-minify": "^0.2.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "^3.0.0",
"babel-preset-stage-0": "^6.24.1",
Expand Down
6 changes: 3 additions & 3 deletions app/react/src/server/babel_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
],
"plugins": [
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
],
"plugins": "bar-plugin"
Expand All @@ -68,7 +68,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
]
}`,
Expand Down
32 changes: 22 additions & 10 deletions app/react/src/server/config/babel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
const findCacheDir = require('find-cache-dir');

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
cacheDirectory: findCacheDir({ name: 'react-storybook' }),
presets: [
require.resolve('babel-preset-es2015'),
require.resolve('babel-preset-es2016'),
[
require.resolve('babel-preset-env'),
{
targets: {
browsers: ['last 2 versions', 'safari >= 7'],
Copy link
Member

@danielduan danielduan Aug 16, 2017

Choose a reason for hiding this comment

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

we're targeting every browser > 1% in the style prefixer:
https://github.com/storybooks/storybook/blob/master/app/react/src/server/config/defaults/webpack.config.js#L30
I think whichever one we decide on, we should keep the two uniform.

my opinion is that storybook should be on as wide of a platform as we can effortlessly support so users can view what components look like in say IE10 or FF ESR releases. last 2 versions seem really restrictive to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for this remark, what do you suggest we use?

Copy link
Member

Choose a reason for hiding this comment

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

I like what the other config is targeting. maybe we can merge the safari >= 7 in there as well. seems to basically support most browsers in the last 5 years

Copy link
Member

@Hypnosphi Hypnosphi Aug 16, 2017

Choose a reason for hiding this comment

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

so users can view what components look like in say IE10

https://www.xfive.co/blog/stop-supporting-ie10-ie9-ie8/

Copy link
Member

Choose a reason for hiding this comment

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

@Hypnosphi we can't make these kind of decisions for developers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Every tool for or -working in- browsers has made some consideration on how backwards compatible they are.

We can provide what we think is a sane default. We already provide developers with ways to change this.

Copy link
Member

Choose a reason for hiding this comment

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

I vote we target:

  • IE 11
  • FF ESR
  • last 4 versions of Edge, Safari, Chrome, Firefox

},
modules: false,
},
],
require.resolve('babel-preset-stage-0'),
require.resolve('babel-preset-react'),
],
plugins: [
require.resolve('babel-plugin-transform-regenerator'),
Copy link
Member

@danielduan danielduan Aug 16, 2017

Choose a reason for hiding this comment

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

I'm pretty opposed to this plugin because we don't have it in our code base and I would hope we wouldn't need to polyfill it in the future because of the compiled bundle size and performance on browsers that don't natively support generators.

see generators section:
http://incaseofstairs.com/2015/06/es6-feature-performance/

it should probably be a custom babel config if users are looking for this plugin.

Copy link
Member Author

Choose a reason for hiding this comment

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

You think we should prevent people from using async/await/generators if not supported by their browser?

On the other hand we have people opening issues expecting support for it.

And in this same PR I'm asked to support browsers from ±5 years ago.

I feel like I'm, in a no-win situation here 😃

I get where you're coming from. enabling this means enabling it for all browsers even the ones that DO support it right?
I agree that's bed considering how much lower the performance is of the transpiled code vs native implementation.

Copy link
Member

Choose a reason for hiding this comment

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

seems like people are using async in their code and are expecting this. i'd vote that we support out of the box it even if it leads to a little bloat. we can also provide a recipe for how to opt out with custom .babelrc?

Copy link
Member

@danielduan danielduan Aug 17, 2017

Choose a reason for hiding this comment

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

My gripe with polyfills is that that the older and less powerful the system is, the more performance hit you get. I'm okay with single digit multiples to a certain extent, but when generators polyfill to be 100x-700x slower, an operation that takes 10ms to complete will now take 1s-7s on the thread in FF, IE, and old mobile browsers? you pretty much get a frozen window at that point :(

It does look like a popular request so I'm gonna give in and support this one. I really hope people who are using these polyfills know the performance implication of what they're doing too.

[
require.resolve('babel-plugin-transform-runtime'),
{
helpers: true,
polyfill: true,
regenerator: true,
},
],
],
};
32 changes: 21 additions & 11 deletions app/react/src/server/config/babel.prod.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
presets: [
require.resolve('babel-preset-es2015'),
require.resolve('babel-preset-es2016'),
[
require.resolve('babel-preset-env'),
{
targets: {
browsers: ['last 2 versions', 'safari >= 7'],
},
modules: false,
},
],
require.resolve('babel-preset-stage-0'),
require.resolve('babel-preset-react'),
require.resolve('babel-preset-minify'),
],
plugins: [
require.resolve('babel-plugin-transform-regenerator'),
[
require.resolve('babel-plugin-transform-runtime'),
{
helpers: true,
polyfill: true,
regenerator: true,
},
],
],
};
1 change: 1 addition & 0 deletions app/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"babel-loader": "^7.0.0",
"babel-plugin-react-docgen": "^1.6.0",
"babel-preset-env": "^1.6.0",
"babel-preset-minify": "^0.2.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "^3.0.0",
"babel-preset-stage-0": "^6.24.1",
Expand Down
6 changes: 3 additions & 3 deletions app/vue/src/server/babel_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
],
"plugins": [
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
],
"plugins": "bar-plugin"
Expand All @@ -68,7 +68,7 @@ describe('babel_config', () => {
mock({
'.babelrc': `{
"presets": [
"es2015",
"env",
"foo-preset"
]
}`,
Expand Down
33 changes: 20 additions & 13 deletions app/vue/src/server/config/babel.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

const findCacheDir = require('find-cache-dir');

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables a cache directory for faster-rebuilds
// `find-cache-dir` will create the cache directory under the node_modules directory.
cacheDirectory: findCacheDir({ name: 'react-storybook' }),
presets: [
require.resolve('babel-preset-env'),
[
require.resolve('babel-preset-env'),
{
targets: {
browsers: ['last 2 versions', 'safari >= 7'],
},
modules: false,
},
],
require.resolve('babel-preset-stage-0'),
require.resolve('babel-preset-react'),
],
plugins: [
require.resolve('babel-plugin-transform-regenerator'),
[
require.resolve('babel-plugin-transform-runtime'),
{
helpers: true,
polyfill: true,
regenerator: true,
},
],
],
};
31 changes: 21 additions & 10 deletions app/vue/src/server/config/babel.prod.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = {
// Don't try to find .babelrc because we want to force this configuration.
babelrc: false,
presets: [
require.resolve('babel-preset-env'),
[
require.resolve('babel-preset-env'),
{
targets: {
browsers: ['last 2 versions', 'safari >= 7'],
},
modules: false,
},
],
require.resolve('babel-preset-stage-0'),
require.resolve('babel-preset-react'),
require.resolve('babel-preset-minify'),
],
plugins: [
require.resolve('babel-plugin-transform-regenerator'),
[
require.resolve('babel-plugin-transform-runtime'),
{
helpers: true,
polyfill: true,
regenerator: true,
},
],
],
};
11 changes: 4 additions & 7 deletions lib/cli/generators/METEOR/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module.exports = Promise.all([
latestVersion('@storybook/react'),
latestVersion('react'),
latestVersion('react-dom'),
latestVersion('babel-preset-es2015'),
latestVersion('babel-preset-es2016'),
latestVersion('babel-preset-env'),
latestVersion('babel-preset-react'),
latestVersion('babel-preset-stage-1'),
latestVersion('babel-root-slash-import'),
Expand All @@ -20,8 +19,7 @@ module.exports = Promise.all([
storybookVersion,
reactVersion,
reactDomVersion,
presetEs2015Version,
presetEs2016Version,
presetEnvVersion,
presetReactVersion,
presetStage1Version,
rootSlashImportVersion,
Expand All @@ -47,12 +45,11 @@ module.exports = Promise.all([
}
} else {
babelrc = {
presets: ['es2015', 'es2016', 'react', 'stage-1'],
presets: ['env', 'react', 'stage-1'],
plugins: ['babel-root-slash-import'],
};

packageJson.devDependencies['babel-preset-es2015'] = `^${presetEs2015Version}`;
packageJson.devDependencies['babel-preset-es2016'] = `^${presetEs2016Version}`;
packageJson.devDependencies['babel-preset-env'] = `^${presetEnvVersion}`;
packageJson.devDependencies['babel-preset-react'] = `^${presetReactVersion}`;
packageJson.devDependencies['babel-preset-stage-1'] = `^${presetStage1Version}`;
packageJson.devDependencies['babel-root-slash-import'] = `^${rootSlashImportVersion}`;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.2",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"eslint": "^3.19.0",
Expand Down
Loading