Skip to content

Commit

Permalink
feat: update babel plugin, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSergey committed Aug 18, 2022
1 parent 4bc0483 commit 31c3387
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
3 changes: 1 addition & 2 deletions examples/19-react-18/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export const App = () => {

useSsrEffect(() => {
registerEffect(asyncFn).then((data) => {
console.log('test', s);
setState(data);
})
});
}, [s]);

return (
Expand Down
79 changes: 49 additions & 30 deletions examples/19-react-18/src/server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,61 @@ import { serverRender } from '@issr/core';

const app = new Koa();
const router = new Router();
const ABORT_DELAY = 10000;

app.use(serve(path.resolve(__dirname, '../public')));

router.get('/*', async (ctx) => {
ctx.respond = false;
ctx.res.statusCode = 200;
ctx.response.set('content-type', 'text/html');
let didError = false;

const { stream } = await serverRender.stream(() => (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="iSSR example with React 18" />
<title>Issr</title>
</head>
<body>
<noscript
dangerouslySetInnerHTML={{
__html: `<b>Enable JavaScript to run this app.</b>`,
}}
/>
<div id="root">
<App />
</div>
</body>
</html>
), {
streamOptionsFn: (state) => ({
bootstrapScripts: ['/index.js'],
bootstrapScriptContent: `window.SSR_DATA = ${serialize(state, { isJSON: true })}`,
})
});
/**
* NOTE: use promise to force koa waiting for streaming.
*/
return new Promise(async (resolve, reject) => {
const { stream } = await serverRender.stream(() => (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="iSSR example with React 18" />
<title>Issr</title>
</head>
<body>
<noscript
dangerouslySetInnerHTML={{
__html: `<b>Enable JavaScript to run this app.</b>`,
}}
/>
<div id="root">
<App />
</div>
</body>
</html>
), {
streamOptionsFn: (state) => ({
bootstrapScripts: ['/index.js'],
bootstrapScriptContent: `window.SSR_DATA = ${serialize(state, { isJSON: true })}`,
onShellReady() {
ctx.respond = false;
ctx.res.statusCode = didError ? 500 : 200;
ctx.response.set('content-type', 'text/html');
stream.pipe(ctx.res);
ctx.res.end();
resolve();
},
onError() {
didError = true;
reject();
},
})
});

stream.pipe(ctx.res);
setTimeout(() => {
stream.abort();
reject();
}, ABORT_DELAY);
});
});

app
Expand Down
41 changes: 21 additions & 20 deletions packages/babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const createDummy = (globalCache, id, field) => {
useSsrState: 0,
};
}

if (!globalCache[id][field]) {
globalCache[id][field] = 0;
}
Expand Down Expand Up @@ -52,26 +53,7 @@ const BabelISSRPlugin = (api) => {

const useSsrStateNames = Array.isArray(useSsrStateName) ? useSsrStateName : [useSsrStateName];

const useSsrEffects = Array.isArray(useSsrEffectName) ? useSsrEffectName : [useSsrEffectName];

useSsrEffects.forEach((ssrEffectName) => {
if (path.node.callee.name === ssrEffectName) {
const args = path.node.arguments;
const lastItem = args[args.length - 1];
const lastItemIsArray = lastItem.type === 'ArrayExpression';
const firstItemIsFunction = lastItem.type.indexOf('FunctionExpression') >= 0;

if (
(path.node.arguments.length === 2 && lastItemIsArray) ||
(path.node.arguments.length === 1 && firstItemIsFunction)
) {
const id = md5(pathNode.relative(cwd, filename));
createDummy(globalCache, id, 'useSsrEffect');
const effectID = `ssr-effect-${id}-${globalCache[id].useSsrEffect++}`;
path.node.arguments.push(t.StringLiteral(effectID));
}
}
});
const useSsrEffectNames = Array.isArray(useSsrEffectName) ? useSsrEffectName : [useSsrEffectName];

useRegisterEffectNames.forEach((registerEffect) => {
if (path.node.callee.name === registerEffect) {
Expand All @@ -94,6 +76,25 @@ const BabelISSRPlugin = (api) => {
}
}
});

useSsrEffectNames.forEach((ssrEffectName) => {
if (path.node.callee.name === ssrEffectName) {
const args = path.node.arguments;
const lastItem = args[args.length - 1];
const lastItemIsArray = lastItem.type === 'ArrayExpression';
const firstItemIsFunction = lastItem.type.indexOf('FunctionExpression') >= 0;

if (
(path.node.arguments.length === 2 && lastItemIsArray) ||
(path.node.arguments.length === 1 && firstItemIsFunction)
) {
const id = md5(pathNode.relative(cwd, filename));
createDummy(globalCache, id, 'useSsrEffect');
const effectID = `ssr-effect-${id}-${globalCache[id].useSsrEffect++}`;
path.node.arguments.push(t.StringLiteral(effectID));
}
}
});
},
},
};
Expand Down
4 changes: 1 addition & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The easiest way to move your React application to Server-Side Rendering. Handles

## Features
- TypeScript support
- Extremely small size (5kb)
- Small size (5kb)
- Without dependencies
- **iSSR** supports native setState, Redux (thunk, sagas), Mobx, Apollo and other state management libraries

Expand All @@ -23,5 +23,3 @@ Copyright (c) Aleksandrov Sergey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


0 comments on commit 31c3387

Please sign in to comment.