Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): improve loading config file #1615

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading