Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix __dirname error from esmodule build
Browse files Browse the repository at this point in the history
m2rads committed Dec 10, 2024
1 parent 94d4bce commit 52c8db6
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/shortest/src/core/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ import { build, BuildOptions } from 'esbuild';
import { join, resolve } from 'path';
import { mkdirSync, existsSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

export class TestCompiler {
private cacheDir: string;
@@ -25,7 +27,17 @@ export class TestCompiler {
'buffer',
'querystring',
'fsevents'
]
],
banner: {
js: `
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { createRequire } from "module";
const require = createRequire(import.meta.url);
`
}
};

constructor() {
@@ -79,7 +91,9 @@ export class TestCompiler {
});

const code = result.outputFiles[0].text;
return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
const tempFile = join(this.cacheDir, 'config.mjs');
writeFileSync(tempFile, code);
return import(`file://${tempFile}`);
} catch (error) {
throw new Error(`Failed to load config from ${absolutePath}: ${error}`);
}

0 comments on commit 52c8db6

Please sign in to comment.