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

Enable transpiling node_modules #7399

Merged
merged 7 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/core/core/src/Parcel.js
Expand Up @@ -164,7 +164,6 @@ export default class Parcel {
this.#disposable.dispose(),
await this.#requestTracker.writeToCache(),
]);
await this.#farm.callAllWorkers('clearConfigCache', []);
}

async _startNextBuild(): Promise<?BuildEvent> {
Expand Down Expand Up @@ -359,6 +358,8 @@ export default class Parcel {
if (this.isProfiling) {
await this.stopProfiling();
}

await this.#farm.callAllWorkers('clearConfigCache', []);
}
}

Expand Down
16 changes: 14 additions & 2 deletions packages/core/core/src/public/Environment.js
Expand Up @@ -57,13 +57,15 @@ const supportData = {
edge: '16',
firefox: '60',
chrome: '61',
safari: '11',
safari: '10.1',
opera: '48',
ios: '11',
ios: '10.3',
android: '76',
and_chr: '76',
and_ff: '68',
samsung: '8.2',
and_qq: '10.4',
op_mob: '64',
},
'dynamic-import': {
edge: '76',
Expand All @@ -76,6 +78,8 @@ const supportData = {
and_chr: '63',
and_ff: '67',
samsung: '8',
and_qq: '10.4',
op_mob: '64',
},
'worker-module': {
edge: '80',
Expand All @@ -98,6 +102,8 @@ const supportData = {
and_chr: '64',
and_ff: '62',
samsung: '9.2',
and_qq: '10.4',
op_mob: '64',
},
'arrow-functions': {
chrome: '47',
Expand All @@ -109,6 +115,12 @@ const supportData = {
ios: '10',
samsung: '5',
electron: '0.36',
android: '50',
qq: '10.4',
baidu: '7.12',
kaios: '2.5',
and_qq: '12.12',
op_mob: '64',
},
};

Expand Down
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"sourceType": "module"
}
}
@@ -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.

@@ -0,0 +1,5 @@
{
"name": "parcel-test-browser-browserslist",
"private": true,
"browserslist": ["last 2 Chrome versions", "IE >= 11"]
}
17 changes: 13 additions & 4 deletions packages/core/integration-tests/test/transpilation.js
Expand Up @@ -56,17 +56,26 @@ describe('transpilation', function () {
assert(modern.includes('class Bar'));
});

it('should not transpile node_modules by default', async function () {
it('should transpile node_modules by default', async function () {
await bundle(
path.join(__dirname, '/integration/babel-node-modules/index.js'),
);

let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(/class \S+ \{/.test(file));
assert(!/class \S+ \{/.test(file));
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 not support JSX in node_modules', async function () {
// $FlowFixMe
await assert.rejects(() =>
bundle(
path.join(__dirname, '/integration/babel-node-modules-jsx/index.js'),
),
);
});

it('should compile node_modules with a source field in package.json when not symlinked', async function () {
await bundle(
path.join(
__dirname,
Expand All @@ -75,7 +84,7 @@ describe('transpilation', function () {
);

let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!file.includes('function Foo'));
assert(file.includes('function Foo'));
assert(file.includes('function Bar'));
});

Expand Down
68 changes: 33 additions & 35 deletions packages/transformers/js/src/JSTransformer.js
Expand Up @@ -290,49 +290,47 @@ export default (new Transformer({
]);

let targets;
if (asset.isSource) {
if (asset.env.isElectron() && asset.env.engines.electron) {
targets = {
electron: semver.minVersion(asset.env.engines.electron)?.toString(),
};
} else if (asset.env.isBrowser() && asset.env.engines.browsers) {
targets = {};
if (asset.env.isElectron() && asset.env.engines.electron) {
targets = {
electron: semver.minVersion(asset.env.engines.electron)?.toString(),
};
} else if (asset.env.isBrowser() && asset.env.engines.browsers) {
targets = {};

let browsers = Array.isArray(asset.env.engines.browsers)
? asset.env.engines.browsers
: [asset.env.engines.browsers];
let browsers = Array.isArray(asset.env.engines.browsers)
? asset.env.engines.browsers
: [asset.env.engines.browsers];

// If the output format is esmodule, exclude browsers
// that support them natively so that we transpile less.
if (asset.env.outputFormat === 'esmodule') {
browsers = [...browsers, ...ESMODULE_BROWSERS];
}
// If the output format is esmodule, exclude browsers
// that support them natively so that we transpile less.
if (asset.env.outputFormat === 'esmodule') {
browsers = [...browsers, ...ESMODULE_BROWSERS];
}

browsers = browserslist(browsers);
for (let browser of browsers) {
let [name, version] = browser.split(' ');
if (BROWSER_MAPPING.hasOwnProperty(name)) {
name = BROWSER_MAPPING[name];
if (!name) {
continue;
}
browsers = browserslist(browsers);
for (let browser of browsers) {
let [name, version] = browser.split(' ');
if (BROWSER_MAPPING.hasOwnProperty(name)) {
name = BROWSER_MAPPING[name];
if (!name) {
continue;
}
}

let [major, minor = '0', patch = '0'] = version
.split('-')[0]
.split('.');
let semverVersion = `${major}.${minor}.${patch}`;
let [major, minor = '0', patch = '0'] = version
.split('-')[0]
.split('.');
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
continue;
}
let semverVersion = `${major}.${minor}.${patch}`;

if (
targets[name] == null ||
semver.gt(targets[name], semverVersion)
) {
targets[name] = semverVersion;
}
if (targets[name] == null || semver.gt(targets[name], semverVersion)) {
targets[name] = semverVersion;
}
} else if (asset.env.isNode() && asset.env.engines.node) {
targets = {node: semver.minVersion(asset.env.engines.node)?.toString()};
}
} else if (asset.env.isNode() && asset.env.engines.node) {
targets = {node: semver.minVersion(asset.env.engines.node)?.toString()};
}

let env: EnvMap = {};
Expand Down