diff --git a/lib/prerender.js b/lib/prerender.js index 2648ca4..8bc9367 100644 --- a/lib/prerender.js +++ b/lib/prerender.js @@ -15,7 +15,7 @@ const port = 7784; class Prerender extends Plugin { constructor( builtAppTree, - { urls, indexFile, emptyFile, fastbootOptions, requestsPerFastboot }, + { urls, indexFile, emptyFile, flatOutput, fastbootOptions, requestsPerFastboot }, ui, plugins, rootURL @@ -24,6 +24,7 @@ class Prerender extends Plugin { this.urls = urls || []; this.indexFile = indexFile || 'index.html'; this.emptyFile = emptyFile || '_empty.html'; + this.flatOutput = flatOutput || false; this.ui = ui; this.plugins = plugins; this.rootURL = rootURL; @@ -189,11 +190,13 @@ class Prerender extends Plugin { } async _writeFile(url, html) { - let filename = path.join( - this.outputPath, - url.replace(this.rootURL, '/'), - this.indexFile - ); + let relativePath = url.replace(this.rootURL, '/'); + let filename; + if (this.flatOutput && relativePath !== '/') { + filename = path.join(this.outputPath, relativePath + '.html'); + } else { + filename = path.join(this.outputPath, relativePath, this.indexFile); + } await mkdirp(path.dirname(filename)); await writeFile(filename, html); } diff --git a/node-tests/flat-output-test.js b/node-tests/flat-output-test.js new file mode 100644 index 0000000..161abf3 --- /dev/null +++ b/node-tests/flat-output-test.js @@ -0,0 +1,52 @@ +const { module: Qmodule, test } = require('qunit'); +const fs = require('fs'); +const path = require('path'); +const os = require('os'); + +const Prerender = require('../lib/prerender'); + +function createPrerender(options = {}) { + let instance = Object.create(Prerender.prototype); + instance.rootURL = options.rootURL || '/'; + instance.indexFile = options.indexFile || 'index.html'; + instance.flatOutput = options.flatOutput || false; + Object.defineProperty(instance, 'outputPath', { value: options.outputPath }); + return instance; +} + +Qmodule('flatOutput', function (hooks) { + let tmpDir; + + hooks.beforeEach(function () { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prember-test-')); + }); + + hooks.afterEach(function () { + fs.rmSync(tmpDir, { recursive: true }); + }); + + test('default output writes index.html in subdirectory', async function (assert) { + let prerender = createPrerender({ outputPath: tmpDir }); + await prerender._writeFile('/foo/bar', 'test'); + assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar', 'index.html'))); + }); + + test('flatOutput writes .html files instead of subdirectories', async function (assert) { + let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true }); + await prerender._writeFile('/foo/bar', 'test'); + assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar.html'))); + assert.notOk(fs.existsSync(path.join(tmpDir, 'foo', 'bar', 'index.html'))); + }); + + test('flatOutput still writes index.html for root URL', async function (assert) { + let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true }); + await prerender._writeFile('/', 'root'); + assert.ok(fs.existsSync(path.join(tmpDir, 'index.html'))); + }); + + test('flatOutput works with custom rootURL', async function (assert) { + let prerender = createPrerender({ outputPath: tmpDir, flatOutput: true, rootURL: '/app/' }); + await prerender._writeFile('/app/foo/bar', 'test'); + assert.ok(fs.existsSync(path.join(tmpDir, 'foo', 'bar.html'))); + }); +});