Skip to content

Commit a9f03f1

Browse files
committed
Only compile node_modules with env on CRA v2
1 parent 7c3834a commit a9f03f1

File tree

2 files changed

+71
-50
lines changed

2 files changed

+71
-50
lines changed

packages/app/src/sandbox/eval/presets/create-react-app/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function initialize() {
3636
) {
3737
const babelOptions = {
3838
isV7: true,
39+
compileNodeModulesWithEnv: true,
3940
config: {
4041
plugins: [
4142
'transform-flow-strip-types',

packages/app/src/sandbox/eval/transpilers/babel/worker/babel-worker.js

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,65 @@ function normalizeV7Config(config) {
189189
};
190190
}
191191

192+
function getCustomConfig(
193+
{ config, codeSandboxPlugins },
194+
version: number,
195+
path: string,
196+
options: Object
197+
) {
198+
if (
199+
/^\/node_modules/.test(path) &&
200+
/\.js$/.test(path) &&
201+
options.compileNodeModulesWithEnv
202+
) {
203+
if (version === 7) {
204+
return {
205+
parserOpts: {
206+
plugins: ['dynamicImport', 'objectRestSpread'],
207+
},
208+
presets: ['env', 'react'],
209+
plugins: [
210+
'transform-modules-commonjs',
211+
'proposal-class-properties',
212+
'@babel/plugin-transform-runtime',
213+
...codeSandboxPlugins,
214+
],
215+
};
216+
}
217+
218+
return {
219+
presets: ['es2015', 'react', 'stage-0'],
220+
plugins: [
221+
'transform-es2015-modules-commonjs',
222+
'transform-class-properties',
223+
[
224+
'transform-runtime',
225+
{
226+
helpers: false,
227+
polyfill: false,
228+
regenerator: true,
229+
},
230+
],
231+
[
232+
'transform-regenerator',
233+
{
234+
// Async functions are converted to generators by babel-preset-env
235+
async: false,
236+
},
237+
],
238+
...codeSandboxPlugins,
239+
],
240+
};
241+
}
242+
243+
return {
244+
...config,
245+
plugins: config.plugins
246+
? [...config.plugins, ...codeSandboxPlugins]
247+
: codeSandboxPlugins,
248+
};
249+
}
250+
192251
async function compile(code, customConfig, path) {
193252
try {
194253
let result;
@@ -357,70 +416,31 @@ self.addEventListener('message', async event => {
357416
lastConfig = stringifiedConfig;
358417
}
359418

360-
const plugins = [...(config.plugins || [])];
419+
const codeSandboxPlugins = [];
361420

362421
if (!disableCodeSandboxPlugins) {
363-
plugins.push('dynamic-import-node');
422+
codeSandboxPlugins.push('dynamic-import-node');
364423

365424
if (loaderOptions.dynamicCSSModules) {
366-
plugins.push('dynamic-css-modules');
425+
codeSandboxPlugins.push('dynamic-css-modules');
367426
}
368427

369428
if (!sandboxOptions || sandboxOptions.infiniteLoopProtection) {
370-
plugins.push('babel-plugin-transform-prevent-infinite-loops');
429+
codeSandboxPlugins.push('babel-plugin-transform-prevent-infinite-loops');
371430
}
372431
}
373432

374-
plugins.push([
433+
codeSandboxPlugins.push([
375434
'babel-plugin-detective',
376435
{ source: true, nodes: true, generated: true },
377436
]);
378437

379-
const customConfig =
380-
/^\/node_modules/.test(path) && /\.js$/.test(path)
381-
? {
382-
parserOpts: version === 7 && {
383-
plugins: ['dynamicImport', 'objectRestSpread'],
384-
},
385-
presets:
386-
version === 7 ? ['env', 'react'] : ['es2015', 'react', 'stage-0'],
387-
plugins: [
388-
version === 7
389-
? 'transform-modules-commonjs'
390-
: 'transform-es2015-modules-commonjs',
391-
version === 7
392-
? 'proposal-class-properties'
393-
: 'transform-class-properties',
394-
...(version === 7
395-
? ['@babel/plugin-transform-runtime']
396-
: [
397-
[
398-
'transform-runtime',
399-
{
400-
helpers: false,
401-
polyfill: false,
402-
regenerator: true,
403-
},
404-
],
405-
[
406-
'transform-regenerator',
407-
{
408-
// Async functions are converted to generators by babel-preset-env
409-
async: false,
410-
},
411-
],
412-
]),
413-
'dynamic-import-node',
414-
[
415-
'babel-plugin-detective',
416-
{ source: true, nodes: true, generated: true },
417-
],
418-
].filter(Boolean),
419-
}
420-
: {
421-
...config,
422-
plugins,
423-
};
438+
const customConfig = getCustomConfig(
439+
{ config, codeSandboxPlugins },
440+
version,
441+
path,
442+
loaderOptions
443+
);
424444

425445
const flattenedPresets = flatten(customConfig.presets || []);
426446
const flattenedPlugins = flatten(customConfig.plugins || []);

0 commit comments

Comments
 (0)