Skip to content

Commit 29269b5

Browse files
authored
Resolve ESModules by default (#4191)
* Default to esmodules * New packager * Fix edge case with handling exports * Test #1 * Test #2 * Test #2 * New try * Patch resolve in babel * Fix plugin * Logging * Remove esmodule check * Change fallback * Put check back * Remove isEsModule check code * Put convert esmodule code back in * Optimize for speed * Set a better default config * Simplify boolean * Set default babel configs
1 parent 4cbf5be commit 29269b5

File tree

28 files changed

+295
-89
lines changed

28 files changed

+295
-89
lines changed

packages/app/config/babel.dev.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ module.exports = {
1313
require.resolve('@babel/preset-env'),
1414
{
1515
targets: {
16-
chrome: 70,
16+
browsers: 'last 1 chrome versions',
1717
// We currently minify with uglify
1818
// Remove after https://github.com/mishoo/UglifyJS2/issues/448
1919
},
2020
// Disable polyfill transforms
2121
useBuiltIns: false,
2222
modules: false,
23-
forceAllTransforms: !process.env.LOCAL_DEV,
2423
},
2524
],
2625
// JSX, Flow
@@ -29,18 +28,11 @@ module.exports = {
2928
].filter(Boolean),
3029
plugins: [
3130
require.resolve('react-hot-loader/babel'),
32-
require.resolve('@babel/plugin-transform-template-literals'),
33-
require.resolve('@babel/plugin-transform-destructuring'),
34-
require.resolve('@babel/plugin-proposal-object-rest-spread'),
35-
require.resolve('@babel/plugin-proposal-class-properties'),
36-
require.resolve('@babel/plugin-proposal-optional-chaining'),
37-
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
38-
require.resolve('@babel/plugin-transform-runtime'),
3931
require.resolve('babel-plugin-lodash'),
40-
require.resolve('@babel/plugin-syntax-dynamic-import'),
4132
require.resolve('babel-plugin-styled-components'),
4233
require.resolve('babel-plugin-macros'),
4334
require.resolve('babel-plugin-graphql-tag'),
35+
require.resolve('@babel/plugin-proposal-class-properties'),
4436
require.resolve('@babel/plugin-transform-react-display-name'),
4537
],
4638
};

packages/app/config/webpack.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const APP_HOT = Boolean(process.env.APP_HOT);
2222
const __DEV__ = NODE_ENV === 'development'; // eslint-disable-line no-underscore-dangle
2323
const __PROD__ = NODE_ENV === 'production'; // eslint-disable-line no-underscore-dangle
2424
// const __TEST__ = NODE_ENV === 'test'; // eslint-disable-line no-underscore-dangle
25-
const babelConfig = __DEV__ && !SANDBOX_ONLY ? babelDev : babelProd;
25+
const babelConfig = __DEV__ ? babelDev : babelProd;
2626
const publicPath = SANDBOX_ONLY || __DEV__ ? '/' : getHost.default() + '/';
2727
const isLint = 'LINT' in process.env;
2828

