-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.js
More file actions
102 lines (93 loc) · 2.69 KB
/
build.js
File metadata and controls
102 lines (93 loc) · 2.69 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
// this whole thing is a really fragile hack
// but it works so ¯\_(ツ)_/¯
const stylisPath = require.resolve('stylis')
const { promisify } = require('util')
const j = require('jscodeshift')
const request = require('request-promise-native')
const fs = require('fs')
const babylon = require('babylon')
const prettier = require('prettier')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const removeOptions = src =>
j(src)
.find(j.SwitchStatement)
.forEach(thing => {
thing.value.cases = thing.value.cases.filter(node => {
if (!node.test) return true
switch (node.test.value) {
case 'keyframe':
case 'cascade':
case 'preserve':
case 'semicolon':
case 'compress':
case 'global': {
return false
}
default: {
return true
}
}
})
})
.toSource()
const setOptions = src =>
j(src)
.find(j.VariableDeclarator)
.forEach(path => {
switch (path.value.id.name) {
case 'escape':
case 'keyed': {
path.value.init.value = 0
return
}
case 'semicolon': {
path.value.init.value = 1
}
}
})
.toSource()
const removeUMDWrapper = src =>
j(src)
.find(j.Program)
.forEach(thing => {
delete thing.value.body[1]
})
.toSource()
async function doThing() {
const stylisSrc = (await readFile(stylisPath))
.toString()
.replace(
'cascade + quote + bracket + atrule === 0 && id !== KEYFRAME && code !== SEMICOLON',
'true === false'
)
.replace(
'comment + quote + parentheses + bracket + semicolon === 0',
'true === false'
)
.replace('(insert === 1)', '(true === false)')
.replace('input.charCodeAt(9)*keyed', '0')
.replace('switch (cascade + level) {', 'switch (2) {')
.replace('compress*code === 0', 'true')
.replace(`typeof(output = result) !== 'string'`, 'output = result')
const result = setOptions(removeOptions(stylisSrc))
// await writeFile('./src/stylis.js', result)
console.log('start request')
const data = (await request('https://closure-compiler.appspot.com/compile', {
method: 'POST',
form: {
js_code: result,
compilation_level: 'ADVANCED_OPTIMIZATIONS',
output_format: 'text',
output_info: 'compiled_code'
}
})).toString()
const srcWithoutUMDWrapper = removeUMDWrapper(data)
const finalSrc =
srcWithoutUMDWrapper +
'\nexport default ' +
babylon.parse(srcWithoutUMDWrapper).program.body[0].declarations[0].id.name
await writeFile('./src/stylis.min.js', prettier.format(finalSrc))
console.log('done')
}
doThing()