From 84a58f34a03718d9567daa0881a6987174331597 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 6 Aug 2018 22:35:02 -0400 Subject: [PATCH] add test for exporting with custom basepath --- src/api/export.ts | 14 ++++++-------- test/common/test.js | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/api/export.ts b/src/api/export.ts index d114f6d..051aaad 100644 --- a/src/api/export.ts +++ b/src/api/export.ts @@ -51,9 +51,10 @@ async function execute(emitter: EventEmitter, { const port = await ports.find(3000); const origin = `http://localhost:${port}`; + const root = new URL(basepath || '', origin); emitter.emit('info', { - message: `Crawling ${origin}` + message: `Crawling ${root.href}` }); const proc = child_process.fork(path.resolve(`${build}/server.js`), [], { @@ -71,7 +72,8 @@ async function execute(emitter: EventEmitter, { const deferreds = new Map(); function get_deferred(pathname: string) { - pathname = pathname.replace(`/${basepath}`, '') + pathname = pathname.replace(root.pathname, ''); + if (!deferreds.has(pathname)) { deferreds.set(pathname, new Deferred()); } @@ -108,7 +110,7 @@ async function execute(emitter: EventEmitter, { }); async function handle(url: URL) { - const pathname = url.pathname || '/'; + const pathname = (url.pathname.replace(root.pathname, '') || '/'); if (seen.has(pathname)) return; seen.add(pathname); @@ -141,11 +143,7 @@ async function execute(emitter: EventEmitter, { return ports.wait(port) .then(() => { // TODO all static routes - if (basepath) { - return handle(new URL(`/${basepath}/`, origin)); - } else { - return handle(new URL('/', origin)); - } + return handle(root); }) .then(() => proc.kill()); } diff --git a/test/common/test.js b/test/common/test.js index 300f94c..c46751f 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -59,9 +59,19 @@ describe('sapper', function() { basepath: '/custom-basepath' }); - describe('export', () => { + testExport({}); + + testExport({ basepath: '/custom-basepath' }); +}); + +function testExport({ basepath = '' }) { + describe(basepath ? `export --basepath ${basepath}` : 'export', () => { before(() => { - return exec(`node ${cli} export`); + if (basepath) { + process.env.BASEPATH = basepath; + } + + return exec(`node ${cli} export ${basepath ? `--basepath ${basepath}` : ''}`); }); it('export all pages', () => { @@ -96,7 +106,10 @@ describe('sapper', function() { 'service-worker.js', 'svelte-logo-192.png', 'svelte-logo-512.png', - ]; + ].map(file => { + return basepath ? path.join(basepath.replace(/^\//, ''), file) : file; + }); + // Client scripts that should show up in the extraction directory. const expectedClientRegexes = [ /client\/[^/]+\/main(\.\d+)?\.js/, @@ -126,7 +139,7 @@ describe('sapper', function() { }); }); }); -}); +} function run({ mode, basepath = '' }) { describe(`mode=${mode}`, function () {