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
136 lines (114 loc) · 3.64 KB
/
playground.js
File metadata and controls
136 lines (114 loc) · 3.64 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
import init, {transform} from '../node/pkg';
let enc = new TextEncoder();
let dec = new TextDecoder();
let inputs = document.querySelectorAll('input[type=number]');
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,
};
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');
}
}
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)
};
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;
}
async function update() {
await init();
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();
}
loadPlaygroundState();
update();
source.oninput = update;
unusedSymbols.oninput = update;
for (let input of document.querySelectorAll('input')) {
input.oninput = update;
}