tailwind.com 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/wasi.mjs ADDED
@@ -0,0 +1,435 @@
1
+ // A WASI host (preview 1) for tailwind.wasm, with a file system in memory.
2
+ // It runs in a browser and in Node.js, with no dependency.
3
+ //
4
+ // const files = new Files()
5
+ // files.write('/app/app.css', '@import "tailwindcss";')
6
+ // const module = await WebAssembly.compileStreaming(fetch('tailwind.wasm'))
7
+ // const result = await run(module, { args: ['-i', 'app.css'], files, preopens: { '/': '/app' } })
8
+ // // result: { status, stdout, stderr }
9
+ //
10
+ // Under WASI preview 1, the working directory of the program is always
11
+ // "/". So the directory of the project is the root "/" of the program, as
12
+ // with `runWasm` of js/node.mjs. Each run makes a new instance of the
13
+ // module, and the files stay for the next run. The host has only
14
+ // the functions that tailwind.wasm imports. It has no symbolic links, and
15
+ // a wait (`poll_oneoff`) does not wait.
16
+
17
+ const ERRNO = {
18
+ SUCCESS: 0, BADF: 8, EXIST: 20, INVAL: 28, IO: 29, ISDIR: 31, NOENT: 44,
19
+ NOSYS: 52, NOTDIR: 54, NOTEMPTY: 55, NOTSUP: 58, SPIPE: 70,
20
+ }
21
+ const FILETYPE = { UNKNOWN: 0, CHARACTER_DEVICE: 2, DIRECTORY: 3, REGULAR_FILE: 4 }
22
+ const OFLAGS = { CREAT: 1, DIRECTORY: 2, EXCL: 4, TRUNC: 8 }
23
+ const FDFLAGS = { APPEND: 1 }
24
+ const ALL_RIGHTS = 0x1fffffffn
25
+
26
+ class Exit {
27
+ constructor(code) {
28
+ this.code = code
29
+ }
30
+ }
31
+
32
+ class Failure {
33
+ constructor(errno) {
34
+ this.errno = errno
35
+ }
36
+ }
37
+
38
+ let nextInode = 1n
39
+
40
+ function file(data) {
41
+ return { kind: FILETYPE.REGULAR_FILE, data, inode: nextInode++, time: now() }
42
+ }
43
+
44
+ function directory() {
45
+ return { kind: FILETYPE.DIRECTORY, children: new Map(), inode: nextInode++, time: now() }
46
+ }
47
+
48
+ function now() {
49
+ return BigInt(Date.now()) * 1000000n
50
+ }
51
+
52
+ const encoder = new TextEncoder()
53
+ const decoder = new TextDecoder()
54
+
55
+ // The file system. Paths are absolute, with "/" between the names.
56
+ export class Files {
57
+ constructor() {
58
+ this.root = directory()
59
+ }
60
+
61
+ // The node of a path, or a Failure.
62
+ find(path, from = this.root) {
63
+ let node = from
64
+ const stack = [from]
65
+ for (const name of path.split('/')) {
66
+ if (name === '' || name === '.') continue
67
+ if (name === '..') {
68
+ if (stack.length > 1) stack.pop()
69
+ node = stack[stack.length - 1]
70
+ continue
71
+ }
72
+ if (node.kind !== FILETYPE.DIRECTORY) throw new Failure(ERRNO.NOTDIR)
73
+ const child = node.children.get(name)
74
+ if (!child) throw new Failure(ERRNO.NOENT)
75
+ node = child
76
+ stack.push(node)
77
+ }
78
+ return node
79
+ }
80
+
81
+ // The parent directory of a path, and the last name.
82
+ parent(path, from = this.root) {
83
+ const names = path.split('/').filter((n) => n !== '' && n !== '.')
84
+ const last = names.pop()
85
+ if (last === undefined || last === '..') throw new Failure(ERRNO.INVAL)
86
+ const dir = this.find(names.join('/'), from)
87
+ if (dir.kind !== FILETYPE.DIRECTORY) throw new Failure(ERRNO.NOTDIR)
88
+ return [dir, last]
89
+ }
90
+
91
+ mkdir(path) {
92
+ let node = this.root
93
+ for (const name of path.split('/').filter(Boolean)) {
94
+ let child = node.children.get(name)
95
+ if (!child) node.children.set(name, (child = directory()))
96
+ node = child
97
+ }
98
+ return node
99
+ }
100
+
101
+ // Write a file (text or bytes). The directories are made.
102
+ write(path, content) {
103
+ const names = path.split('/').filter(Boolean)
104
+ const name = names.pop()
105
+ const dir = this.mkdir(names.join('/'))
106
+ dir.children.set(name, file(typeof content === 'string' ? encoder.encode(content) : content))
107
+ }
108
+
109
+ read(path) {
110
+ const node = this.find(path)
111
+ if (node.kind !== FILETYPE.REGULAR_FILE) throw new Error(`${path} is not a file`)
112
+ return node.data
113
+ }
114
+
115
+ readText(path) {
116
+ return decoder.decode(this.read(path))
117
+ }
118
+
119
+ exists(path) {
120
+ try {
121
+ this.find(path)
122
+ return true
123
+ } catch {
124
+ return false
125
+ }
126
+ }
127
+
128
+ remove(path) {
129
+ const [dir, name] = this.parent(path)
130
+ dir.children.delete(name)
131
+ }
132
+ }
133
+
134
+ // Run the module once. options: args, env (an object), files (default: a
135
+ // new Files), stdin (text or bytes), and preopens: the directories that
136
+ // the program sees, as { "path in the program": "path in files" }
137
+ // (default { "/": "/" }). A second directory, such as { "/packages":
138
+ // "/packages" }, is not in the root of the program, so the scanner of
139
+ // the project does not see it. imports: other import modules, or a
140
+ // function that gets a function of the exports of the instance and gives
141
+ // them. promising: start the program with WebAssembly.promising, for an
142
+ // import that suspends it (js/engine.mjs).
143
+ export async function run(module, options = {}) {
144
+ const files = options.files ?? new Files()
145
+ const args = ['tailwind', ...(options.args ?? [])]
146
+ const env = { PWD: '/', ...(options.env ?? {}) }
147
+ const stdin = typeof options.stdin === 'string' ? encoder.encode(options.stdin) : (options.stdin ?? new Uint8Array())
148
+ const stdout = []
149
+ const stderr = []
150
+
151
+ // The open files. 0, 1, 2: the standard streams. 3 and more: the
152
+ // directories of `preopens`.
153
+ const fds = new Map([
154
+ [0, { stream: 'stdin', position: 0 }],
155
+ [1, { stream: 'stdout' }],
156
+ [2, { stream: 'stderr' }],
157
+ ])
158
+ let nextFd = 3
159
+ for (const [guest, host] of Object.entries(options.preopens ?? { '/': '/' })) {
160
+ const node = files.exists(host) ? files.find(host) : null
161
+ if (node?.kind !== FILETYPE.DIRECTORY) throw new Error(`${host} is not a directory`)
162
+ fds.set(nextFd++, { node, preopen: guest })
163
+ }
164
+
165
+ let memory
166
+ const view = () => new DataView(memory.buffer)
167
+ const bytes = () => new Uint8Array(memory.buffer)
168
+ const text = (pointer, length) => decoder.decode(bytes().subarray(pointer, pointer + length))
169
+
170
+ function entry(fd) {
171
+ const e = fds.get(fd)
172
+ if (!e) throw new Failure(ERRNO.BADF)
173
+ return e
174
+ }
175
+
176
+ function directoryOf(fd) {
177
+ const e = entry(fd)
178
+ if (!e.node || e.node.kind !== FILETYPE.DIRECTORY) throw new Failure(ERRNO.NOTDIR)
179
+ return e.node
180
+ }
181
+
182
+ function writeFilestat(pointer, node) {
183
+ const v = view()
184
+ v.setBigUint64(pointer, 0n, true)
185
+ v.setBigUint64(pointer + 8, node?.inode ?? 0n, true)
186
+ v.setUint8(pointer + 16, node ? node.kind : FILETYPE.CHARACTER_DEVICE)
187
+ v.setBigUint64(pointer + 24, 1n, true)
188
+ v.setBigUint64(pointer + 32, BigInt(node?.kind === FILETYPE.REGULAR_FILE ? node.data.length : 0), true)
189
+ for (const offset of [40, 48, 56]) v.setBigUint64(pointer + offset, node?.time ?? 0n, true)
190
+ }
191
+
192
+ // The data of a list of iovecs or ciovecs.
193
+ function vectors(pointer, count) {
194
+ const v = view()
195
+ const list = []
196
+ for (let i = 0; i < count; i++) list.push([v.getUint32(pointer + i * 8, true), v.getUint32(pointer + i * 8 + 4, true)])
197
+ return list
198
+ }
199
+
200
+ // The functions of the module "wasi_snapshot_preview1". Each one gives
201
+ // an errno; a Failure gives its errno.
202
+ const functions = {
203
+ args_sizes_get(countPointer, sizePointer) {
204
+ view().setUint32(countPointer, args.length, true)
205
+ view().setUint32(sizePointer, args.reduce((n, a) => n + encoder.encode(a).length + 1, 0), true)
206
+ },
207
+ args_get(listPointer, bufferPointer) {
208
+ putStrings(args, listPointer, bufferPointer)
209
+ },
210
+ environ_sizes_get(countPointer, sizePointer) {
211
+ const list = Object.entries(env).map(([k, v]) => `${k}=${v}`)
212
+ view().setUint32(countPointer, list.length, true)
213
+ view().setUint32(sizePointer, list.reduce((n, a) => n + encoder.encode(a).length + 1, 0), true)
214
+ },
215
+ environ_get(listPointer, bufferPointer) {
216
+ putStrings(Object.entries(env).map(([k, v]) => `${k}=${v}`), listPointer, bufferPointer)
217
+ },
218
+ random_get(pointer, length) {
219
+ const target = bytes().subarray(pointer, pointer + length)
220
+ for (let i = 0; i < length; i += 65536) crypto.getRandomValues(target.subarray(i, Math.min(length, i + 65536)))
221
+ },
222
+ clock_time_get(id, _precision, pointer) {
223
+ const time = id === 0 ? now() : BigInt(Math.round(performance.now() * 1e6))
224
+ view().setBigUint64(pointer, time, true)
225
+ },
226
+ sched_yield() {},
227
+ poll_oneoff(inPointer, outPointer, count, eventsPointer) {
228
+ const v = view()
229
+ for (let i = 0; i < count; i++) {
230
+ const sub = inPointer + i * 48
231
+ const event = outPointer + i * 32
232
+ v.setBigUint64(event, v.getBigUint64(sub, true), true)
233
+ v.setUint16(event + 8, 0, true)
234
+ v.setUint8(event + 10, v.getUint8(sub + 8))
235
+ }
236
+ v.setUint32(eventsPointer, count, true)
237
+ },
238
+ proc_exit(code) {
239
+ throw new Exit(code)
240
+ },
241
+
242
+ fd_prestat_get(fd, pointer) {
243
+ const e = entry(fd)
244
+ if (!e.preopen) throw new Failure(ERRNO.BADF)
245
+ view().setUint8(pointer, 0)
246
+ view().setUint32(pointer + 4, encoder.encode(e.preopen).length, true)
247
+ },
248
+ fd_prestat_dir_name(fd, pointer, length) {
249
+ const e = entry(fd)
250
+ if (!e.preopen) throw new Failure(ERRNO.BADF)
251
+ bytes().set(encoder.encode(e.preopen).subarray(0, length), pointer)
252
+ },
253
+ fd_fdstat_get(fd, pointer) {
254
+ const e = entry(fd)
255
+ const v = view()
256
+ v.setUint8(pointer, e.node ? e.node.kind : FILETYPE.CHARACTER_DEVICE)
257
+ v.setUint16(pointer + 2, e.append ? FDFLAGS.APPEND : 0, true)
258
+ // With all the rights a standard stream is not a terminal for
259
+ // wasi-libc: the program writes no colors.
260
+ v.setBigUint64(pointer + 8, ALL_RIGHTS, true)
261
+ v.setBigUint64(pointer + 16, ALL_RIGHTS, true)
262
+ },
263
+ fd_filestat_get(fd, pointer) {
264
+ writeFilestat(pointer, entry(fd).node)
265
+ },
266
+ fd_close(fd) {
267
+ entry(fd)
268
+ fds.delete(fd)
269
+ },
270
+ fd_seek(fd, offset, whence, pointer) {
271
+ const e = entry(fd)
272
+ if (!e.node || e.node.kind !== FILETYPE.REGULAR_FILE) throw new Failure(ERRNO.SPIPE)
273
+ const base = whence === 0 ? 0 : whence === 1 ? e.position : e.node.data.length
274
+ const position = base + Number(offset)
275
+ if (position < 0) throw new Failure(ERRNO.INVAL)
276
+ e.position = position
277
+ view().setBigUint64(pointer, BigInt(position), true)
278
+ },
279
+ fd_read(fd, iovs, count, pointer) {
280
+ const e = entry(fd)
281
+ let source
282
+ if (e.stream === 'stdin') source = stdin
283
+ else if (e.node?.kind === FILETYPE.REGULAR_FILE) source = e.node.data
284
+ else throw new Failure(e.node ? ERRNO.ISDIR : ERRNO.BADF)
285
+ let total = 0
286
+ for (const [buffer, length] of vectors(iovs, count)) {
287
+ const part = source.subarray(e.position, e.position + length)
288
+ bytes().set(part, buffer)
289
+ e.position += part.length
290
+ total += part.length
291
+ if (part.length < length) break
292
+ }
293
+ view().setUint32(pointer, total, true)
294
+ },
295
+ fd_write(fd, iovs, count, pointer) {
296
+ const e = entry(fd)
297
+ const parts = vectors(iovs, count).map(([buffer, length]) => bytes().slice(buffer, buffer + length))
298
+ const total = parts.reduce((n, p) => n + p.length, 0)
299
+ if (e.stream === 'stdout') stdout.push(...parts)
300
+ else if (e.stream === 'stderr') stderr.push(...parts)
301
+ else if (e.node?.kind === FILETYPE.REGULAR_FILE) {
302
+ const node = e.node
303
+ if (e.append) e.position = node.data.length
304
+ const end = e.position + total
305
+ if (end > node.data.length) {
306
+ const larger = new Uint8Array(Math.max(end, node.data.length * 2))
307
+ larger.set(node.data)
308
+ node.data = larger.subarray(0, end)
309
+ }
310
+ for (const part of parts) {
311
+ node.data.set(part, e.position)
312
+ e.position += part.length
313
+ }
314
+ node.time = now()
315
+ } else throw new Failure(ERRNO.BADF)
316
+ view().setUint32(pointer, total, true)
317
+ },
318
+ fd_readdir(fd, buffer, length, cookie, pointer) {
319
+ const node = directoryOf(fd)
320
+ const names = ['.', '..', ...node.children.keys()]
321
+ const v = view()
322
+ let used = 0
323
+ for (let i = Number(cookie); i < names.length; i++) {
324
+ const name = encoder.encode(names[i])
325
+ const child = i < 2 ? node : node.children.get(names[i])
326
+ const header = new DataView(new ArrayBuffer(24))
327
+ header.setBigUint64(0, BigInt(i + 1), true)
328
+ header.setBigUint64(8, child.inode, true)
329
+ header.setUint32(16, name.length, true)
330
+ header.setUint8(20, child.kind)
331
+ const record = new Uint8Array(24 + name.length)
332
+ record.set(new Uint8Array(header.buffer))
333
+ record.set(name, 24)
334
+ // A record that does not fit is cut: the caller then asks again
335
+ // with a larger buffer.
336
+ const part = record.subarray(0, Math.min(record.length, length - used))
337
+ bytes().set(part, buffer + used)
338
+ used += part.length
339
+ if (used === length) break
340
+ }
341
+ v.setUint32(pointer, used, true)
342
+ },
343
+ path_filestat_get(fd, _flags, pathPointer, pathLength, pointer) {
344
+ writeFilestat(pointer, files.find(text(pathPointer, pathLength), directoryOf(fd)))
345
+ },
346
+ path_create_directory(fd, pathPointer, pathLength) {
347
+ const [dir, name] = files.parent(text(pathPointer, pathLength), directoryOf(fd))
348
+ if (dir.children.has(name)) throw new Failure(ERRNO.EXIST)
349
+ dir.children.set(name, directory())
350
+ },
351
+ path_readlink(fd, pathPointer, pathLength) {
352
+ // No symbolic links: a path that is there is not one.
353
+ files.find(text(pathPointer, pathLength), directoryOf(fd))
354
+ throw new Failure(ERRNO.INVAL)
355
+ },
356
+ path_open(fd, _dirflags, pathPointer, pathLength, oflags, _base, _inheriting, fdflags, pointer) {
357
+ const path = text(pathPointer, pathLength)
358
+ const from = directoryOf(fd)
359
+ let node
360
+ try {
361
+ node = files.find(path, from)
362
+ if (oflags & OFLAGS.EXCL && oflags & OFLAGS.CREAT) throw new Failure(ERRNO.EXIST)
363
+ } catch (error) {
364
+ if (!(error instanceof Failure) || error.errno !== ERRNO.NOENT || !(oflags & OFLAGS.CREAT)) throw error
365
+ const [dir, name] = files.parent(path, from)
366
+ node = file(new Uint8Array())
367
+ dir.children.set(name, node)
368
+ }
369
+ if (oflags & OFLAGS.DIRECTORY && node.kind !== FILETYPE.DIRECTORY) throw new Failure(ERRNO.NOTDIR)
370
+ if (oflags & OFLAGS.TRUNC) {
371
+ if (node.kind === FILETYPE.DIRECTORY) throw new Failure(ERRNO.ISDIR)
372
+ node.data = new Uint8Array()
373
+ node.time = now()
374
+ }
375
+ const opened = nextFd++
376
+ fds.set(opened, { node, position: 0, append: Boolean(fdflags & FDFLAGS.APPEND) })
377
+ view().setUint32(pointer, opened, true)
378
+ },
379
+ }
380
+
381
+ function putStrings(list, listPointer, bufferPointer) {
382
+ const v = view()
383
+ let at = bufferPointer
384
+ list.forEach((item, i) => {
385
+ const data = encoder.encode(item)
386
+ v.setUint32(listPointer + i * 4, at, true)
387
+ bytes().set(data, at)
388
+ bytes()[at + data.length] = 0
389
+ at += data.length + 1
390
+ })
391
+ }
392
+
393
+ const imports = {}
394
+ for (const [name, fn] of Object.entries(functions)) {
395
+ imports[name] = (...values) => {
396
+ try {
397
+ fn(...values)
398
+ return ERRNO.SUCCESS
399
+ } catch (error) {
400
+ if (error instanceof Failure) return error.errno
401
+ throw error
402
+ }
403
+ }
404
+ }
405
+ for (const { module: from, name } of WebAssembly.Module.imports(module)) {
406
+ if (from === 'wasi_snapshot_preview1' && !imports[name]) imports[name] = () => ERRNO.NOSYS
407
+ }
408
+
409
+ // Other import modules: `options.imports` is a function of a function
410
+ // that gives the exports of the instance.
411
+ let instance = null
412
+ const others = typeof options.imports === 'function' ? options.imports(() => instance.exports) : (options.imports ?? {})
413
+ instance = await WebAssembly.instantiate(module, { ...others, wasi_snapshot_preview1: imports })
414
+ memory = instance.exports.memory
415
+ let status = 0
416
+ try {
417
+ // With `promising`, an import can suspend the program (JS Promise
418
+ // Integration): the start gives a promise.
419
+ if (options.promising) await WebAssembly.promising(instance.exports._start)()
420
+ else instance.exports._start()
421
+ } catch (error) {
422
+ if (!(error instanceof Exit)) throw error
423
+ status = error.code
424
+ }
425
+ const join = (parts) => {
426
+ const all = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
427
+ let at = 0
428
+ for (const p of parts) {
429
+ all.set(p, at)
430
+ at += p.length
431
+ }
432
+ return decoder.decode(all)
433
+ }
434
+ return { status, stdout: join(stdout), stderr: join(stderr) }
435
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tailwind.com",
3
- "version": "0.4.0",
4
- "description": "Tailwind CSS in one executable file, with no Node.js at run time: npx tailwind.com, and the same compiler as WebAssembly",
3
+ "version": "0.6.0",
4
+ "description": "Tailwind CSS in one executable file, with no Node.js at run time: npx tailwind.com, and the same compiler as WebAssembly, also in the browser",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",
@@ -15,9 +15,17 @@
15
15
  "bin": {
16
16
  "tailwind.com": "js/tailwind.com.mjs"
17
17
  },
18
+ "jsdelivr": "./js/page.js",
19
+ "unpkg": "./js/page.js",
18
20
  "exports": {
19
- ".": "./js/node.mjs",
20
- "./tailwind.wasm": "./runtime/tailwind.wasm",
21
+ ".": {
22
+ "browser": "./js/browser.mjs",
23
+ "default": "./js/node.mjs"
24
+ },
25
+ "./tailwind.wasm": {
26
+ "browser": "./runtime/tailwind-browser.wasm",
27
+ "default": "./runtime/tailwind.wasm"
28
+ },
21
29
  "./package.json": "./package.json"
22
30
  },
23
31
  "files": [