Skip to content

Commit db93764

Browse files
committed
fix: load plugins with jiti
1 parent 0778e49 commit db93764

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

packages/cli/src/actions/action-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ export async function loadPluginModule(provider: string, basePath: string) {
291291
}
292292
}
293293

294+
// try jiti import for bare package specifiers (handles workspace packages)
295+
try {
296+
const result = (await jiti.import(moduleSpec, { default: true })) as CliPlugin;
297+
return result;
298+
} catch {
299+
// fall through to last resort
300+
}
301+
294302
// last resort, try to import as esm directly
295303
try {
296304
const mod = await import(moduleSpec);

packages/cli/test/generate.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,40 @@ model User {
272272
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
273273
});
274274

275+
it('should load plugin from a bare package specifier via jiti', async () => {
276+
const modelWithBarePlugin = `
277+
plugin foo {
278+
provider = 'my-test-plugin'
279+
}
280+
281+
model User {
282+
id String @id @default(cuid())
283+
}
284+
`;
285+
const { workDir } = await createProject(modelWithBarePlugin);
286+
// Create a fake node_modules package with a TS entry point
287+
// This can only be resolved by jiti, not by native import() or fs.existsSync checks
288+
const pkgDir = path.join(workDir, 'node_modules/my-test-plugin');
289+
fs.mkdirSync(pkgDir, { recursive: true });
290+
fs.writeFileSync(
291+
path.join(pkgDir, 'package.json'),
292+
JSON.stringify({ name: 'my-test-plugin', main: './index.ts' }),
293+
);
294+
fs.writeFileSync(
295+
path.join(pkgDir, 'index.ts'),
296+
`
297+
const plugin = {
298+
name: 'test-bare-plugin',
299+
statusText: 'Testing bare plugin',
300+
async generate() {},
301+
};
302+
export default plugin;
303+
`,
304+
);
305+
runCli('generate', workDir);
306+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
307+
});
308+
275309
it('should prefer CLI options over @core/typescript plugin settings for generateModels and generateInput', async () => {
276310
const modelWithPlugin = `
277311
plugin typescript {

0 commit comments

Comments
 (0)