-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
98 lines (90 loc) · 3.02 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
import fs from 'node:fs'
import path from 'node:path'
import * as rollup from 'rollup'
import * as esbuild from 'esbuild'
import * as dts from '@hyrious/dts'
import * as SASS from 'sass'
import { version, peerDependencies } from './package.json'
import { createRequire } from 'node:module'
const sass = (): esbuild.Plugin => ({
name: 'inline-sass',
setup({ onLoad, esbuild }) {
onLoad({ filter: /\.scss/ }, async args => {
if (args.suffix !== '?inline') return
const { css } = SASS.compile(args.path, { style: 'compressed' })
const { outputFiles } = await esbuild.build({
stdin: {
contents: css,
loader: 'css',
resolveDir: path.dirname(args.path),
sourcefile: args.path,
},
logLevel: 'silent',
bundle: true,
minify: true,
write: false,
outdir: 'dist',
})
const contents = outputFiles[0].text.trimEnd()
return { contents, loader: 'text' }
})
}
})
// vanilla-lazyload has "browser": "dist/file.min.js", which is not ESM
// correct it by choosing the "module" field
const lazyload = (): esbuild.Plugin => ({
name: 'lazyload',
setup({ onResolve }) {
const require = createRequire(import.meta.url)
onResolve({ filter: /^vanilla-lazyload$/ }, args => {
const cjs = require.resolve(args.path)
const esm = cjs.replace('lazyload.min.js', 'lazyload.esm.js')
return { path: esm }
})
}
})
fs.rmSync('dist', { recursive: true, force: true })
let bundle = await rollup.rollup({
input: 'src/index.ts',
external: ['jspdf'],
plugins: [{
name: 'esbuild',
async load(id) {
const { outputFiles } = await esbuild.build({
entryPoints: [id],
bundle: true,
format: 'esm',
outfile: id.replace(/\.ts$/, '.js'),
sourcemap: true,
write: false,
target: ['es2017'],
plugins: [sass(), lazyload()],
define: {
__VERSION__: JSON.stringify(version)
},
external: Object.keys({
'@netless/window-manager': '*',
...peerDependencies,
})
})
let code: any, map: any
for (const { path, text } of outputFiles) {
if (path.endsWith('.map')) map = text;
else code = text;
}
return { code, map }
}
}]
})
await Promise.all([
bundle.write({ file: 'dist/index.mjs', format: 'es', sourcemap: true, sourcemapExcludeSources: true }),
bundle.write({ file: 'dist/index.js', format: 'cjs', sourcemap: true, sourcemapExcludeSources: true, interop: 'auto', exports: 'named' }),
bundle.write({ file: 'dist/index.global.js', format: 'iife', name: 'NetlessAppPresentation', exports: 'named' }),
])
await bundle.close()
// replace `import('jspdf')` in global js with `window.jspdf`
let code = fs.readFileSync('dist/index.global.js', 'utf-8')
code = code.replace(`await import('jspdf')`, `jspdf`)
fs.writeFileSync('dist/index.global.js', code)
if (process.env.DTS != '0')
await dts.build('src/index.ts', 'dist/index.d.ts', { exclude: ['@netless/window-manager'] })