This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup-plugin.js
75 lines (60 loc) · 1.91 KB
/
rollup-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fs from 'node:fs/promises';
import path from 'node:path';
import { preprocessEmbeddedTemplates } from 'ember-template-imports/lib/preprocess-embedded-templates.js';
import {
TEMPLATE_TAG_NAME,
TEMPLATE_TAG_PLACEHOLDER,
} from 'ember-template-imports/lib/util.js';
export default function firstClassComponentTemplates() {
return {
name: 'preprocess-fccts',
async resolveId(source, importer, options) {
if (source.endsWith('.hbs')) return;
for (let ext of ['', '.gjs', '.gts']) {
let result = await this.resolve(source + ext, importer, {
...options,
skipSelf: true,
});
if (result?.external) {
return;
}
if (FCCT_EXTENSION.test(result?.id)) {
return resolutionFor(result.id);
}
}
},
async load(id) {
let originalId = this.getModuleInfo(id)?.meta?.fccts?.originalId ?? id;
if (originalId !== id) {
this.addWatchFile(originalId);
}
if (FCCT_EXTENSION.test(originalId)) {
return await preprocessTemplates(originalId);
}
},
};
}
const FCCT_EXTENSION = /\.g([jt]s)$/;
function resolutionFor(originalId) {
return {
id: originalId.replace(FCCT_EXTENSION, '.$1'),
meta: {
fccts: { originalId },
},
};
}
async function preprocessTemplates(id) {
let ember = (await import('ember-source')).default;
let contents = await fs.readFile(id, 'utf-8');
// This is basically taken directly from `ember-template-imports`
let result = preprocessEmbeddedTemplates(contents, {
relativePath: path.relative('.', id),
getTemplateLocalsRequirePath: ember.absolutePaths.templateCompiler,
getTemplateLocalsExportPath: '_GlimmerSyntax.getTemplateLocals',
templateTag: TEMPLATE_TAG_NAME,
templateTagReplacement: TEMPLATE_TAG_PLACEHOLDER,
includeSourceMaps: true,
includeTemplateTokens: true,
});
return result.output;
}