react-cosmos-plugin-vite 6.0.0-canary.fe0c7d1.0 → 6.0.0

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.
@@ -0,0 +1 @@
1
+ export declare const RENDERER_FILENAME = "renderer.html";
@@ -0,0 +1 @@
1
+ export const RENDERER_FILENAME = 'renderer.html';
@@ -1,5 +1,5 @@
1
1
  import path from 'node:path';
2
- import { fileExists, moduleExists, resolveLoose, } from 'react-cosmos';
2
+ import { fileExists, resolveFromSilent, resolveLoose, } from 'react-cosmos';
3
3
  export function createCosmosViteConfig(cosmosConfig) {
4
4
  const { rootDir } = cosmosConfig;
5
5
  const configInput = cosmosConfig.vite || {};
@@ -24,8 +24,9 @@ function getViteConfigPath({ configPath }, rootDir) {
24
24
  return false;
25
25
  }
26
26
  if (typeof configPath == 'undefined') {
27
- const defaultAbsPath = resolveLoose(rootDir, 'vite.config.js');
28
- return moduleExists(defaultAbsPath) ? defaultAbsPath : false;
27
+ return (resolveFromSilent(rootDir, './vite.config.ts') ||
28
+ resolveFromSilent(rootDir, './vite.config.js') ||
29
+ false);
29
30
  }
30
31
  const absPath = resolveLoose(rootDir, configPath);
31
32
  if (!fileExists(absPath)) {
@@ -1 +1 @@
1
- export declare function createViteRendererIndex(userDepsModuleId: string): string;
1
+ export declare function createViteRendererIndex(userImportsModuleId: string): string;
@@ -1,12 +1,12 @@
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
 
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,6 +1,6 @@
1
1
  import { CosmosConfig } from 'react-cosmos';
2
2
  import { Plugin } from 'rollup';
3
3
  import { CosmosViteConfig } from './createCosmosViteConfig.js';
4
- export declare const userDepsVirtualModuleId = "virtual:cosmos-userdeps";
5
- export declare const userDepsResolvedModuleId: string;
4
+ export declare const userImportsVirtualModuleId = "virtual:cosmos-imports";
5
+ export declare const userImportsResolvedModuleId: string;
6
6
  export declare function reactCosmosViteRollupPlugin(cosmosConfig: CosmosConfig, cosmosViteConfig: CosmosViteConfig): Plugin;
@@ -1,29 +1,32 @@
1
1
  import path from 'node:path';
2
- import { generateUserDepsModule, getPlaygroundUrl, } from 'react-cosmos';
2
+ import { findUserModulePaths, generateUserImports, getPlaygroundUrl, slash, } from 'react-cosmos';
3
3
  import { createViteRendererIndex } from './createViteRendererIndex.js';
4
- export const userDepsVirtualModuleId = 'virtual:cosmos-userdeps';
5
- export const userDepsResolvedModuleId = '\0' + userDepsVirtualModuleId;
4
+ export const userImportsVirtualModuleId = 'virtual:cosmos-imports';
5
+ export const userImportsResolvedModuleId = '\0' + userImportsVirtualModuleId;
6
6
  const defaultIndexPattern = new RegExp(`^(src\\${path.sep})?(index|main)\.(js|ts)x?$`);
7
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
+ const modulePaths = findUserModulePaths(cosmosConfig);
21
+ return generateUserImports({
21
22
  cosmosConfig,
23
+ modulePaths,
22
24
  rendererConfig: {
23
25
  playgroundUrl: getPlaygroundUrl(cosmosConfig),
24
26
  containerQuerySelector: cosmosConfig.dom.containerQuerySelector,
25
27
  },
26
28
  relativeToDir: null,
29
+ typeScript: false,
27
30
  });
28
31
  }
29
32
  else {
@@ -31,14 +34,16 @@ export function reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig) {
31
34
  }
32
35
  },
33
36
  transform(src, id) {
34
- const isRendererIndex = cosmosViteConfig.indexPath
35
- ? cosmosViteConfig.indexPath === id
37
+ const absPath = cosmosViteConfig.indexPath &&
38
+ absoluteIndexPath(cosmosViteConfig.indexPath, cosmosConfig.rootDir);
39
+ const isRendererIndex = absPath
40
+ ? absPath === id
36
41
  : path.relative(cosmosConfig.rootDir, id).match(defaultIndexPattern);
37
42
  if (isRendererIndex) {
38
43
  const relPath = path.relative(process.cwd(), id);
39
44
  console.log(`[Cosmos] Replacing vite index module at ${relPath}`);
40
45
  return {
41
- code: createViteRendererIndex(userDepsVirtualModuleId),
46
+ code: createViteRendererIndex(userImportsVirtualModuleId),
42
47
  map: null,
43
48
  };
44
49
  }
@@ -48,3 +53,8 @@ export function reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig) {
48
53
  },
49
54
  };
50
55
  }
56
+ function absoluteIndexPath(indexPath, rootDir) {
57
+ return indexPath.startsWith(rootDir)
58
+ ? indexPath
59
+ : slash(path.join(rootDir, indexPath));
60
+ }
@@ -1,2 +1,2 @@
1
1
  import { CosmosConfig, CosmosConfigPluginArgs } from 'react-cosmos';
2
- export declare function viteConfigPlugin({ cosmosConfig, }: CosmosConfigPluginArgs): Promise<CosmosConfig>;
2
+ export declare function viteConfigPlugin({ cosmosConfig, command, }: CosmosConfigPluginArgs): Promise<CosmosConfig>;
@@ -1,10 +1,17 @@
1
+ import path from 'node:path';
1
2
  import { findNextAvailablePort, } from 'react-cosmos';
3
+ import { RENDERER_FILENAME } from './constants.js';
2
4
  import { getCosmosVitePort } from './createCosmosViteConfig.js';
