|
1 | 1 | import { GlintExtensionPreprocess } from '@glint/core/config-types';
|
2 |
| -import { parseTemplates } from 'ember-template-imports'; |
3 | 2 | import { GLOBAL_TAG, PreprocessData, TemplateLocation } from './common';
|
4 |
| - |
| 3 | +/** |
| 4 | + * We don't need to actually generate the code that would be emitted for |
| 5 | + * a real app to build, we can use placeholders, so we have less offsets |
| 6 | + * to worry about (precompile, setComponentTemplate, their imports, etc) |
| 7 | + */ |
5 | 8 | const TEMPLATE_START = `[${GLOBAL_TAG}\``;
|
6 | 9 | const TEMPLATE_END = '`]';
|
7 | 10 |
|
| 11 | +// NOTE: This import is a lie -- We are not real ESM, and this is compiled to CJS. |
| 12 | +// ESM usage would be different here, and this is the CJS version of content-tag. |
| 13 | +import { Preprocessor } from 'content-tag'; |
| 14 | +const p = new Preprocessor(); |
| 15 | + |
| 16 | +interface Parsed { |
| 17 | + type: 'expression' | 'class-member'; |
| 18 | + tagName: 'template'; |
| 19 | + contents: string; |
| 20 | + range: { |
| 21 | + start: number; |
| 22 | + end: number; |
| 23 | + }; |
| 24 | + contentRange: { |
| 25 | + start: number; |
| 26 | + end: number; |
| 27 | + }; |
| 28 | + startRange: { |
| 29 | + end: number; |
| 30 | + start: number; |
| 31 | + }; |
| 32 | + endRange: { |
| 33 | + start: number; |
| 34 | + end: number; |
| 35 | + }; |
| 36 | +} |
| 37 | + |
8 | 38 | export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, path) => {
|
9 |
| - let templates = parseTemplates(source, path, { templateTag: 'template' }).filter( |
10 |
| - (match) => match.type === 'template-tag' |
11 |
| - ); |
| 39 | + let templates = p.parse(source, path) as Parsed[]; |
12 | 40 |
|
13 | 41 | let templateLocations: Array<TemplateLocation> = [];
|
14 | 42 | let segments: Array<string> = [];
|
15 | 43 | let sourceOffset = 0;
|
16 | 44 | let delta = 0;
|
17 | 45 |
|
18 | 46 | for (let template of templates) {
|
19 |
| - let { start, end } = template; |
20 |
| - let startTagLength = template.start[0].length; |
21 |
| - let endTagLength = template.end[0].length; |
22 |
| - let startTagOffset = start.index ?? -1; |
23 |
| - let endTagOffset = end.index ?? -1; |
| 47 | + let startTagLength = template.startRange.end - template.startRange.start; |
| 48 | + let endTagLength = template.endRange.end - template.endRange.start; |
| 49 | + let startTagOffset = template.startRange.start; |
| 50 | + let endTagOffset = template.endRange.start; |
24 | 51 |
|
25 | 52 | if (startTagOffset === -1 || endTagOffset === -1) continue;
|
26 | 53 |
|
|
0 commit comments