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

[RFC] Support source field in package.json to enable babel on symlinked modules #1101

Merged
merged 3 commits into from
May 1, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Enable babel on symlinked node_modules with package.source field
devongovett committed Mar 30, 2018

Verified

This commit was signed with the committer’s verified signature. The key has expired.
tvdeyen Thomas von Deyen
commit 9d7078449055d4d19968e74ec6b476bacc59ed9a
24 changes: 17 additions & 7 deletions src/transforms/babel.js
Original file line number Diff line number Diff line change
@@ -89,9 +89,18 @@ async function getBabelConfig(asset) {
return asset.babelConfig;
}

let babelrc = await getBabelRc(asset);
let envConfig = await getEnvConfig(asset, !!babelrc);
let jsxConfig = getJSXConfig(asset, !!babelrc);
// Consider the module source code rather than precompiled if the resolver
// used the `source` field, or it is not in node_modules.
let isSource =
!!(asset.package && asset.package.source) ||
!asset.name.includes(NODE_MODULES);

// Try to resolve a .babelrc file. If one is found, consider the module source code.
let babelrc = await getBabelRc(asset, isSource);
isSource = isSource || !!babelrc;

let envConfig = await getEnvConfig(asset, isSource);
let jsxConfig = getJSXConfig(asset, isSource);

// Merge the babel-preset-env config and the babelrc if needed
if (babelrc && !shouldIgnoreBabelrc(asset.name, babelrc)) {
@@ -162,8 +171,9 @@ function getPluginName(p) {
* Finds a .babelrc for an asset. By default, .babelrc files inside node_modules are not used.
* However, there are some exceptions:
* - if `browserify.transforms` includes "babelify" in package.json (for legacy module compat)
* - the `source` field in package.json is used by the resolver
*/
async function getBabelRc(asset) {
async function getBabelRc(asset, isSource) {
// Support legacy browserify packages
let browserify = asset.package && asset.package.browserify;
if (browserify && Array.isArray(browserify.transform)) {
@@ -182,7 +192,7 @@ async function getBabelRc(asset) {
}

// If this asset is not in node_modules, always use the .babelrc
if (!asset.name.includes(NODE_MODULES)) {
if (isSource) {
return await findBabelRc(asset);
}

@@ -224,7 +234,7 @@ async function getEnvConfig(asset, isSourceModule) {

// If this is the app module, the source and target will be the same, so just compile everything.
// Otherwise, load the source engines and generate a babel-present-env config.
if (asset.name.includes(NODE_MODULES) && !isSourceModule) {
if (!isSourceModule) {
let sourceEngines = await getTargetEngines(asset, false);
let sourceEnv = (await getEnvPlugins(sourceEngines, false)) || targetEnv;

@@ -264,7 +274,7 @@ async function getEnvPlugins(targets, useBuiltIns = false) {
*/
function getJSXConfig(asset, isSourceModule) {
// Don't enable JSX in node_modules
if (asset.name.includes(NODE_MODULES) && !isSourceModule) {
if (!isSourceModule) {
return null;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"sourceType": "module"
}
}
4 changes: 4 additions & 0 deletions test/integration/babel-node-modules-source-unlinked/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foo from 'foo';

export {Foo};
export class Bar {}

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "parcel-test-browser-browserslist",
"browserslist": ["last 2 Chrome versions", "IE >= 11"]
}
6 changes: 6 additions & 0 deletions test/integration/babel-node-modules-source/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"sourceType": "module"
}
}
4 changes: 4 additions & 0 deletions test/integration/babel-node-modules-source/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foo from 'foo';

export {Foo};
export class Bar {}

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

4 changes: 4 additions & 0 deletions test/integration/babel-node-modules-source/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "parcel-test-browser-browserslist",
"browserslist": ["last 2 Chrome versions", "IE >= 11"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"source": true
}
18 changes: 18 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
@@ -723,6 +723,24 @@ describe('javascript', function() {
assert(!file.includes('class Bar {}'));
});

it('should compile node_modules when symlinked with a source field in package.json', async function() {
await bundle(__dirname + '/integration/babel-node-modules-source/index.js');

let file = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(!file.includes('class Foo {}'));
assert(!file.includes('class Bar {}'));
});

it('should not compile node_modules with a source field in package.json when not symlinked', async function() {
await bundle(
__dirname + '/integration/babel-node-modules-source-unlinked/index.js'
);

let file = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
assert(file.includes('class Foo {}'));
assert(!file.includes('class Bar {}'));
});

it('should support compiling JSX', async function() {
await bundle(__dirname + '/integration/jsx/index.jsx');