-
Notifications
You must be signed in to change notification settings - Fork 382
/
style.ts
executable file
·54 lines (49 loc) · 1.93 KB
/
style.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
/*
* 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 styleCompiler from '@lwc/style-compiler';
import { normalizeToCompilerError, TransformerErrors } from '@lwc/errors';
import { NormalizedTransformOptions } from '../options';
import { TransformResult } from './transformer';
/**
* 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 config 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.
* @throws Compilation errors
*/
export default function styleTransform(
src: string,
filename: string,
config: NormalizedTransformOptions
): TransformResult {
const { customProperties } = config.stylesheetConfig;
const styleCompilerConfig = {
customProperties: {
resolverModule:
customProperties.resolution.type === 'module'
? customProperties.resolution.name
: undefined,
},
scoped: config.scopedStyles,
disableSyntheticShadowSupport: config.disableSyntheticShadowSupport,
apiVersion: config.apiVersion,
};
let res;
try {
res = styleCompiler.transform(src, filename, styleCompilerConfig);
} catch (e) {
throw normalizeToCompilerError(TransformerErrors.CSS_TRANSFORMER_ERROR, e, { filename });
}
// Rollup only cares about the mappings property on the map. Since producing a source map for
// the styles doesn't make sense, the transform returns an empty mappings.
return {
code: res.code,
map: { mappings: '' },
};
}