Skip to content

Commit

Permalink
fix: Resolution of runtime modules when transitive (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Aug 19, 2024
1 parent 8fe2620 commit f84ff80
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/witty-sloths-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@inox-tools/request-state': patch
'@inox-tools/request-nanostores': patch
---

Fixes resolution of runtime modules when integration is a transitive dependency with a strict package manager.
3 changes: 0 additions & 3 deletions packages/request-nanostores/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./runtime/*": {
"default": "./dist/runtime/*.js"
}
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/request-nanostores/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from 'astro-integration-kit';
import { plugin } from './plugin.js';
import requestState from '@inox-tools/request-state';
import { z } from 'astro/zod';

export default defineIntegration({
name: '@inox-tools/request-nanostores',
Expand All @@ -15,6 +14,7 @@ export default defineIntegration({
hooks: {
'astro:config:setup': (params) => {
if (!hasIntegration(params, { name: '@inox-tools/request-state' })) {
params.logger.debug('Adding request-state integration');
addIntegration(params, {
ensureUnique: true,
integration: requestState(),
Expand Down
8 changes: 6 additions & 2 deletions packages/request-nanostores/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createResolver } from 'astro-integration-kit';
import type { Plugin } from 'vite';

const MODULE_ID = '@it-astro:request-nanostores';
const RESOLVED_MODULE_ID = `\x00${MODULE_ID}`;

export const plugin = (): Plugin => {
const { resolve } = createResolver(import.meta.url);

return {
name: '@inox-tools/request-nanostores/plugin',
resolveId(id) {
Expand All @@ -12,10 +15,11 @@ export const plugin = (): Plugin => {
load(id, options = {}) {
if (id !== RESOLVED_MODULE_ID) return;

const source = options.ssr ? 'server' : 'client';
const source = options.ssr ? 'server.js' : 'client.js';
const importPath = resolve('runtime', source);

return `
export { shared } from '@inox-tools/request-nanostores/runtime/${source}';
export { shared } from '${importPath}';
`.trim();
},
};
Expand Down
8 changes: 0 additions & 8 deletions packages/request-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./events": {
"types": "./dist/events.d.ts",
"default": "./dist/events.js"
},
"./runtime/*": {
"types": "./dist/runtime/*.d.ts",
"default": "./dist/runtime/*.js"
}
},
"files": [
Expand Down
9 changes: 7 additions & 2 deletions packages/request-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { defineIntegration, addVitePlugin } from 'astro-integration-kit';
import { defineIntegration, addVitePlugin, createResolver } from 'astro-integration-kit';
import { plugin } from './plugin.js';

export default defineIntegration({
name: '@inox-tools/request-state',
setup() {
const { resolve } = createResolver(import.meta.url);

return {
hooks: {
'astro:config:setup': (params) => {
const { addMiddleware } = params;

params.logger.debug('Adding request-state middleware');
addMiddleware({
order: 'pre',
entrypoint: '@inox-tools/request-state/runtime/middleware',
entrypoint: resolve('runtime/middleware.js'),
});

params.logger.debug('Adding request-state virtual module');
addVitePlugin(params, {
warnDuplicated: true,
plugin: plugin(),
Expand Down
11 changes: 9 additions & 2 deletions packages/request-state/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createResolver } from 'astro-integration-kit';
import type { Plugin } from 'vite';

const MODULE_ID = '@it-astro:state';
const RESOLVED_MODULE_ID = `\x00${MODULE_ID}`;

export const plugin = (): Plugin => {
const { resolve } = createResolver(import.meta.url);

return {
name: '@inox-tools/request-state/vite-plugin',
resolveId(id) {
Expand All @@ -14,9 +17,13 @@ export const plugin = (): Plugin => {
load(id, options) {
if (id !== RESOLVED_MODULE_ID) return;

const stateSource = options?.ssr ? 'serverState' : 'clientState';
const stateSource = options?.ssr ? 'serverState.js' : 'clientState.js';
const importPath = resolve('runtime', stateSource);

return `export {setState, getState} from '@inox-tools/request-state/runtime/${stateSource}';`;
return `
export {setState, getState} from '${importPath}';
export {ServerStateLoaded} from '${resolve('events.js')}';
`.trim();
},
};
};
2 changes: 1 addition & 1 deletion packages/request-state/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
target: 'node18',
bundle: false,
dts: {
entry: ['src/index.ts', 'src/events.ts'],
entry: ['src/index.ts'],
banner: '/// <reference path="../virtual.d.ts" />\n',
},
sourcemap: true,
Expand Down
4 changes: 3 additions & 1 deletion packages/request-state/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
declare module '@it-astro:state' {
export const getState: (key: string, valueIfMissing?: unknown) => unknown;
export const setState: (key: string, value: unknown) => void;

export { ServerStateLoaded } from './src/events.js';
}

declare global {
interface DocumentEventMap {
[ServerStateLoaded.NAME]: import('@inox-tools/request-state/events').ServerStateLoaded;
[ServerStateLoaded.NAME]: import('./src/events.js').ServerStateLoaded;
}
}
4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"outputs": ["dist/**"],
"outputLogs": "new-only"
},
"preview": {
"dependsOn": ["build"],
"outputLogs": "new-only"
},
"dev": {
"cache": false,
"persistent": true
Expand Down

0 comments on commit f84ff80

Please sign in to comment.