react-cosmos-plugin-vite 6.0.0-alpha.9 → 6.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
1
  {
2
2
  "name": "Vite",
3
- "devServer": "dist/viteDevServerPlugin.js",
4
- "export": "dist/viteExportPlugin.js"
3
+ "server": "dist/viteServerPlugin.js"
5
4
  }
@@ -0,0 +1 @@
1
+ export declare const RENDERER_FILENAME = "renderer.html";
@@ -0,0 +1 @@
1
+ export const RENDERER_FILENAME = 'renderer.html';
@@ -0,0 +1,10 @@
1
+ import { CosmosConfig } from 'react-cosmos';
2
+ export type CosmosViteConfig = {
3
+ configPath: string | false;
4
+ indexPath: string | null;
5
+ port: number;
6
+ };
7
+ type CosmosViteConfigInput = Partial<CosmosViteConfig>;
8
+ export declare function createCosmosViteConfig(cosmosConfig: CosmosConfig): CosmosViteConfig;
9
+ export declare function getCosmosVitePort(cosmosViteConfig: CosmosViteConfigInput): number;
10
+ export {};
@@ -0,0 +1,46 @@
1
+ import path from 'node:path';
2
+ import { fileExists, resolveFromSilent, resolveLoose, } from 'react-cosmos';
3
+ export function createCosmosViteConfig(cosmosConfig) {
4
+ const { rootDir } = cosmosConfig;
5
+ const configInput = cosmosConfig.vite || {};
6
+ const configPath = getViteConfigPath(configInput, rootDir);
7
+ logViteConfigInfo(configPath);
8
+ return {
9
+ configPath,
10
+ indexPath: configInput.indexPath
11
+ ? resolveLoose(rootDir, configInput.indexPath)
12
+ : null,
13
+ port: getCosmosVitePort(configInput),
14
+ };
15
+ }
16
+ export function getCosmosVitePort(cosmosViteConfig) {
17
+ const { port = 5050 } = cosmosViteConfig;
18
+ return port;
19
+ }
20
+ function getViteConfigPath({ configPath }, rootDir) {
21
+ // User can choose to prevent automatical use of an existing vite.config.js
22
+ // file (relative to the root dir) by setting configPath to false
23
+ if (configPath === false) {
24
+ return false;
25
+ }
26
+ if (typeof configPath == 'undefined') {
27
+ return (resolveFromSilent(rootDir, './vite.config.ts') ||
28
+ resolveFromSilent(rootDir, './vite.config.js') ||
29
+ false);
30
+ }
31
+ const absPath = resolveLoose(rootDir, configPath);
32
+ if (!fileExists(absPath)) {
33
+ const relPath = path.relative(process.cwd(), absPath);
34
+ throw new Error(`Vite config not found at path: ${relPath}`);
35
+ }
36
+ return absPath;
37
+ }
38
+ function logViteConfigInfo(viteConfigPath) {
39
+ if (viteConfigPath) {
40
+ const relPath = path.relative(process.cwd(), viteConfigPath);
41
+ console.log(`[Cosmos] Using vite config found at ${relPath}`);
42
+ }
43
+ else {
44
+ console.log(`[Cosmos] No vite config found, using default settings`);
45
+ }
46
+ }
@@ -1 +1 @@
1
- export declare function createViteRendererIndex(userDepsModuleId: string): string;
1
+ export declare function createViteRendererIndex(userImportsModuleId: string): string;
@@ -1,15 +1,18 @@
1
- export function createViteRendererIndex(userDepsModuleId) {
1
+ export function createViteRendererIndex(userImportsModuleId) {
2
2
  return `
3
- import { mountDomRenderer } from 'react-cosmos-dom';
3
+ import { mountDomRenderer } from 'react-cosmos-plugin-vite/dom';
4
4
 
5
5
  mount();
6
6
 
7
7
  async function mount() {
8
8
  // Use dynamic import to reload updated modules upon hot reloading
9
- const args = await import('${userDepsModuleId}');
9
+ const args = await import('${userImportsModuleId}');
10
10
  mountDomRenderer(args);
11
11
  }
12
12
 
13
+ // Fallback for when React Refresh isn't enabled.
14
+ // Under normal conditions accepting HMR updates here isn't necessary because
15
+ // individual React modules will be hot reloaded by React Refresh.
13
16
  if (import.meta.hot) {
14
17
  import.meta.hot.accept(() => {
15
18
  mount();
package/dist/dom.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from 'react-cosmos-dom';
package/dist/dom.js ADDED
@@ -0,0 +1 @@
1
+ export * from 'react-cosmos-dom';
@@ -1,5 +1,6 @@
1
- import { CosmosConfig } from 'react-cosmos/server.js';
1
+ import { CosmosConfig } from 'react-cosmos';
2
2
  import { Plugin } from 'rollup';
3
- export declare const userDepsVirtualModuleId = "virtual:cosmos-userdeps";
4
- export declare const userDepsResolvedModuleId: string;
5
- export declare function reactCosmosViteRollupPlugin(cosmosConfig: CosmosConfig): Plugin;
3
+ import { CosmosViteConfig } from './createCosmosViteConfig.js';
4
+ export declare const userImportsVirtualModuleId = "virtual:cosmos-imports";
5
+ export declare const userImportsResolvedModuleId: string;
6
+ export declare function reactCosmosViteRollupPlugin(cosmosConfig: CosmosConfig, cosmosViteConfig: CosmosViteConfig): Plugin;
@@ -1,29 +1,30 @@
1
- import path from 'path';
2
- import { generateUserDepsModule, getPlaygroundUrl, } from 'react-cosmos/server.js';
1
+ import path from 'node:path';
2
+ import { generateUserImports, getPlaygroundUrl, } from 'react-cosmos';
3
3
  import { createViteRendererIndex } from './createViteRendererIndex.js';
4
- export const userDepsVirtualModuleId = 'virtual:cosmos-userdeps';
5
- export const userDepsResolvedModuleId = '\0' + userDepsVirtualModuleId;
6
- const defaultIndexPattern = /^index\.(js|ts)x?$/;
7
- export function reactCosmosViteRollupPlugin(cosmosConfig) {
4
+ export const userImportsVirtualModuleId = 'virtual:cosmos-imports';
5
+ export const userImportsResolvedModuleId = '\0' + userImportsVirtualModuleId;
6
+ const defaultIndexPattern = new RegExp(`^(src\\${path.sep})?(index|main)\.(js|ts)x?$`);
7
+ export function reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig) {
8
8
  return {
9
9
  name: 'react-cosmos-vite-renderer',
10
10
  resolveId(id) {
11
- if (id === userDepsVirtualModuleId) {
12
- return userDepsResolvedModuleId;
11
+ if (id === userImportsVirtualModuleId) {
12
+ return userImportsResolvedModuleId;
13
13
  }
14
14
  else {
15
15
  return null;
16
16
  }
17
17
  },
18
18
  load(id) {
19
- if (id == userDepsResolvedModuleId) {
20
- return generateUserDepsModule({
19
+ if (id == userImportsResolvedModuleId) {
20
+ return generateUserImports({
21
21
  cosmosConfig,
22
22
  rendererConfig: {
23
23
  playgroundUrl: getPlaygroundUrl(cosmosConfig),
24
- // TODO: Allow passing dom.containerQuerySelector
24
+ containerQuerySelector: cosmosConfig.dom.containerQuerySelector,
25
25
  },
26
26
  relativeToDir: null,
27
+ typeScript: false,
27
28
  });
28
29
  }
29
30
  else {
@@ -31,10 +32,14 @@ export function reactCosmosViteRollupPlugin(cosmosConfig) {
31
32
  }
32
33
  },
33
34
  transform(src, id) {
34
- // TODO: Allow indexFile customization via cosmosConfig.vite.indexFile
35
- if (path.relative(cosmosConfig.rootDir, id).match(defaultIndexPattern)) {
35
+ const isRendererIndex = cosmosViteConfig.indexPath
36
+ ? cosmosViteConfig.indexPath === id
37
+ : path.relative(cosmosConfig.rootDir, id).match(defaultIndexPattern);
38
+ if (isRendererIndex) {
39
+ const relPath = path.relative(process.cwd(), id);
40
+ console.log(`[Cosmos] Replacing vite index module at ${relPath}`);
36
41
  return {
37
- code: createViteRendererIndex(userDepsVirtualModuleId),
42
+ code: createViteRendererIndex(userImportsVirtualModuleId),
38
43
  map: null,
39
44
  };
40
45
  }
@@ -0,0 +1,2 @@
1
+ import { CosmosConfig, CosmosConfigPluginArgs } from 'react-cosmos';
2
+ export declare function viteConfigPlugin({ cosmosConfig, command, }: CosmosConfigPluginArgs): Promise<CosmosConfig>;
@@ -0,0 +1,25 @@
1
+ import path from 'node:path';
2
+ import { findNextAvailablePort, } from 'react-cosmos';
3
+ import { RENDERER_FILENAME } from './constants.js';
4
+ import { getCosmosVitePort } from './createCosmosViteConfig.js';
5
+ export async function viteConfigPlugin({ cosmosConfig, command, }) {
6
+ if (cosmosConfig.rendererUrl) {
7
+ return cosmosConfig;
8
+ }
9
+ if (command === 'export') {
10
+ return {
11
+ ...cosmosConfig,
12
+ rendererUrl: path.join(cosmosConfig.publicUrl, RENDERER_FILENAME),
13
+ };
14
+ }
15
+ const cosmosViteConfig = cosmosConfig.vite || {};
16
+ const port = await findNextAvailablePort(getCosmosVitePort(cosmosViteConfig), cosmosConfig.portRetries);
17
+ return {
18
+ ...cosmosConfig,
19
+ rendererUrl: `http://localhost:${port}`,
20
+ vite: {
21
+ ...cosmosViteConfig,
22
+ port: port,
23
+ },
24
+ };
25
+ }
@@ -1,2 +1,2 @@
1
- import { DevServerPluginArgs } from 'react-cosmos/server.js';
2
- export default function viteDevServerPlugin({ platformType, cosmosConfig, }: DevServerPluginArgs): Promise<(() => void) | undefined>;
1
+ import { DevServerPluginArgs } from 'react-cosmos';
2
+ export declare function viteDevServerPlugin({ cosmosConfig, platform, }: DevServerPluginArgs): Promise<(() => void) | undefined>;
@@ -1,29 +1,35 @@
1
- import { startFixtureWatcher, } from 'react-cosmos/server.js';
1
+ import { startFixtureWatcher } from 'react-cosmos';
2
+ import { pickRendererUrl } from 'react-cosmos-core';
2
3
  import { createServer } from 'vite';
