Skip to content

Commit c1c12e3

Browse files
committed
refactor: Switch to preact-iso for lazy loading
1 parent 29da589 commit c1c12e3

File tree

13 files changed

+80
-6
lines changed

13 files changed

+80
-6
lines changed

jsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4+
"module": "ESNext",
45
"moduleResolution": "Node",
56
"allowJs": true,
67
"checkJs": true,

packages/cli/lib/lib/webpack/webpack-base-config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,12 @@ module.exports = function createBaseConfig(env) {
292292
new MiniCssExtractPlugin({
293293
filename:
294294
isProd && !env.isServer ? '[name].[contenthash:5].css' : '[name].css',
295-
chunkFilename: isProd
296-
? '[name].chunk.[contenthash:5].css'
297-
: '[name].chunk.css',
295+
chunkFilename: pathData => {
296+
const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-');
297+
return isProd
298+
? `${chunkName}.chunk.[chunkhash:5].css`
299+
: `${chunkName}.chunk.css`;
300+
},
298301
}),
299302
ProgressBarPlugin({
300303
format:

packages/cli/lib/lib/webpack/webpack-client-config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ async function clientConfig(env) {
8888
}
8989
return env.isProd ? '[name].[chunkhash:5].js' : '[name].js';
9090
},
91-
chunkFilename: '[name].chunk.[chunkhash:5].js',
91+
chunkFilename: pathData => {
92+
const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-');
93+
return env.isProd
94+
? `${chunkName}.chunk.[chunkhash:5].js`
95+
: `${chunkName}.chunk.js`;
96+
},
9297
},
9398

9499
resolveLoader: {
@@ -173,6 +178,7 @@ function isProd(env) {
173178
cache: true,
174179

175180
optimization: {
181+
chunkIds: 'named',
176182
minimizer: [
177183
new TerserPlugin({
178184
extractComments: false,

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"p-retry": "^4.5.0",
4343
"polka": "^0.5.2",
4444
"preact": "^10.6.5",
45+
"preact-iso": "^2.3.0",
4546
"preact-render-to-string": "^5.1.19",
4647
"preact-router": "^3.0.1",
4748
"puppeteer": "^9.1.1",

packages/cli/tests/build.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { join } = require('path');
1+
const { join, relative } = require('path');
22
const { access, mkdir, readdir, readFile, rename, unlink, writeFile } =
33
require('fs').promises;
44
const looksLike = require('html-looks-like');
55
const { create, build, buildFast } = require('./lib/cli');
6-
const { snapshot } = require('./lib/utils');
6+
const { expand, snapshot } = require('./lib/utils');
77
const { subject } = require('./lib/output');
88
const images = require('./images/build');
99
const minimatch = require('minimatch');
@@ -84,6 +84,26 @@ describe('preact build', () => {
8484
await expect(buildFast(dir)).resolves.not.toThrow();
8585
});
8686

87+
it.only('lazy loads routes with preact-iso `lazy`', async () => {
88+
let dir = await subject('lazy-load-iso');
89+
await buildFast(dir, { prerender: false });
90+
91+
let output = await expand(join(dir, 'build')).then(arr => {
92+
return arr.map(x => relative(dir, x));
93+
});
94+
95+
let expected = [
96+
/build\/a\.chunk\.\w{5}\.js$/,
97+
/build\/a\.chunk\.\w{5}\.css$/,
98+
/build\/b\.chunk\.\w{5}\.js$/,
99+
/build\/b\.chunk\.\w{5}\.css$/,
100+
];
101+
102+
expected.forEach(pattern => {
103+
expect(output.find(file => pattern.test(file))).not.toBeUndefined();
104+
});
105+
});
106+
87107
it('should patch global location object', async () => {
88108
let dir = await subject('location-patch');
89109

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './a.css';
2+
3+
export default () => <h1>Lazy Load A</h1>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: blue;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './b.css';
2+
3+
export default () => <h1>Lazy Load B</h1>;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ErrorBoundary, lazy, Router } from 'preact-iso';
2+
3+
// Asynchronous, code-splitted:
4+
const A = lazy(() => import('./a.js'));
5+
const B = lazy(() => import('./b.js'));
6+
7+
export default function App() {
8+
return (
9+
<ErrorBoundary>
10+
<Router>
11+
<A path="/" />
12+
<B path="/b" />
13+
</Router>
14+
</ErrorBoundary>
15+
);
16+
}

0 commit comments

Comments
 (0)