Skip to content

Commit

Permalink
refactor: Switch to preact-iso for lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Aug 13, 2022
1 parent 29da589 commit c1c12e3
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"allowJs": true,
"checkJs": true,
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/lib/lib/webpack/webpack-base-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ module.exports = function createBaseConfig(env) {
new MiniCssExtractPlugin({
filename:
isProd && !env.isServer ? '[name].[contenthash:5].css' : '[name].css',
chunkFilename: isProd
? '[name].chunk.[contenthash:5].css'
: '[name].chunk.css',
chunkFilename: pathData => {
const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-');
return isProd
? `${chunkName}.chunk.[chunkhash:5].css`
: `${chunkName}.chunk.css`;
},
}),
ProgressBarPlugin({
format:
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/lib/lib/webpack/webpack-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ async function clientConfig(env) {
}
return env.isProd ? '[name].[chunkhash:5].js' : '[name].js';
},
chunkFilename: '[name].chunk.[chunkhash:5].js',
chunkFilename: pathData => {
const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-');
return env.isProd
? `${chunkName}.chunk.[chunkhash:5].js`
: `${chunkName}.chunk.js`;
},
},

resolveLoader: {
Expand Down Expand Up @@ -173,6 +178,7 @@ function isProd(env) {
cache: true,

optimization: {
chunkIds: 'named',
minimizer: [
new TerserPlugin({
extractComments: false,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"p-retry": "^4.5.0",
"polka": "^0.5.2",
"preact": "^10.6.5",
"preact-iso": "^2.3.0",
"preact-render-to-string": "^5.1.19",
"preact-router": "^3.0.1",
"puppeteer": "^9.1.1",
Expand Down
24 changes: 22 additions & 2 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path');
const { join, relative } = require('path');
const { access, mkdir, readdir, readFile, rename, unlink, writeFile } =
require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build, buildFast } = require('./lib/cli');
const { snapshot } = require('./lib/utils');
const { expand, snapshot } = require('./lib/utils');
const { subject } = require('./lib/output');
const images = require('./images/build');
const minimatch = require('minimatch');
Expand Down Expand Up @@ -84,6 +84,26 @@ describe('preact build', () => {
await expect(buildFast(dir)).resolves.not.toThrow();
});

it.only('lazy loads routes with preact-iso `lazy`', async () => {
let dir = await subject('lazy-load-iso');
await buildFast(dir, { prerender: false });

let output = await expand(join(dir, 'build')).then(arr => {
return arr.map(x => relative(dir, x));
});

let expected = [
/build\/a\.chunk\.\w{5}\.js$/,
/build\/a\.chunk\.\w{5}\.css$/,
/build\/b\.chunk\.\w{5}\.js$/,
/build\/b\.chunk\.\w{5}\.css$/,
];

expected.forEach(pattern => {
expect(output.find(file => pattern.test(file))).not.toBeUndefined();
});
});

it('should patch global location object', async () => {
let dir = await subject('location-patch');

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './a.css';

export default () => <h1>Lazy Load A</h1>;
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: blue;
}
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './b.css';

export default () => <h1>Lazy Load B</h1>;
16 changes: 16 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ErrorBoundary, lazy, Router } from 'preact-iso';

// Asynchronous, code-splitted:
const A = lazy(() => import('./a.js'));
const B = lazy(() => import('./b.js'));

export default function App() {
return (
<ErrorBoundary>
<Router>
<A path="/" />
<B path="/b" />
</Router>
</ErrorBoundary>
);
}
4 changes: 4 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"name": "preact-lazy-load-iso"
}
6 changes: 6 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/preact.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (config, env, helpers) {
// TODO: clean up later, just removes the async loader
if (env.isProd && !env.isServer) {
config.module.rules.pop();
}
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11386,6 +11386,11 @@ postcss@^8.4.13:
picocolors "^1.0.0"
source-map-js "^1.0.2"

preact-iso@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/preact-iso/-/preact-iso-2.3.0.tgz#ab6c5de28df9e0f7a0589dd2175a83ba821f69ec"
integrity sha512-taJmRidbWrjHEhoVoxXS2Kvxa6X3jXSsTtD7rSYeJuxnPNr1ghCu1JUzCrRxmZwTUNWIqwUpNi+AJoLtvCPN7g==

preact-render-to-string@^5.1.19:
version "5.1.19"
resolved "https://registry.yarnpkg.com/preact-render-to-string/-/preact-render-to-string-5.1.19.tgz#ffae7c3bd1680be5ecf5991d41fe3023b3051e0e"
Expand Down

0 comments on commit c1c12e3

Please sign in to comment.