Skip to content

Commit

Permalink
Release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wille committed Sep 4, 2024
1 parent 2bc2d84 commit bd2d0a3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 79 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

# vite-preload

This plugin will significantly speed up your server rendered vite application by preloading async modules as early as possible and it will avoid Flash Of Unstyled Content (FOUC) by including stylesheets from async modules in the initial HTML.
This plugin will significantly speed up your server rendered vite application by preloading async modules and stylesheets as early as possible and help you avoiding Flash Of Unstyled Content (FOUC) by including stylesheets from async modules in the initial HTML.

## Explainer

Vite supports `React.lazy()` and dynamic imports just fine but any lazy imported modules and its CSS imports will not be injected into html or DOM until the entrypoint module has imported them.

It's a common pattern to have each page/route in your application lazy loaded especially if you are migrating to Vite from something else like webpack with loadable-components.

This plugin will collect which modules are rendered on the server and help you inject `<link>` tags and `Link` preload headers for early hints.

Read more:

- [Backend Integration](https://vitejs.dev/guide/backend-integration.html)
- [Server Side Rendering](https://vitejs.dev/guide/ssr.html)
This plugin will collect which modules are rendered on the server and help you inject `<link>` tags in the HTML `<head>` and `Link` preload headers for [103 Early Hints](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/103)

## Without preloading

Expand Down Expand Up @@ -175,3 +170,9 @@ Card.preload();
### Using dynamic imports with react-router Lazy Routes

See https://reactrouter.com/en/main/guides/ssr#lazy-routes


### Read more:

- [Backend Integration](https://vitejs.dev/guide/backend-integration.html)
- [Server Side Rendering](https://vitejs.dev/guide/ssr.html)
25 changes: 3 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-preload",
"version": "0.0.2",
"description": "Speed up your Vite application by preloading async modules and stylesheets as early as possible",
"version": "1.0.0",
"description": "Speed up your Vite application by preloading server rendered lazy modules and stylesheets as early as possible",
"main": "dist/index.cjs",
"module": "dist/index.js",
"exports": {
Expand All @@ -23,7 +23,6 @@
"./dist"
],
"scripts": {
"prepare": "npm run build",
"test": "tsc",
"build": "rollup -c rollup.config.js"
},
Expand All @@ -37,7 +36,6 @@
"@rollup/plugin-typescript": "^11.1.6",
"@types/babel__parser": "^7.0.0",
"@types/babel__traverse": "^7.20.6",
"@types/debug": "^4.1.12",
"@types/node": "^22.3.0",
"@types/react": "^18.3.3",
"prettier": "^3.3.3",
Expand All @@ -46,15 +44,22 @@
"typescript": "^5.5.4"
},
"peerDependencies": {
"debug": "^4",
"react": ">= 18",
"vite": "^5.4.0"
"vite": "^5"
},
"dependencies": {
"@babel/generator": "^7.25.0",
"@babel/parser": "^7.25.3",
"@babel/traverse": "^7.25.3",
"@babel/types": "^7.25.2",
"mime": "^4.0.4"
}
},
"keywords": [
"react",
"css",
"ssr",
"esmodules",
"vite",
"vite-plugin"
]
}
3 changes: 0 additions & 3 deletions src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
sortPreloads,
} from './utils';
import mime from 'mime';
import debug from 'debug';

const log = debug('vite-preload');

interface ManifestChunk {
src: string;
Expand Down
28 changes: 21 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ interface PluginOptions {
* Internal
*/
__internal_importHelperModuleName?: string;

/**
* Enables extensive build logs
*/
debug?: boolean;
}

const hookFunctionName = '__collectModule';
Expand All @@ -29,6 +34,7 @@ const includeJsx = /\.(jsx|tsx)$/;

export default function preloadPlugin({
__internal_importHelperModuleName = 'vite-preload/__internal',
debug,
}: PluginOptions = {}): Plugin {
const lazyImportedModules = new Set();
const injectedModules = new Set();
Expand Down Expand Up @@ -90,9 +96,11 @@ export default function preloadPlugin({
continue;
}

this.info(
`dynamically imports ${path.relative(process.cwd(), resolved.id)}`
);
if (debug) {
this.info(
`dynamically imports ${path.relative(process.cwd(), resolved.id)}`
);
}

lazyImportedModules.add(resolved.id);
}
Expand Down Expand Up @@ -138,7 +146,11 @@ export default function preloadPlugin({
});

if (injected) {
this.info('Injected __collectModule in React component');
if (debug) {
this.info(
'Injected __collectModule in React component'
);
}
count++;
const output = generate(ast, {}, code);
injectedModules.add(id);
Expand All @@ -153,9 +165,11 @@ export default function preloadPlugin({
},

buildEnd() {
const s = lazyImportedModules.difference(injectedModules);
for (const z of s) {
this.info(`${z} was not injected`);
if (debug) {
const s = lazyImportedModules.difference(injectedModules);
for (const z of s) {
this.warn(`${z} was not injected`);
}
}
this.info(`${count} hook calls injected`);
},
Expand Down
33 changes: 0 additions & 33 deletions test.cjs

This file was deleted.

0 comments on commit bd2d0a3

Please sign in to comment.