3
- import { reactCosmosViteRollupPlugin, userDepsResolvedModuleId, } from './reactCosmosViteRollupPlugin.js';
4
- export default async function viteDevServerPlugin({ platformType, cosmosConfig, }) {
5
- if (platformType !== 'web')
4
+ import { createCosmosViteConfig } from './createCosmosViteConfig.js';
5
+ import { reactCosmosViteRollupPlugin, userImportsResolvedModuleId, } from './reactCosmosViteRollupPlugin.js';
6
+ export async function viteDevServerPlugin({ cosmosConfig, platform, }) {
7
+ if (platform !== 'web')
6
8
  return;
7
- const { rendererUrl } = cosmosConfig;
9
+ const rendererUrl = pickRendererUrl(cosmosConfig.rendererUrl, 'dev');
8
10
  if (!rendererUrl) {
9
- throw new Error('Vite plugin requires cosmosConfig.rendererUrl to be set (eg. "http://localhost:5050")');
11
+ throw new Error('Vite plugin requires cosmosConfig.rendererUrl to be set');
10
12
  }
13
+ const { rootDir } = cosmosConfig;
14
+ const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
11
15
  const server = await createServer({
12
- configFile: false,
13
- root: cosmosConfig.rootDir,
16
+ // Last time I checked the config file is merged with this inline config:
17
+ // https://github.com/vitejs/vite/blob/07bd6d14e545d05c6a29cf341f117fcfe9536ba4/packages/vite/src/node/config.ts#L418
18
+ configFile: cosmosViteConfig.configPath,
19
+ root: rootDir,
20
+ base: cosmosConfig.publicUrl,
14
21
  server: {
15
22
  // https://vitejs.dev/config/server-options.html#server-host
16
23
  host: '0.0.0.0',
17
24
  port: parseInt(new URL(rendererUrl).port, 10),
18
25
  },
19
- plugins: [reactCosmosViteRollupPlugin(cosmosConfig)],
26
+ plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
20
27
  });
21
28
  await server.listen();
22
- server.printUrls();
23
29
  const watcher = await startFixtureWatcher(cosmosConfig, 'add', () => {
24
- const module = server.moduleGraph.getModuleById(userDepsResolvedModuleId);
30
+ const module = server.moduleGraph.getModuleById(userImportsResolvedModuleId);
25
31
  if (!module) {
26
- throw new Error(`Vite module graph doesn't contain module with id ${userDepsResolvedModuleId}`);
32
+ throw new Error(`Vite module graph doesn't contain module with id ${userImportsResolvedModuleId}`);
27
33
  }
28
34
  server.moduleGraph.invalidateModule(module);
29
35
  server.reloadModule(module);
@@ -1,2 +1,2 @@
1
- import { ExportPluginArgs } from 'react-cosmos/server.js';
2
- export default function viteExportPlugin({ cosmosConfig, }: ExportPluginArgs): Promise<void>;
1
+ import { ExportPluginArgs } from 'react-cosmos';
2
+ export declare function viteExportPlugin({ cosmosConfig }: ExportPluginArgs): Promise<void>;
@@ -1,22 +1,23 @@
1
1
  import { rename } from 'node:fs/promises';
2
- import path from 'path';
3
- import { removeLeadingSlash } from 'react-cosmos-core';
4
- import { RENDERER_FILENAME } from 'react-cosmos/server.js';
2
+ import path from 'node:path';
5
3
  import { build } from 'vite';
4
+ import { RENDERER_FILENAME } from './constants.js';
5
+ import { createCosmosViteConfig } from './createCosmosViteConfig.js';
6
6
  import { reactCosmosViteRollupPlugin } from './reactCosmosViteRollupPlugin.js';
7
- export default async function viteExportPlugin({ cosmosConfig, }) {
8
- const { exportPath, publicUrl } = cosmosConfig;
7
+ export async function viteExportPlugin({ cosmosConfig }) {
8
+ const { rootDir, exportPath, publicUrl } = cosmosConfig;
9
+ const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
10
+ const outDir = path.join(exportPath, publicUrl);
9
11
  await build({
10
- configFile: false,
11
- root: cosmosConfig.rootDir,
12
+ configFile: cosmosViteConfig.configPath,
13
+ root: rootDir,
12
14
  base: publicUrl,
13
15
  build: {
14
- outDir: path.resolve(exportPath, removeLeadingSlash(publicUrl)),
16
+ outDir,
15
17
  emptyOutDir: false,
16
18
  minify: false,
17
19
  },
18
- plugins: [reactCosmosViteRollupPlugin(cosmosConfig)],
20
+ plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
19
21
  });
20
- // Make way for the Playground's index.html
21
- await rename(path.resolve(exportPath, 'index.html'), path.resolve(exportPath, RENDERER_FILENAME));
22
+ await rename(path.join(outDir, 'index.html'), path.join(outDir, RENDERER_FILENAME));
22
23
  }
@@ -0,0 +1,3 @@
1
+ import { CosmosServerPlugin } from 'react-cosmos';
2
+ declare const viteServerPlugin: CosmosServerPlugin;
3
+ export default viteServerPlugin;
@@ -0,0 +1,10 @@
1
+ import { viteConfigPlugin } from './viteConfigPlugin.js';
2
+ import { viteDevServerPlugin } from './viteDevServerPlugin.js';
3
+ import { viteExportPlugin } from './viteExportPlugin.js';
4
+ const viteServerPlugin = {
5
+ name: 'vite',
6
+ config: viteConfigPlugin,
7
+ devServer: viteDevServerPlugin,
8
+ export: viteExportPlugin,
9
+ };
10
+ export default viteServerPlugin;
package/dom.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/dom.js';
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "react-cosmos-plugin-vite",
3
- "version": "6.0.0-alpha.9",
3
+ "version": "6.0.0-beta.2",
4
4
  "description": "Vite plugin for React Cosmos",
5
5
  "repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-plugin-vite",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "cosmos.plugin.json",
9
9
  "dependencies": {
10
- "react-cosmos-core": "^6.0.0-alpha.9",
11
- "react-cosmos-dom": "^6.0.0-alpha.9"
10
+ "react-cosmos-core": "6.0.0-beta.2",
11
+ "react-cosmos-dom": "6.0.0-beta.2"
12
12
  },
13
13
  "peerDependencies": {
14
14
  "vite": "*"
15
15
  },
16
- "gitHead": "b155b223f904f5cc29e2f13ac79ab7c4b302ed2b"
16
+ "gitHead": "f3e3893b0439132e083228f312c62e4379f39762"
17
17
  }