-
Notifications
You must be signed in to change notification settings - Fork 386
/
styles.ts
30 lines (28 loc) · 1.06 KB
/
styles.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
/*
* Copyright (c) 2018, salesforce.com, 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 babylon from '@babel/parser';
import * as styleCompiler from '@lwc/style-compiler';
import { normalizeToCompilerError, TransformerErrors } from '@lwc/errors';
import { Config as StylesheetConfig } from '@lwc/style-compiler/dist/types/index';
import { Statement } from '@babel/types';
export default function parseInlineStyles(
src: string,
stylesheetConfig: StylesheetConfig
): Statement[] {
let result;
try {
result = styleCompiler.transform(src, 'template_inline_styles', stylesheetConfig);
} catch (e) {
throw normalizeToCompilerError(TransformerErrors.CSS_IN_HTML_ERROR, e);
}
// The style compiler produces a module string
const { code } = result;
// Convert it to an AST
const parsed = babylon.parse(code, { sourceType: 'module' });
// Return the body of the module
return parsed.program.body;
}