Skip to content

Commit b8f8931

Browse files
committed
feat: dirname and filename support
1 parent 4e1a9fb commit b8f8931

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

scripts/build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const stdlib = require('node-stdlib-browser')
99
const importPath = require('./esbuild-import-path')
1010
const correctSet = require('./esbuild-correct-set')
1111
const vendorSourceMap = require('./esbuild-vendor-sourcemap')
12+
const dirnameAndFilenameSupport = require('./esbuild-dirname-and-filename')
1213

1314
const client = () => {
1415
const make = async (format) => {
@@ -45,6 +46,7 @@ const client = () => {
4546
importPath,
4647
correctSet,
4748
vendorSourceMap,
49+
dirnameAndFilenameSupport
4850
],
4951
external: ['fontkit-next'],
5052
})
@@ -79,6 +81,7 @@ const node = () => {
7981
importPath,
8082
correctSet,
8183
vendorSourceMap,
84+
dirnameAndFilenameSupport
8285
],
8386
external: ['fontkit-next', 'pdfkit'], // TODO: Remove external pdfkit
8487
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/);
5+
6+
module.exports = {
7+
name: "dirname",
8+
setup(build) {
9+
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => {
10+
if (!filePath.match(nodeModules)) {
11+
let contents = fs.readFileSync(filePath, "utf8");
12+
const loader = path.extname(filePath).substring(1);
13+
const dirname = path.dirname(filePath);
14+
contents = contents
15+
.replace("__dirname", `"${dirname}"`)
16+
.replace("__filename", `"${filePath}"`);
17+
return {
18+
contents,
19+
loader,
20+
};
21+
}
22+
});
23+
},
24+
};

0 commit comments

Comments
 (0)