-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
85 lines (74 loc) · 2.49 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
import {createFilter} from '@rollup/pluginutils';
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.
* @default: null
*/
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 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: null,
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, {});
// export default const css;
let defaultExportName;
// export default '...';
let isDeclarationLiteral = false;
const magicString = transformAst(code, {ast: ast},
node => {
if (node.type === 'ExportDefaultDeclaration') {
defaultExportName = node.declaration.name;
isDeclarationLiteral = node.declaration.type === 'Literal';
}
},
);
if (!defaultExportName && !isDeclarationLiteral) {
return;
}
magicString.walk(node => {
if (defaultExportName && node.type === 'VariableDeclaration') {
const exportedVar = node.declarations.find(d => d.id.name === defaultExportName);
if (exportedVar) {
exportedVar.init.edit.update(`cssTag\`${escape(exportedVar.init.value)}\``);
}
}
if (isDeclarationLiteral && node.type === 'ExportDefaultDeclaration') {
node.declaration.edit.update(`cssTag\`${escape(node.declaration.value)}\``)
}
});
magicString.prepend(`import {css as cssTag} from '${opts.importPackage}';\n`);
return {
code: magicString.toString(),
map: magicString.generateMap({
hires: true,
}),
};
},
};
};