-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
149 lines (128 loc) · 4.19 KB
/
Copy pathbuild.mjs
File metadata and controls
149 lines (128 loc) · 4.19 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
import * as fs from 'node:fs'
import { readFile } from 'node:fs/promises'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import esbuild from 'esbuild'
/**
* @returns {import('esbuild').Plugin}
*/
function patchRecast() {
return {
// https://github.com/benjamn/recast/issues/611
name: 'patch-recast',
setup(build) {
build.onLoad({ filter: /recast[\/\\]lib[\/\\]patcher\.js$/ }, async (args) => {
let original = await fs.promises.readFile(args.path, 'utf8')
return {
contents: original
.replace(
'var nls = needsLeadingSpace(lines, oldNode.loc, newLines);',
'var nls = oldNode.type !== "TemplateElement" && needsLeadingSpace(lines, oldNode.loc, newLines);',
)
.replace(
'var nts = needsTrailingSpace(lines, oldNode.loc, newLines)',
'var nts = oldNode.type !== "TemplateElement" && needsTrailingSpace(lines, oldNode.loc, newLines)',
),
}
})
},
}
}
/**
* @returns {import('esbuild').Plugin}
*/
function patchJiti() {
return {
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.promises.readFile(args.path, 'utf8')
return {
contents: original.replace(
'createRequire(import.meta.url)("../dist/babel.cjs")',
'require("../dist/babel.cjs")',
),
}
})
},
}
}
/**
* @returns {import('esbuild').Plugin}
*/
function patchCjsInterop() {
return {
name: 'patch-cjs-interop',
setup(build) {
build.onEnd(async () => {
let outfile = './dist/index.mjs'
let content = await fs.promises.readFile(outfile)
// Prepend `createRequire`
let code = [
`import {createRequire as __global__createRequire__} from 'module'`,
`import {dirname as __global__dirname__} from 'path'`,
`import {fileURLToPath as __global__fileURLToPath__} from 'url'`,
// CJS interop fixes
`const require=__global__createRequire__(import.meta.url)`,
`const __filename=__global__fileURLToPath__(import.meta.url)`,
`const __dirname=__global__dirname__(__filename)`,
]
content = `${code.join('\n')}\n${content}`
fs.promises.writeFile(outfile, content)
})
},
}
}
/**
* @returns {import('esbuild').Plugin}
*/
function inlineCssImports() {
return {
name: 'inline-css-imports',
setup(build) {
// Inline CSS imports
build.onLoad({ filter: /\.css$/ }, async (args) => {
let content = await readFile(args.path, 'utf-8')
return {
loader: 'js',
contents: `export default ${JSON.stringify(content)}`,
}
})
// Inline preflight in v3
// TODO: This needs a test
build.onLoad({ filter: /corePlugins\.js$/ }, async (args) => {
let preflightPath = path.resolve(path.dirname(args.path), './css/preflight.css')
let preflightContent = await readFile(preflightPath, 'utf-8')
let content = await readFile(args.path, 'utf-8')
// This is a bit fragile but this is to inline preflight for the
// *bundled* version which means a failing test should be enough
content = content.replace(
`_fs.default.readFileSync(_path.join(__dirname, "./css/preflight.css"), "utf8")`,
JSON.stringify(preflightContent),
)
return {
loader: 'js',
contents: content,
}
})
},
}
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
let context = await esbuild.context({
bundle: true,
platform: 'node',
target: 'node14.21.3',
external: ['prettier'],
minify: process.argv.includes('--minify'),
entryPoints: [path.resolve(__dirname, './src/index.ts')],
outfile: path.resolve(__dirname, './dist/index.mjs'),
format: 'esm',
plugins: [patchRecast(), patchJiti(), patchCjsInterop(), inlineCssImports()],
})
await context.rebuild()
if (process.argv.includes('--watch')) {
await context.watch()
}
await context.dispose()