-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
188 lines (158 loc) · 4.96 KB
/
build.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
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
177
178
179
180
181
182
183
184
185
186
187
188
import { Processor } from 'windicss/lib'
const fs = require('fs')
const path = require('path')
import { createUtils, UserOptions } from '@windicss/plugin-utils'
export function generateWindi(classes: string, wantedSelector: string) {
const P = new Processor({});
const cstm = P.compile(classes, "");
const fullResp = cstm.styleSheet.build(false);
return fullResp.replaceAll(`.${cstm.className}`, wantedSelector)
}
async function generateStyles(html: string, preflight?: boolean, asSheet?: boolean) {
let K = createUtils()
await K.ensureInit()
let out = await K.applyExtractors(html)
// Get windi processor
const processor = new Processor({
})
const htmlClasses = (out.classes as string[]).join(' ')
// Generate preflight based on the html we input
const preflightSheet = processor.preflight(html)
// Process the html classes to an interpreted style sheet
const interpretedSheet = processor.interpret(htmlClasses).styleSheet
if(asSheet === true) {
return interpretedSheet;
}
// Build styles
const APPEND = false
const MINIFY = false
let sheet = interpretedSheet;
if(preflight !== false) {
sheet = sheet.extend(preflightSheet, APPEND);
}
const styles = sheet.build(MINIFY)
//@ts-ignore
return minifyCss(styles);
}
const minifyCss = (someCss: string) : string => {
return (new (require("clean-css"))({})).minify(someCss).styles;
}
import { sync as globSync } from 'glob'
import { readFileSync, writeFileSync } from 'fs'
import { StyleSheet } from 'windicss/types/utils/style'
type ResolveArgs = {importer: string, path: string};
let preactAlias = {
name: 'preact-alias',
setup(build: any) {
let path = require('path')
let jsfiles = globSync('**/*.tsx', {
dot: false,
nodir: true,
ignore: ['.git/*', 'node_modules/**/*'],
})
build.onResolve({ filter: /^virtual:windi$/ }, (args: ResolveArgs) => {
return {
path: args.importer,
namespace: 'windi',
}
})
build.onResolve({ filter: /^base16/ }, (args: ResolveArgs) => {
return {
path: path.resolve("node_modules/base16/lib/monokai.js")
}
});
build.onLoad({filter: /vendor\.css/}, async (x: any) => {
const M = readFileSync(x.path, 'utf8');
const txt2 = await generateStyles(M, false, true) as StyleSheet;
let newCss = '';
M.replaceAll(/\/\*\s*@windi\s*(.*?)\s*\*\/\s*(.*?) {/gms, (...groups: string[]) => {
const windiCls = groups[1];
const wantedSel = groups[2];
wantedSel.split(",").map(x => {
newCss += generateWindi(windiCls, x);
});
return groups[0];
});
return { contents: minifyCss(readFileSync(x.path, 'utf8')) + newCss, loader: 'text' }
})
build.onLoad({ filter: /.*/, namespace: 'windi' }, async (args: { path: string }) => {
console.log(`Putting styles...`)
const srcjs = jsfiles.map(x => {
// console.log(`Scanning for classes in ${x}`)
return fs.readFileSync(x, 'utf8')
}).join('\n')
const css = await generateStyles(srcjs)
const f = `
const windicss = ${JSON.stringify(css)};
export default windicss;
`
return {
contents: f,
}
})
build.onResolve({ filter: /^react$/ }, (args: any) => {
let f = path.resolve('node_modules/preact/dist/preact.min.js')
return {
path: f,
}
})
},
}
const opts: Record<string, any> = {};
const watchFlag = process.argv.findIndex(x => x === '--watch') > 0;
import {spawn, spawnSync} from "child_process"
let attempt = 0;
function buildDemo(retry: boolean) {
let args: [string, object] = ["node build.js" + (retry ? " --watch" : ""), {cwd: "demos", shell: true, stdio: "inherit", }];
if(!retry) {
spawnSync(...args)
} else {
spawn(...args).on("close", () => {
let interval = 1000 * Math.pow(1.4, attempt + 1);
setTimeout(() => {
buildDemo(true);
attempt++;
attempt = attempt % 5;
}, interval);
});
}
}
if(watchFlag) {
opts['watch'] = true;
buildDemo(true)
} else {
buildDemo(false)
}
let outputDir = 'dist';
const isDebug = 'watch' in opts;
const SHOW_REPORT = false; // show stats about asset size
([/*'esm',*/ 'iife']).map(fmt => {
const outf = path.join(outputDir, 'index.' + (fmt == 'esm' ? 'mjs' : 'js'));
console.log(`Building ${fmt} into ${outf}...`)
require('esbuild').build({
entryPoints: ['src/index.tsx'],
bundle: true,
minify: !isDebug,
keepNames: isDebug,
sourcemap: true,
loader: {
'.svg': 'text',
'.css': 'text'
},
format: fmt,
outfile: outf,
plugins: [preactAlias],
metafile: true,
...opts,
}).catch(() => process.exit(1)).then((res: any) => {
console.log("Built!")
let esbuildMeta = require('esbuild').analyzeMetafile(res.metafile);
esbuildMeta.then((report: any) => {
if(SHOW_REPORT)
console.log(report);
})
if("watch" in opts) {
console.log("Continuing to watch...")
}
})
});