From 1b6dfd3580c88cbaf88f13a9e7b962392c643a57 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 2 Sep 2018 20:33:00 -0400 Subject: [PATCH] ignore things that look like temp files when generating manifest data - fixes #220 --- src/core/create_manifest_data.ts | 6 +++- test/unit/create_manifest_data/index.ts | 33 ++++++++++++------- .../samples/lockfiles/foo.js | 0 .../samples/lockfiles/foo.js_tmp | 0 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 test/unit/create_manifest_data/samples/lockfiles/foo.js create mode 100644 test/unit/create_manifest_data/samples/lockfiles/foo.js_tmp diff --git a/src/core/create_manifest_data.ts b/src/core/create_manifest_data.ts index 83788c2..367317b 100644 --- a/src/core/create_manifest_data.ts +++ b/src/core/create_manifest_data.ts @@ -30,13 +30,16 @@ export default function create_manifest_data(cwd = locations.routes()): Manifest const file = path.relative(cwd, resolved); const is_dir = fs.statSync(resolved).isDirectory(); + const ext = path.extname(basename); + if (!is_dir && !/^\.[a-z]+$/i.test(ext)) return null; // filter out tmp files etc + const segment = is_dir ? basename : basename.slice(0, -path.extname(basename).length); const parts = get_parts(segment); const is_index = is_dir ? false : basename.startsWith('index.'); - const is_page = path.extname(basename) === '.html'; + const is_page = ext === '.html'; parts.forEach(part => { if (/\]\[/.test(part.content)) { @@ -57,6 +60,7 @@ export default function create_manifest_data(cwd = locations.routes()): Manifest is_page }; }) + .filter(Boolean) .sort(comparator); items.forEach(item => { diff --git a/test/unit/create_manifest_data/index.ts b/test/unit/create_manifest_data/index.ts index e847b8a..d21a537 100644 --- a/test/unit/create_manifest_data/index.ts +++ b/test/unit/create_manifest_data/index.ts @@ -1,10 +1,10 @@ import * as path from 'path'; import * as assert from 'assert'; -import manifest_data from '../../../src/core/create_manifest_data'; +import create_manifest_data from '../../../src/core/create_manifest_data'; describe('manifest_data', () => { it('creates routes', () => { - const { components, pages, server_routes } = manifest_data(path.join(__dirname, 'samples/basic')); + const { components, pages, server_routes } = create_manifest_data(path.join(__dirname, 'samples/basic')); const index = { name: 'index', file: 'index.html' }; const about = { name: 'about', file: 'about.html' }; @@ -68,7 +68,7 @@ describe('manifest_data', () => { }); it('encodes invalid characters', () => { - const { components, pages } = manifest_data(path.join(__dirname, 'samples/encoding')); + const { components, pages } = create_manifest_data(path.join(__dirname, 'samples/encoding')); // had to remove ? and " because windows @@ -90,7 +90,7 @@ describe('manifest_data', () => { }); it('allows regex qualifiers', () => { - const { pages } = manifest_data(path.join(__dirname, 'samples/qualifiers')); + const { pages } = create_manifest_data(path.join(__dirname, 'samples/qualifiers')); assert.deepEqual(pages.map(p => p.pattern), [ /^\/([0-9-a-z]{3,})\/?$/, @@ -100,7 +100,7 @@ describe('manifest_data', () => { }); it('sorts routes correctly', () => { - const { pages } = manifest_data(path.join(__dirname, 'samples/sorting')); + const { pages } = create_manifest_data(path.join(__dirname, 'samples/sorting')); assert.deepEqual(pages.map(p => p.parts.map(part => part && part.component.file)), [ ['index.html'], @@ -116,7 +116,7 @@ describe('manifest_data', () => { }); it('ignores files and directories with leading underscores', () => { - const { server_routes } = manifest_data(path.join(__dirname, 'samples/hidden-underscore')); + const { server_routes } = create_manifest_data(path.join(__dirname, 'samples/hidden-underscore')); assert.deepEqual(server_routes.map(r => r.file), [ 'index.js', @@ -125,7 +125,7 @@ describe('manifest_data', () => { }); it('ignores files and directories with leading dots except .well-known', () => { - const { server_routes } = manifest_data(path.join(__dirname, 'samples/hidden-dot')); + const { server_routes } = create_manifest_data(path.join(__dirname, 'samples/hidden-dot')); assert.deepEqual(server_routes.map(r => r.file), [ '.well-known/dnt-policy.txt.js' @@ -134,24 +134,35 @@ describe('manifest_data', () => { it('fails on clashes', () => { assert.throws(() => { - const { pages } = manifest_data(path.join(__dirname, 'samples/clash-pages')); + const { pages } = create_manifest_data(path.join(__dirname, 'samples/clash-pages')); }, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/); assert.throws(() => { - const { server_routes } = manifest_data(path.join(__dirname, 'samples/clash-routes')); + const { server_routes } = create_manifest_data(path.join(__dirname, 'samples/clash-routes')); console.log(server_routes); }, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/); }); it('fails if dynamic params are not separated', () => { assert.throws(() => { - manifest_data(path.join(__dirname, 'samples/invalid-params')); + create_manifest_data(path.join(__dirname, 'samples/invalid-params')); }, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/); }); it('errors when trying to use reserved characters in route regexp', () => { assert.throws(() => { - manifest_data(path.join(__dirname, 'samples/invalid-qualifier')); + create_manifest_data(path.join(__dirname, 'samples/invalid-qualifier')); }, /Invalid route \[foo\(\[a-z\]\(\[0-9\]\)\)\].js — cannot use \(, \), \? or \: in route qualifiers/); }); + + it('ignores things that look like lockfiles' , () => { + const { server_routes } = create_manifest_data(path.join(__dirname, 'samples/lockfiles')); + + assert.deepEqual(server_routes, [{ + file: 'foo.js', + name: 'route_foo', + params: [], + pattern: /^\/foo$/ + }]); + }); }); \ No newline at end of file diff --git a/test/unit/create_manifest_data/samples/lockfiles/foo.js b/test/unit/create_manifest_data/samples/lockfiles/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/unit/create_manifest_data/samples/lockfiles/foo.js_tmp b/test/unit/create_manifest_data/samples/lockfiles/foo.js_tmp new file mode 100644 index 0000000..e69de29