From 98dbe999cd61c7fe5a4842e9e7975438a57b0a4f Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Tue, 7 Aug 2018 17:43:12 -0700 Subject: [PATCH 1/8] detect and apply stripping flow types --- package.json | 1 + src/transforms/babel.js | 20 ++++++++++++++++++++ yarn.lock | 11 +++++++++++ 3 files changed, 32 insertions(+) diff --git a/package.json b/package.json index fe02cf5cd76..b959c046957 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "babel-core": "^6.25.0", "babel-generator": "^6.25.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", + "babel-plugin-transform-flow-strip-types": "^6.22.0", "babel-plugin-transform-react-jsx": "^6.24.1", "babel-preset-env": "^1.7.0", "babel-template": "^6.26.0", diff --git a/src/transforms/babel.js b/src/transforms/babel.js index 841d6a65ba9..905dd33a517 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -103,6 +103,7 @@ async function getBabelConfig(asset) { let envConfig = await getEnvConfig(asset, isSource); let jsxConfig = await getJSXConfig(asset, isSource); + let flowConfig = getFlowConfig(asset, isSource); // Merge the babel-preset-env config and the babelrc if needed if (babelrc && !shouldIgnoreBabelrc(asset.name, babelrc)) { @@ -148,6 +149,11 @@ async function getBabelConfig(asset) { return jsxConfig; } + // If there is a Flow config, return that + if (flowConfig) { + return flowConfig; + } + // Otherwise, don't run babel at all return null; } @@ -301,3 +307,17 @@ async function getJSXConfig(asset, isSourceModule) { }; } } + +/** + * Generates a babel config for stripping away Flow types. + */ +function getFlowConfig(asset, isSourceModule) { + if (!isSourceModule && asset.contents.includes('@flow')) { + return { + plugins: [[require('babel-plugin-transform-flow-strip-types')]], + internal: true + }; + } + + return null; +} diff --git a/yarn.lock b/yarn.lock index 21ec9283731..1cf4e26fe45 100644 --- a/yarn.lock +++ b/yarn.lock @@ -539,6 +539,10 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de" +babel-plugin-syntax-flow@^6.18.0: + version "6.18.0" + resolved "https://registry.yarnpkg.com/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d" + babel-plugin-syntax-jsx@^6.8.0: version "6.18.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946" @@ -737,6 +741,13 @@ babel-plugin-transform-exponentiation-operator@^6.22.0: babel-plugin-syntax-exponentiation-operator "^6.8.0" babel-runtime "^6.22.0" +babel-plugin-transform-flow-strip-types@^6.22.0: + version "6.22.0" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-strip-types/-/babel-plugin-transform-flow-strip-types-6.22.0.tgz#84cb672935d43714fdc32bce84568d87441cf7cf" + dependencies: + babel-plugin-syntax-flow "^6.18.0" + babel-runtime "^6.22.0" + babel-plugin-transform-react-jsx@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-jsx/-/babel-plugin-transform-react-jsx-6.24.1.tgz#840a028e7df460dfc3a2d29f0c0d91f6376e66a3" From 9511e9759d23fb3a3d5747766568ee14b4d741ce Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Wed, 8 Aug 2018 09:23:56 -0700 Subject: [PATCH 2/8] add test --- test/integration/babel-strip-flow-types/index.js | 5 +++++ .../node_modules/flow-typed/index.js | 4 ++++ test/javascript.js | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 test/integration/babel-strip-flow-types/index.js create mode 100644 test/integration/babel-strip-flow-types/node_modules/flow-typed/index.js diff --git a/test/integration/babel-strip-flow-types/index.js b/test/integration/babel-strip-flow-types/index.js new file mode 100644 index 00000000000..ca01de4a7ed --- /dev/null +++ b/test/integration/babel-strip-flow-types/index.js @@ -0,0 +1,5 @@ +const flowModule = require('flow-typed'); + +module.exports = function() { + return flowModule(); +} \ No newline at end of file diff --git a/test/integration/babel-strip-flow-types/node_modules/flow-typed/index.js b/test/integration/babel-strip-flow-types/node_modules/flow-typed/index.js new file mode 100644 index 00000000000..f689a67b3bb --- /dev/null +++ b/test/integration/babel-strip-flow-types/node_modules/flow-typed/index.js @@ -0,0 +1,4 @@ +// @flow +import type { OptionsType } from 'Types'; + +module.exports = () => 'hello world'; \ No newline at end of file diff --git a/test/javascript.js b/test/javascript.js index 5a05db1b17b..ccb7cb98f6b 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -1346,4 +1346,11 @@ describe('javascript', function() { assert(output.includes('')); assert(output.includes('Other page')); }); + + it('should strip away flow types of node modules', async function() { + await bundle(__dirname + '/integration/babel-strip-flow-types/index.js'); + + let file = await fs.readFile(__dirname + '/dist/index.js', 'utf8'); + assert(!file.includes('OptionsType')); + }); }); From 9301f5ed5c6e81c822ecc7adaf7c4ad01d05ae7e Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Wed, 8 Aug 2018 09:27:06 -0700 Subject: [PATCH 3/8] improve test --- test/javascript.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/javascript.js b/test/javascript.js index ccb7cb98f6b..224e3ebb8a1 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -1347,8 +1347,14 @@ describe('javascript', function() { assert(output.includes('Other page')); }); - it('should strip away flow types of node modules', async function() { - await bundle(__dirname + '/integration/babel-strip-flow-types/index.js'); + it.only('should strip away flow types of node modules', async function() { + let b = await bundle( + __dirname + '/integration/babel-strip-flow-types/index.js' + ); + + let output = await run(b); + assert.equal(typeof output, 'function'); + assert.equal(output(), 'hello world'); let file = await fs.readFile(__dirname + '/dist/index.js', 'utf8'); assert(!file.includes('OptionsType')); From 48bff7eec7ab7ddc0f4d23be5c3d870777d1a260 Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Wed, 8 Aug 2018 09:38:51 -0700 Subject: [PATCH 4/8] performance improvement --- src/transforms/babel.js | 5 ++++- test/javascript.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/transforms/babel.js b/src/transforms/babel.js index 905dd33a517..6fbba72aa6c 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -312,7 +312,10 @@ async function getJSXConfig(asset, isSourceModule) { * Generates a babel config for stripping away Flow types. */ function getFlowConfig(asset, isSourceModule) { - if (!isSourceModule && asset.contents.includes('@flow')) { + if ( + !isSourceModule && + asset.contents.substring(0, 20).indexOf('@flow') > -1 + ) { return { plugins: [[require('babel-plugin-transform-flow-strip-types')]], internal: true diff --git a/test/javascript.js b/test/javascript.js index 224e3ebb8a1..645effd14c0 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -1347,7 +1347,7 @@ describe('javascript', function() { assert(output.includes('Other page')); }); - it.only('should strip away flow types of node modules', async function() { + it('should strip away flow types of node modules', async function() { let b = await bundle( __dirname + '/integration/babel-strip-flow-types/index.js' ); From 3e5a15862e9ccc9f1cfcdc3eeb1020c1fe7a7682 Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Thu, 23 Aug 2018 13:50:58 -0700 Subject: [PATCH 5/8] using Reg.test and also use on source --- src/transforms/babel.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/transforms/babel.js b/src/transforms/babel.js index 6fbba72aa6c..dad46d3e87c 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -311,11 +311,8 @@ async function getJSXConfig(asset, isSourceModule) { /** * Generates a babel config for stripping away Flow types. */ -function getFlowConfig(asset, isSourceModule) { - if ( - !isSourceModule && - asset.contents.substring(0, 20).indexOf('@flow') > -1 - ) { +function getFlowConfig(asset) { + if (/^(\/{2}|\/\*+) *@flow/.test(asset.contents.substring(0, 20))) { return { plugins: [[require('babel-plugin-transform-flow-strip-types')]], internal: true From ac94de43841b5bd4cac39cf3d5a687a20491ddef Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Thu, 23 Aug 2018 14:02:43 -0700 Subject: [PATCH 6/8] merge configs --- src/transforms/babel.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/transforms/babel.js b/src/transforms/babel.js index a967ea1fb60..5cbabc503f8 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -131,16 +131,24 @@ async function getBabelConfig(asset) { hasPlugin(babelrc.presets, 'react') || hasPlugin(babelrc.plugins, 'transform-react-jsx'); + let hasFlow = hasPlugin(babelrc.plugins, 'transform-flow-strip-types'); + if (!hasReact) { mergeConfigs(babelrc, jsxConfig); } + if (!hasFlow && flowConfig) { + mergeConfigs(babelrc, flowConfig); + } + return babelrc; } // If there is a babel-preset-env config, and it isn't empty use that - if (envConfig && (envConfig.plugins.length > 0 || jsxConfig)) { + if (envConfig && (envConfig.plugins.length > 0 || jsxConfig || flowConfig)) { mergeConfigs(envConfig, jsxConfig); + mergeConfigs(envConfig, flowConfig); + return envConfig; } From 73c46c0ad9164ee5ad3b81220f12653218214249 Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Thu, 23 Aug 2018 14:03:28 -0700 Subject: [PATCH 7/8] reorder flow check --- src/transforms/babel.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transforms/babel.js b/src/transforms/babel.js index 5cbabc503f8..42d8ac6880c 100644 --- a/src/transforms/babel.js +++ b/src/transforms/babel.js @@ -131,12 +131,13 @@ async function getBabelConfig(asset) { hasPlugin(babelrc.presets, 'react') || hasPlugin(babelrc.plugins, 'transform-react-jsx'); - let hasFlow = hasPlugin(babelrc.plugins, 'transform-flow-strip-types'); - if (!hasReact) { mergeConfigs(babelrc, jsxConfig); } + // Add Flow stripping config if it isn't already specified in the babelrc + let hasFlow = hasPlugin(babelrc.plugins, 'transform-flow-strip-types'); + if (!hasFlow && flowConfig) { mergeConfigs(babelrc, flowConfig); } From e74e92ee965521b8f4e11cec7e8ff98855b1a6be Mon Sep 17 00:00:00 2001 From: DeMoorJasper Date: Thu, 23 Aug 2018 14:07:41 -0700 Subject: [PATCH 8/8] fix missing dep --- package.json | 1 + yarn.lock | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index df360a1a96e..bcb0a9a0f33 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "babel-core": "^6.25.0", "babel-generator": "^6.25.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", + "babel-plugin-transform-flow-strip-types": "^6.22.0", "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-plugin-transform-react-jsx": "^6.24.1", "babel-preset-env": "^1.7.0", diff --git a/yarn.lock b/yarn.lock index 6797c36c8a4..14e83f0fa63 100644 --- a/yarn.lock +++ b/yarn.lock @@ -523,6 +523,10 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de" +babel-plugin-syntax-flow@^6.18.0: + version "6.18.0" + resolved "https://artifactory.corp.adobe.com:443/artifactory/api/npm/npm-livefyre-release/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d" + babel-plugin-syntax-jsx@^6.8.0: version "6.18.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946" @@ -725,6 +729,13 @@ babel-plugin-transform-exponentiation-operator@^6.22.0: babel-plugin-syntax-exponentiation-operator "^6.8.0" babel-runtime "^6.22.0" +babel-plugin-transform-flow-strip-types@^6.22.0: + version "6.22.0" + resolved "https://artifactory.corp.adobe.com:443/artifactory/api/npm/npm-livefyre-release/babel-plugin-transform-flow-strip-types/-/babel-plugin-transform-flow-strip-types-6.22.0.tgz#84cb672935d43714fdc32bce84568d87441cf7cf" + dependencies: + babel-plugin-syntax-flow "^6.18.0" + babel-runtime "^6.22.0" + babel-plugin-transform-object-rest-spread@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06" @@ -7244,4 +7255,4 @@ yargs@~3.10.0: camelcase "^1.0.2" cliui "^2.1.0" decamelize "^1.0.0" - window-size "0.1.0" \ No newline at end of file + window-size "0.1.0"