-
-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathesbuild.mjs
More file actions
190 lines (164 loc) · 5.87 KB
/
esbuild.mjs
File metadata and controls
190 lines (164 loc) · 5.87 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
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
189
190
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import esbuild from 'esbuild'
import minimist from 'minimist'
import checker from 'license-checker'
const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const args = minimist(process.argv.slice(2), {
boolean: ['watch', 'minify'],
string: ['outfile', 'outdir'],
})
console.log('- Preparing')
let ctx = await esbuild.context({
entryPoints: args._,
bundle: true,
platform: 'node',
external: ['pnpapi', 'vscode', 'lightningcss', '@tailwindcss/oxide'],
format: 'cjs',
define: {
'process.env.TEST': '0',
},
outdir: args.outdir,
outfile: args.outfile,
minify: args.minify,
plugins: [
{
name: 'css',
setup(build) {
build.onResolve({ filter: /\.css$/, namespace: 'file' }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'css',
}))
build.onLoad({ filter: /.*/, namespace: 'css' }, async (args) => ({
contents: `
export default ${JSON.stringify(await fs.readFile(args.path, 'utf8'))}
`,
}))
},
},
{
name: 'patch-jiti',
setup(build) {
// TODO: Switch to rolldown and see if we can chunk split this instead?
build.onLoad({ filter: /jiti\/lib\/jiti\.mjs$/ }, async (args) => {
let original = await fs.readFile(args.path, 'utf8')
return {
contents: original.replace(
'createRequire(import.meta.url)("../dist/babel.cjs")',
'require("../dist/babel.cjs")',
),
}
})
},
},
{
// https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
name: 'native-node-modules',
setup(build) {
// If a ".node" file is imported within a module in the "file" namespace, resolve
// it to an absolute path and put it into the "node-file" virtual namespace.
build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'node-file',
}))
// Files in the "node-file" virtual namespace call "require()" on the
// path from esbuild of the ".node" file in the output directory.
build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
contents: `
import path from ${JSON.stringify(args.path)}
import { resolve } from 'path'
module.exports = require(resolve(__dirname, path))
`,
}))
// If a ".node" file is imported within a module in the "node-file" namespace, put
// it in the "file" namespace where esbuild's default loading behavior will handle
// it. It is already an absolute path since we resolved it to one above.
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
path: args.path,
namespace: 'file',
}))
// Tell esbuild's default loading behavior to use the "file" loader for
// these ".node" files.
let opts = build.initialOptions
opts.loader = opts.loader || {}
opts.loader['.node'] = 'file'
},
},
{
name: 'generate-notices',
async setup() {
let exclude = [
/^@types\//,
'esbuild',
'rimraf',
'prettier',
'typescript',
'license-checker',
]
let allLicenses = {
...(await getLicenses(path.resolve(__dirname, 'packages/tailwindcss-language-server'))),
...(await getLicenses(path.resolve(__dirname, 'packages/tailwindcss-language-service'))),
}
let allDeps = [
...(await getDeps(path.resolve(__dirname, 'packages/tailwindcss-language-server'), true)),
...(await getDeps(path.resolve(__dirname, 'packages/tailwindcss-language-service'))),
]
function isExcluded(name) {
for (let pattern of exclude) {
if (typeof pattern === 'string') {
if (name === pattern) {
return true
}
} else if (pattern.test(name)) {
return true
}
}
return false
}
async function getDeps(dir, dev = false) {
let pkg = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf-8'))
let deps = Object.entries(pkg['dependencies'] ?? {})
if (dev) deps.push(...Object.entries(pkg['devDependencies'] ?? {}))
return deps.map(([name, version]) => `${name}@${version}`)
}
function getLicenses(dir) {
return new Promise((resolve, reject) => {
checker.init({ start: dir }, (err, packages) => {
if (err) return reject(err)
return resolve(packages)
})
})
}
let contents = []
for (let pkg in allLicenses) {
if (!allDeps.includes(pkg)) continue
let parts = pkg.split('@')
let name = parts.slice(0, parts.length - 1).join('@')
if (isExcluded(name)) continue
let license = allLicenses[pkg].licenseFile
? (await fs.readFile(allLicenses[pkg].licenseFile, 'utf-8')).trim()
: undefined
if (!license) continue
contents.push(`${pkg}\n\n${license}`)
}
await fs.writeFile(
path.resolve(__dirname, 'packages/tailwindcss-language-server/ThirdPartyNotices.txt'),
contents.join(`\n\n${'='.repeat(80)}\n\n`),
'utf-8',
)
},
},
],
})
console.log('- Building')
await ctx.rebuild()
if (args.watch) {
console.log('- Watching')
await ctx.watch()
} else {
console.log('- Cleaning up')
await ctx.dispose()
}