packages/app/integration-tests/tests/sandboxes.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ function loadSandbox(page, sandboxId, timeout) {
8181
page.goto(sandboxUrl(sandboxId), {
8282
timeout: 0, // we manage the timeout ourselves
8383
});
84+
page.on('console', msg => {
85+
for (let i = 0; i < msg.args().length; ++i) {
86+
console.log(`${i}: ${msg.args()[i]}`); // eslint-disable-line
87+
}
88+
});
89+
page.on('error', msg => {
90+
console.log('error', msg); // eslint-disable-line
91+
});
8492
await pageLoaded(page);
8593
clearTimeout(timer);
8694
await page.waitFor(2 * SECOND);

packages/app/src/sandbox/eval/manager.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,8 +1088,6 @@ export default class Manager implements IEvaluator {
10881088

10891089
if (
10901090
!this.manifest.contents[tModule.module.path] ||
1091-
(tModule.module.path.endsWith('.js') &&
1092-
tModule.module.requires == null) ||
10931091
tModule.module.downloaded
10941092
) {
10951093
// Only save modules that are not precomputed

packages/app/src/sandbox/eval/presets/angular-cli/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,36 @@ export default function initialize() {
224224
]);
225225

226226
preset.registerTranspiler(module => /\.js$/.test(module.path), [
227-
{ transpiler: babelTranspiler },
227+
{
228+
transpiler: babelTranspiler,
229+
options: {
230+
config: {
231+
presets: ['es2015', 'react', 'stage-0'],
232+
plugins: [
233+
'transform-async-to-generator',
234+
'transform-object-rest-spread',
235+
'transform-decorators-legacy',
236+
'transform-class-properties',
237+
// Polyfills the runtime needed for async/await and generators
238+
[
239+
'transform-runtime',
240+
{
241+
helpers: false,
242+
polyfill: false,
243+
regenerator: true,
244+
},
245+
],
246+
[
247+
'transform-regenerator',
248+
{
249+
// Async functions are converted to generators by babel-preset-env
250+
async: false,
251+
},
252+
],
253+
],
254+
},
255+
},
256+
},
228257
]);
229258

230259
preset.registerTranspiler(module => /\.json$/.test(module.path), [

packages/app/src/sandbox/eval/presets/create-react-app/v1.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,36 @@ export default function initialize() {
2626
]);
2727

2828
preset.registerTranspiler(module => /\.jsx?$/.test(module.path), [
29-
{ transpiler: babelTranspiler },
29+
{
30+
transpiler: babelTranspiler,
31+
options: {
32+
config: {
33+
presets: ['es2015', 'react', 'stage-0'],
34+
plugins: [
35+
'transform-async-to-generator',
36+
'transform-object-rest-spread',
37+
'transform-decorators-legacy',
38+
'transform-class-properties',
39+
// Polyfills the runtime needed for async/await and generators
40+
[
41+
'transform-runtime',
42+
{
43+
helpers: false,
44+
polyfill: false,
45+
regenerator: true,
46+
},
47+
],
48+
[
49+
'transform-regenerator',
50+
{
51+
// Async functions are converted to generators by babel-preset-env
52+
async: false,
53+
},
54+
],
55+
],
56+
},
57+
},
58+
},
3059
]);
3160

3261
preset.registerTranspiler(module => /\.json$/.test(module.path), [

packages/app/src/sandbox/eval/presets/cxjs/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ export default function initialize() {
2323
options: {
2424
dynamicCSSModules: true,
2525
compileNodeModulesWithEnv: true,
26+
27+
config: {
28+
presets: ['es2015', 'react', 'stage-0'],
29+
plugins: [
30+
'transform-async-to-generator',
31+
'transform-object-rest-spread',
32+
'transform-decorators-legacy',
33+
'transform-class-properties',
34+
// Polyfills the runtime needed for async/await and generators
35+
[
36+
'transform-runtime',
37+
{
38+
helpers: false,
39+
polyfill: false,
40+
regenerator: true,
41+
},
42+
],
43+
[
44+
'transform-regenerator',
45+
{
46+
// Async functions are converted to generators by babel-preset-env
47+
async: false,
48+
},
49+
],
50+
],
51+
},
2652
},
2753
},
2854
]);

packages/app/src/sandbox/eval/transpiled-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,10 +1116,10 @@ export default class TranspiledModule {
11161116
};
11171117

11181118
const isNpmDependnecy = this.module.path.startsWith('/node_modules/');
1119+
const canOptimizeSize = sourceEqualsCompiled && optimizeForSize;
11191120
// Don't cache source if it didn't change, also don't cache changed source from npm
11201121
// dependencies as we can compile those really quickly.
1121-
const shouldCacheTranspiledSource =
1122-
!(sourceEqualsCompiled && optimizeForSize) && !isNpmDependnecy;
1122+
const shouldCacheTranspiledSource = !canOptimizeSize && !isNpmDependnecy;
11231123

11241124
if (shouldCacheTranspiledSource) {
11251125
serializableObject.source = this.source;

packages/app/src/sandbox/eval/transpilers/babel/babel-parser.js renamed to packages/app/src/sandbox/eval/transpilers/babel/babel-parser.ts

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
11
// @flow
22

33
const DEFAULT_BABEL_CONFIG = {
4-
presets: ['es2015', 'react', 'stage-0'],
5-
plugins: [
6-
'transform-async-to-generator',
7-
'transform-object-rest-spread',
8-
'transform-decorators-legacy',
9-
'transform-class-properties',
10-
// Polyfills the runtime needed for async/await and generators
11-
[
12-
'transform-runtime',
13-
{
14-
helpers: false,
15-
polyfill: false,
16-
regenerator: true,
17-
},
18-
],
19-
[
20-
'transform-regenerator',
21-
{
22-
// Async functions are converted to generators by babel-preset-env
23-
async: false,
24-
},
25-
],
26-
],
4+
presets: ['env', 'react'],
5+
plugins: [],
276
};
287

298
/**
309
* Parses the .babelrc if it exists, if it doesn't it will return a default config
3110
*/
3211
export default function getBabelConfig(
3312
config: Object | undefined,
34-
loaderOptions: Object,
13+
loaderOptions: { disableCodeSandboxPlugins?: boolean },
3514
path: string,
3615
isV7: boolean = false
3716
) {
@@ -41,7 +20,13 @@ export default function getBabelConfig(
4120
return resolvedConfig;
4221
}
4322

44-
const finalConfig = {
23+
const finalConfig: Partial<{
24+
sourceMaps: string;
25+
sourceFileName: string;
26+
filename: string;
27+
plugins: string[];
28+
presets: string[];
29+
}> = {
4530
...resolvedConfig,
4631
sourceMaps: 'inline',
4732
sourceFileName: path,

0 commit comments

Comments
 (0)