Skip to content

Commit 7e02b45

Browse files
authored
Rewrite of bundler (codesandbox#143)
* Remove babel-standalone from vendor * Updated rewrite * Loader support * Fix zip files and caching * Preact support * Typescript transpiling * Add JSON to Preact * Support for typescript dependency fetching * Create 'Export to Preact' * Fix wrong order in transpilation * Fix transpilers
1 parent 72c0835 commit 7e02b45

File tree

78 files changed

+2010
-1083
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2010
-1083
lines changed

config/webpack.config.js

Lines changed: 95 additions & 111 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"babel-preset-react": "^6.24.1",
2222
"babel-runtime": "^6.20.0",
2323
"babili-webpack-plugin": "^0.1.1",
24+
"buffer-loader": "^0.0.1",
2425
"case-sensitive-paths-webpack-plugin": "^2.0.0",
2526
"chalk": "1.1.3",
2627
"connect-history-api-fallback": "1.3.0",
@@ -70,6 +71,7 @@
7071
"strip-ansi": "3.0.1",
7172
"style-loader": "^0.18.2",
7273
"sw-precache-webpack-plugin": "^0.11.4",
74+
"typescript": "^2.4.2",
7375
"url-loader": "^0.5.9",
7476
"webpack": "^3.5.4",
7577
"webpack-dev-middleware": "^1.11.0",
@@ -83,6 +85,7 @@
8385
"babel-helper-vue-jsx-merge-props": "^2.0.2",
8486
"babel-plugin-detective": "^2.0.0",
8587
"babel-plugin-dynamic-import-node": "^1.0.2",
88+
"babel-plugin-jsx-pragmatic": "^1.0.2",
8689
"babel-plugin-syntax-jsx": "^6.18.0",
8790
"babel-plugin-system-import": "^1.1.5",
8891
"babel-plugin-system-import-transformer": "3.1.0",
@@ -91,6 +94,7 @@
9194
"babel-plugin-transform-remove-strict-mode": "^0.0.2",
9295
"babel-plugin-transform-vue-jsx": "^3.5.0",
9396
"babel-polyfill": "^6.23.0",
97+
"babel-preset-env": "^1.6.0",
9498
"babel-preset-stage-0": "^6.24.1",
9599
"babel-standalone": "^6.25.0",
96100
"base64-loader": "^1.0.0",

src/app/components/sandbox/CodeEditor/monaco/workers/linter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ const defaultConfig = {
258258
// https://github.com/benmosher/eslint-plugin-import/tree/master/docs/rules
259259
'import/first': 'error',
260260
'import/no-amd': 'error',
261-
'import/no-webpack-loader-syntax': 'error',
261+
'import/no-webpack-loader-syntax': 'warn',
262262

263263
// https://github.com/yannickcr/eslint-plugin-react/tree/master/docs/rules
264264
'react/jsx-no-comment-textnodes': 'warn',
@@ -278,7 +278,7 @@ const defaultConfig = {
278278
'react/no-deprecated': 'warn',
279279
'react/no-direct-mutation-state': 'warn',
280280
'react/no-is-mounted': 'warn',
281-
'react/react-in-jsx-scope': 'error',
281+
// 'react/react-in-jsx-scope': 'error',
282282
'react/require-render-return': 'error',
283283
'react/style-prop-object': 'warn',
284284

src/app/pages/Sandbox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const mapStateToProps = createSelector(sandboxesSelector, sandboxes => ({
4040
const mapDispatchToProps = dispatch => ({
4141
sandboxActions: bindActionCreators(sandboxActionCreators, dispatch),
4242
});
43-
class SandboxPage extends React.PureComponent<Props, $FlowFixMeState> {
43+
class SandboxPage extends React.PureComponent<Props, State> {
4444
componentDidMount() {
4545
if (
4646
window.screen.availWidth < 800 &&

src/app/store/entities/sandboxes/utils/create-zip/index.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import JSZip from 'jszip';
33
import { saveAs } from 'file-saver';
44

55
import type { Sandbox, Module, Directory } from 'common/types';
6-
import { react, vue } from 'common/templates/index';
6+
import { react, vue, preact } from 'common/templates/index';
77

88
const CSSTag = (resource: string) =>
99
`<link rel="stylesheet" type="text/css" href="${resource}" media="all">`;
@@ -22,7 +22,7 @@ export function getResourceTag(resource: string) {
2222

2323
export function getIndexHtmlBody(modules: Array<Module>) {
2424
const indexHtmlModule = modules.find(
25-
m => m.title === 'index.html' && m.directoryShortid == null,
25+
m => m.title === 'index.html' && m.directoryShortid == null
2626
);
2727

2828
if (indexHtmlModule) {
@@ -56,7 +56,7 @@ export function createPackageJSON(
5656
dependencies,
5757
devDependencies,
5858
scripts,
59-
extra: Object,
59+
extra: Object
6060
) {
6161
const name = slugify(sandbox.title || sandbox.id);
6262
const version = `0.0.${sandbox.version}`;
@@ -69,18 +69,18 @@ export function createPackageJSON(
6969
dependencies: { ...sandbox.npmDependencies, ...dependencies },
7070
devDependencies,
7171
scripts,
72-
...(extra || {}),
72+
...(extra || {})
7373
},
7474
null,
75-
' ',
75+
' '
7676
);
7777
}
7878

7979
export function createDirectoryWithFiles(
8080
modules: Array<Module>,
8181
directories: Array<Directory>,
8282
directory: Directory,
83-
zip,
83+
zip
8484
) {
8585
const newZip = zip.folder(directory.title);
8686

@@ -96,21 +96,27 @@ export function createDirectoryWithFiles(
9696
export default (async function createZip(
9797
sandbox: Sandbox,
9898
modules: Array<Module>,
99-
directories: Array<Directory>,
99+
directories: Array<Directory>
100100
) {
101101
const zip = new JSZip();
102102

103+
let promise = null;
103104
if (sandbox.template === react.name) {
104-
import('./create-react-app').then(generator => {
105-
generator.default(zip, sandbox, modules, directories);
106-
});
105+
promise = import('./create-react-app').then(generator =>
106+
generator.default(zip, sandbox, modules, directories)
107+
);
107108
} else if (sandbox.template === vue.name) {
108-
import('./vue-cli').then(generator => {
109-
generator.default(zip, sandbox, modules, directories);
110-
});
109+
promise = import('./vue-cli').then(generator =>
110+
generator.default(zip, sandbox, modules, directories)
111+
);
112+
} else if (sandbox.template === preact.name) {
113+
promise = import('./preact-cli').then(generator =>
114+
generator.default(zip, sandbox, modules, directories)
115+
);
116+
}
117+
if (promise) {
118+
await promise;
119+
const file = await zip.generateAsync({ type: 'blob' });
120+
saveAs(file, `${slugify(sandbox.title || sandbox.id)}.zip`);
111121
}
112-
113-
const file = await zip.generateAsync({ type: 'blob' });
114-
115-
saveAs(file, `${slugify(sandbox.title || sandbox.id)}.zip`);
116122
});
Binary file not shown.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import type { Sandbox, Module, Directory } from 'common/types';
2+
3+
import files from 'buffer-loader!./files.zip';
4+
import {
5+
getResourceTag,
6+
getIndexHtmlBody,
7+
createPackageJSON,
8+
createDirectoryWithFiles
9+
} from '../';
10+
11+
const getHTML = (modules, resources) =>
12+
`<!DOCTYPE html>
13+
<html lang="en">
14+
<head>
15+
<meta charset="utf-8">
16+
<title><%= htmlWebpackPlugin.options.title %></title>
17+
<meta name="viewport" content="width=device-width,initial-scale=1">
18+
<meta name="mobile-web-app-capable" content="yes">
19+
<meta name="apple-mobile-web-app-capable" content="yes">
20+
<link rel="manifest" href="/manifest.json">
21+
<% if (htmlWebpackPlugin.options.manifest.theme_color) { %>
22+
<meta name="theme-color" content="<%= htmlWebpackPlugin.options.manifest.theme_color %>">
23+
<% } %>
24+
<% for (var chunk of webpack.chunks) { %>
25+
<% if (chunk.names.length === 1 && chunk.names[0] === 'polyfills') continue; %>
26+
<% for (var file of chunk.files) { %>
27+
<% if (htmlWebpackPlugin.options.preload && file.match(/\.(js|css)$/)) { %>
28+
<link rel="preload" href="<%= htmlWebpackPlugin.files.publicPath + file %>" as="<%= file.match(/\.css$/)?'style':'script' %>">
29+
<% } else if (file.match(/manifest\.json$/)) { %>
30+
<link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath + file %>">
31+
<% } %>
32+
<% } %>
33+
<% } %>
34+
${resources.map(getResourceTag).join('\n')}
35+
</head>
36+
<body>
37+
${getIndexHtmlBody(modules)}
38+
<%= htmlWebpackPlugin.options.ssr({
39+
url: '/'
40+
}) %>
41+
<script defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
42+
<script>window.fetch||document.write('<script src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"><\/script>')</script>
43+
</body>
44+
</html>
45+
`;
46+
47+
export default function createZip(
48+
zip,
49+
sandbox: Sandbox,
50+
modules: Array<Module>,
51+
directories: Array<Directory>
52+
) {
53+
const src = zip.folder('src');
54+
return src.loadAsync(files).then(src => {
55+
modules
56+
.filter(x => x.directoryShortid == null)
57+
.filter(x => x.title !== 'index.html') // This will be included in the body
58+
.forEach(x => src.file(x.title, x.code));
59+
60+
directories
61+
.filter(x => x.directoryShortid == null)
62+
.forEach(x => createDirectoryWithFiles(modules, directories, x, src));
63+
64+
if (
65+
modules.filter(
66+
m => m.directoryShortid == null && m.title === 'index.html'
67+
).length ||
68+
sandbox.externalResources.length
69+
) {
70+
src.file('template.html', getHTML(modules, sandbox.externalResources));
71+
}
72+
73+
zip.file(
74+
'package.json',
75+
createPackageJSON(
76+
sandbox,
77+
{},
78+
{
79+
eslint: '^4.5.0',
80+
'eslint-config-synacor': '^1.1.0',
81+
'if-env': '^1.0.0',
82+
less: '^2.7.2',
83+
'less-loader': '^4.0.5',
84+
'node-sass': '^4.5.3',
85+
'preact-cli': '^1.4.1',
86+
'sass-loader': '^6.0.6',
87+
stylus: '^0.54.5',
88+
'stylus-loader': '^3.0.1'
89+
},
90+
{
91+
test: 'eslint . && preact test',
92+
start:
93+
'if-env NODE_ENV=production && npm run -s serve || npm run -s dev',
94+
build: 'preact build',
95+
serve: 'preact build && preact serve',
96+
dev: 'preact watch'
97+
},
98+
{
99+
eslintConfig: {
100+
extends: 'eslint-config-synacor'
101+
}
102+
}
103+
)
104+
);
105+
});
106+
}
Binary file not shown.

src/app/store/entities/sandboxes/utils/create-zip/vue-cli/files/babelrc

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/app/store/entities/sandboxes/utils/create-zip/vue-cli/files/config/dev.env.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)