ember-repl 2.0.63 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +200 -35
- package/addon-main.cjs +5 -0
- package/dist/browser/cjs/eval.d.ts +10 -0
- package/dist/browser/cjs/eval.d.ts.map +1 -0
- package/dist/browser/cjs/eval.js +22 -0
- package/dist/browser/cjs/eval.js.map +1 -0
- package/dist/browser/cjs/index.d.ts +7 -0
- package/dist/browser/cjs/index.js +43 -0
- package/dist/browser/cjs/index.js.map +1 -0
- package/dist/browser/compile/formats.d.ts +15 -0
- package/dist/browser/compile/formats.js +169 -0
- package/dist/browser/compile/formats.js.map +1 -0
- package/dist/browser/compile/index.d.ts +32 -0
- package/dist/browser/compile/index.js +90 -0
- package/dist/browser/compile/index.js.map +1 -0
- package/dist/browser/compile/markdown-to-ember.d.ts +18 -0
- package/dist/browser/compile/markdown-to-ember.js +237 -0
- package/dist/browser/compile/markdown-to-ember.js.map +1 -0
- package/dist/browser/compile/types.d.ts +7 -0
- package/dist/browser/compile/types.js +2 -0
- package/dist/browser/compile/types.js.map +1 -0
- package/dist/browser/esm/index.d.ts +8 -0
- package/dist/browser/esm/index.js +67 -0
- package/dist/browser/esm/index.js.map +1 -0
- package/dist/browser/eti/babel-plugin.d.ts +54 -0
- package/dist/browser/eti/babel-plugin.js +95 -0
- package/dist/browser/eti/babel-plugin.js.map +1 -0
- package/dist/browser/eti/debug.d.ts +2 -0
- package/dist/browser/eti/debug.js +9 -0
- package/dist/browser/eti/debug.js.map +1 -0
- package/dist/browser/eti/parse-templates.d.ts +56 -0
- package/dist/browser/eti/parse-templates.js +181 -0
- package/dist/browser/eti/parse-templates.js.map +1 -0
- package/dist/browser/eti/preprocess.d.ts +57 -0
- package/dist/browser/eti/preprocess.js +270 -0
- package/dist/browser/eti/preprocess.js.map +1 -0
- package/dist/browser/eti/template-tag-transform.d.ts +15 -0
- package/dist/browser/eti/template-tag-transform.js +46 -0
- package/dist/browser/eti/template-tag-transform.js.map +1 -0
- package/dist/browser/eti/util.d.ts +14 -0
- package/dist/browser/eti/util.js +39 -0
- package/dist/browser/eti/util.js.map +1 -0
- package/dist/browser/gjs.d.ts +4 -0
- package/dist/browser/gjs.js +40 -0
- package/dist/browser/gjs.js.map +1 -0
- package/{hbs.d.ts → dist/browser/hbs.d.ts} +7 -7
- package/dist/browser/hbs.js +91 -0
- package/dist/browser/hbs.js.map +1 -0
- package/dist/browser/index.d.ts +6 -0
- package/dist/browser/index.js +6 -0
- package/dist/browser/index.js.map +1 -0
- package/{js.d.ts → dist/browser/js.d.ts} +3 -6
- package/dist/browser/js.js +38 -0
- package/dist/browser/js.js.map +1 -0
- package/{known-modules.d.ts → dist/browser/known-modules.d.ts} +6 -5
- package/dist/browser/known-modules.js +46 -0
- package/dist/browser/known-modules.js.map +1 -0
- package/dist/browser/types.d.ts +21 -0
- package/dist/browser/types.js +2 -0
- package/dist/browser/types.js.map +1 -0
- package/{utils.d.ts → dist/browser/utils.d.ts} +8 -3
- package/dist/browser/utils.js +46 -0
- package/dist/browser/utils.js.map +1 -0
- package/dist/build/ember-cli.cjs +36 -0
- package/dist/test-support/index.d.ts +2 -0
- package/dist/test-support/index.js +8 -0
- package/dist/test-support/index.js.map +1 -0
- package/package.json +122 -125
- package/{addon → src/browser}/cjs/eval.ts +9 -5
- package/src/browser/cjs/index.ts +44 -0
- package/src/browser/compile/formats.ts +168 -0
- package/src/browser/compile/index.ts +131 -0
- package/src/browser/compile/markdown-to-ember.ts +318 -0
- package/src/browser/compile/types.ts +7 -0
- package/src/browser/esm/index.ts +80 -0
- package/src/browser/eti/babel-plugin.ts +105 -0
- package/src/browser/eti/debug.ts +7 -0
- package/src/browser/eti/parse-templates.ts +284 -0
- package/src/browser/eti/preprocess.ts +187 -0
- package/src/browser/eti/template-tag-transform.ts +100 -0
- package/src/browser/eti/util.ts +72 -0
- package/src/browser/gjs.ts +59 -0
- package/{addon → src/browser}/hbs.ts +24 -12
- package/{addon → src/browser}/index.ts +1 -0
- package/{addon → src/browser}/js.ts +6 -2
- package/{addon → src/browser}/known-modules.ts +4 -2
- package/{addon → src/browser}/types.ts +6 -1
- package/{addon → src/browser}/utils.ts +6 -2
- package/src/build/ember-cli.cjs +36 -0
- package/src/test-support/index.ts +5 -0
- package/.github/renovate.json5 +0 -93
- package/.github/workflows/ci.yml +0 -120
- package/.github/workflows/lint.yml +0 -88
- package/.github/workflows/types.yml +0 -30
- package/CHANGELOG.md +0 -745
- package/addon/cjs/index.ts +0 -100
- package/addon/esm/index.ts +0 -131
- package/cjs/eval.d.ts +0 -8
- package/cjs/index.d.ts +0 -10
- package/config/environment.js +0 -5
- package/esm/index.d.ts +0 -11
- package/index.d.ts +0 -5
- package/index.js +0 -105
- package/tsconfig.json +0 -56
- package/types/dummy/index.d.ts +0 -1
- package/types/global.d.ts +0 -43
- package/types/overrides.d.ts +0 -18
- package/types.d.ts +0 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel-plugin.js","sources":["../../../src/browser/eti/babel-plugin.ts"],"sourcesContent":["import { ImportUtil } from 'babel-import-util';\n\nimport { transformTemplateTag } from './template-tag-transform';\nimport * as util from './util';\n\nimport type { NodePath } from '@babel/traverse';\nimport type { CallExpression, Class, Program } from '@babel/types';\n\n/**\n * This Babel plugin takes parseable code emitted by the string-based\n * preprocessor plugin in this package and converts it into calls to\n * the standardized `precompileTemplate` macro from `@ember/template-compilation`.\n *\n * Its goal is to convert code like this:\n *\n * ```js\n * import { hbs } from 'ember-template-imports';\n *\n * const A = hbs(`A`, {...});\n * const B = [__GLIMMER_TEMPLATE(`B`, {...})];\n * class C {\n * template = hbs(`C`, {...});\n * }\n *\n * [__GLIMMER_TEMPLATE(`default`, {...})];\n *\n * class D {\n * [__GLIMMER_TEMPLATE(`D`, {...})]\n * }\n * ```\n *\n * Into this:\n *\n * ```js\n * import { precompileTemplate } from '@ember/template-compilation';\n * import { setComponentTemplate } from '@ember/component';\n * import templateOnlyComponent from '@ember/component/template-only';\n *\n * const A = setComponentTemplate(\n * precompileTemplate(`A`, {...}),\n * templateOnlyComponent('this-module.js', 'A')\n * );\n * const B = setComponentTemplate(\n * precompileTemplate(`B`, {...}),\n * templateOnlyComponent('this-module.js', 'B')\n * );\n * class C {}\n * setComponentTemplate(precompileTemplate(`C`, {...}), C);\n *\n * export default setComponentTemplate(\n * precompileTemplate(`default`, {...}),\n * templateOnlyComponent('this-module.js', '_thisModule')\n * );\n *\n * class D {}\n * setComponentTemplate(precompileTemplate(`D`, {...}), D);\n * ```\n */\nexport default function (babel: any) {\n let t = babel.types;\n\n let visitor: any = {\n Program: {\n enter(path: NodePath<Program>, state: any) {\n state.importUtil = new ImportUtil(t, path);\n },\n },\n\n // Process class bodies before things like class properties get transformed\n // into imperative constructor code that we can't recognize. Taken directly\n // from babel-plugin-htmlbars-inline-precompile https://git.io/JMi1G\n Class(path: NodePath<Class>, state: any) {\n let bodyPath = path.get('body.body');\n\n if (!Array.isArray(bodyPath)) return;\n\n bodyPath.forEach((path) => {\n if (path.type !== 'ClassProperty') return;\n\n let keyPath = path.get('key');\n let valuePath = path.get('value');\n\n if (Array.isArray(keyPath)) return;\n\n if (keyPath && visitor[keyPath.type]) {\n visitor[keyPath.type](keyPath, state);\n }\n\n if (Array.isArray(valuePath)) return;\n\n if (valuePath && visitor[valuePath.type]) {\n visitor[valuePath.type](valuePath, state);\n }\n });\n },\n\n CallExpression(path: NodePath<CallExpression>, state: any) {\n if (util.isTemplateTag(path)) {\n transformTemplateTag(t, path, state);\n }\n },\n };\n\n return { visitor };\n}\n"],"names":["babel","t","types","visitor","Program","enter","path","state","importUtil","ImportUtil","Class","bodyPath","get","Array","isArray","forEach","type","keyPath","valuePath","CallExpression","util","transformTemplateTag"],"mappings":";;;;AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA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,IAAIf,OAAO,CAACe,SAAS,CAACF,IAAI,CAAC,EAAE;UACxCb,OAAO,CAACe,SAAS,CAACF,IAAI,CAAC,CAACE,SAAS,EAAEX,KAAK,CAAC,CAAA;AAC3C,SAAA;AACF,OAAC,CAAC,CAAA;KACH;AAEDY,IAAAA,cAAcA,CAACb,IAA8B,EAAEC,KAAU,EAAE;AACzD,MAAA,IAAIa,aAAkB,CAACd,IAAI,CAAC,EAAE;AAC5Be,QAAAA,oBAAoB,CAACpB,CAAC,EAAEK,IAAI,EAAEC,KAAK,CAAC,CAAA;AACtC,OAAA;AACF,KAAA;GACD,CAAA;EAED,OAAO;AAAEJ,IAAAA,OAAAA;GAAS,CAAA;AACpB;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.js","sources":["../../../src/browser/eti/debug.ts"],"sourcesContent":["export function expect<T>(value: T | null | undefined, message: string): T {\n if (value === undefined || value === null) {\n throw new Error(`LIBRARY BUG: ${message}`);\n }\n\n return value;\n}\n"],"names":["expect","value","message","undefined","Error"],"mappings":"AAAO,SAASA,MAAMA,CAAIC,KAA2B,EAAEC,OAAe,EAAK;AACzE,EAAA,IAAID,KAAK,KAAKE,SAAS,IAAIF,KAAK,KAAK,IAAI,EAAE;AACzC,IAAA,MAAM,IAAIG,KAAK,CAAE,CAAeF,aAAAA,EAAAA,OAAQ,EAAC,CAAC,CAAA;AAC5C,GAAA;AAEA,EAAA,OAAOD,KAAK,CAAA;AACd;;;;"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
type TemplateMatch = TemplateTagMatch;
|
|
2
|
+
interface TemplateTagMatch {
|
|
3
|
+
type: 'template-tag';
|
|
4
|
+
tagName: string;
|
|
5
|
+
start: RegExpMatchArray;
|
|
6
|
+
end: RegExpMatchArray;
|
|
7
|
+
contents: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents a static import of a template literal.
|
|
11
|
+
*/
|
|
12
|
+
interface StaticImportConfig {
|
|
13
|
+
/**
|
|
14
|
+
* The path to the package from which we want to import the template literal
|
|
15
|
+
* (e.g.: 'ember-cli-htmlbars')
|
|
16
|
+
*/
|
|
17
|
+
importPath: string;
|
|
18
|
+
/**
|
|
19
|
+
* The name of the template literal (e.g.: 'hbs') or 'default' if this package
|
|
20
|
+
* exports a default function
|
|
21
|
+
*/
|
|
22
|
+
importIdentifier: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The input options to instruct parseTemplates on how to parse the input.
|
|
26
|
+
*
|
|
27
|
+
* @param templateTag
|
|
28
|
+
*/
|
|
29
|
+
interface ParseTemplatesOptions {
|
|
30
|
+
/** Tag to use, if parsing template tags is enabled. */
|
|
31
|
+
templateTag?: string;
|
|
32
|
+
}
|
|
33
|
+
declare const DEFAULT_PARSE_TEMPLATES_OPTIONS: {
|
|
34
|
+
templateTag: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Parses a template to find all possible valid matches for an embedded template.
|
|
38
|
+
* Supported syntaxes are template literals:
|
|
39
|
+
*
|
|
40
|
+
* hbs`Hello, world!`
|
|
41
|
+
*
|
|
42
|
+
* And template tags
|
|
43
|
+
*
|
|
44
|
+
* <template></template>
|
|
45
|
+
*
|
|
46
|
+
* The parser excludes any values found within strings recursively, and also
|
|
47
|
+
* excludes any string literals with dynamic segments (e.g `${}`) since these
|
|
48
|
+
* cannot be valid templates.
|
|
49
|
+
*
|
|
50
|
+
* @param template The template to parse
|
|
51
|
+
* @param relativePath Relative file path for the template (for errors)
|
|
52
|
+
* @param options optional configuration options for how to parse templates
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
declare function parseTemplates(template: string, relativePath: string, options?: ParseTemplatesOptions): TemplateMatch[];
|
|
56
|
+
export { TemplateMatch, TemplateTagMatch, StaticImportConfig, ParseTemplatesOptions, DEFAULT_PARSE_TEMPLATES_OPTIONS, parseTemplates };
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { expect } from './debug.js';
|
|
2
|
+
import { TEMPLATE_TAG_NAME } from './util.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a static import of a template literal.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The input options to instruct parseTemplates on how to parse the input.
|
|
10
|
+
*
|
|
11
|
+
* @param templateTag
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const escapeChar = '\\';
|
|
15
|
+
const stringDelimiter = /['"]/;
|
|
16
|
+
const singleLineCommentStart = /\/\//;
|
|
17
|
+
const newLine = /\n/;
|
|
18
|
+
const multiLineCommentStart = /\/\*/;
|
|
19
|
+
const multiLineCommentEnd = /\*\//;
|
|
20
|
+
const templateLiteralStart = /([$a-zA-Z_][0-9a-zA-Z_$]*)?`/;
|
|
21
|
+
const templateLiteralEnd = /`/;
|
|
22
|
+
const dynamicSegmentStart = /\${/;
|
|
23
|
+
const blockStart = /{/;
|
|
24
|
+
const dynamicSegmentEnd = /}/;
|
|
25
|
+
function isEscaped(template, _offset) {
|
|
26
|
+
let offset = expect(_offset, 'Expected an index to check escaping');
|
|
27
|
+
let count = 0;
|
|
28
|
+
while (template[offset - 1] === escapeChar) {
|
|
29
|
+
count++;
|
|
30
|
+
offset--;
|
|
31
|
+
}
|
|
32
|
+
return count % 2 === 1;
|
|
33
|
+
}
|
|
34
|
+
const DEFAULT_PARSE_TEMPLATES_OPTIONS = {
|
|
35
|
+
templateTag: TEMPLATE_TAG_NAME
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses a template to find all possible valid matches for an embedded template.
|
|
40
|
+
* Supported syntaxes are template literals:
|
|
41
|
+
*
|
|
42
|
+
* hbs`Hello, world!`
|
|
43
|
+
*
|
|
44
|
+
* And template tags
|
|
45
|
+
*
|
|
46
|
+
* <template></template>
|
|
47
|
+
*
|
|
48
|
+
* The parser excludes any values found within strings recursively, and also
|
|
49
|
+
* excludes any string literals with dynamic segments (e.g `${}`) since these
|
|
50
|
+
* cannot be valid templates.
|
|
51
|
+
*
|
|
52
|
+
* @param template The template to parse
|
|
53
|
+
* @param relativePath Relative file path for the template (for errors)
|
|
54
|
+
* @param options optional configuration options for how to parse templates
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
function parseTemplates(template, relativePath, options = DEFAULT_PARSE_TEMPLATES_OPTIONS) {
|
|
58
|
+
const results = [];
|
|
59
|
+
const templateTag = options?.templateTag;
|
|
60
|
+
const templateTagStart = new RegExp(`<${templateTag}[^<]*>`);
|
|
61
|
+
const templateTagEnd = new RegExp(`</${templateTag}>`);
|
|
62
|
+
const argumentsMatchRegex = new RegExp(`<${templateTag}[^<]*\\S[^<]*>`);
|
|
63
|
+
const allTokens = new RegExp([singleLineCommentStart.source, newLine.source, multiLineCommentStart.source, multiLineCommentEnd.source, stringDelimiter.source, templateLiteralStart.source, templateLiteralEnd.source, dynamicSegmentStart.source, dynamicSegmentEnd.source, blockStart.source, templateTagStart.source, templateTagEnd.source].join('|'), 'g');
|
|
64
|
+
const tokens = Array.from(template.matchAll(allTokens));
|
|
65
|
+
while (tokens.length > 0) {
|
|
66
|
+
const currentToken = tokens.shift(); // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
|
67
|
+
|
|
68
|
+
parseToken(results, template, currentToken, tokens, true);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Parse the current token. If top level, then template tags can be parsed.
|
|
73
|
+
* Else, we are nested within a dynamic segment, which is currently unsupported.
|
|
74
|
+
*/
|
|
75
|
+
function parseToken(results, template, token, tokens, isTopLevel = false) {
|
|
76
|
+
if (token[0].match(multiLineCommentStart)) {
|
|
77
|
+
parseMultiLineComment(results, template, token, tokens);
|
|
78
|
+
} else if (token[0].match(singleLineCommentStart)) {
|
|
79
|
+
parseSingleLineComment(results, template, token, tokens);
|
|
80
|
+
} else if (token[0].match(templateLiteralStart)) {
|
|
81
|
+
parseTemplateLiteral(template, tokens);
|
|
82
|
+
} else if (isTopLevel && templateTag !== undefined && templateTagStart && token[0].match(templateTagStart)) {
|
|
83
|
+
parseTemplateTag(results, template, token, tokens, templateTag);
|
|
84
|
+
} else if (token[0].match(stringDelimiter)) {
|
|
85
|
+
parseString(results, template, token, tokens);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Parse a template literal. If a dynamic segment is found, enters the dynamic
|
|
91
|
+
* segment and parses it recursively. If no dynamic segments are found and the
|
|
92
|
+
* literal is top level (e.g. not nested within a dynamic segment) and has a
|
|
93
|
+
* tag, pushes it into the list of results.
|
|
94
|
+
*/
|
|
95
|
+
function parseTemplateLiteral(template, tokens) {
|
|
96
|
+
while (tokens.length > 0) {
|
|
97
|
+
let currentToken = expect(tokens.shift(), 'expected token');
|
|
98
|
+
if (isEscaped(template, currentToken.index)) continue;
|
|
99
|
+
if (currentToken[0].match(templateLiteralEnd)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Parse a string. All tokens within a string are ignored
|
|
107
|
+
* since there are no dynamic segments within these.
|
|
108
|
+
*/
|
|
109
|
+
function parseString(_results, template, startToken, tokens) {
|
|
110
|
+
while (tokens.length > 0) {
|
|
111
|
+
const currentToken = expect(tokens.shift(), 'expected token');
|
|
112
|
+
if (currentToken[0] === startToken[0] && !isEscaped(template, currentToken.index)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Parse a single-line comment. All tokens within a single-line comment are ignored
|
|
120
|
+
* since there are no dynamic segments within them.
|
|
121
|
+
*/
|
|
122
|
+
function parseSingleLineComment(_results, _template, _startToken, tokens) {
|
|
123
|
+
while (tokens.length > 0) {
|
|
124
|
+
const currentToken = expect(tokens.shift(), 'expected token');
|
|
125
|
+
if (currentToken[0] === '\n') {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Parse a multi-line comment. All tokens within a multi-line comment are ignored
|
|
133
|
+
* since there are no dynamic segments within them.
|
|
134
|
+
*/
|
|
135
|
+
function parseMultiLineComment(_results, _template, _startToken, tokens) {
|
|
136
|
+
while (tokens.length > 0) {
|
|
137
|
+
const currentToken = expect(tokens.shift(), 'expected token');
|
|
138
|
+
if (currentToken[0] === '*/') {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Parses a template tag. Continues parsing until the template tag has closed,
|
|
146
|
+
* accounting for nested template tags.
|
|
147
|
+
*/
|
|
148
|
+
function parseTemplateTag(results, _template, startToken, tokens, templateTag) {
|
|
149
|
+
let stack = 1;
|
|
150
|
+
if (argumentsMatchRegex && startToken[0].match(argumentsMatchRegex)) {
|
|
151
|
+
throw new Error(`embedded template preprocessing currently does not support passing arguments, found args in: ${relativePath}`);
|
|
152
|
+
}
|
|
153
|
+
while (tokens.length > 0) {
|
|
154
|
+
const currentToken = expect(tokens.shift(), 'expected token');
|
|
155
|
+
if (currentToken[0].match(templateTagStart)) {
|
|
156
|
+
stack++;
|
|
157
|
+
} else if (currentToken[0].match(templateTagEnd)) {
|
|
158
|
+
stack--;
|
|
159
|
+
}
|
|
160
|
+
if (stack === 0) {
|
|
161
|
+
let contents = '';
|
|
162
|
+
if (startToken.index !== undefined) {
|
|
163
|
+
const templateStart = startToken.index + startToken[0].length;
|
|
164
|
+
contents = template.slice(templateStart, currentToken.index);
|
|
165
|
+
}
|
|
166
|
+
results.push({
|
|
167
|
+
type: 'template-tag',
|
|
168
|
+
tagName: templateTag,
|
|
169
|
+
contents: contents,
|
|
170
|
+
start: startToken,
|
|
171
|
+
end: currentToken
|
|
172
|
+
});
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return results;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { DEFAULT_PARSE_TEMPLATES_OPTIONS, parseTemplates };
|
|
181
|
+
//# sourceMappingURL=parse-templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-templates.js","sources":["../../../src/browser/eti/parse-templates.ts"],"sourcesContent":["import { expect } from './debug';\nimport { TEMPLATE_TAG_NAME } from './util';\n\nexport type TemplateMatch = TemplateTagMatch;\n\nexport interface TemplateTagMatch {\n type: 'template-tag';\n tagName: string;\n start: RegExpMatchArray;\n end: RegExpMatchArray;\n contents: string;\n}\n\n/**\n * Represents a static import of a template literal.\n */\nexport interface StaticImportConfig {\n /**\n * The path to the package from which we want to import the template literal\n * (e.g.: 'ember-cli-htmlbars')\n */\n importPath: string;\n /**\n * The name of the template literal (e.g.: 'hbs') or 'default' if this package\n * exports a default function\n */\n importIdentifier: string;\n}\n\n/**\n * The input options to instruct parseTemplates on how to parse the input.\n *\n * @param templateTag\n */\nexport interface ParseTemplatesOptions {\n /** Tag to use, if parsing template tags is enabled. */\n templateTag?: string;\n}\n\nconst escapeChar = '\\\\';\nconst stringDelimiter = /['\"]/;\n\nconst singleLineCommentStart = /\\/\\//;\nconst newLine = /\\n/;\nconst multiLineCommentStart = /\\/\\*/;\nconst multiLineCommentEnd = /\\*\\//;\n\nconst templateLiteralStart = /([$a-zA-Z_][0-9a-zA-Z_$]*)?`/;\nconst templateLiteralEnd = /`/;\n\nconst dynamicSegmentStart = /\\${/;\nconst blockStart = /{/;\nconst dynamicSegmentEnd = /}/;\n\nfunction isEscaped(template: string, _offset: number | undefined) {\n let offset = expect(_offset, 'Expected an index to check escaping');\n\n let count = 0;\n\n while (template[offset - 1] === escapeChar) {\n count++;\n offset--;\n }\n\n return count % 2 === 1;\n}\n\nexport const DEFAULT_PARSE_TEMPLATES_OPTIONS = {\n templateTag: TEMPLATE_TAG_NAME,\n};\n\n/**\n * Parses a template to find all possible valid matches for an embedded template.\n * Supported syntaxes are template literals:\n *\n * hbs`Hello, world!`\n *\n * And template tags\n *\n * <template></template>\n *\n * The parser excludes any values found within strings recursively, and also\n * excludes any string literals with dynamic segments (e.g `${}`) since these\n * cannot be valid templates.\n *\n * @param template The template to parse\n * @param relativePath Relative file path for the template (for errors)\n * @param options optional configuration options for how to parse templates\n * @returns\n */\nexport function parseTemplates(\n template: string,\n relativePath: string,\n options: ParseTemplatesOptions = DEFAULT_PARSE_TEMPLATES_OPTIONS\n): TemplateMatch[] {\n const results: TemplateMatch[] = [];\n const templateTag = options?.templateTag;\n\n const templateTagStart = new RegExp(`<${templateTag}[^<]*>`);\n const templateTagEnd = new RegExp(`</${templateTag}>`);\n const argumentsMatchRegex = new RegExp(`<${templateTag}[^<]*\\\\S[^<]*>`);\n\n const allTokens = new RegExp(\n [\n singleLineCommentStart.source,\n newLine.source,\n multiLineCommentStart.source,\n multiLineCommentEnd.source,\n stringDelimiter.source,\n templateLiteralStart.source,\n templateLiteralEnd.source,\n dynamicSegmentStart.source,\n dynamicSegmentEnd.source,\n blockStart.source,\n templateTagStart.source,\n templateTagEnd.source,\n ].join('|'),\n 'g'\n );\n\n const tokens = Array.from(template.matchAll(allTokens));\n\n while (tokens.length > 0) {\n const currentToken = tokens.shift()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion\n\n parseToken(results, template, currentToken, tokens, true);\n }\n\n /**\n * Parse the current token. If top level, then template tags can be parsed.\n * Else, we are nested within a dynamic segment, which is currently unsupported.\n */\n function parseToken(\n results: TemplateMatch[],\n template: string,\n token: RegExpMatchArray,\n tokens: RegExpMatchArray[],\n isTopLevel = false\n ) {\n if (token[0].match(multiLineCommentStart)) {\n parseMultiLineComment(results, template, token, tokens);\n } else if (token[0].match(singleLineCommentStart)) {\n parseSingleLineComment(results, template, token, tokens);\n } else if (token[0].match(templateLiteralStart)) {\n parseTemplateLiteral(template, tokens);\n } else if (\n isTopLevel &&\n templateTag !== undefined &&\n templateTagStart &&\n token[0].match(templateTagStart)\n ) {\n parseTemplateTag(results, template, token, tokens, templateTag);\n } else if (token[0].match(stringDelimiter)) {\n parseString(results, template, token, tokens);\n }\n }\n\n /**\n * Parse a template literal. If a dynamic segment is found, enters the dynamic\n * segment and parses it recursively. If no dynamic segments are found and the\n * literal is top level (e.g. not nested within a dynamic segment) and has a\n * tag, pushes it into the list of results.\n */\n function parseTemplateLiteral(template: string, tokens: RegExpMatchArray[]) {\n while (tokens.length > 0) {\n let currentToken = expect(tokens.shift(), 'expected token');\n\n if (isEscaped(template, currentToken.index)) continue;\n\n if (currentToken[0].match(templateLiteralEnd)) {\n return;\n }\n }\n }\n\n /**\n * Parse a string. All tokens within a string are ignored\n * since there are no dynamic segments within these.\n */\n function parseString(\n _results: TemplateMatch[],\n template: string,\n startToken: RegExpMatchArray,\n tokens: RegExpMatchArray[]\n ) {\n while (tokens.length > 0) {\n const currentToken = expect(tokens.shift(), 'expected token');\n\n if (currentToken[0] === startToken[0] && !isEscaped(template, currentToken.index)) {\n return;\n }\n }\n }\n\n /**\n * Parse a single-line comment. All tokens within a single-line comment are ignored\n * since there are no dynamic segments within them.\n */\n function parseSingleLineComment(\n _results: TemplateMatch[],\n _template: string,\n _startToken: RegExpMatchArray,\n tokens: RegExpMatchArray[]\n ) {\n while (tokens.length > 0) {\n const currentToken = expect(tokens.shift(), 'expected token');\n\n if (currentToken[0] === '\\n') {\n return;\n }\n }\n }\n\n /**\n * Parse a multi-line comment. All tokens within a multi-line comment are ignored\n * since there are no dynamic segments within them.\n */\n function parseMultiLineComment(\n _results: TemplateMatch[],\n _template: string,\n _startToken: RegExpMatchArray,\n tokens: RegExpMatchArray[]\n ) {\n while (tokens.length > 0) {\n const currentToken = expect(tokens.shift(), 'expected token');\n\n if (currentToken[0] === '*/') {\n return;\n }\n }\n }\n\n /**\n * Parses a template tag. Continues parsing until the template tag has closed,\n * accounting for nested template tags.\n */\n function parseTemplateTag(\n results: TemplateMatch[],\n _template: string,\n startToken: RegExpMatchArray,\n tokens: RegExpMatchArray[],\n templateTag: string\n ) {\n let stack = 1;\n\n if (argumentsMatchRegex && startToken[0].match(argumentsMatchRegex)) {\n throw new Error(\n `embedded template preprocessing currently does not support passing arguments, found args in: ${relativePath}`\n );\n }\n\n while (tokens.length > 0) {\n const currentToken = expect(tokens.shift(), 'expected token');\n\n if (currentToken[0].match(templateTagStart)) {\n stack++;\n } else if (currentToken[0].match(templateTagEnd)) {\n stack--;\n }\n\n if (stack === 0) {\n let contents = '';\n\n if (startToken.index !== undefined) {\n const templateStart = startToken.index + startToken[0].length;\n\n contents = template.slice(templateStart, currentToken.index);\n }\n\n results.push({\n type: 'template-tag',\n tagName: templateTag,\n contents: contents,\n start: startToken,\n end: currentToken,\n });\n\n return;\n }\n }\n }\n\n return results;\n}\n"],"names":["escapeChar","stringDelimiter","singleLineCommentStart","newLine","multiLineCommentStart","multiLineCommentEnd","templateLiteralStart","templateLiteralEnd","dynamicSegmentStart","blockStart","dynamicSegmentEnd","isEscaped","template","_offset","offset","expect","count","DEFAULT_PARSE_TEMPLATES_OPTIONS","templateTag","TEMPLATE_TAG_NAME","parseTemplates","relativePath","options","results","templateTagStart","RegExp","templateTagEnd","argumentsMatchRegex","allTokens","source","join","tokens","Array","from","matchAll","length","currentToken","shift","parseToken","token","isTopLevel","match","parseMultiLineComment","parseSingleLineComment","parseTemplateLiteral","undefined","parseTemplateTag","parseString","index","_results","startToken","_template","_startToken","stack","Error","contents","templateStart","slice","push","type","tagName","start","end"],"mappings":";;;AAaA;AACA;AACA;;AAcA;AACA;AACA;AACA;AACA;;AAMA,MAAMA,UAAU,GAAG,IAAI,CAAA;AACvB,MAAMC,eAAe,GAAG,MAAM,CAAA;AAE9B,MAAMC,sBAAsB,GAAG,MAAM,CAAA;AACrC,MAAMC,OAAO,GAAG,IAAI,CAAA;AACpB,MAAMC,qBAAqB,GAAG,MAAM,CAAA;AACpC,MAA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face PreprocessOptionsEager {
|
|
2
|
+
importIdentifier?: string;
|
|
3
|
+
importPath?: string;
|
|
4
|
+
templateTag?: string;
|
|
5
|
+
templateTagReplacement?: string;
|
|
6
|
+
relativePath: string;
|
|
7
|
+
includeSourceMaps: boolean;
|
|
8
|
+
includeTemplateTokens: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface PreprocessOptionsLazy {
|
|
11
|
+
importIdentifier?: string;
|
|
12
|
+
importPath?: string;
|
|
13
|
+
templateTag?: string;
|
|
14
|
+
templateTagReplacement?: string;
|
|
15
|
+
relativePath: string;
|
|
16
|
+
includeSourceMaps: boolean;
|
|
17
|
+
includeTemplateTokens: boolean;
|
|
18
|
+
}
|
|
19
|
+
type PreprocessOptions = PreprocessOptionsLazy | PreprocessOptionsEager;
|
|
20
|
+
interface PreprocessedOutput {
|
|
21
|
+
output: string;
|
|
22
|
+
replacements: Replacement[];
|
|
23
|
+
}
|
|
24
|
+
interface Replacement {
|
|
25
|
+
type: 'start' | 'end';
|
|
26
|
+
index: number;
|
|
27
|
+
oldLength: number;
|
|
28
|
+
newLength: number;
|
|
29
|
+
originalLine: number;
|
|
30
|
+
originalCol: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Preprocesses all embedded templates within a JavaScript or TypeScript file.
|
|
34
|
+
* This function replaces all embedded templates that match our template syntax
|
|
35
|
+
* with valid, parseable JS. Optionally, it can also include a source map, and
|
|
36
|
+
* it can also include all possible values used within the template.
|
|
37
|
+
*
|
|
38
|
+
* Input:
|
|
39
|
+
*
|
|
40
|
+
* <template><MyComponent/><template>
|
|
41
|
+
*
|
|
42
|
+
* Output:
|
|
43
|
+
*
|
|
44
|
+
* [GLIMMER_TEMPLATE(`<MyComponent/>`, { scope() { return {MyComponent}; } })];
|
|
45
|
+
*
|
|
46
|
+
* It can also be used with template literals to provide the in scope values:
|
|
47
|
+
*
|
|
48
|
+
* Input:
|
|
49
|
+
*
|
|
50
|
+
* hbs`<MyComponent/>`;
|
|
51
|
+
*
|
|
52
|
+
* Output
|
|
53
|
+
*
|
|
54
|
+
* hbs(`<MyComponent/>`, { scope() { return {MyComponent}; } });
|
|
55
|
+
*/
|
|
56
|
+
declare function preprocessEmbeddedTemplates(template: string, options: PreprocessOptions): PreprocessedOutput;
|
|
57
|
+
export { preprocessEmbeddedTemplates };
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { getTemplateLocals } from '@glimmer/syntax';
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
|
+
import { expect } from './debug.js';
|
|
4
|
+
import { parseTemplates } from './parse-templates.js';
|
|
5
|
+
|
|
6
|
+
function getDefaultExportFromCjs (x) {
|
|
7
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
var toString = {}.toString;
|
|
11
|
+
var isarray = Array.isArray || function (arr) {
|
|
12
|
+
return toString.call(arr) == '[object Array]';
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/*!
|
|
16
|
+
* isobject <https://github.com/jonschlinkert/isobject>
|
|
17
|
+
*
|
|
18
|
+
* Copyright (c) 2014-2015, Jon Schlinkert.
|
|
19
|
+
* Licensed under the MIT License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
var isArray$1 = isarray;
|
|
23
|
+
var isobject = function isObject(val) {
|
|
24
|
+
return val != null && typeof val === 'object' && isArray$1(val) === false;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* line-column - Convert efficiently index to/from line-column in a string
|
|
29
|
+
* @module lineColumn
|
|
30
|
+
* @license MIT
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
var isArray = isarray;
|
|
34
|
+
var isObject = isobject;
|
|
35
|
+
var lineColumn = LineColumnFinder;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Finder for index and line-column from given string.
|
|
39
|
+
*
|
|
40
|
+
* You can call this without `new` operator as it returns an instance anyway.
|
|
41
|
+
*
|
|
42
|
+
* @class
|
|
43
|
+
* @param {string} str - A string to be parsed.
|
|
44
|
+
* @param {Object|number} [options] - Options.
|
|
45
|
+
* This can be an index in the string for shorthand of `lineColumn(str, index)`.
|
|
46
|
+
* @param {number} [options.origin=1] - The origin value of line and column.
|
|
47
|
+
*/
|
|
48
|
+
function LineColumnFinder(str, options) {
|
|
49
|
+
if (!(this instanceof LineColumnFinder)) {
|
|
50
|
+
if (typeof options === "number") {
|
|
51
|
+
return new LineColumnFinder(str).fromIndex(options);
|
|
52
|
+
}
|
|
53
|
+
return new LineColumnFinder(str, options);
|
|
54
|
+
}
|
|
55
|
+
this.str = str || "";
|
|
56
|
+
this.lineToIndex = buildLineToIndex(this.str);
|
|
57
|
+
options = options || {};
|
|
58
|
+
this.origin = typeof options.origin === "undefined" ? 1 : options.origin;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Find line and column from index in the string.
|
|
63
|
+
*
|
|
64
|
+
* @param {number} index - Index in the string. (0-origin)
|
|
65
|
+
* @return {Object|null}
|
|
66
|
+
* Found line number and column number in object `{ line: X, col: Y }`.
|
|
67
|
+
* If the given index is out of range, it returns `null`.
|
|
68
|
+
*/
|
|
69
|
+
LineColumnFinder.prototype.fromIndex = function (index) {
|
|
70
|
+
if (index < 0 || index >= this.str.length || isNaN(index)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
var line = findLowerIndexInRangeArray(index, this.lineToIndex);
|
|
74
|
+
return {
|
|
75
|
+
line: line + this.origin,
|
|
76
|
+
col: index - this.lineToIndex[line] + this.origin
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Find index from line and column in the string.
|
|
82
|
+
*
|
|
83
|
+
* @param {number|Object|Array} line - Line number in the string.
|
|
84
|
+
* This can be an Object of `{ line: X, col: Y }`, or
|
|
85
|
+
* an Array of `[line, col]`.
|
|
86
|
+
* @param {number} [column] - Column number in the string.
|
|
87
|
+
* This must be omitted or undefined when Object or Array is given
|
|
88
|
+
* to the first argument.
|
|
89
|
+
* @return {number}
|
|
90
|
+
* Found index in the string. (always 0-origin)
|
|
91
|
+
* If the given line or column is out of range, it returns `-1`.
|
|
92
|
+
*/
|
|
93
|
+
LineColumnFinder.prototype.toIndex = function (line, column) {
|
|
94
|
+
if (typeof column === "undefined") {
|
|
95
|
+
if (isArray(line) && line.length >= 2) {
|
|
96
|
+
return this.toIndex(line[0], line[1]);
|
|
97
|
+
}
|
|
98
|
+
if (isObject(line) && "line" in line && ("col" in line || "column" in line)) {
|
|
99
|
+
return this.toIndex(line.line, "col" in line ? line.col : line.column);
|
|
100
|
+
}
|
|
101
|
+
return -1;
|
|
102
|
+
}
|
|
103
|
+
if (isNaN(line) || isNaN(column)) {
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
line -= this.origin;
|
|
107
|
+
column -= this.origin;
|
|
108
|
+
if (line >= 0 && column >= 0 && line < this.lineToIndex.length) {
|
|
109
|
+
var lineIndex = this.lineToIndex[line];
|
|
110
|
+
var nextIndex = line === this.lineToIndex.length - 1 ? this.str.length : this.lineToIndex[line + 1];
|
|
111
|
+
if (column < nextIndex - lineIndex) {
|
|
112
|
+
return lineIndex + column;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return -1;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build an array of indexes of each line from a string.
|
|
120
|
+
*
|
|
121
|
+
* @private
|
|
122
|
+
* @param str {string} An input string.
|
|
123
|
+
* @return {number[]} Built array of indexes. The key is line number.
|
|
124
|
+
*/
|
|
125
|
+
function buildLineToIndex(str) {
|
|
126
|
+
var lines = str.split("\n"),
|
|
127
|
+
lineToIndex = new Array(lines.length),
|
|
128
|
+
index = 0;
|
|
129
|
+
for (var i = 0, l = lines.length; i < l; i++) {
|
|
130
|
+
lineToIndex[i] = index;
|
|
131
|
+
index += lines[i].length + /* "\n".length */1;
|
|
132
|
+
}
|
|
133
|
+
return lineToIndex;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Find a lower-bound index of a value in a sorted array of ranges.
|
|
138
|
+
*
|
|
139
|
+
* Assume `arr = [0, 5, 10, 15, 20]` and
|
|
140
|
+
* this returns `1` for `value = 7` (5 <= value < 10),
|
|
141
|
+
* and returns `3` for `value = 18` (15 <= value < 20).
|
|
142
|
+
*
|
|
143
|
+
* @private
|
|
144
|
+
* @param arr {number[]} An array of values representing ranges.
|
|
145
|
+
* @param value {number} A value to be searched.
|
|
146
|
+
* @return {number} Found index. If not found `-1`.
|
|
147
|
+
*/
|
|
148
|
+
function findLowerIndexInRangeArray(value, arr) {
|
|
149
|
+
if (value >= arr[arr.length - 1]) {
|
|
150
|
+
return arr.length - 1;
|
|
151
|
+
}
|
|
152
|
+
var min = 0,
|
|
153
|
+
max = arr.length - 2,
|
|
154
|
+
mid;
|
|
155
|
+
while (min < max) {
|
|
156
|
+
mid = min + (max - min >> 1);
|
|
157
|
+
if (value < arr[mid]) {
|
|
158
|
+
max = mid - 1;
|
|
159
|
+
} else if (value >= arr[mid + 1]) {
|
|
160
|
+
min = mid + 1;
|
|
161
|
+
} else {
|
|
162
|
+
// value >= arr[mid] && value < arr[mid + 1]
|
|
163
|
+
min = mid;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return min;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
var lineColumn$1 = /*@__PURE__*/getDefaultExportFromCjs(lineColumn);
|
|
171
|
+
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
173
|
+
function getMatchStartAndEnd(match) {
|
|
174
|
+
return {
|
|
175
|
+
start: expect(match.index, 'Expected regular expression match to have an index'),
|
|
176
|
+
end: expect(match.index, 'Expected regular expression match to have an index') + match[0].length
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function replacementFrom(template, index, oldLength, newLength, type) {
|
|
180
|
+
const loc = expect(lineColumn$1(template).fromIndex(index), 'BUG: expected to find a line/column based on index');
|
|
181
|
+
return {
|
|
182
|
+
type,
|
|
183
|
+
index,
|
|
184
|
+
oldLength,
|
|
185
|
+
newLength,
|
|
186
|
+
originalCol: loc.col,
|
|
187
|
+
originalLine: loc.line
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function replaceMatch(s, match, startReplacement, endReplacement, template, includeTemplateTokens) {
|
|
191
|
+
const {
|
|
192
|
+
start: openStart,
|
|
193
|
+
end: openEnd
|
|
194
|
+
} = getMatchStartAndEnd(match.start);
|
|
195
|
+
const {
|
|
196
|
+
start: closeStart,
|
|
197
|
+
end: closeEnd
|
|
198
|
+
} = getMatchStartAndEnd(match.end);
|
|
199
|
+
let options = '';
|
|
200
|
+
if (includeTemplateTokens) {
|
|
201
|
+
const tokensString = getTemplateLocals(template.slice(openEnd, closeStart)).filter(local => local.match(/^[$A-Z_][0-9A-Z_$]*$/i)).join(',');
|
|
202
|
+
if (tokensString.length > 0) {
|
|
203
|
+
options = `, scope: () => ({${tokensString}})`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const newStart = `${startReplacement}\``;
|
|
207
|
+
const newEnd = `\`, { strictMode: true${options} }${endReplacement}`;
|
|
208
|
+
s.overwrite(openStart, openEnd, newStart);
|
|
209
|
+
s.overwrite(closeStart, closeEnd, newEnd);
|
|
210
|
+
ensureBackticksEscaped(s, openEnd + 1, closeStart - 1);
|
|
211
|
+
return [replacementFrom(template, openStart, openEnd - openStart, newStart.length, 'start'), replacementFrom(template, closeStart, closeEnd - closeStart, newEnd.length, 'end')];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Preprocesses all embedded templates within a JavaScript or TypeScript file.
|
|
216
|
+
* This function replaces all embedded templates that match our template syntax
|
|
217
|
+
* with valid, parseable JS. Optionally, it can also include a source map, and
|
|
218
|
+
* it can also include all possible values used within the template.
|
|
219
|
+
*
|
|
220
|
+
* Input:
|
|
221
|
+
*
|
|
222
|
+
* <template><MyComponent/><template>
|
|
223
|
+
*
|
|
224
|
+
* Output:
|
|
225
|
+
*
|
|
226
|
+
* [GLIMMER_TEMPLATE(`<MyComponent/>`, { scope() { return {MyComponent}; } })];
|
|
227
|
+
*
|
|
228
|
+
* It can also be used with template literals to provide the in scope values:
|
|
229
|
+
*
|
|
230
|
+
* Input:
|
|
231
|
+
*
|
|
232
|
+
* hbs`<MyComponent/>`;
|
|
233
|
+
*
|
|
234
|
+
* Output
|
|
235
|
+
*
|
|
236
|
+
* hbs(`<MyComponent/>`, { scope() { return {MyComponent}; } });
|
|
237
|
+
*/
|
|
238
|
+
function preprocessEmbeddedTemplates(template, options) {
|
|
239
|
+
const {
|
|
240
|
+
templateTag,
|
|
241
|
+
templateTagReplacement,
|
|
242
|
+
includeTemplateTokens,
|
|
243
|
+
relativePath
|
|
244
|
+
} = options;
|
|
245
|
+
const parseTemplatesOptions = {
|
|
246
|
+
templateTag
|
|
247
|
+
};
|
|
248
|
+
const matches = parseTemplates(template, relativePath, parseTemplatesOptions);
|
|
249
|
+
const replacements = [];
|
|
250
|
+
const s = new MagicString(template);
|
|
251
|
+
for (const match of matches) {
|
|
252
|
+
if (match.type === 'template-tag') {
|
|
253
|
+
replacements.push(...replaceMatch(s, match, `[${templateTagReplacement}(`, ')]', template, includeTemplateTokens));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
let output = s.toString();
|
|
257
|
+
return {
|
|
258
|
+
output,
|
|
259
|
+
replacements
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function ensureBackticksEscaped(s, start, end) {
|
|
263
|
+
if (start >= end) return;
|
|
264
|
+
let content = s.slice(start, end);
|
|
265
|
+
content = content.replace(/(?<!\\)`/g, '\\`');
|
|
266
|
+
s.overwrite(start, end, content, false);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export { preprocessEmbeddedTemplates };
|
|
270
|
+
//# sourceMappingURL=preprocess.js.map
|