-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathtransform.ts
62 lines (55 loc) · 2.04 KB
/
transform.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
/*
* 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 postcss from 'postcss';
import { getAPIVersionFromNumber } from '@lwc/shared';
import serialize from './serialize';
import postcssLwc from './postcss-lwc-plugin';
/** Configuration options for CSS transforms. */
export interface Config {
/**
* CSS custom properties configuration
* @deprecated Custom property transforms are deprecated because IE11 and other legacy browsers are no longer supported.
*/
// TODO [#3266]: Remove StylesheetConfig as part of breaking change wishlist
customProperties?: {
/** Name of the module to resolve custom properties lookup */
resolverModule?: string;
};
/** Token that is used for scoping in light DOM scoped styles */
scoped?: boolean;
/** When set to true, synthetic shadow DOM support is removed from the output JavaScript */
disableSyntheticShadowSupport?: boolean;
/** The API version to associate with the compiled stylesheet */
apiVersion?: number;
}
/**
* Transforms CSS for use with LWC components.
* @param src Contents of the CSS source file
* @param id Filename of the CSS source file
* @param config Transformation options
* @returns Transformed CSS
* @example
* const {transform} = require('@lwc/style-compiler');
* const source = `
* :host {
* opacity: 0.4;
* }
* span {
* text-transform: uppercase;
* }`;
* const { code } = transform(source, 'example.css');
*/
export function transform(src: string, id: string, config: Config = {}): { code: string } {
if (src === '') {
return { code: 'export default undefined' };
}
const scoped = !!config.scoped;
const apiVersion = getAPIVersionFromNumber(config.apiVersion);
const plugins = [postcssLwc({ scoped, apiVersion })];
const result = postcss(plugins).process(src, { from: id }).sync();
return { code: serialize(result, config) };
}