Skip to content

Commit cb1fe94

Browse files
committed
Source-loader: Fix preamble insertion
1 parent 2ecf3e1 commit cb1fe94

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`insertAfterImports addon-notes 1`] = `
4+
"import React from 'react';
5+
6+
import BaseButton from '../components/BaseButton';
7+
import markdownNotes from './notes/notes.md';
8+
INSERT
9+
10+
const markdownString = '...';"
11+
`;
12+
313
exports[`insertAfterImports imports 1`] = `
414
"import foo;
515
import bar;
616
INSERT
717
whatever;"
818
`;
919

20+
exports[`insertAfterImports multi-line imports 1`] = `
21+
"import foo;
22+
import {
23+
bar
24+
} from baz;
25+
INSERT
26+
whatever;"
27+
`;
28+
1029
exports[`insertAfterImports no imports 1`] = `
1130
"INSERT
1231
foo bar;
1332
baz;"
1433
`;
34+
35+
exports[`insertAfterImports single-line imports 1`] = `
36+
"import foo;
37+
import { bar } from baz;
38+
INSERT
39+
whatever;"
40+
`;

lib/source-loader/src/server/build.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export function insertAfterImports(insert, source) {
66
if (start === -1) {
77
start = 0;
88
} else {
9+
if (/import\s+{/.test(source.slice(start + 1, start + 10))) {
10+
start = source.indexOf('}', start + 1);
11+
}
912
start = 1 + source.indexOf('\n', start + 1);
1013
}
1114
const imports = source.slice(0, start);
@@ -49,7 +52,7 @@ var __LOCAL_DEPENDENCIES__ = ${JSON.stringify(localDependencies)};
4952
var __IDS_TO_FRAMEWORKS__ = ${JSON.stringify(idsToFrameworks)};
5053
/* eslint-enable no-unused-vars,@typescript-eslint/no-unused-vars */
5154
`;
52-
// const annotated = insertAfterImports(preamble, source);
55+
// return insertAfterImports(preamble, source);
5356
return `${preamble}${source}`;
5457
}
5558
);

lib/source-loader/src/server/build.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,36 @@ whatever;
1919
`.trim();
2020
expect(insertAfterImports(insert, hasImports)).toMatchSnapshot();
2121
});
22+
23+
it('single-line imports', () => {
24+
const hasImports = `
25+
import foo;
26+
import { bar } from baz;
27+
whatever;
28+
`.trim();
29+
expect(insertAfterImports(insert, hasImports)).toMatchSnapshot();
30+
});
31+
32+
it('multi-line imports', () => {
33+
const hasImports = `
34+
import foo;
35+
import {
36+
bar
37+
} from baz;
38+
whatever;
39+
`.trim();
40+
expect(insertAfterImports(insert, hasImports)).toMatchSnapshot();
41+
});
42+
43+
it('addon-notes', () => {
44+
const notesStory = `
45+
import React from 'react';
46+
47+
import BaseButton from '../components/BaseButton';
48+
import markdownNotes from './notes/notes.md';
49+
50+
const markdownString = '...';
51+
`.trim();
52+
expect(insertAfterImports(insert, notesStory)).toMatchSnapshot();
53+
});
2254
});

0 commit comments

Comments
 (0)