3
- export async function viteConfigPlugin({ cosmosConfig, }) {
4
- const { rendererUrl } = cosmosConfig;
5
- if (rendererUrl) {
5
+ export async function viteConfigPlugin({ cosmosConfig, command, }) {
6
+ if (cosmosConfig.rendererUrl) {
6
7
  return cosmosConfig;
7
8
  }
9
+ if (command === 'export') {
10
+ return {
11
+ ...cosmosConfig,
12
+ rendererUrl: path.join(cosmosConfig.publicUrl, RENDERER_FILENAME),
13
+ };
14
+ }
8
15
  const cosmosViteConfig = cosmosConfig.vite || {};
9
16
  const port = await findNextAvailablePort(getCosmosVitePort(cosmosViteConfig), cosmosConfig.portRetries);
10
17
  return {
@@ -1,2 +1,2 @@
1
1
  import { DevServerPluginArgs } from 'react-cosmos';
2
- export declare function viteDevServerPlugin({ platformType, cosmosConfig, }: DevServerPluginArgs): Promise<(() => void) | undefined>;
2
+ export declare function viteDevServerPlugin({ cosmosConfig, platform, }: DevServerPluginArgs): Promise<(() => void) | undefined>;
@@ -1,13 +1,14 @@
1
1
  import { startFixtureWatcher } from 'react-cosmos';
2
+ import { pickRendererUrl } from 'react-cosmos-core';
2
3
  import { createServer } from 'vite';
3
4
  import { createCosmosViteConfig } from './createCosmosViteConfig.js';
4
- import { reactCosmosViteRollupPlugin, userDepsResolvedModuleId, } from './reactCosmosViteRollupPlugin.js';
5
- export async function viteDevServerPlugin({ platformType, cosmosConfig, }) {
6
- if (platformType !== 'web')
5
+ import { reactCosmosViteRollupPlugin, userImportsResolvedModuleId, } from './reactCosmosViteRollupPlugin.js';
6
+ export async function viteDevServerPlugin({ cosmosConfig, platform, }) {
7
+ if (platform !== 'web')
7
8
  return;
8
- const { rendererUrl } = cosmosConfig;
9
+ const rendererUrl = pickRendererUrl(cosmosConfig.rendererUrl, 'dev');
9
10
  if (!rendererUrl) {
10
- 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');
11
12
  }
12
13
  const { rootDir } = cosmosConfig;
13
14
  const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
@@ -16,18 +17,22 @@ export async function viteDevServerPlugin({ platformType, cosmosConfig, }) {
16
17
  // https://github.com/vitejs/vite/blob/07bd6d14e545d05c6a29cf341f117fcfe9536ba4/packages/vite/src/node/config.ts#L418
17
18
  configFile: cosmosViteConfig.configPath,
18
19
  root: rootDir,
20
+ base: cosmosConfig.publicUrl,
19
21
  server: {
20
22
  // https://vitejs.dev/config/server-options.html#server-host
21
23
  host: '0.0.0.0',
22
24
  port: parseInt(new URL(rendererUrl).port, 10),
25
+ // Prevent auto opening Cosmos renderer in browser when user has this
26
+ // option enabled in their Vite config
27
+ open: false,
23
28
  },
24
29
  plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
25
30
  });
26
31
  await server.listen();
27
32
  const watcher = await startFixtureWatcher(cosmosConfig, 'add', () => {
28
- const module = server.moduleGraph.getModuleById(userDepsResolvedModuleId);
33
+ const module = server.moduleGraph.getModuleById(userImportsResolvedModuleId);
29
34
  if (!module) {
30
- throw new Error(`Vite module graph doesn't contain module with id ${userDepsResolvedModuleId}`);
35
+ throw new Error(`Vite module graph doesn't contain module with id ${userImportsResolvedModuleId}`);
31
36
  }
32
37
  server.moduleGraph.invalidateModule(module);
33
38
  server.reloadModule(module);
@@ -1,24 +1,23 @@
1
1
  import { rename } from 'node:fs/promises';
2
- import path from 'path';
3
- import { RENDERER_FILENAME } from 'react-cosmos';
4
- import { removeLeadingSlash } from 'react-cosmos-core';
2
+ import path from 'node:path';
5
3
  import { build } from 'vite';
4
+ import { RENDERER_FILENAME } from './constants.js';
6
5
  import { createCosmosViteConfig } from './createCosmosViteConfig.js';
7
6
  import { reactCosmosViteRollupPlugin } from './reactCosmosViteRollupPlugin.js';
8
7
  export async function viteExportPlugin({ cosmosConfig }) {
9
8
  const { rootDir, exportPath, publicUrl } = cosmosConfig;
10
9
  const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
10
+ const outDir = path.join(exportPath, publicUrl);
11
11
  await build({
12
12
  configFile: cosmosViteConfig.configPath,
13
13
  root: rootDir,
14
14
  base: publicUrl,
15
15
  build: {
16
- outDir: path.resolve(exportPath, removeLeadingSlash(publicUrl)),
16
+ outDir,
17
17
  emptyOutDir: false,
18
18
  minify: false,
19
19
  },
20
20
  plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
21
21
  });
22
- // Make way for the Playground's index.html
23
- 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));
24
23
  }
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-canary.fe0c7d1.0+fe0c7d1",
3
+ "version": "6.0.0",
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-canary.fe0c7d1.0+fe0c7d1",
11
- "react-cosmos-dom": "6.0.0-canary.fe0c7d1.0+fe0c7d1"
10
+ "react-cosmos-core": "^6.0.0",
11
+ "react-cosmos-dom": "^6.0.0"
12
12
  },
13
13
  "peerDependencies": {
14
14
  "vite": "*"
15
15
  },
16
- "gitHead": "fe0c7d1b5b90f48d6109bc18a2750ab2a347d016"
16
+ "gitHead": "db4fd8fd917598f1f16d4c291be76217436422ee"
17
17
  }