Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 456a329

Browse files
committed
Strip comments and add tests
1 parent 089967a commit 456a329

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"http-link-header": "^1.0.2",
2323
"shimport": "^1.0.1",
2424
"sourcemap-codec": "^1.4.6",
25-
"string-hash": "^1.1.3"
25+
"string-hash": "^1.1.3",
26+
"strip-comments": "^2.0.1"
2627
},
2728
"devDependencies": {
2829
"@types/mocha": "^5.2.7",

src/core/create_manifest_data.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import stripComments from 'strip-comments';
34
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
45
import { posixify, reserved_words } from '../utils';
56

7+
export function string_has_preload(source: string) {
8+
return /export\s+.*?preload/.test(stripComments(source));
9+
}
10+
611
export default function create_manifest_data(cwd: string, extensions: string = '.svelte .html'): ManifestData {
712

813
const component_extensions = extensions.split(' ');
@@ -12,22 +17,24 @@ export default function create_manifest_data(cwd: string, extensions: string = '
1217
throw new Error(`As of Sapper 0.21, the routes/ directory should become src/routes/`);
1318
}
1419

15-
function has_preload(file: string) {
20+
function file_has_preload(file: string) {
1621
const source = fs.readFileSync(path.join(cwd, file), 'utf-8');
17-
return /export\s+.*?preload/.test(source);
22+
return string_has_preload(source);
1823
}
1924

2025
function find_layout(file_name: string, component_name: string, dir: string = '') {
2126
const ext = component_extensions.find((ext) => fs.existsSync(path.join(cwd, dir, `${file_name}${ext}`)));
2227
const file = posixify(path.join(dir, `${file_name}${ext}`))
2328

24-
return ext
25-
? {
29+
if (!ext) {
30+
return null;
31+
}
32+
33+
return {
2634
name: component_name,
2735
file: file,
28-
has_preload: has_preload(file)
29-
}
30-
: null;
36+
has_preload: file_has_preload(file)
37+
};
3138
}
3239

3340
const components: PageComponent[] = [];
@@ -153,7 +160,7 @@ export default function create_manifest_data(cwd: string, extensions: string = '
153160
const component = {
154161
name: get_slug(item.file),
155162
file: item.file,
156-
has_preload: has_preload(item.file)
163+
has_preload: file_has_preload(item.file)
157164
};
158165

159166
components.push(component);

test/unit/create_manifest_data/test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
import * as path from 'path';
22
import * as assert from 'assert';
3-
import create_manifest_data from '../../../src/core/create_manifest_data';
3+
import create_manifest_data, { string_has_preload } from '../../../src/core/create_manifest_data';
4+
5+
describe('string_has_preload', () => {
6+
it('should detect async preload', () => {
7+
const source = `
8+
<script context="module">
9+
export async function preload() {
10+
const client = getClient();
11+
let response = await client.query({ query: GET_POSTS });
12+
let posts = response.data.getPosts;
13+
return { posts };
14+
}
15+
</script>
16+
`;
17+
assert.ok(string_has_preload(source));
18+
});
19+
20+
it('should detect preload in composed function', () => {
21+
const source = `
22+
<script context="module">
23+
export const preload = verifyUser(catchErrors(function () { console.log('test'); }));
24+
</script>
25+
`;
26+
assert.ok(string_has_preload(source));
27+
});
28+
29+
it('should detect reusable preload functions', () => {
30+
const source = `
31+
<script context="module">
32+
export { preload } from './userData'
33+
</script>
34+
`;
35+
assert.ok(string_has_preload(source));
36+
});
37+
38+
it('should not detect preload functions in single line comment', () => {
39+
const source = `
40+
<script context="module">
41+
//export { preload } from './userData'
42+
</script>
43+
`;
44+
assert.ok(!string_has_preload(source));
45+
});
46+
47+
it('should not detect preload functions in comment block', () => {
48+
const source = `
49+
<script context="module">
50+
/*
51+
export async function preload() {
52+
const client = getClient();
53+
let response = await client.query({ query: GET_POSTS });
54+
let posts = response.data.getPosts;
55+
return { posts };
56+
}
57+
*/
58+
</script>
59+
`;
60+
assert.ok(!string_has_preload(source));
61+
});
62+
});
463

564
describe('manifest_data', () => {
665
it('creates routes', () => {

0 commit comments

Comments
 (0)