From f3399677f5f74ac28e26386dc4c0b65c83c5b1f1 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 12:54:00 -0600 Subject: [PATCH 01/13] Remove HTML build --- .gitignore | 1 - build/html.js | 42 ------------------------------------------ package.json | 5 ++--- 3 files changed, 2 insertions(+), 46 deletions(-) delete mode 100644 build/html.js diff --git a/.gitignore b/.gitignore index 842110d23..3deac7b4b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ *.log /_playwright-report /_playwright-results -/docs/preview.html /lib /node_modules diff --git a/build/html.js b/build/html.js deleted file mode 100644 index 5e029efc8..000000000 --- a/build/html.js +++ /dev/null @@ -1,42 +0,0 @@ -import * as fs from 'node:fs'; -import * as path from 'node:path'; -import * as url from 'node:url'; -import prettier from 'prettier'; -import stripIndent from 'common-tags/lib/stripIndent/index.js'; - -const __filename = url.fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const prettierConfig = prettier.resolveConfig.sync(__dirname); - -// Preview -// ============================================================================= -function generatePreview() { - const comment = stripIndent` - - `; - const srcFile = 'index.html'; - const srcPath = path.resolve(__dirname, '..', 'docs'); - const srcHTML = fs.readFileSync(path.resolve(srcPath, srcFile), 'utf8'); - const outFile = 'preview.html'; - const outPath = path.resolve(__dirname, '..', 'docs'); - const outHTML = srcHTML - // Append comment - .replace(/()/, `${comment}\n$1`) - // Modify title - .replace(/(<\/title>)/, ' (Preview)$1') - // Replace CDN URLs with local paths - .replace(/\/\/cdn.jsdelivr.net\/npm\/docsify@4\//g, '/'); - const formattedHTML = prettier.format(outHTML, { - ...prettierConfig, - filepath: outFile, - }); - - console.log(`\nBuilding ${outFile} in ${outPath}`); - - fs.writeFileSync(path.resolve(outPath, outFile), formattedHTML); -} - -generatePreview(); diff --git a/package.json b/package.json index f8f81eeca..810317abf 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,9 @@ "build:css:min": "node build/mincss.js", "build:css": "mkdirp lib/themes && node build/css -o lib/themes", "build:emoji": "node ./build/emoji.js", - "build:html": "node ./build/html.js", "build:js": "cross-env NODE_ENV=production node build/build.js", "build:test": "npm run build && npm test", - "build": "rimraf lib themes && run-s build:js build:css build:css:min build:cover build:emoji build:html", + "build": "rimraf lib themes && run-s build:js build:css build:css:min build:cover build:emoji", "dev": "run-p serve:dev watch:*", "docker:build:test": "npm run docker:cli -- build:test", "docker:build": "docker build -f Dockerfile -t docsify-test:local .", @@ -49,7 +48,7 @@ "prettier": "prettier . --write", "pub:next": "cross-env RELEASE_TAG=next sh build/release.sh", "pub": "sh build/release.sh", - "serve:dev": "npm run build:html && npm run serve -- --dev", + "serve:dev": "npm run serve -- --dev", "serve": "node server", "test:e2e": "playwright test", "test:integration": "npm run jest -- --selectProjects integration", From f453f4b803e81653c13b3f363181d87f3b4e690a Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 12:54:46 -0600 Subject: [PATCH 02/13] Fix CSS watch script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 810317abf..1701ca9fc 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "test:integration": "npm run jest -- --selectProjects integration", "test:unit": "npm run jest -- --selectProjects unit", "test": "npm run jest && run-s test:e2e", - "watch:css": "npm run css -- -o themes -w", + "watch:css": "npm run build:css -- -w", "watch:js": "node build/build.js" }, "husky": { From 04bd7322521fc62c20962ae717149490f83c33b5 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 12:55:04 -0600 Subject: [PATCH 03/13] Add Vercal configuration --- .gitignore | 1 + middleware.js | 32 ++++++++++++++++++++ server.config.js | 78 ++++++++++++++++++++++++++++++------------------ server.js | 2 +- vercel.json | 12 +++++--- 5 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 middleware.js diff --git a/.gitignore b/.gitignore index 3deac7b4b..a8c5765d0 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ # exceptions !.gitkeep +.vercel diff --git a/middleware.js b/middleware.js new file mode 100644 index 000000000..fe6a14be9 --- /dev/null +++ b/middleware.js @@ -0,0 +1,32 @@ +import serverConfigs from './server.config.js'; + +const { rewriteRules } = serverConfigs.dev; + +// Exports +// ============================================================================= +export const config = { + matcher: ['/preview/(index.html)?'], +}; + +// Serve virtual /preview/index.html +// Note: See vercel.json for preview routing configuration +// 1. Fetch index.html from /docs/ directory +// 2. Replace CDN URLs with local paths (see rewriteRules) +// 3. Return preview HTML +export default async function middleware(request) { + const { origin } = new URL(request.url); + const indexURL = `${origin}/docs/index.html`; + const indexHTML = await fetch(indexURL).then(res => res.text()); + const previewHTML = rewriteRules.reduce( + (html, rule) => html.replace(rule.match, rule.replace), + indexHTML + ); + + return new Response(previewHTML, { + status: 200, + headers: { + 'content-type': 'text/html', + 'x-robots-tag': 'noindex', + }, + }); +} diff --git a/server.config.js b/server.config.js index d33765749..44d42891b 100644 --- a/server.config.js +++ b/server.config.js @@ -3,52 +3,65 @@ import * as url from 'node:url'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -const defaults = { + +// Production (CDN URLs, watch disabled) +const prod = { hostname: '127.0.0.1', notify: false, open: false, + port: 8080, + server: { + baseDir: './docs', + }, + snippet: false, + ui: false, +}; + +// Development (local URLs, watch enabled) +const dev = { + ...prod, + files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], + port: 3000, rewriteRules: [ - // Replace remote URLs with local paths + // Replace CDN URLs with local paths { - // Changelog match: /https?.*\/CHANGELOG.md/g, replace: '/CHANGELOG.md', }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, ], server: { - baseDir: 'docs', - index: 'preview.html', + ...prod.server, routes: { '/changelog.md': path.resolve(__dirname, 'CHANGELOG.md'), '/lib': path.resolve(__dirname, 'lib'), '/node_modules': path.resolve(__dirname, 'node_modules'), // Required for automated Vue tests }, }, - snippet: false, - ui: false, + snippet: true, }; -export default { - // Development (preview, local URLs, watch enabled) - dev: { - ...defaults, - files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], - port: 3000, - open: true, - snippet: true, - }, - // Production (index, CDN URLs, watch disabled) - prod: { - ...defaults, - port: 8080, - server: { - ...defaults.server, - index: 'index.html', - }, - }, - // Test (preview, local URLs, watch disabled) - test: { - ...defaults, +// Test (local URLs, watch disabled) +const test = { + ...dev, + port: 4000, + server: { + ...dev.server, middleware: [ // Blank page required for test environment { @@ -60,6 +73,13 @@ export default { }, }, ], - port: 4000, }, + snippet: false, + watch: false, +}; + +export default { + dev, + prod, + test, }; diff --git a/server.js b/server.js index 1d1c834d2..8be7d5b3d 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,6 @@ const configName = const settings = serverConfigs[configName]; // prettier-ignore -console.log(`\nStarting ${configName} server (${settings.server.index}, watch: ${Boolean(settings.files)})\n`); +console.log(`\nStarting ${configName} server (watch: ${Boolean(settings.files)})\n`); bsServer.init(settings); diff --git a/vercel.json b/vercel.json index 79ff17433..da3464f69 100644 --- a/vercel.json +++ b/vercel.json @@ -1,9 +1,13 @@ { - "redirects": [ + "headers": [ { - "source": "/", - "destination": "./docs/preview.html", - "permanent": true + "source": "/(.*)", + "headers": [{ "key": "x-robots-tag", "value": "noindex" }] } + ], + "redirects": [{ "source": "/", "destination": "/preview/" }], + "rewrites": [ + { "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" }, + { "source": "/preview/:path*", "destination": "/docs/:path*" } ] } From 1b9d200654e6441d626ce8cd173d071a98346b79 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:06:33 -0600 Subject: [PATCH 04/13] =?UTF-8?q?Tem=20fix=20for=20Vercal=20=E2=80=9Cunsup?= =?UTF-8?q?ported=20modules:=E2=80=9D=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/middleware.js b/middleware.js index fe6a14be9..e929c44e5 100644 --- a/middleware.js +++ b/middleware.js @@ -1,6 +1,26 @@ -import serverConfigs from './server.config.js'; - -const { rewriteRules } = serverConfigs.dev; +const rewriteRules = [ + // Replace CDN URLs with local paths + { + match: /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md', + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, +]; // Exports // ============================================================================= From 5a0a2897f648d989d6766758a91163f55b6e387c Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:26:56 -0600 Subject: [PATCH 05/13] Update server start console message --- server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 8be7d5b3d..ab4f98b56 100644 --- a/server.js +++ b/server.js @@ -6,8 +6,10 @@ const args = process.argv.slice(2); const configName = Object.keys(serverConfigs).find(name => args.includes(`--${name}`)) || 'prod'; const settings = serverConfigs[configName]; +const isWatch = Boolean(settings.files) && settings.watch !== false; +const urlType = configName === 'prod' ? 'CDN' : 'local'; // prettier-ignore -console.log(`\nStarting ${configName} server (watch: ${Boolean(settings.files)})\n`); +console.log(`\nStarting ${configName} server (${urlType} URLs, watch: ${isWatch})\n`); bsServer.init(settings); From 208ddbbe1a87b0526a0256900f4990f7ec539d63 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:27:24 -0600 Subject: [PATCH 06/13] =?UTF-8?q?Fix=20for=20Vercal=20=E2=80=9Cunsupported?= =?UTF-8?q?=20modules=E2=80=9D=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.js | 24 +----------------------- server.config.js | 49 +++++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/middleware.js b/middleware.js index e929c44e5..ff03f5657 100644 --- a/middleware.js +++ b/middleware.js @@ -1,26 +1,4 @@ -const rewriteRules = [ - // Replace CDN URLs with local paths - { - match: /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md', - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, - replace: '/lib/docsify.min.js', - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, - replace: '/lib/', - }, -]; +import { rewriteRules } from './server.config.js'; // Exports // ============================================================================= diff --git a/server.config.js b/server.config.js index 44d42891b..5efa290bb 100644 --- a/server.config.js +++ b/server.config.js @@ -4,6 +4,31 @@ import * as url from 'node:url'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); +// Rewrite rules shared with Vercel middleware.js +export const rewriteRules = [ + // Replace CDN URLs with local paths + { + match: /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md', + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, +]; + // Production (CDN URLs, watch disabled) const prod = { hostname: '127.0.0.1', @@ -22,29 +47,7 @@ const dev = { ...prod, files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], port: 3000, - rewriteRules: [ - // Replace CDN URLs with local paths - { - match: /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md', - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, - replace: '/lib/docsify.min.js', - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, - replace: '/lib/', - }, - ], + rewriteRules, server: { ...prod.server, routes: { From 8f7f57fff808d8f505d9088c322a6462a335cb32 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:41:01 -0600 Subject: [PATCH 07/13] =?UTF-8?q?Fix=20for=20Vercal=20=E2=80=9Cunsupported?= =?UTF-8?q?=20modules=E2=80=9D=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 4 +-- middleware.js | 4 ++- playwright.config.js | 4 +-- server.config.js => server.configs.js | 49 +++++++++++++-------------- server.js | 2 +- test/config/server.js | 4 +-- 6 files changed, 33 insertions(+), 34 deletions(-) rename server.config.js => server.configs.js (60%) diff --git a/jest.config.js b/jest.config.js index cc983caea..d6969f50f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ -import serverConfig from './server.config.js'; +import serverConfigs from './server.configs.js'; -const { hostname, port } = serverConfig.test; +const { hostname, port } = serverConfigs.test; const TEST_HOST = `http://${hostname}:${port}`; const sharedConfig = { errorOnDeprecated: true, diff --git a/middleware.js b/middleware.js index ff03f5657..6a3150b6d 100644 --- a/middleware.js +++ b/middleware.js @@ -1,4 +1,6 @@ -import { rewriteRules } from './server.config.js'; +import serverConfigs from './server.configs.js'; + +const { rewriteRules } = serverConfigs.dev; // Exports // ============================================================================= diff --git a/playwright.config.js b/playwright.config.js index 49f3bf874..b2157d633 100644 --- a/playwright.config.js +++ b/playwright.config.js @@ -1,7 +1,7 @@ import { devices } from '@playwright/test'; -import serverConfig from './server.config.js'; +import serverConfigs from './server.configs.js'; -const { hostname, port } = serverConfig.test; +const { hostname, port } = serverConfigs.test; const TEST_HOST = `http://${hostname}:${port}`; process.env.TEST_HOST = TEST_HOST; diff --git a/server.config.js b/server.configs.js similarity index 60% rename from server.config.js rename to server.configs.js index 5efa290bb..44d42891b 100644 --- a/server.config.js +++ b/server.configs.js @@ -4,31 +4,6 @@ import * as url from 'node:url'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -// Rewrite rules shared with Vercel middleware.js -export const rewriteRules = [ - // Replace CDN URLs with local paths - { - match: /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md', - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, - replace: '/lib/docsify.min.js', - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, - replace: '/lib/', - }, -]; - // Production (CDN URLs, watch disabled) const prod = { hostname: '127.0.0.1', @@ -47,7 +22,29 @@ const dev = { ...prod, files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], port: 3000, - rewriteRules, + rewriteRules: [ + // Replace CDN URLs with local paths + { + match: /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md', + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, + ], server: { ...prod.server, routes: { diff --git a/server.js b/server.js index ab4f98b56..366c9a1e4 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,5 @@ import { create } from 'browser-sync'; -import serverConfigs from './server.config.js'; +import serverConfigs from './server.configs.js'; const bsServer = create(); const args = process.argv.slice(2); diff --git a/test/config/server.js b/test/config/server.js index f5c8fa0c6..aceea3804 100644 --- a/test/config/server.js +++ b/test/config/server.js @@ -1,13 +1,13 @@ import * as process from 'node:process'; import { create } from 'browser-sync'; -import config from '../../server.config.js'; +import serverConfigs from '../../server.configs.js'; const bsServer = create(); export async function startServer() { // Wait for server to start return new Promise(resolve => { - const settings = config.test; + const settings = serverConfigs.test; console.log('\n'); From f0b47ec234cfd1cfba6a003d7c82ef2150adb440 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:45:59 -0600 Subject: [PATCH 08/13] =?UTF-8?q?Fix=20for=20Vercal=20=E2=80=9Cunsupported?= =?UTF-8?q?=20modules=E2=80=9D=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.js | 4 +--- rewriterules.config.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 rewriterules.config.js diff --git a/middleware.js b/middleware.js index 6a3150b6d..6f612f936 100644 --- a/middleware.js +++ b/middleware.js @@ -1,6 +1,4 @@ -import serverConfigs from './server.configs.js'; - -const { rewriteRules } = serverConfigs.dev; +import rewriteRules from './rewriterules.config.js'; // Exports // ============================================================================= diff --git a/rewriterules.config.js b/rewriterules.config.js new file mode 100644 index 000000000..cef981231 --- /dev/null +++ b/rewriterules.config.js @@ -0,0 +1,23 @@ +export default [ + // Replace CDN URLs with local paths + { + match: /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md', + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, +]; From 47656e102a6566e6c6429a13aae4be9b45fe9271 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 13:56:06 -0600 Subject: [PATCH 09/13] =?UTF-8?q?Fix=20for=20Vercal=20=E2=80=9Cunsupported?= =?UTF-8?q?=20modules=E2=80=9D=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.js | 27 +++++++++++++++++++++++++-- rewriterules.config.js | 23 ----------------------- server.configs.js | 25 ++----------------------- 3 files changed, 27 insertions(+), 48 deletions(-) delete mode 100644 rewriterules.config.js diff --git a/middleware.js b/middleware.js index 6f612f936..2bc9f4e19 100644 --- a/middleware.js +++ b/middleware.js @@ -1,11 +1,34 @@ -import rewriteRules from './rewriterules.config.js'; - // Exports // ============================================================================= export const config = { matcher: ['/preview/(index.html)?'], }; +// Rewrite rules shared with local server configurations +export const rewriteRules = [ + // Replace CDN URLs with local paths + { + match: /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md', + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, + replace: '/lib/docsify.min.js', + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, + replace: '/lib/', + }, +]; + // Serve virtual /preview/index.html // Note: See vercel.json for preview routing configuration // 1. Fetch index.html from /docs/ directory diff --git a/rewriterules.config.js b/rewriterules.config.js deleted file mode 100644 index cef981231..000000000 --- a/rewriterules.config.js +++ /dev/null @@ -1,23 +0,0 @@ -export default [ - // Replace CDN URLs with local paths - { - match: /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md', - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, - replace: '/lib/docsify.min.js', - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, - replace: '/lib/', - }, -]; diff --git a/server.configs.js b/server.configs.js index 44d42891b..24cec22db 100644 --- a/server.configs.js +++ b/server.configs.js @@ -1,5 +1,6 @@ import * as path from 'node:path'; import * as url from 'node:url'; +import { rewriteRules } from './middleware.js'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -22,29 +23,7 @@ const dev = { ...prod, files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], port: 3000, - rewriteRules: [ - // Replace CDN URLs with local paths - { - match: /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md', - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*(?=["'])/g, - replace: '/lib/docsify.min.js', - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match: /(?:https?:)*\/\/.*cdn.*docsify[@\d.latest]*\/(?:lib\/)/g, - replace: '/lib/', - }, - ], + rewriteRules, server: { ...prod.server, routes: { From 8e1adec1db8769da882febb37735eca8bba64417 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 14:04:52 -0600 Subject: [PATCH 10/13] Switch to named server config exports --- jest.config.js | 4 ++-- playwright.config.js | 4 ++-- server.configs.js | 20 +++++++------------- server.js | 10 ++++------ test/config/server.js | 4 ++-- 5 files changed, 17 insertions(+), 25 deletions(-) diff --git a/jest.config.js b/jest.config.js index d6969f50f..66ac68ded 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ -import serverConfigs from './server.configs.js'; +import { testConfig } from './server.configs.js'; -const { hostname, port } = serverConfigs.test; +const { hostname, port } = testConfig; const TEST_HOST = `http://${hostname}:${port}`; const sharedConfig = { errorOnDeprecated: true, diff --git a/playwright.config.js b/playwright.config.js index b2157d633..2c7e8894d 100644 --- a/playwright.config.js +++ b/playwright.config.js @@ -1,7 +1,7 @@ import { devices } from '@playwright/test'; -import serverConfigs from './server.configs.js'; +import { testConfig } from './server.configs.js'; -const { hostname, port } = serverConfigs.test; +const { hostname, port } = testConfig; const TEST_HOST = `http://${hostname}:${port}`; process.env.TEST_HOST = TEST_HOST; diff --git a/server.configs.js b/server.configs.js index 24cec22db..2f0889c72 100644 --- a/server.configs.js +++ b/server.configs.js @@ -6,7 +6,7 @@ const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Production (CDN URLs, watch disabled) -const prod = { +export const prodConfig = { hostname: '127.0.0.1', notify: false, open: false, @@ -19,13 +19,13 @@ const prod = { }; // Development (local URLs, watch enabled) -const dev = { - ...prod, +export const devConfig = { + ...prodConfig, files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'], port: 3000, rewriteRules, server: { - ...prod.server, + ...prodConfig.server, routes: { '/changelog.md': path.resolve(__dirname, 'CHANGELOG.md'), '/lib': path.resolve(__dirname, 'lib'), @@ -36,11 +36,11 @@ const dev = { }; // Test (local URLs, watch disabled) -const test = { - ...dev, +export const testConfig = { + ...devConfig, port: 4000, server: { - ...dev.server, + ...devConfig.server, middleware: [ // Blank page required for test environment { @@ -56,9 +56,3 @@ const test = { snippet: false, watch: false, }; - -export default { - dev, - prod, - test, -}; diff --git a/server.js b/server.js index 366c9a1e4..d6da12cbe 100644 --- a/server.js +++ b/server.js @@ -1,15 +1,13 @@ import { create } from 'browser-sync'; -import serverConfigs from './server.configs.js'; +import { devConfig, prodConfig } from './server.configs.js'; const bsServer = create(); const args = process.argv.slice(2); -const configName = - Object.keys(serverConfigs).find(name => args.includes(`--${name}`)) || 'prod'; -const settings = serverConfigs[configName]; -const isWatch = Boolean(settings.files) && settings.watch !== false; +const config = args.includes('--dev') ? devConfig : prodConfig; +const isWatch = Boolean(config.files) && config.watch !== false; const urlType = configName === 'prod' ? 'CDN' : 'local'; // prettier-ignore console.log(`\nStarting ${configName} server (${urlType} URLs, watch: ${isWatch})\n`); -bsServer.init(settings); +bsServer.init(config); diff --git a/test/config/server.js b/test/config/server.js index aceea3804..d896b58f6 100644 --- a/test/config/server.js +++ b/test/config/server.js @@ -1,13 +1,13 @@ import * as process from 'node:process'; import { create } from 'browser-sync'; -import serverConfigs from '../../server.configs.js'; +import { testConfig } from '../../server.configs.js'; const bsServer = create(); export async function startServer() { // Wait for server to start return new Promise(resolve => { - const settings = serverConfigs.test; + const settings = testConfig; console.log('\n'); From 8d06856467bd681d3bd8d2b13c31ebb5dad10f8d Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 14:13:17 -0600 Subject: [PATCH 11/13] Serve Vercel preview from root --- middleware.js | 2 +- vercel.json | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/middleware.js b/middleware.js index 2bc9f4e19..b31631463 100644 --- a/middleware.js +++ b/middleware.js @@ -1,7 +1,7 @@ // Exports // ============================================================================= export const config = { - matcher: ['/preview/(index.html)?'], + matcher: ['/(index.html)?'], }; // Rewrite rules shared with local server configurations diff --git a/vercel.json b/vercel.json index da3464f69..7f7435077 100644 --- a/vercel.json +++ b/vercel.json @@ -5,9 +5,5 @@ "headers": [{ "key": "x-robots-tag", "value": "noindex" }] } ], - "redirects": [{ "source": "/", "destination": "/preview/" }], - "rewrites": [ - { "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" }, - { "source": "/preview/:path*", "destination": "/docs/:path*" } - ] + "rewrites": [{ "source": "/:path*", "destination": "/docs/:path*" }] } From 0c2d2379ebd544ceae8d3985c445928a46ef2431 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 14:15:05 -0600 Subject: [PATCH 12/13] Revert "Serve Vercel preview from root" This reverts commit c13422a8f12c931720f0fa2dfbad4e4ed23d09ad. --- middleware.js | 2 +- vercel.json | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/middleware.js b/middleware.js index b31631463..2bc9f4e19 100644 --- a/middleware.js +++ b/middleware.js @@ -1,7 +1,7 @@ // Exports // ============================================================================= export const config = { - matcher: ['/(index.html)?'], + matcher: ['/preview/(index.html)?'], }; // Rewrite rules shared with local server configurations diff --git a/vercel.json b/vercel.json index 7f7435077..da3464f69 100644 --- a/vercel.json +++ b/vercel.json @@ -5,5 +5,9 @@ "headers": [{ "key": "x-robots-tag", "value": "noindex" }] } ], - "rewrites": [{ "source": "/:path*", "destination": "/docs/:path*" }] + "redirects": [{ "source": "/", "destination": "/preview/" }], + "rewrites": [ + { "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" }, + { "source": "/preview/:path*", "destination": "/docs/:path*" } + ] } From acd69cdaef8396d111a34b36153cd120502a192c Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Thu, 21 Dec 2023 14:24:45 -0600 Subject: [PATCH 13/13] Fix console output --- server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index d6da12cbe..4d58c713b 100644 --- a/server.js +++ b/server.js @@ -4,8 +4,9 @@ import { devConfig, prodConfig } from './server.configs.js'; const bsServer = create(); const args = process.argv.slice(2); const config = args.includes('--dev') ? devConfig : prodConfig; +const configName = config === devConfig ? 'development' : 'production'; const isWatch = Boolean(config.files) && config.watch !== false; -const urlType = configName === 'prod' ? 'CDN' : 'local'; +const urlType = config === devConfig ? 'local' : 'CDN'; // prettier-ignore console.log(`\nStarting ${configName} server (${urlType} URLs, watch: ${isWatch})\n`);