-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
lightningcss.ts
84 lines (72 loc) 路 2.14 KB
/
lightningcss.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
import { init, transform } from "../deps/lightningcss.ts";
import { merge } from "../core/utils.ts";
import { Page } from "../core/filesystem.ts";
import { prepareAsset, saveAsset } from "./source_maps.ts";
import type { DeepPartial, Site } from "../core.ts";
import type { TransformOptions } from "../deps/lightningcss.ts";
export interface Options {
/** The list of extensions this plugin applies to */
extensions: string[];
/** Options passed to parcel_css */
options: Omit<TransformOptions, "filename" | "code">;
}
// Default options
export const defaults: Options = {
extensions: [".css"],
options: {
minify: true,
drafts: {
nesting: true,
customMedia: true,
},
targets: {
android: version(98),
chrome: version(98),
edge: version(98),
firefox: version(97),
ios_saf: version(15),
safari: version(15),
opera: version(83),
samsung: version(16),
},
},
};
// Init parcelCSS
await init();
/** A plugin to load all CSS files and process them using parcelCSS */
export default function (userOptions?: DeepPartial<Options>) {
return (site: Site) => {
const options = merge(defaults, userOptions);
site.loadAssets(options.extensions);
site.process(options.extensions, parcelCSS);
function parcelCSS(file: Page) {
const { content, filename, sourceMap, enableSourceMap } = prepareAsset(
site,
file,
);
// Process the code with parcelCSS
const code = new TextEncoder().encode(content);
const transformOptions: TransformOptions = {
filename,
code,
sourceMap: enableSourceMap,
inputSourceMap: JSON.stringify(sourceMap),
...options.options,
};
const result = transform(transformOptions);
const decoder = new TextDecoder();
saveAsset(
site,
file,
decoder.decode(result.code),
enableSourceMap ? decoder.decode(result.map!) : undefined,
);
}
};
}
/**
* Convert a version number to a single 24-bit number
*/
export function version(major: number, minor = 0, patch = 0): number {
return (major << 16) | (minor << 8) | patch;
}