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

Detect and apply stripping flow types #1864

Merged
merged 9 commits into from Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/transforms/babel.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -148,6 +149,11 @@ async function getBabelConfig(asset) {
return jsxConfig;
}

// If there is a Flow config, return that
if (flowConfig) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess we also need to merge the flow config with the generated env/jsx configs above right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably, fixed it

return flowConfig;
}

// Otherwise, don't run babel at all
return null;
}
Expand Down Expand Up @@ -301,3 +307,17 @@ async function getJSXConfig(asset, isSourceModule) {
};
}
}

/**
* Generates a babel config for stripping away Flow types.
*/
function getFlowConfig(asset) {
if (/^(\/{2}|\/\*+) *@flow/.test(asset.contents.substring(0, 20))) {
return {
plugins: [[require('babel-plugin-transform-flow-strip-types')]],
internal: true
};
}

return null;
}
5 changes: 5 additions & 0 deletions test/integration/babel-strip-flow-types/index.js
@@ -0,0 +1,5 @@
const flowModule = require('flow-typed');

module.exports = function() {
return flowModule();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/javascript.js
Expand Up @@ -1346,4 +1346,17 @@ describe('javascript', function() {
assert(output.includes('<html>'));
assert(output.includes('Other page'));
});

it('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'));
});
});
11 changes: 11 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down