Skip to content

Commit

Permalink
perf: babel-transform for jsx only
Browse files Browse the repository at this point in the history
  • Loading branch information
fritx committed Apr 18, 2020
1 parent 7d80ea2 commit bdfa90c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"typescript": "3.5.2"
},
"scripts": {
"deploy": "npm run build && gh-pages -d ./build -b master -r https://github.com/coldemo/coldemo.github.io",
"deploy": "npm run build && gh-pages -d ./build -b master -r git@github.com:coldemo/coldemo.github.io.git",
"dev": "npm start",
"start": "react-scripts start",
"build": "react-scripts build",
Expand Down
31 changes: 19 additions & 12 deletions src/playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
loadCss,
appendJs,
appendCss,
wrapCode,
} from './util';

Object.assign(window, {
Expand Down Expand Up @@ -124,18 +125,24 @@ export let Playground: React.FC = () => {

Object.assign(window, { setRendering }); // without side-effect

let doPreview = useCallback(async (code: string) => {
if (!code) return;
setCompiling(true);
try {
let res = await babelTransform(code);
setPreview(res);
} catch (err) {
displayError(err);
} finally {
setCompiling(false);
}
}, []);
let doPreview = useCallback(
async (code: string) => {
if (!code) return;
setCompiling(true);
try {
let res = wrapCode(code);
if (file && file.endsWith('.jsx')) {
res = await babelTransform(res);
}
setPreview(res);
} catch (err) {
displayError(err);
} finally {
setCompiling(false);
}
},
[file]
);

// let persistEditor = useCallback((code: string) => {
// localStorage.setItem(storeKeyCode, code);
Expand Down
35 changes: 1 addition & 34 deletions src/playground/babelMaster.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
let worker = new Worker('./babel-worker.js');

let wrapCode = (code: string) => {
return `
(async () => {
setRendering(true)
// mountNode.innerHTML = '' // can cause error in react
ReactDOM.unmountComponentAtNode(mountNode)
try {
let { useRef, useMemo, useState, useEffect, useLayoutEffect, useReducer, useContext, useCallback, useImperativeHandle } = React
;;${code};;
let isVueLike = _.isPlainObject(App)
if (isVueLike) {
if (!mountNode.children[0]) {
let innerNode = document.createElement('div')
mountNode.appendChild(innerNode)
}
let curr = mountNode.children[0]
new Vue(App).$mount(curr)
} else {
ReactDOM.render(<App />, mountNode)
}
} catch (err) {
console.error(['displayError', err])
displayError(err)
} finally {
setRendering(false)
}
})()
`.trim();
};

export let babelTransform = async (code: string): Promise<string> => {
return new Promise((resolve, reject) => {
let fullCode = wrapCode(code);

worker.onerror = e => {
console.error(['worker onerror', e]);
reject(new Error(`worker onerror - ${code.slice(0, 100)}`));
Expand All @@ -48,6 +15,6 @@ export let babelTransform = async (code: string): Promise<string> => {
resolve(res.data as string);
}
};
worker.postMessage(fullCode);
worker.postMessage(code);
});
};
31 changes: 31 additions & 0 deletions src/playground/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,34 @@ export let appendCss = (code: string) => {
el.innerHTML = code;
window.mountNode.appendChild(el);
};

export let wrapCode = (code: string) => {
return `
(async () => {
setRendering(true)
// mountNode.innerHTML = '' // can cause error in react
ReactDOM.unmountComponentAtNode(mountNode)
try {
let { useRef, useMemo, useState, useEffect, useLayoutEffect, useReducer, useContext, useCallback, useImperativeHandle } = React
;;${code};;
let isVueLike = _.isPlainObject(App)
if (isVueLike) {
if (!mountNode.children[0]) {
let innerNode = document.createElement('div')
mountNode.appendChild(innerNode)
}
let curr = mountNode.children[0]
new Vue(App).$mount(curr)
} else {
ReactDOM.render(React.createElement(App), mountNode)
}
} catch (err) {
console.error(['displayError', err])
displayError(err)
} finally {
setRendering(false)
}
})()
`.trim();
};

0 comments on commit bdfa90c

Please sign in to comment.