Skip to content

Commit

Permalink
fix: dev imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jan 7, 2025
1 parent d37a786 commit 497a83f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 691 deletions.
9 changes: 3 additions & 6 deletions packages/directive-functions-plugin/src/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type CompileDirectivesOpts = ParseAstOptions & {
directiveFnsById: Record<string, DirectiveFn>
}) => string
replacer: ReplacerFn
devSplitImporter: string
// devSplitImporter: string
}

export type ParseAstOptions = {
Expand Down Expand Up @@ -247,7 +247,7 @@ export function findDirectives(
replacer?: ReplacerFn
splitFunctionName?: string | null
directiveSplitParam: string
devSplitImporter: string
// devSplitImporter: string
},
) {
const directiveFnsById: Record<string, DirectiveFn> = {}
Expand Down Expand Up @@ -481,10 +481,7 @@ export function findDirectives(
: {}),
...(replacer.includes('$$splitImportFn$$')
? {
$$splitImportFn$$:
process.env.NODE_ENV === 'production'
? `(...args) => import(${JSON.stringify(splitFilename)}).then(module => module.default(...args))`
: `(...args) => ${opts.devSplitImporter}(${JSON.stringify(splitFilename)}).then(module => module.default(...args))`,
$$splitImportFn$$: `(...args) => import(${JSON.stringify(splitFilename)}).then(module => module.default(...args))`,
}
: {}),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/directive-functions-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type { DirectiveFn, CompileDirectivesOpts } from './compilers'
export type DirectiveFunctionsViteOptions = Pick<
CompileDirectivesOpts,
'directive' | 'directiveLabel' | 'getRuntimeCode' | 'replacer'
// | 'devSplitImporter'
>

const createDirectiveRx = (directive: string) =>
Expand Down Expand Up @@ -48,7 +49,6 @@ export function TanStackDirectiveFunctionsPlugin(
filename: id,
// globalThis.app currently refers to Vinxi's app instance. In the future, it can just be the
// vite dev server instance we get from Nitro.
devSplitImporter: `(globalThis.app.getRouter('server').internals.devServer.ssrLoadModule)`,
})

opts.onDirectiveFnsById?.(directiveFnsById)
Expand Down
12 changes: 6 additions & 6 deletions packages/directive-functions-plugin/tests/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const clientConfig: Omit<CompileDirectivesOpts, 'code'> = {
filename: ${JSON.stringify(opts.filename)},
functionId: ${JSON.stringify(opts.functionId)},
})`,
devSplitImporter: `devImport`,
// devSplitImporter: `devImport`,
}

const ssrConfig: Omit<CompileDirectivesOpts, 'code'> = {
Expand All @@ -28,7 +28,7 @@ const ssrConfig: Omit<CompileDirectivesOpts, 'code'> = {
filename: ${JSON.stringify(opts.filename)},
functionId: ${JSON.stringify(opts.functionId)},
})`,
devSplitImporter: `devImport`,
// devSplitImporter: `devImport`,
}

const serverConfig: Omit<CompileDirectivesOpts, 'code'> = {
Expand All @@ -49,7 +49,7 @@ const serverConfig: Omit<CompileDirectivesOpts, 'code'> = {
filename: ${JSON.stringify(opts.filename)},
functionId: ${JSON.stringify(opts.functionId)},
})`,
devSplitImporter: `devImport`,
// devSplitImporter: `devImport`,
}

describe('server function compilation', () => {
Expand Down Expand Up @@ -641,7 +641,7 @@ describe('server function compilation', () => {
});
outer(outer_useServer);
const outer_useServer_1 = createServerRpc({
fn: (...args) => devImport("test.ts?tsr-directive-use-server-split=outer_useServer_1").then(module => module.default(...args)),
fn: (...args) => import("test.ts?tsr-directive-use-server-split=outer_useServer_1").then(module => module.default(...args)),
filename: "test.ts",
functionId: "test_ts_tsr-directive-use-server-split_outer_useServer--outer_useServer_1"
});
Expand All @@ -653,7 +653,7 @@ describe('server function compilation', () => {
import { createServerRpc } from "my-rpc-lib-server";
const outer_useServer = createServerRpc({
fn: (...args) => devImport("test.ts?tsr-directive-use-server-split=outer_useServer").then(module => module.default(...args)),
fn: (...args) => import("test.ts?tsr-directive-use-server-split=outer_useServer").then(module => module.default(...args)),
filename: "test.ts",
functionId: "test_ts_tsr-directive-use-server-split_outer_useServer_1--outer_useServer"
});
Expand Down Expand Up @@ -878,7 +878,7 @@ describe('server function compilation', () => {
return 'hello from the server';
};
const myServerFn_createServerFn_handler = createServerRpc({
fn: (...args) => devImport("test.ts?tsr-directive-use-server-split=myServerFn_createServerFn_handler").then(module => module.default(...args)),
fn: (...args) => import("test.ts?tsr-directive-use-server-split=myServerFn_createServerFn_handler").then(module => module.default(...args)),
filename: "test.ts",
functionId: "test_ts_tsr-directive-use-server-split_myServerFn2_createServerFn_handler--myServerFn_createServerFn_handler"
});
Expand Down
Loading

0 comments on commit 497a83f

Please sign in to comment.