-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathtransform.ts
More file actions
97 lines (88 loc) · 3.08 KB
/
transform.ts
File metadata and controls
97 lines (88 loc) · 3.08 KB
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
/*
* 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';
import { StyleCompilerCtx } from './utils/error-recovery';
/** 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;
/** When set to true, enables error recovery mode to collect multiple errors */
experimentalErrorRecoveryMode?: boolean;
}
/**
* 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
* import { transform } from '@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; errors?: Error[] } {
if (src === '') {
return { code: 'export default undefined' };
}
const scoped = !!config.scoped;
const apiVersion = getAPIVersionFromNumber(config.apiVersion);
const disableSyntheticShadowSupport = !!config.disableSyntheticShadowSupport;
const errorRecoveryMode = !!config.experimentalErrorRecoveryMode;
// Create error recovery context
const ctx = new StyleCompilerCtx(errorRecoveryMode, id);
const plugins = [
postcssLwc({
scoped,
apiVersion,
disableSyntheticShadowSupport,
ctx,
}),
];
// Wrap PostCSS processing with error recovery for parsing errors
let result;
try {
result = postcss(plugins).process(src, { from: id }).sync();
} catch (error) {
if (errorRecoveryMode && error instanceof postcss.CssSyntaxError) {
ctx.errors.push(error);
// eslint-disable-next-line preserve-caught-error
throw AggregateError(ctx.errors);
} else {
throw error;
}
}
if (errorRecoveryMode && ctx.hasErrors()) {
throw AggregateError(ctx.errors);
}
return { code: serialize(result, config) };
}