Skip to content

Commit 8af0ec3

Browse files
author
Ives van Hoorne
committed
Improve babel worker size
1 parent 7ab5ea0 commit 8af0ec3

File tree

11 files changed

+259
-51
lines changed

11 files changed

+259
-51
lines changed

config/babel.dev.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ module.exports = {
66
cacheDirectory: true,
77
presets: [
88
// Latest stable ECMAScript features
9-
['env', { modules: false }],
9+
[
10+
'env',
11+
{
12+
targets: {
13+
ie: 11,
14+
// We currently minify with uglify
15+
// Remove after https://github.com/mishoo/UglifyJS2/issues/448
16+
uglify: true,
17+
},
18+
// Disable polyfill transforms
19+
useBuiltIns: false,
20+
modules: false,
21+
},
22+
],
1023
// JSX, Flow
1124
'react',
1225
],

config/babel.prod.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,20 @@ module.exports = {
55
babelrc: false,
66
presets: [
77
// Latest stable ECMAScript features
8-
['env', { modules: false }],
8+
[
9+
'env',
10+
{
11+
targets: {
12+
ie: 11,
13+
// We currently minify with uglify
14+
// Remove after https://github.com/mishoo/UglifyJS2/issues/448
15+
uglify: true,
16+
},
17+
// Disable polyfill transforms
18+
useBuiltIns: false,
19+
modules: false,
20+
},
21+
],
922
// JSX, Flow
1023
'react',
1124
],

src/common/__snapshots__/utl.test.js.snap renamed to src/common/__snapshots__/url.test.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,97 @@
33
exports[`url parameters can autoresize 1`] = `
44
Object {
55
"autoResize": true,
6+
"enableEslint": false,
67
"hideNavigation": false,
78
"isEditorScreen": false,
89
"isInProjectView": true,
910
"isPreviewScreen": false,
11+
"useCodeMirror": false,
1012
}
1113
`;
1214

1315
exports[`url parameters can handle multiple options 1`] = `
1416
Object {
1517
"autoResize": true,
1618
"currentModule": "test",
19+
"enableEslint": false,
1720
"hideNavigation": true,
1821
"isEditorScreen": true,
1922
"isInProjectView": true,
2023
"isPreviewScreen": false,
24+
"useCodeMirror": false,
2125
}
2226
`;
2327

2428
exports[`url parameters can hide navigation 1`] = `
2529
Object {
2630
"autoResize": false,
31+
"enableEslint": false,
2732
"hideNavigation": true,
2833
"isEditorScreen": false,
2934
"isInProjectView": true,
3035
"isPreviewScreen": false,
36+
"useCodeMirror": false,
3137
}
3238
`;
3339

3440
exports[`url parameters doesn't set unknown fields 1`] = `
3541
Object {
3642
"autoResize": false,
43+
"enableEslint": false,
3744
"hideNavigation": false,
3845
"isEditorScreen": false,
3946
"isInProjectView": true,
4047
"isPreviewScreen": false,
48+
"useCodeMirror": false,
4149
}
4250
`;
4351

4452
exports[`url parameters keeps everything false on normal urls 1`] = `
4553
Object {
4654
"autoResize": false,
55+
"enableEslint": false,
4756
"hideNavigation": false,
4857
"isEditorScreen": false,
4958
"isInProjectView": true,
5059
"isPreviewScreen": false,
60+
"useCodeMirror": false,
5161
}
5262
`;
5363

5464
exports[`url parameters sets current module if there is one 1`] = `
5565
Object {
5666
"autoResize": false,
5767
"currentModule": "test",
68+
"enableEslint": false,
5869
"hideNavigation": false,
5970
"isEditorScreen": false,
6071
"isInProjectView": true,
6172
"isPreviewScreen": false,
73+
"useCodeMirror": false,
6274
}
6375
`;
6476

6577
exports[`url parameters sets editor view 1`] = `
6678
Object {
6779
"autoResize": false,
80+
"enableEslint": false,
6881
"hideNavigation": false,
6982
"isEditorScreen": true,
7083
"isInProjectView": true,
7184
"isPreviewScreen": false,
85+
"useCodeMirror": false,
7286
}
7387
`;
7488

7589
exports[`url parameters sets preview view 1`] = `
7690
Object {
7791
"autoResize": false,
92+
"enableEslint": false,
7893
"hideNavigation": false,
7994
"isEditorScreen": false,
8095
"isInProjectView": true,
8196
"isPreviewScreen": true,
97+
"useCodeMirror": false,
8298
}
8399
`;
File renamed without changes.

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// @flow
22

33
import flatten from 'lodash/flatten';
4-
import dynamicImportPlugin from 'babel-plugin-dynamic-import-node';
4+
import dynamicImportPlugin from './plugins/babel-plugin-dynamic-import-node';
5+
import detective from './plugins/babel-plugin-detective';
56

67
import { buildWorkerError } from '../utils/worker-error-handler';
78
import getDependencies from './get-require-statements';
@@ -25,6 +26,7 @@ declare var Babel: {
2526
};
2627

2728
Babel.registerPlugin('dynamic-import-node', dynamicImportPlugin);
29+
Babel.registerPlugin('babel-plugin-detective', detective);
2830

2931
self.addEventListener('message', async event => {
3032
const { code, path, config } = event.data;
@@ -45,7 +47,11 @@ self.addEventListener('message', async event => {
4547
Babel.registerPlugin('jsx-pragmatic', pragmaticPlugin);
4648
}
4749

48-
const plugins = [...config.plugins, 'dynamic-import-node'];
50+
const plugins = [
51+
...config.plugins,
52+
'dynamic-import-node',
53+
['babel-plugin-detective', { source: true, nodes: true }],
54+
];
4955

5056
const customConfig = {
5157
...config,
@@ -55,7 +61,7 @@ self.addEventListener('message', async event => {
5561
try {
5662
const result = Babel.transform(code, customConfig);
5763

58-
const dependencies = getDependencies(result.ast);
64+
const dependencies = getDependencies(detective.metadata(result));
5965

6066
dependencies.forEach(dependency => {
6167
self.postMessage({
Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
1-
import traverse from 'babel-traverse';
1+
export default function getRequireStatements(metadata) {
2+
if (!metadata) return [];
23

3-
export default function getRequireStatements(ast) {
4-
const dependencies = [];
4+
const res = {};
55

6-
traverse(ast, {
7-
enter(path) {
8-
if (
9-
path.node.type === 'CallExpression' &&
10-
path.node.callee.name === 'require' &&
11-
path.node.arguments[0]
12-
) {
13-
if (path.node.arguments[0].type === 'StringLiteral') {
14-
dependencies.push({
15-
type: 'direct',
16-
path: path.node.arguments[0].value,
17-
});
18-
// Dynamic require
19-
} else if (path.node.arguments[0].type === 'BinaryExpression') {
20-
// Sometimes these require statements get converted to
21-
// `require('' + ('./' + undefined.props.type))`;
22-
// We need to check for these wild beasts
23-
if (path.node.arguments[0].right.type === 'BinaryExpression') {
24-
if (path.node.arguments[0].right.left.type === 'StringLiteral') {
25-
dependencies.push({
26-
type: 'glob',
27-
path: path.node.arguments[0].right.left.value,
28-
});
29-
}
30-
} else if (path.node.arguments[0].left.type === 'StringLiteral') {
31-
dependencies.push({
32-
type: 'glob',
33-
path: path.node.arguments[0].left.value,
34-
});
35-
}
36-
}
6+
if (metadata.strings) {
7+
metadata.strings.forEach(s => {
8+
res[s.value] = 'direct';
9+
});
10+
}
11+
if (metadata.expressions) {
12+
metadata.expressions.forEach(s => {
13+
if (s.type === 'BinaryExpression') {
14+
res[s.left.value] = 'glob';
15+
} else if (s.type === 'TemplateLiteral') {
16+
res[s.quasis[0].value.raw] = 'glob';
3717
}
38-
},
39-
});
18+
});
19+
}
4020

41-
return dependencies;
21+
const total = [];
22+
const paths = Object.keys(res);
23+
for (let i = 0; i < paths.length; i += 1) {
24+
total.push({ path: paths[i], type: res[paths[i]] });
25+
}
26+
27+
return total;
4228
}

src/sandbox/eval/transpilers/babel/get-require-statements.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import { transform } from 'babel-standalone';
2+
import dynamicImportSyntax from 'babel-plugin-syntax-dynamic-import';
3+
4+
import detective from './plugins/babel-plugin-detective';
5+
import importNode from './plugins/babel-plugin-dynamic-import-node';
26
import getRequireStatements from './get-require-statements';
3-
import importNode from 'babel-plugin-dynamic-import-node';
47

58
describe('get-require-statements', () => {
69
function testAst(code) {
7-
const { ast } = transform(code, {
10+
const result = transform(code, {
811
presets: ['es2015', 'react'],
912
plugins: [
1013
'transform-async-to-generator',
1114
'transform-object-rest-spread',
1215
'transform-class-properties',
1316
'transform-decorators-legacy',
17+
dynamicImportSyntax,
1418
importNode,
19+
[detective, { source: true, nodes: true }],
1520
],
1621
});
1722

18-
expect(getRequireStatements(ast)).toMatchSnapshot();
23+
expect(getRequireStatements(detective.metadata(result))).toMatchSnapshot();
1924
}
2025

2126
it('can find simple requires', () => {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
var wrapListener = require('./wrap-listener');
3+
4+
function listener(path, file, opts) {
5+
(path.isLiteral() && path.type !== 'TemplateLiteral'
6+
? addString
7+
: addExpression)(path.node, file, opts);
8+
}
9+
10+
function addString(node, file, opts) {
11+
var val = attachNodes(opts) ? node : node.value;
12+
13+
requireMetadata(file).strings.push(val);
14+
}
15+
16+
function addExpression(node, file, opts) {
17+
var val;
18+
19+
if (attachNodes(opts)) {
20+
val = node;
21+
} else {
22+
val = { start: node.start, end: node.end };
23+
val.loc = {
24+
start: copyLoc(node.loc.start),
25+
end: copyLoc(node.loc.end),
26+
};
27+
}
28+
29+
if (attachExpressionSource(opts)) {
30+
val.code = file.code.slice(val.start, val.end);
31+
}
32+
33+
requireMetadata(file).expressions.push(val);
34+
35+
return val;
36+
}
37+
38+
function copyLoc(loc) {
39+
return loc && { line: loc.line, column: loc.column };
40+
}
41+
42+
function requireMetadata(file) {
43+
var metadata = file.metadata;
44+
45+
if (!metadata.requires) {
46+
metadata.requires = {
47+
strings: [],
48+
expressions: [],
49+
};
50+
}
51+
52+
return metadata.requires;
53+
}
54+
55+
// OPTION EXTRACTION:
56+
57+
function attachExpressionSource(opts) {
58+
return Boolean(opts && opts.source);
59+
}
60+
61+
function attachNodes(opts) {
62+
return Boolean(opts && opts.nodes);
63+
}
64+
65+
module.exports = wrapListener(listener, 'detective');
66+
67+
module.exports.metadata = function extractMetadataFromResult(result) {
68+
return result.metadata.requires;
69+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function({ template, types: t }) {
2+
const buildImport = template(`
3+
Promise.resolve().then(() => require(SOURCE))
4+
`);
5+
6+
return {
7+
visitor: {
8+
Import(path) {
9+
const importArguments = path.parentPath.node.arguments;
10+
const newImport = buildImport({
11+
SOURCE:
12+
t.isStringLiteral(importArguments[0]) ||
13+
t.isTemplateLiteral(importArguments[0])
14+
? importArguments
15+
: t.templateLiteral(
16+
[
17+
t.templateElement({ raw: '', cooked: '' }),
18+
t.templateElement({ raw: '', cooked: '' }, true),
19+
],
20+
importArguments
21+
),
22+
});
23+
path.parentPath.replaceWith(newImport);
24+
},
25+
},
26+
};
27+
}

0 commit comments

Comments
 (0)