forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.js
More file actions
176 lines (150 loc) · 4.59 KB
/
playground.js
File metadata and controls
176 lines (150 loc) · 4.59 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import * as localWasm from '../node/pkg';
let wasm;
let enc = new TextEncoder();
let dec = new TextDecoder();
let inputs = document.querySelectorAll('input[type=number]');
async function loadVersions() {
const {versions} = await fetch('https://data.jsdelivr.com/v1/package/npm/@parcel/css-wasm').then(r => r.json());
versions
.map(v => {
const option = document.createElement('option');
option.value = v;
option.textContent = v;
return option;
})
.forEach(o => {
version.appendChild(o);
})
}
async function loadWasm() {
if (version.value === 'local') {
wasm = localWasm;
} else {
wasm = await new Function('version', 'return import(`https://cdn.jsdelivr.net/npm/@parcel/css-wasm@${version}/parcel_css_node.js`)')(version.value);
}
await wasm.default();
}
function loadPlaygroundState() {
const hash = window.location.hash.slice(1);
try {
const playgroundState = JSON.parse(decodeURIComponent(hash));
reflectPlaygroundState(playgroundState);
} catch {
const initialPlaygroundState = {
minify: minify.checked,
nesting: nesting.checked,
targets: getTargets(),
source: source.value,
version: version.value,
};
reflectPlaygroundState(initialPlaygroundState);
}
}
function reflectPlaygroundState(playgroundState) {
if (typeof playgroundState.minify !== 'undefined') {
minify.checked = playgroundState.minify;
}
if (typeof playgroundState.cssModules !== 'undefined') {
cssModules.checked = playgroundState.cssModules;
compiledModules.hidden = !playgroundState.cssModules;
}
if (typeof playgroundState.analyzeDependencies !== 'undefined') {
analyzeDependencies.checked = playgroundState.analyzeDependencies;
compiledDependencies.hidden = !playgroundState.analyzeDependencies;
}
if (typeof playgroundState.nesting !== 'undefined') {
nesting.checked = playgroundState.nesting;
}
if (typeof playgroundState.customMedia !== 'undefined') {
customMedia.checked = playgroundState.customMedia;
}
if (playgroundState.targets) {
const {targets} = playgroundState;
for (let input of inputs) {
let value = targets[input.id];
input.value = value == null ? '' : value >> 16;
}
}
if (playgroundState.source) {
source.value = playgroundState.source;
}
if (playgroundState.unusedSymbols) {
unusedSymbols.value = playgroundState.unusedSymbols.join('\n');
}
if (playgroundState.version) {
version.value = playgroundState.version;
}
}
function savePlaygroundState() {
const playgroundState = {
minify: minify.checked,
nesting: nesting.checked,
customMedia: customMedia.checked,
cssModules: cssModules.checked,
analyzeDependencies: analyzeDependencies.checked,
targets: getTargets(),
source: source.value,
unusedSymbols: unusedSymbols.value.split('\n').map(v => v.trim()).filter(Boolean),
version: version.value,
};
const hash = encodeURIComponent(JSON.stringify(playgroundState));
if (
typeof URL === 'function' &&
typeof history === 'object' &&
typeof history.replaceState === 'function'
) {
const url = new URL(location.href);
url.hash = hash;
history.replaceState(null, null, url);
} else {
location.hash = hash;
}
}
function getTargets() {
let targets = {};
for (let input of inputs) {
if (input.value !== '') {
targets[input.id] = input.valueAsNumber << 16;
}
}
return targets;
}
function update() {
const {transform} = wasm;
const targets = getTargets();
let res = transform({
filename: 'test.css',
code: enc.encode(source.value),
minify: minify.checked,
targets: Object.keys(targets).length === 0 ? null : targets,
drafts: {
nesting: nesting.checked,
customMedia: customMedia.checked
},
cssModules: cssModules.checked,
analyzeDependencies: analyzeDependencies.checked,
unusedSymbols: unusedSymbols.value.split('\n').map(v => v.trim()).filter(Boolean)
});
compiled.value = dec.decode(res.code);
compiledModules.value = JSON.stringify(res.exports, false, 2);
compiledModules.hidden = !cssModules.checked;
compiledDependencies.value = JSON.stringify(res.dependencies, false, 2);
compiledDependencies.hidden = !analyzeDependencies.checked;
savePlaygroundState();
}
async function main() {
await loadVersions();
loadPlaygroundState();
await loadWasm();
update();
source.oninput = update;
unusedSymbols.oninput = update;
for (let input of document.querySelectorAll('input')) {
input.oninput = update;
}
version.onchange = async () => {
await loadWasm();
update();
};
}
main();