Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/silly-chairs-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@workflow/nitro': patch
---

Skip Workflow transforms for generated Nitro build artifacts.
51 changes: 51 additions & 0 deletions packages/nitro/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Nitro } from 'nitro/types';
import { describe, expect, it, onTestFinished } from 'vitest';
import { LocalBuilder, VercelBuilder } from './builders.js';
import nitroModule from './index.js';
import { workflow as viteWorkflow } from './vite.js';

type StubOptions = {
routing: boolean;
Expand Down Expand Up @@ -180,3 +181,53 @@ describe('@workflow/nitro projectRoot', () => {
});
}
});

describe('@workflow/nitro transform boundaries', () => {
it('does not re-transform generated Nitro build artifacts', async () => {
const rollupBeforeHooks: Array<(nitro: any, config: any) => void> = [];
const nitro = createNitroStub({ routing: true });
nitro.hooks.hook = (
name: string,
hook: (nitro: any, config: any) => void
) => {
if (name === 'rollup:before') rollupBeforeHooks.push(hook);
};

const plugins = viteWorkflow();
const viteTransform = plugins.find(
(plugin) => plugin.name === 'workflow:transform'
) as any;
const viteNitro = plugins.find(
(plugin) => plugin.name === 'workflow:nitro'
) as any;

await viteNitro.nitro.setup(nitro);

const config: { plugins: any[] } = { plugins: [] };
for (const hook of rollupBeforeHooks) {
hook(nitro, config);
}
const nitroTransform = config.plugins.find(
(plugin: { name?: string }) => plugin.name === 'workflow:transform'
);

const code = `
import { WORKFLOW_SERIALIZE, WORKFLOW_DESERIALIZE } from '@workflow/serde';
export class Serializable {
static [WORKFLOW_SERIALIZE](value) { return value; }
static [WORKFLOW_DESERIALIZE]() { return new Serializable(); }
}
`;
const generatedId = '/tmp/.nitro/vite/services/ssr/assets/index.js';
const siblingId = '/tmp/.nitro-source/index.js';

for (const transform of [viteTransform, nitroTransform]) {
await expect(
transform.transform.call({}, code, generatedId)
).resolves.toBeNull();
await expect(
transform.transform.call({}, code, siblingId)
).resolves.not.toBeNull();
}
});
});
12 changes: 6 additions & 6 deletions packages/nitro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ export default {
const isVercelDeploy =
!nitro.options.dev && nitro.options.preset === 'vercel';

// Pre-built workflow bundles directory - must be excluded from re-transformation
const workflowBuildDir = join(nitro.options.buildDir, 'workflow');
// Nitro build artifacts have already been transformed; exclude the whole
// build directory from re-transformation.
const nitroBuildDir = `${nitro.options.buildDir.replace(/[\\/]+$/, '')}/`;

// Add transform plugin at the BEGINNING to run before other transforms
// (especially before class property transforms that rename classes like _ClassName)
nitro.hooks.hook('rollup:before', (_nitro: Nitro, config: RollupConfig) => {
(config.plugins as Array<unknown>).unshift(
workflowTransformPlugin({
// Exclude pre-built workflow bundles from re-transformation
// These are already processed and re-processing causes issues like
// undefined class references when Nitro's bundler renames variables
exclude: [workflowBuildDir],
// Nitro build artifacts have already been transformed.
// Re-processing them can duplicate class registration.
exclude: [nitroBuildDir],
})
);
});
Expand Down
13 changes: 6 additions & 7 deletions packages/nitro/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import { workflowTransformPlugin } from '@workflow/rollup';
import { workflowHotUpdatePlugin } from '@workflow/vite';
import type { Nitro } from 'nitro/types';
import type {} from 'nitro/vite';
import { join } from 'pathe';
import type { Plugin } from 'vite';
import { LocalBuilder } from './builders.js';
import type { ModuleOptions } from './index.js';
import nitroModule from './index.js';

export function workflow(options?: ModuleOptions): Plugin[] {
let builder: LocalBuilder;
let workflowBuildDir: string;
let nitroBuildDir: string;
const enqueue = createBuildQueue();

// Create a lazy transform plugin that excludes the workflow build directory
// Create a lazy transform plugin that excludes Nitro build artifacts.
// The exclusion path is set during nitro setup, so we need to defer plugin creation
const lazyTransformPlugin: Plugin = {
name: 'workflow:transform',
transform(code, id) {
// Delegate to the actual transform plugin with exclusion
// workflowBuildDir is set during nitro setup before transforms run
// nitroBuildDir is set during nitro setup before transforms run
const plugin = workflowTransformPlugin({
exclude: workflowBuildDir ? [workflowBuildDir] : [],
exclude: nitroBuildDir ? [nitroBuildDir] : [],
});
return (plugin.transform as Function)?.call(this, code, id);
},
Expand All @@ -34,8 +33,8 @@ export function workflow(options?: ModuleOptions): Plugin[] {
name: 'workflow:nitro',
nitro: {
setup: (nitro: Nitro) => {
// Capture the workflow build directory for exclusion
workflowBuildDir = join(nitro.options.buildDir, 'workflow');
// Capture the Nitro build directory for exclusion
nitroBuildDir = `${nitro.options.buildDir.replace(/[\\/]+$/, '')}/`;
nitro.options.workflow = {
...nitro.options.workflow,
...options,
Expand Down
Loading