-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
135 lines (120 loc) · 3.89 KB
/
index.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
import { createFilter } from '@rollup/pluginutils';
import type {
ArrowFunctionExpression,
Identifier,
Literal,
Node,
TemplateLiteral,
} from 'estree';
import * as transformAst from 'transform-ast';
import { PluginOption } from 'vite';
interface PostcssLitOptions {
/**
* A glob (or array of globs) of files to include
*
* @default: '**/*.{css,sss,pcss,styl,stylus,sass,scss,less}?(*)'
*/
include?: string | string[];
/**
* A glob (or array of globs) of files to exclude
*
* The default filter is used to prevent `<style>` HTML tags from being processed in Vite contexts
* @default '**/*\?direct*'
*/
exclude?: string | string[];
/**
* A string denoting the name of the package from which to import the `css`
* template tag function. For lit-element this can be changed to 'lit-element'
*
* @default 'lit'
*/
importPackage?: string;
}
const CSS_TAG = 'cssTag';
const escape = (str: string): string =>
str.replace(/`/g, '\\`').replace(/\\(?!`)/g, '\\\\');
export = function postcssLit(options: PostcssLitOptions = {}): PluginOption {
const defaultOptions: PostcssLitOptions = {
include: '**/*.{css,sss,pcss,styl,stylus,sass,scss,less}?(*)',
exclude: '**/*?direct*',
importPackage: 'lit',
};
const opts: PostcssLitOptions = { ...defaultOptions, ...options };
const filter = createFilter(opts.include, opts.exclude);
return {
name: 'postcss-lit',
enforce: 'post',
transform(code, id) {
if (!filter(id)) return;
const ast = this.parse(code, {});
let defaultExportName: string;
let cssStringNode: Literal | TemplateLiteral;
const magicString = transformAst(code, { ast: ast });
magicString.walk((node: Node) => {
if (node.type === 'ExportDefaultDeclaration') {
switch (node.declaration.type) {
case 'Literal': // export default '...';
case 'TemplateLiteral': // export default `...`;
cssStringNode = node.declaration;
break;
case 'Identifier': // const css = '...'; export default css;
defaultExportName = node.declaration.name;
break;
case 'CallExpression': {
// export default (() => '...')();
const arrowFunctionBody = (
node.declaration.callee as ArrowFunctionExpression
).body;
if (
arrowFunctionBody.type === 'Literal' ||
arrowFunctionBody.type === 'TemplateLiteral'
) {
cssStringNode = arrowFunctionBody;
}
break;
}
default:
}
}
});
if (!cssStringNode) {
if (!defaultExportName) {
this.warn(`Unrecognized default export in file ${id}`);
return;
}
magicString.walk((node: Node) => {
if (node.type === 'VariableDeclaration') {
const exportedVar = node.declarations.find(
d =>
(d.id as Identifier)?.name === defaultExportName &&
(d.init?.type === 'Literal' ||
d.init?.type === 'TemplateLiteral'),
);
if (exportedVar) {
cssStringNode = exportedVar.init as typeof cssStringNode;
}
}
});
}
if (!cssStringNode) {
return this.error(`Unrecognized export expression in file ${id}`);
}
if (cssStringNode.type === 'Literal') {
cssStringNode.edit.update(
`${CSS_TAG}\`${escape(cssStringNode.value as string)}\``,
);
} else {
cssStringNode.edit.prepend(CSS_TAG);
}
magicString.prepend(
`import {css as ${CSS_TAG}} from '${opts.importPackage}';\n`,
);
return {
code: magicString.toString(),
map: magicString.generateMap({
hires: true,
}),
};
},
};
};