ember-repl 3.0.0-beta.6 → 3.0.0-beta.7
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/declarations/browser/gjs.d.ts +1 -1
- package/declarations/browser/gjs.d.ts.map +1 -1
- package/declarations/browser/known-modules.d.ts +4 -0
- package/declarations/browser/known-modules.d.ts.map +1 -1
- package/dist/browser/cjs/index.js +1 -1
- package/dist/browser/cjs/index.js.map +1 -1
- package/dist/browser/esm/index.js +1 -1
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/gjs.js +12 -13
- package/dist/browser/gjs.js.map +1 -1
- package/dist/browser/known-modules.js +4 -4
- package/dist/browser/known-modules.js.map +1 -1
- package/package.json +52 -51
- package/src/browser/cjs/index.ts +1 -1
- package/src/browser/esm/index.ts +1 -1
- package/src/browser/gjs.ts +13 -15
- package/src/browser/known-modules.ts +4 -2
- package/declarations/browser/eti/babel-plugin.d.ts +0 -54
- package/declarations/browser/eti/babel-plugin.d.ts.map +0 -1
- package/declarations/browser/eti/debug.d.ts +0 -2
- package/declarations/browser/eti/debug.d.ts.map +0 -1
- package/declarations/browser/eti/parse-templates.d.ts +0 -56
- package/declarations/browser/eti/parse-templates.d.ts.map +0 -1
- package/declarations/browser/eti/preprocess.d.ts +0 -58
- package/declarations/browser/eti/preprocess.d.ts.map +0 -1
- package/declarations/browser/eti/template-tag-transform.d.ts +0 -15
- package/declarations/browser/eti/template-tag-transform.d.ts.map +0 -1
- package/declarations/browser/eti/util.d.ts +0 -14
- package/declarations/browser/eti/util.d.ts.map +0 -1
- package/dist/browser/eti/babel-plugin.js +0 -95
- package/dist/browser/eti/babel-plugin.js.map +0 -1
- package/dist/browser/eti/debug.js +0 -9
- package/dist/browser/eti/debug.js.map +0 -1
- package/dist/browser/eti/parse-templates.js +0 -181
- package/dist/browser/eti/parse-templates.js.map +0 -1
- package/dist/browser/eti/preprocess.js +0 -106
- package/dist/browser/eti/preprocess.js.map +0 -1
- package/dist/browser/eti/template-tag-transform.js +0 -46
- package/dist/browser/eti/template-tag-transform.js.map +0 -1
- package/dist/browser/eti/util.js +0 -39
- package/dist/browser/eti/util.js.map +0 -1
- package/src/browser/eti/babel-plugin.ts +0 -105
- package/src/browser/eti/debug.ts +0 -7
- package/src/browser/eti/parse-templates.ts +0 -284
- package/src/browser/eti/preprocess.ts +0 -187
- package/src/browser/eti/template-tag-transform.ts +0 -100
- package/src/browser/eti/util.ts +0 -72
package/dist/browser/eti/util.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// const Greeting = <template>Hello</template>
|
|
2
|
-
const TEMPLATE_TAG_NAME = 'template';
|
|
3
|
-
const TEMPLATE_TAG_PLACEHOLDER = '__GLIMMER_TEMPLATE';
|
|
4
|
-
function isTemplateTag(callExpressionPath) {
|
|
5
|
-
const callee = callExpressionPath.get('callee');
|
|
6
|
-
return !Array.isArray(callee) && callee.isIdentifier() && callee.node.name === TEMPLATE_TAG_PLACEHOLDER;
|
|
7
|
-
}
|
|
8
|
-
function buildPrecompileTemplateCall(t, callExpressionPath, state) {
|
|
9
|
-
const callee = callExpressionPath.get('callee');
|
|
10
|
-
return t.callExpression(state.importUtil.import(callee, '@ember/template-compilation', 'precompileTemplate'), callExpressionPath.node.arguments);
|
|
11
|
-
}
|
|
12
|
-
function registerRefs(newPath, getRefPaths) {
|
|
13
|
-
if (Array.isArray(newPath)) {
|
|
14
|
-
if (newPath.length > 1) {
|
|
15
|
-
throw new Error('registerRefs is only meant to handle single node transformations. Received more than one path node.');
|
|
16
|
-
}
|
|
17
|
-
newPath = newPath[0];
|
|
18
|
-
}
|
|
19
|
-
const refPaths = getRefPaths(newPath);
|
|
20
|
-
for (const ref of refPaths) {
|
|
21
|
-
if (!ref.isIdentifier()) {
|
|
22
|
-
throw new Error('ember-template-imports internal assumption that refPath should of type identifier. Please open an issue.');
|
|
23
|
-
}
|
|
24
|
-
const binding = ref.scope.getBinding(ref.node.name);
|
|
25
|
-
if (binding !== undefined) {
|
|
26
|
-
binding.reference(ref);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
const SUPPORTED_EXTENSIONS = ['.js', '.ts', '.gjs', '.gts'];
|
|
31
|
-
function isSupportedScriptFileExtension(filePath) {
|
|
32
|
-
return SUPPORTED_EXTENSIONS.some(ext => filePath.endsWith(ext));
|
|
33
|
-
}
|
|
34
|
-
function isStrictMode(templateInfo) {
|
|
35
|
-
return templateInfo.type === 'template-tag';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export { TEMPLATE_TAG_NAME, TEMPLATE_TAG_PLACEHOLDER, buildPrecompileTemplateCall, isStrictMode, isSupportedScriptFileExtension, isTemplateTag, registerRefs };
|
|
39
|
-
//# sourceMappingURL=util.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sources":["../../../src/browser/eti/util.ts"],"sourcesContent":["import type { TemplateMatch } from './parse-templates.ts';\nimport type { NodePath } from '@babel/traverse';\nimport type { CallExpression } from '@babel/types';\nimport type { ImportUtil } from 'babel-import-util';\n\n// const Greeting = <template>Hello</template>\nexport const TEMPLATE_TAG_NAME = 'template';\nexport const TEMPLATE_TAG_PLACEHOLDER = '__GLIMMER_TEMPLATE';\n\nexport function isTemplateTag(callExpressionPath: NodePath<CallExpression>) {\n const callee = callExpressionPath.get('callee');\n\n return (\n !Array.isArray(callee) && callee.isIdentifier() && callee.node.name === TEMPLATE_TAG_PLACEHOLDER\n );\n}\n\nexport function buildPrecompileTemplateCall(\n t: any,\n callExpressionPath: NodePath<CallExpression>,\n state: {\n importUtil: ImportUtil;\n }\n): CallExpression {\n const callee = callExpressionPath.get('callee');\n\n return t.callExpression(\n state.importUtil.import(callee, '@ember/template-compilation', 'precompileTemplate'),\n callExpressionPath.node.arguments\n );\n}\n\nexport function registerRefs(\n newPath: string | string[],\n getRefPaths: (path: string) => NodePath[]\n) {\n if (Array.isArray(newPath)) {\n if (newPath.length > 1) {\n throw new Error(\n 'registerRefs is only meant to handle single node transformations. Received more than one path node.'\n );\n }\n\n newPath = newPath[0] as string;\n }\n\n const refPaths = getRefPaths(newPath);\n\n for (const ref of refPaths) {\n if (!ref.isIdentifier()) {\n throw new Error(\n 'ember-template-imports internal assumption that refPath should of type identifier. Please open an issue.'\n );\n }\n\n const binding = ref.scope.getBinding(ref.node.name);\n\n if (binding !== undefined) {\n binding.reference(ref);\n }\n }\n}\n\nconst SUPPORTED_EXTENSIONS = ['.js', '.ts', '.gjs', '.gts'];\n\nexport function isSupportedScriptFileExtension(filePath: string) {\n return SUPPORTED_EXTENSIONS.some((ext) => filePath.endsWith(ext));\n}\n\nexport function isStrictMode(templateInfo: TemplateMatch): boolean {\n return templateInfo.type === 'template-tag';\n}\n"],"names":["TEMPLATE_TAG_NAME","TEMPLATE_TAG_PLACEHOLDER","isTemplateTag","callExpressionPath","callee","get","Array","isArray","isIdentifier","node","name","buildPrecompileTemplateCall","t","state","callExpression","importUtil","import","arguments","registerRefs","newPath","getRefPaths","length","Error","refPaths","ref","binding","scope","getBinding","undefined","reference","SUPPORTED_EXTENSIONS","isSupportedScriptFileExtension","filePath","some","ext","endsWith","isStrictMode","templateInfo","type"],"mappings":"AAKA;AACO,MAAMA,iBAAiB,GAAG,WAAU;AACpC,MAAMC,wBAAwB,GAAG,qBAAoB;AAErD,SAASC,aAAaA,CAACC,kBAA4C,EAAE;AAC1E,EAAA,MAAMC,MAAM,GAAGD,kBAAkB,CAACE,GAAG,CAAC,QAAQ,CAAC,CAAA;EAE/C,OACE,CAACC,KAAK,CAACC,OAAO,CAACH,MAAM,CAAC,IAAIA,MAAM,CAACI,YAAY,EAAE,IAAIJ,MAAM,CAACK,IAAI,CAACC,IAAI,KAAKT,wBAAwB,CAAA;AAEpG,CAAA;AAEO,SAASU,2BAA2BA,CACzCC,CAAM,EACNT,kBAA4C,EAC5CU,KAEC,EACe;AAChB,EAAA,MAAMT,MAAM,GAAGD,kBAAkB,CAACE,GAAG,CAAC,QAAQ,CAAC,CAAA;EAE/C,OAAOO,CAAC,CAACE,cAAc,CACrBD,KAAK,CAACE,UAAU,CAACC,MAAM,CAACZ,MAAM,EAAE,6BAA6B,EAAE,oBAAoB,CAAC,EACpFD,kBAAkB,CAACM,IAAI,CAACQ,SAC1B,CAAC,CAAA;AACH,CAAA;AAEO,SAASC,YAAYA,CAC1BC,OAA0B,EAC1BC,WAAyC,EACzC;AACA,EAAA,IAAId,KAAK,CAACC,OAAO,CAACY,OAAO,CAAC,EAAE;AAC1B,IAAA,IAAIA,OAAO,CAACE,MAAM,GAAG,CAAC,EAAE;AACtB,MAAA,MAAM,IAAIC,KAAK,CACb,qGACF,CAAC,CAAA;AACH,KAAA;AAEAH,IAAAA,OAAO,GAAGA,OAAO,CAAC,CAAC,CAAW,CAAA;AAChC,GAAA;AAEA,EAAA,MAAMI,QAAQ,GAAGH,WAAW,CAACD,OAAO,CAAC,CAAA;AAErC,EAAA,KAAK,MAAMK,GAAG,IAAID,QAAQ,EAAE;AAC1B,IAAA,IAAI,CAACC,GAAG,CAAChB,YAAY,EAAE,EAAE;AACvB,MAAA,MAAM,IAAIc,KAAK,CACb,0GACF,CAAC,CAAA;AACH,KAAA;AAEA,IAAA,MAAMG,OAAO,GAAGD,GAAG,CAACE,KAAK,CAACC,UAAU,CAACH,GAAG,CAACf,IAAI,CAACC,IAAI,CAAC,CAAA;IAEnD,IAAIe,OAAO,KAAKG,SAAS,EAAE;AACzBH,MAAAA,OAAO,CAACI,SAAS,CAACL,GAAG,CAAC,CAAA;AACxB,KAAA;AACF,GAAA;AACF,CAAA;AAEA,MAAMM,oBAAoB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AAEpD,SAASC,8BAA8BA,CAACC,QAAgB,EAAE;AAC/D,EAAA,OAAOF,oBAAoB,CAACG,IAAI,CAAEC,GAAG,IAAKF,QAAQ,CAACG,QAAQ,CAACD,GAAG,CAAC,CAAC,CAAA;AACnE,CAAA;AAEO,SAASE,YAAYA,CAACC,YAA2B,EAAW;AACjE,EAAA,OAAOA,YAAY,CAACC,IAAI,KAAK,cAAc,CAAA;AAC7C;;;;"}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { ImportUtil } from 'babel-import-util';
|
|
2
|
-
|
|
3
|
-
import { transformTemplateTag } from './template-tag-transform.ts';
|
|
4
|
-
import * as util from './util.ts';
|
|
5
|
-
|
|
6
|
-
import type { NodePath } from '@babel/traverse';
|
|
7
|
-
import type { CallExpression, Class, Program } from '@babel/types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* This Babel plugin takes parseable code emitted by the string-based
|
|
11
|
-
* preprocessor plugin in this package and converts it into calls to
|
|
12
|
-
* the standardized `precompileTemplate` macro from `@ember/template-compilation`.
|
|
13
|
-
*
|
|
14
|
-
* Its goal is to convert code like this:
|
|
15
|
-
*
|
|
16
|
-
* ```js
|
|
17
|
-
* import { hbs } from 'ember-template-imports';
|
|
18
|
-
*
|
|
19
|
-
* const A = hbs(`A`, {...});
|
|
20
|
-
* const B = [__GLIMMER_TEMPLATE(`B`, {...})];
|
|
21
|
-
* class C {
|
|
22
|
-
* template = hbs(`C`, {...});
|
|
23
|
-
* }
|
|
24
|
-
*
|
|
25
|
-
* [__GLIMMER_TEMPLATE(`default`, {...})];
|
|
26
|
-
*
|
|
27
|
-
* class D {
|
|
28
|
-
* [__GLIMMER_TEMPLATE(`D`, {...})]
|
|
29
|
-
* }
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* Into this:
|
|
33
|
-
*
|
|
34
|
-
* ```js
|
|
35
|
-
* import { precompileTemplate } from '@ember/template-compilation';
|
|
36
|
-
* import { setComponentTemplate } from '@ember/component';
|
|
37
|
-
* import templateOnlyComponent from '@ember/component/template-only';
|
|
38
|
-
*
|
|
39
|
-
* const A = setComponentTemplate(
|
|
40
|
-
* precompileTemplate(`A`, {...}),
|
|
41
|
-
* templateOnlyComponent('this-module.js', 'A')
|
|
42
|
-
* );
|
|
43
|
-
* const B = setComponentTemplate(
|
|
44
|
-
* precompileTemplate(`B`, {...}),
|
|
45
|
-
* templateOnlyComponent('this-module.js', 'B')
|
|
46
|
-
* );
|
|
47
|
-
* class C {}
|
|
48
|
-
* setComponentTemplate(precompileTemplate(`C`, {...}), C);
|
|
49
|
-
*
|
|
50
|
-
* export default setComponentTemplate(
|
|
51
|
-
* precompileTemplate(`default`, {...}),
|
|
52
|
-
* templateOnlyComponent('this-module.js', '_thisModule')
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* class D {}
|
|
56
|
-
* setComponentTemplate(precompileTemplate(`D`, {...}), D);
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export default function (babel: any) {
|
|
60
|
-
let t = babel.types;
|
|
61
|
-
|
|
62
|
-
let visitor: any = {
|
|
63
|
-
Program: {
|
|
64
|
-
enter(path: NodePath<Program>, state: any) {
|
|
65
|
-
state.importUtil = new ImportUtil(t, path);
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
// Process class bodies before things like class properties get transformed
|
|
70
|
-
// into imperative constructor code that we can't recognize. Taken directly
|
|
71
|
-
// from babel-plugin-htmlbars-inline-precompile https://git.io/JMi1G
|
|
72
|
-
Class(path: NodePath<Class>, state: any) {
|
|
73
|
-
let bodyPath = path.get('body.body');
|
|
74
|
-
|
|
75
|
-
if (!Array.isArray(bodyPath)) return;
|
|
76
|
-
|
|
77
|
-
bodyPath.forEach((path) => {
|
|
78
|
-
if (path.type !== 'ClassProperty') return;
|
|
79
|
-
|
|
80
|
-
let keyPath = path.get('key');
|
|
81
|
-
let valuePath = path.get('value');
|
|
82
|
-
|
|
83
|
-
if (Array.isArray(keyPath)) return;
|
|
84
|
-
|
|
85
|
-
if (keyPath && visitor[keyPath.type]) {
|
|
86
|
-
visitor[keyPath.type](keyPath, state);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (Array.isArray(valuePath)) return;
|
|
90
|
-
|
|
91
|
-
if (valuePath && visitor[valuePath.type]) {
|
|
92
|
-
visitor[valuePath.type](valuePath, state);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
CallExpression(path: NodePath<CallExpression>, state: any) {
|
|
98
|
-
if (util.isTemplateTag(path)) {
|
|
99
|
-
transformTemplateTag(t, path, state);
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
return { visitor };
|
|
105
|
-
}
|
package/src/browser/eti/debug.ts
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import { expect } from './debug.ts';
|
|
2
|
-
import { TEMPLATE_TAG_NAME } from './util.ts';
|
|
3
|
-
|
|
4
|
-
export type TemplateMatch = TemplateTagMatch;
|
|
5
|
-
|
|
6
|
-
export interface TemplateTagMatch {
|
|
7
|
-
type: 'template-tag';
|
|
8
|
-
tagName: string;
|
|
9
|
-
start: RegExpMatchArray;
|
|
10
|
-
end: RegExpMatchArray;
|
|
11
|
-
contents: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Represents a static import of a template literal.
|
|
16
|
-
*/
|
|
17
|
-
export interface StaticImportConfig {
|
|
18
|
-
/**
|
|
19
|
-
* The path to the package from which we want to import the template literal
|
|
20
|
-
* (e.g.: 'ember-cli-htmlbars')
|
|
21
|
-
*/
|
|
22
|
-
importPath: string;
|
|
23
|
-
/**
|
|
24
|
-
* The name of the template literal (e.g.: 'hbs') or 'default' if this package
|
|
25
|
-
* exports a default function
|
|
26
|
-
*/
|
|
27
|
-
importIdentifier: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The input options to instruct parseTemplates on how to parse the input.
|
|
32
|
-
*
|
|
33
|
-
* @param templateTag
|
|
34
|
-
*/
|
|
35
|
-
export interface ParseTemplatesOptions {
|
|
36
|
-
/** Tag to use, if parsing template tags is enabled. */
|
|
37
|
-
templateTag?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const escapeChar = '\\';
|
|
41
|
-
const stringDelimiter = /['"]/;
|
|
42
|
-
|
|
43
|
-
const singleLineCommentStart = /\/\//;
|
|
44
|
-
const newLine = /\n/;
|
|
45
|
-
const multiLineCommentStart = /\/\*/;
|
|
46
|
-
const multiLineCommentEnd = /\*\//;
|
|
47
|
-
|
|
48
|
-
const templateLiteralStart = /([$a-zA-Z_][0-9a-zA-Z_$]*)?`/;
|
|
49
|
-
const templateLiteralEnd = /`/;
|
|
50
|
-
|
|
51
|
-
const dynamicSegmentStart = /\${/;
|
|
52
|
-
const blockStart = /{/;
|
|
53
|
-
const dynamicSegmentEnd = /}/;
|
|
54
|
-
|
|
55
|
-
function isEscaped(template: string, _offset: number | undefined) {
|
|
56
|
-
let offset = expect(_offset, 'Expected an index to check escaping');
|
|
57
|
-
|
|
58
|
-
let count = 0;
|
|
59
|
-
|
|
60
|
-
while (template[offset - 1] === escapeChar) {
|
|
61
|
-
count++;
|
|
62
|
-
offset--;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return count % 2 === 1;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export const DEFAULT_PARSE_TEMPLATES_OPTIONS = {
|
|
69
|
-
templateTag: TEMPLATE_TAG_NAME,
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Parses a template to find all possible valid matches for an embedded template.
|
|
74
|
-
* Supported syntaxes are template literals:
|
|
75
|
-
*
|
|
76
|
-
* hbs`Hello, world!`
|
|
77
|
-
*
|
|
78
|
-
* And template tags
|
|
79
|
-
*
|
|
80
|
-
* <template></template>
|
|
81
|
-
*
|
|
82
|
-
* The parser excludes any values found within strings recursively, and also
|
|
83
|
-
* excludes any string literals with dynamic segments (e.g `${}`) since these
|
|
84
|
-
* cannot be valid templates.
|
|
85
|
-
*
|
|
86
|
-
* @param template The template to parse
|
|
87
|
-
* @param relativePath Relative file path for the template (for errors)
|
|
88
|
-
* @param options optional configuration options for how to parse templates
|
|
89
|
-
* @returns
|
|
90
|
-
*/
|
|
91
|
-
export function parseTemplates(
|
|
92
|
-
template: string,
|
|
93
|
-
relativePath: string,
|
|
94
|
-
options: ParseTemplatesOptions = DEFAULT_PARSE_TEMPLATES_OPTIONS
|
|
95
|
-
): TemplateMatch[] {
|
|
96
|
-
const results: TemplateMatch[] = [];
|
|
97
|
-
const templateTag = options?.templateTag;
|
|
98
|
-
|
|
99
|
-
const templateTagStart = new RegExp(`<${templateTag}[^<]*>`);
|
|
100
|
-
const templateTagEnd = new RegExp(`</${templateTag}>`);
|
|
101
|
-
const argumentsMatchRegex = new RegExp(`<${templateTag}[^<]*\\S[^<]*>`);
|
|
102
|
-
|
|
103
|
-
const allTokens = new RegExp(
|
|
104
|
-
[
|
|
105
|
-
singleLineCommentStart.source,
|
|
106
|
-
newLine.source,
|
|
107
|
-
multiLineCommentStart.source,
|
|
108
|
-
multiLineCommentEnd.source,
|
|
109
|
-
stringDelimiter.source,
|
|
110
|
-
templateLiteralStart.source,
|
|
111
|
-
templateLiteralEnd.source,
|
|
112
|
-
dynamicSegmentStart.source,
|
|
113
|
-
dynamicSegmentEnd.source,
|
|
114
|
-
blockStart.source,
|
|
115
|
-
templateTagStart.source,
|
|
116
|
-
templateTagEnd.source,
|
|
117
|
-
].join('|'),
|
|
118
|
-
'g'
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
const tokens = Array.from(template.matchAll(allTokens));
|
|
122
|
-
|
|
123
|
-
while (tokens.length > 0) {
|
|
124
|
-
const currentToken = tokens.shift()!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
|
125
|
-
|
|
126
|
-
parseToken(results, template, currentToken, tokens, true);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Parse the current token. If top level, then template tags can be parsed.
|
|
131
|
-
* Else, we are nested within a dynamic segment, which is currently unsupported.
|
|
132
|
-
*/
|
|
133
|
-
function parseToken(
|
|
134
|
-
results: TemplateMatch[],
|
|
135
|
-
template: string,
|
|
136
|
-
token: RegExpMatchArray,
|
|
137
|
-
tokens: RegExpMatchArray[],
|
|
138
|
-
isTopLevel = false
|
|
139
|
-
) {
|
|
140
|
-
if (token[0].match(multiLineCommentStart)) {
|
|
141
|
-
parseMultiLineComment(results, template, token, tokens);
|
|
142
|
-
} else if (token[0].match(singleLineCommentStart)) {
|
|
143
|
-
parseSingleLineComment(results, template, token, tokens);
|
|
144
|
-
} else if (token[0].match(templateLiteralStart)) {
|
|
145
|
-
parseTemplateLiteral(template, tokens);
|
|
146
|
-
} else if (
|
|
147
|
-
isTopLevel &&
|
|
148
|
-
templateTag !== undefined &&
|
|
149
|
-
templateTagStart &&
|
|
150
|
-
token[0].match(templateTagStart)
|
|
151
|
-
) {
|
|
152
|
-
parseTemplateTag(results, template, token, tokens, templateTag);
|
|
153
|
-
} else if (token[0].match(stringDelimiter)) {
|
|
154
|
-
parseString(results, template, token, tokens);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Parse a template literal. If a dynamic segment is found, enters the dynamic
|
|
160
|
-
* segment and parses it recursively. If no dynamic segments are found and the
|
|
161
|
-
* literal is top level (e.g. not nested within a dynamic segment) and has a
|
|
162
|
-
* tag, pushes it into the list of results.
|
|
163
|
-
*/
|
|
164
|
-
function parseTemplateLiteral(template: string, tokens: RegExpMatchArray[]) {
|
|
165
|
-
while (tokens.length > 0) {
|
|
166
|
-
let currentToken = expect(tokens.shift(), 'expected token');
|
|
167
|
-
|
|
168
|
-
if (isEscaped(template, currentToken.index)) continue;
|
|
169
|
-
|
|
170
|
-
if (currentToken[0].match(templateLiteralEnd)) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Parse a string. All tokens within a string are ignored
|
|
178
|
-
* since there are no dynamic segments within these.
|
|
179
|
-
*/
|
|
180
|
-
function parseString(
|
|
181
|
-
_results: TemplateMatch[],
|
|
182
|
-
template: string,
|
|
183
|
-
startToken: RegExpMatchArray,
|
|
184
|
-
tokens: RegExpMatchArray[]
|
|
185
|
-
) {
|
|
186
|
-
while (tokens.length > 0) {
|
|
187
|
-
const currentToken = expect(tokens.shift(), 'expected token');
|
|
188
|
-
|
|
189
|
-
if (currentToken[0] === startToken[0] && !isEscaped(template, currentToken.index)) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Parse a single-line comment. All tokens within a single-line comment are ignored
|
|
197
|
-
* since there are no dynamic segments within them.
|
|
198
|
-
*/
|
|
199
|
-
function parseSingleLineComment(
|
|
200
|
-
_results: TemplateMatch[],
|
|
201
|
-
_template: string,
|
|
202
|
-
_startToken: RegExpMatchArray,
|
|
203
|
-
tokens: RegExpMatchArray[]
|
|
204
|
-
) {
|
|
205
|
-
while (tokens.length > 0) {
|
|
206
|
-
const currentToken = expect(tokens.shift(), 'expected token');
|
|
207
|
-
|
|
208
|
-
if (currentToken[0] === '\n') {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Parse a multi-line comment. All tokens within a multi-line comment are ignored
|
|
216
|
-
* since there are no dynamic segments within them.
|
|
217
|
-
*/
|
|
218
|
-
function parseMultiLineComment(
|
|
219
|
-
_results: TemplateMatch[],
|
|
220
|
-
_template: string,
|
|
221
|
-
_startToken: RegExpMatchArray,
|
|
222
|
-
tokens: RegExpMatchArray[]
|
|
223
|
-
) {
|
|
224
|
-
while (tokens.length > 0) {
|
|
225
|
-
const currentToken = expect(tokens.shift(), 'expected token');
|
|
226
|
-
|
|
227
|
-
if (currentToken[0] === '*/') {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Parses a template tag. Continues parsing until the template tag has closed,
|
|
235
|
-
* accounting for nested template tags.
|
|
236
|
-
*/
|
|
237
|
-
function parseTemplateTag(
|
|
238
|
-
results: TemplateMatch[],
|
|
239
|
-
_template: string,
|
|
240
|
-
startToken: RegExpMatchArray,
|
|
241
|
-
tokens: RegExpMatchArray[],
|
|
242
|
-
templateTag: string
|
|
243
|
-
) {
|
|
244
|
-
let stack = 1;
|
|
245
|
-
|
|
246
|
-
if (argumentsMatchRegex && startToken[0].match(argumentsMatchRegex)) {
|
|
247
|
-
throw new Error(
|
|
248
|
-
`embedded template preprocessing currently does not support passing arguments, found args in: ${relativePath}`
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
while (tokens.length > 0) {
|
|
253
|
-
const currentToken = expect(tokens.shift(), 'expected token');
|
|
254
|
-
|
|
255
|
-
if (currentToken[0].match(templateTagStart)) {
|
|
256
|
-
stack++;
|
|
257
|
-
} else if (currentToken[0].match(templateTagEnd)) {
|
|
258
|
-
stack--;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (stack === 0) {
|
|
262
|
-
let contents = '';
|
|
263
|
-
|
|
264
|
-
if (startToken.index !== undefined) {
|
|
265
|
-
const templateStart = startToken.index + startToken[0].length;
|
|
266
|
-
|
|
267
|
-
contents = template.slice(templateStart, currentToken.index);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
results.push({
|
|
271
|
-
type: 'template-tag',
|
|
272
|
-
tagName: templateTag,
|
|
273
|
-
contents: contents,
|
|
274
|
-
start: startToken,
|
|
275
|
-
end: currentToken,
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
return results;
|
|
284
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import { getTemplateLocals } from '@glimmer/syntax';
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
import lineColumn from 'line-column';
|
|
8
|
-
import MagicString from 'magic-string';
|
|
9
|
-
|
|
10
|
-
import { expect } from './debug.ts';
|
|
11
|
-
import { parseTemplates } from './parse-templates.ts';
|
|
12
|
-
|
|
13
|
-
import type { ParseTemplatesOptions, TemplateMatch } from './parse-templates.ts';
|
|
14
|
-
|
|
15
|
-
interface PreprocessOptionsEager {
|
|
16
|
-
importIdentifier?: string;
|
|
17
|
-
importPath?: string;
|
|
18
|
-
templateTag?: string;
|
|
19
|
-
templateTagReplacement?: string;
|
|
20
|
-
|
|
21
|
-
relativePath: string;
|
|
22
|
-
includeSourceMaps: boolean;
|
|
23
|
-
includeTemplateTokens: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface PreprocessOptionsLazy {
|
|
27
|
-
importIdentifier?: string;
|
|
28
|
-
importPath?: string;
|
|
29
|
-
templateTag?: string;
|
|
30
|
-
templateTagReplacement?: string;
|
|
31
|
-
|
|
32
|
-
relativePath: string;
|
|
33
|
-
includeSourceMaps: boolean;
|
|
34
|
-
includeTemplateTokens: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type PreprocessOptions = PreprocessOptionsLazy | PreprocessOptionsEager;
|
|
38
|
-
|
|
39
|
-
interface PreprocessedOutput {
|
|
40
|
-
output: string;
|
|
41
|
-
replacements: Replacement[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface Replacement {
|
|
45
|
-
type: 'start' | 'end';
|
|
46
|
-
index: number;
|
|
47
|
-
oldLength: number;
|
|
48
|
-
newLength: number;
|
|
49
|
-
originalLine: number;
|
|
50
|
-
originalCol: number;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function getMatchStartAndEnd(match: RegExpMatchArray) {
|
|
54
|
-
return {
|
|
55
|
-
start: expect(match.index, 'Expected regular expression match to have an index'),
|
|
56
|
-
end:
|
|
57
|
-
expect(match.index, 'Expected regular expression match to have an index') + match[0].length,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function replacementFrom(
|
|
62
|
-
template: string,
|
|
63
|
-
index: number,
|
|
64
|
-
oldLength: number,
|
|
65
|
-
newLength: number,
|
|
66
|
-
type: 'start' | 'end'
|
|
67
|
-
): Replacement {
|
|
68
|
-
const loc = expect(
|
|
69
|
-
lineColumn(template).fromIndex(index),
|
|
70
|
-
'BUG: expected to find a line/column based on index'
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
type,
|
|
75
|
-
index,
|
|
76
|
-
oldLength,
|
|
77
|
-
newLength,
|
|
78
|
-
originalCol: loc.col,
|
|
79
|
-
originalLine: loc.line,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function replaceMatch(
|
|
84
|
-
s: MagicString,
|
|
85
|
-
match: TemplateMatch,
|
|
86
|
-
startReplacement: string,
|
|
87
|
-
endReplacement: string,
|
|
88
|
-
template: string,
|
|
89
|
-
includeTemplateTokens: boolean
|
|
90
|
-
): Replacement[] {
|
|
91
|
-
const { start: openStart, end: openEnd } = getMatchStartAndEnd(match.start);
|
|
92
|
-
const { start: closeStart, end: closeEnd } = getMatchStartAndEnd(match.end);
|
|
93
|
-
|
|
94
|
-
let options = '';
|
|
95
|
-
|
|
96
|
-
if (includeTemplateTokens) {
|
|
97
|
-
const tokensString = getTemplateLocals(template.slice(openEnd, closeStart))
|
|
98
|
-
.filter((local: string) => local.match(/^[$A-Z_][0-9A-Z_$]*$/i))
|
|
99
|
-
.join(',');
|
|
100
|
-
|
|
101
|
-
if (tokensString.length > 0) {
|
|
102
|
-
options = `, scope: () => ({${tokensString}})`;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const newStart = `${startReplacement}\``;
|
|
107
|
-
const newEnd = `\`, { strictMode: true${options} }${endReplacement}`;
|
|
108
|
-
|
|
109
|
-
s.overwrite(openStart, openEnd, newStart);
|
|
110
|
-
s.overwrite(closeStart, closeEnd, newEnd);
|
|
111
|
-
ensureBackticksEscaped(s, openEnd + 1, closeStart - 1);
|
|
112
|
-
|
|
113
|
-
return [
|
|
114
|
-
replacementFrom(template, openStart, openEnd - openStart, newStart.length, 'start'),
|
|
115
|
-
replacementFrom(template, closeStart, closeEnd - closeStart, newEnd.length, 'end'),
|
|
116
|
-
];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Preprocesses all embedded templates within a JavaScript or TypeScript file.
|
|
121
|
-
* This function replaces all embedded templates that match our template syntax
|
|
122
|
-
* with valid, parseable JS. Optionally, it can also include a source map, and
|
|
123
|
-
* it can also include all possible values used within the template.
|
|
124
|
-
*
|
|
125
|
-
* Input:
|
|
126
|
-
*
|
|
127
|
-
* <template><MyComponent/><template>
|
|
128
|
-
*
|
|
129
|
-
* Output:
|
|
130
|
-
*
|
|
131
|
-
* [GLIMMER_TEMPLATE(`<MyComponent/>`, { scope() { return {MyComponent}; } })];
|
|
132
|
-
*
|
|
133
|
-
* It can also be used with template literals to provide the in scope values:
|
|
134
|
-
*
|
|
135
|
-
* Input:
|
|
136
|
-
*
|
|
137
|
-
* hbs`<MyComponent/>`;
|
|
138
|
-
*
|
|
139
|
-
* Output
|
|
140
|
-
*
|
|
141
|
-
* hbs(`<MyComponent/>`, { scope() { return {MyComponent}; } });
|
|
142
|
-
*/
|
|
143
|
-
export function preprocessEmbeddedTemplates(
|
|
144
|
-
template: string,
|
|
145
|
-
options: PreprocessOptions
|
|
146
|
-
): PreprocessedOutput {
|
|
147
|
-
const { templateTag, templateTagReplacement, includeTemplateTokens, relativePath } = options;
|
|
148
|
-
|
|
149
|
-
const parseTemplatesOptions: ParseTemplatesOptions = {
|
|
150
|
-
templateTag,
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const matches = parseTemplates(template, relativePath, parseTemplatesOptions);
|
|
154
|
-
const replacements: Replacement[] = [];
|
|
155
|
-
const s = new MagicString(template);
|
|
156
|
-
|
|
157
|
-
for (const match of matches) {
|
|
158
|
-
if (match.type === 'template-tag') {
|
|
159
|
-
replacements.push(
|
|
160
|
-
...replaceMatch(
|
|
161
|
-
s,
|
|
162
|
-
match,
|
|
163
|
-
`[${templateTagReplacement}(`,
|
|
164
|
-
')]',
|
|
165
|
-
template,
|
|
166
|
-
includeTemplateTokens
|
|
167
|
-
)
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let output = s.toString();
|
|
173
|
-
|
|
174
|
-
return {
|
|
175
|
-
output,
|
|
176
|
-
replacements,
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function ensureBackticksEscaped(s: MagicString, start: number, end: number) {
|
|
181
|
-
if (start >= end) return;
|
|
182
|
-
|
|
183
|
-
let content = s.slice(start, end);
|
|
184
|
-
|
|
185
|
-
content = content.replace(/(?<!\\)`/g, '\\`');
|
|
186
|
-
s.overwrite(start, end, content, false);
|
|
187
|
-
}
|