Skip to content

Commit

Permalink
feat(core): improve user config file loading (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Oct 10, 2024
1 parent aaf3bba commit 01f6ab3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/cli/src/config/loadUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ export const loadUserConfig = async (
userConfigDependencies: [],
}
}
// following code is forked and modified from vite
// forked and modified from https://github.com/vitejs/vite/blob/889bfc0ada6d6cd356bb7a92efdce96298f82fef/packages/vite/src/node/config.ts#L1531
// TODO: we can migrate to something like `bundler-require`, but its `__dirname` support is not as good as vite
const dirnameVarName = '__vite_injected_original_dirname'
const filenameVarName = '__vite_injected_original_filename'
const importMetaUrlVarName = '__vite_injected_original_import_meta_url'
const result = await build({
absWorkingDir: process.cwd(),
entryPoints: [userConfigPath],
outfile: 'out.js',
write: false,
target: ['node18'],
target: [`node${process.versions.node}`],
platform: 'node',
bundle: true,
format: 'esm',
mainFields: ['main'],
sourcemap: 'inline',
metafile: true,
define: {
'__dirname': dirnameVarName,
'__filename': filenameVarName,
'import.meta.url': importMetaUrlVarName,
'import.meta.dirname': dirnameVarName,
'import.meta.filename': filenameVarName,
},
plugins: [
{
Expand All @@ -58,7 +60,7 @@ export const loadUserConfig = async (
name: 'inject-file-scope-variables',
setup(pluginBuild) {
pluginBuild.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
const contents = await fs.readFile(args.path, 'utf8')
const contents = await fs.readFile(args.path, 'utf-8')
const injectValues =
`const ${dirnameVarName} = ${JSON.stringify(
path.dirname(args.path),
Expand Down

0 comments on commit 01f6ab3

Please sign in to comment.