-
Notifications
You must be signed in to change notification settings - Fork 386
/
transformer.ts
executable file
·137 lines (125 loc) · 4.14 KB
/
transformer.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import * as path from 'path';
import { isString } from '@lwc/shared';
import {
TransformerErrors,
generateCompilerError,
invariant,
CompilerDiagnostic,
} from '@lwc/errors';
import { NormalizedTransformOptions, TransformOptions, validateTransformOptions } from '../options';
import styleTransform from './style';
import templateTransformer from './template';
import scriptTransformer from './javascript';
/** The object returned after transforming code. */
export interface TransformResult {
/** The compiled source code. */
code: string;
/** The generated source map. */
map: unknown;
/** Any diagnostic warnings that may have occurred. */
warnings?: CompilerDiagnostic[];
/**
* String tokens used for style scoping in synthetic shadow DOM and `*.scoped.css`, as either
* attributes or classes.
*/
cssScopeTokens?: string[];
}
/**
* Transform the passed source code.
* @param src The source to be transformed. Can be the content of a JavaScript, HTML, or CSS file.
* @param filename The source filename, with extension.
* @param options The transformation options. The `name` and the `namespace` of the component is the
* minimum required for transformation.
* @returns A promise resolving to an object with the generated code, source map and gathered metadata.
* @example
* const source = `
* import { LightningElement } from 'lwc';
* export default class App extends LightningElement {}
* `;
* const filename = 'app.js';
* const options = {
* namespace: 'c',
* name: 'app',
* };
* const { code } = await transform(source, filename, options);
* @deprecated Use {@linkcode transformSync} instead
*/
export function transform(
src: string,
filename: string,
options: TransformOptions
): Promise<TransformResult> {
validateArguments(src, filename);
return new Promise((resolve, reject) => {
try {
const res = transformSync(src, filename, options);
resolve(res);
} catch (error) {
reject(error);
}
});
}
/**
* Transform the passed source code
* @param src The source to be transformed. Can be the content of a JavaScript, HTML, or CSS file.
* @param filename The source filename, with extension.
* @param options The transformation options. The `name` and the `namespace` of the component is the
* minimum required for transformation.
* @returns An object with the generated code, source map and gathered metadata.
* @example
*
* const source = `
* import { LightningElement } from 'lwc';
* export default class App extends LightningElement {}
* `;
* const filename = 'app.js';
* const options = {
* namespace: 'c',
* name: 'app',
* };
* const { code } = transformSync(source, filename, options);
*/
export function transformSync(
src: string,
filename: string,
options: TransformOptions
): TransformResult {
validateArguments(src, filename);
const normalizedOptions = validateTransformOptions(options);
return transformFile(src, filename, normalizedOptions);
}
function validateArguments(src: string, filename: string) {
invariant(isString(src), TransformerErrors.INVALID_SOURCE, [src]);
invariant(isString(filename), TransformerErrors.INVALID_ID, [filename]);
}
function transformFile(
src: string,
filename: string,
options: NormalizedTransformOptions
): TransformResult {
let transformer;
switch (path.extname(filename)) {
case '.html':
transformer = templateTransformer;
break;
case '.css':
transformer = styleTransform;
break;
case '.ts':
case '.js':
transformer = scriptTransformer;
break;
default:
throw generateCompilerError(TransformerErrors.NO_AVAILABLE_TRANSFORMER, {
messageArgs: [filename],
origin: { filename },
});
}
return transformer(src, filename, options);
}