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

Fix env transforms in node_modules #2073

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 9 additions & 6 deletions src/transforms/babel/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ async function getEnvConfig(asset, isSourceModule) {
// Otherwise, load the source engines and generate a babel-present-env config.
if (!isSourceModule) {
let sourceEngines = await getTargetEngines(asset, false);
let sourceEnv = (await getEnvPlugins(sourceEngines, false)) || targetEnv;
let sourceEnv = await getEnvPlugins(sourceEngines, false);

// Do a diff of the returned plugins. We only need to process the remaining plugins to get to the app target.
let sourcePlugins = new Set(sourceEnv.map(p => p[0]));
targetEnv = targetEnv.filter(plugin => {
return !sourcePlugins.has(plugin[0]);
});
// Do a diff of the returned plugins if there is a known environment for the source.
// We only need to process the remaining plugins to get to the app target.
if (sourceEnv) {
let sourcePlugins = new Set(sourceEnv.map(p => p[0]));
targetEnv = targetEnv.filter(plugin => {
return !sourcePlugins.has(plugin[0]);
});
}
}

return {
Expand Down
5 changes: 0 additions & 5 deletions src/utils/getTargetEngines.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ async function getTargetEngines(asset, isTargetApp) {
}
}
}

// Fall back to package.engines.node for node_modules without any browser target info.
if (!isTargetApp && !targets.browsers && typeof nodeVersion === 'string') {
targets.node = nodeVersion;
}
}

// If we didn't find any targets, set some default engines for the target app.
Expand Down
12 changes: 7 additions & 5 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ describe('javascript', function() {
);
});

it('should not compile node_modules by default', async function() {
it('should make node_modules browser compatible', async function() {
await bundle(
path.join(__dirname, '/integration/babel-node-modules/index.js')
);
Expand All @@ -1158,7 +1158,7 @@ describe('javascript', function() {
path.join(__dirname, '/dist/index.js'),
'utf8'
);
assert(/class \S+ \{\}/.test(file));
assert(!/class \S+ \{\}/.test(file));
assert(file.includes('function Bar'));
});

Expand Down Expand Up @@ -1219,7 +1219,7 @@ describe('javascript', function() {
assert(file.includes('function Bar'));
});

it('should not compile node_modules with a source field in package.json when not symlinked', async function() {
it('Should compile node_modules without a defined target to match the projects browserslist', async function() {
await bundle(
path.join(
__dirname,
Expand All @@ -1231,8 +1231,10 @@ describe('javascript', function() {
path.join(__dirname, '/dist/index.js'),
'utf8'
);
assert(!file.includes('function Foo'));
assert(file.includes('function Bar'));

assert(!/class \S+ \{\}/.test(file), 'should not contain a class');
assert(file.includes('function Foo'), 'should have a function Foo');
assert(file.includes('function Bar'), 'should have a function Bar');
});

it('should support compiling JSX', async function() {
Expand Down
Loading