From c0864a128c51d0fbe78c63cfc825129028decffb Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sun, 12 Jul 2020 22:17:32 -0400 Subject: [PATCH 1/2] Code-split bundled WMR for performance --- .gitignore | 1 + rollup.config.js | 6 ++++-- src/build.js | 5 +++-- src/server.js | 2 +- src/start.js | 4 ++-- tsconfig.json | 3 ++- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index b9af4a0e3..b108e67f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ dist node_modules wmr.cjs +wmr.*.cjs yarn.lock diff --git a/rollup.config.js b/rollup.config.js index 6d581075b..4ca1bb8eb 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,9 +9,11 @@ import terser from 'terser'; /** @type {import('rollup').RollupOptions} */ const config = { input: 'src/cli.js', - inlineDynamicImports: true, + preserveEntrySignatures: false, output: { - file: 'wmr.cjs', + dir: '.', + entryFileNames: 'wmr.cjs', + chunkFileNames: 'wmr.[name].cjs', format: 'cjs', compact: true, freeze: false, diff --git a/src/build.js b/src/build.js index 5c8bdfd46..fb8a01a3f 100644 --- a/src/build.js +++ b/src/build.js @@ -1,10 +1,9 @@ -import { bundleProd } from './bundler.js'; import { bundleStats } from './lib/output-utils.js'; import { normalizeOptions } from './lib/normalize-options.js'; import { setCwd } from './plugins/npm-plugin/registry.js'; /** - * @param {Parameters[0]} options + * @param {Parameters[0]} options */ export default async function build(options = {}) { options.out = options.out || 'dist'; @@ -14,6 +13,8 @@ export default async function build(options = {}) { options = await normalizeOptions(options); + const { bundleProd } = await import('./bundler.js'); + const bundleOutput = await bundleProd(options); const stats = bundleStats(bundleOutput); diff --git a/src/server.js b/src/server.js index fad26ae51..783c8e347 100644 --- a/src/server.js +++ b/src/server.js @@ -1,6 +1,5 @@ import { resolve } from 'path'; import { createServer } from 'http'; -import { createHttp2Server } from './lib/http2.js'; import polka from 'polka'; import sirv from 'sirv'; import compression from './lib/polkompress.js'; @@ -38,6 +37,7 @@ export default async function server({ cwd, overlayDir, middleware, http2 = fals if (http2) { try { + const { createHttp2Server } = await import('./lib/http2.js'); app.server = await createHttp2Server(); app.http2 = true; } catch (e) { diff --git a/src/start.js b/src/start.js index 0f4f5f0dc..f1f551f0e 100644 --- a/src/start.js +++ b/src/start.js @@ -1,5 +1,4 @@ import server from './server.js'; -import { bundleDev } from './bundler.js'; import wmrMiddleware from './wmr-middleware.js'; import { getFreePort, getServerAddresses } from './lib/net-utils.js'; import { normalizeOptions } from './lib/normalize-options.js'; @@ -13,7 +12,7 @@ import { setCwd } from './plugins/npm-plugin/registry.js'; */ /** - * @param {Parameters[0] & Parameters[0] & OtherOptions} options + * @param {Parameters[0] & Parameters[0] & OtherOptions} options */ export default async function start(options = {}) { // @todo remove this hack once registry.js is instantiable @@ -22,6 +21,7 @@ export default async function start(options = {}) { options = await normalizeOptions(options); if (options.prebuild) { + const { bundleDev } = await import('./bundler.js'); bundleDev({ ...options, onError: sendError, diff --git a/tsconfig.json b/tsconfig.json index 714e37b80..aa3b1fc49 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,8 @@ "moduleResolution": "node", "allowSyntheticDefaultImports": true, "downlevelIteration": true, - "target": "ESNext" + "target": "ESNext", + "module": "ESNext" }, "typeAcquisition": { "enable": true From 223eb4dbc133ef964641e0386f65995d4036f558 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sun, 12 Jul 2020 22:22:24 -0400 Subject: [PATCH 2/2] include code-splitted core files in compressed size --- .github/workflows/compressed-size.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/compressed-size.yml b/.github/workflows/compressed-size.yml index d5242f1d0..6f2b3d0ac 100644 --- a/.github/workflows/compressed-size.yml +++ b/.github/workflows/compressed-size.yml @@ -12,7 +12,7 @@ jobs: - name: compressed-size-action uses: preactjs/compressed-size-action@v2 with: - pattern: '{wmr.cjs,demo/dist/**/*.{js,css,html}}' + pattern: '{wmr.cjs,wmr.*.cjs,demo/dist/**/*.{js,css,html}}' build-script: ci strip-hash: "\\.(\\w{8})\\.(?:js|css)$" repo-token: '${{ secrets.GITHUB_TOKEN }}'