Skip to content

Commit

Permalink
Detect and apply stripping flow types (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper authored and devongovett committed Aug 23, 2018
1 parent a789305 commit 7a5f51c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/parcel-bundler/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-object-rest-spread": "^6.26.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.7.0",
Expand Down
4 changes: 1 addition & 3 deletions packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -251,9 +251,7 @@ class Bundler extends EventEmitter {
this.entryAssets.add(asset);
} catch (err) {
throw new Error(
`Cannot resolve entry "${entry}" from "${
this.options.rootDir
}"`
`Cannot resolve entry "${entry}" from "${this.options.rootDir}"`
);
}
}
Expand Down
31 changes: 30 additions & 1 deletion packages/core/parcel-bundler/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 @@ -134,12 +135,21 @@ async function getBabelConfig(asset) {
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);
}

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;
}

Expand All @@ -148,6 +158,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;
}
Expand Down Expand Up @@ -309,3 +324,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;
}
@@ -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 packages/core/parcel-bundler/test/javascript.js
Expand Up @@ -1379,4 +1379,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 @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7a5f51c

Please sign in to comment.