react-cosmos-plugin-vite 6.0.0-alpha.13 → 6.0.0-alpha.15

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,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,45 @@
1
+ import path from 'node:path';
2
+ import { fileExists, moduleExists, 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
+ const defaultAbsPath = resolveLoose(rootDir, 'vite.config.js');
28
+ return moduleExists(defaultAbsPath) ? defaultAbsPath : false;
29
+ }
30
+ const absPath = resolveLoose(rootDir, configPath);
31
+ if (!fileExists(absPath)) {
32
+ const relPath = path.relative(process.cwd(), absPath);
33
+ throw new Error(`Vite config not found at path: ${relPath}`);
34
+ }
35
+ return absPath;
36
+ }
37
+ function logViteConfigInfo(viteConfigPath) {
38
+ if (viteConfigPath) {
39
+ const relPath = path.relative(process.cwd(), viteConfigPath);
40
+ console.log(`[Cosmos] Using vite config found at ${relPath}`);
41
+ }
42
+ else {
43
+ console.log(`[Cosmos] No vite config found, using default settings`);
44
+ }
45
+ }
@@ -1,5 +1,6 @@
1
1
  import { CosmosConfig } from 'react-cosmos';
2
2
  import { Plugin } from 'rollup';
3
+ import { CosmosViteConfig } from './createCosmosViteConfig.js';
3
4
  export declare const userDepsVirtualModuleId = "virtual:cosmos-userdeps";
4
5
  export declare const userDepsResolvedModuleId: string;
5
- export declare function reactCosmosViteRollupPlugin(cosmosConfig: CosmosConfig): Plugin;
6
+ export declare function reactCosmosViteRollupPlugin(cosmosConfig: CosmosConfig, cosmosViteConfig: CosmosViteConfig): Plugin;
@@ -1,10 +1,10 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
  import { generateUserDepsModule, getPlaygroundUrl, } from 'react-cosmos';
3
3
  import { createViteRendererIndex } from './createViteRendererIndex.js';
4
4
  export const userDepsVirtualModuleId = 'virtual:cosmos-userdeps';
5
5
  export const userDepsResolvedModuleId = '\0' + userDepsVirtualModuleId;
6
- const defaultIndexPattern = /^index\.(js|ts)x?$/;
7
- export function reactCosmosViteRollupPlugin(cosmosConfig) {
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) {
@@ -21,7 +21,7 @@ export function reactCosmosViteRollupPlugin(cosmosConfig) {
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
27
  });
@@ -31,8 +31,12 @@ export function reactCosmosViteRollupPlugin(cosmosConfig) {
31
31
  }
32
32
  },
33
33
  transform(src, id) {
34
- // TODO: Allow indexFile customization via cosmosConfig.vite.indexFile
35
- if (path.relative(cosmosConfig.rootDir, id).match(defaultIndexPattern)) {
34
+ const isRendererIndex = cosmosViteConfig.indexPath
35
+ ? cosmosViteConfig.indexPath === id
36
+ : path.relative(cosmosConfig.rootDir, id).match(defaultIndexPattern);
37
+ if (isRendererIndex) {
38
+ const relPath = path.relative(process.cwd(), id);
39
+ console.log(`[Cosmos] Replacing vite index module at ${relPath}`);
36
40
  return {
37
41
  code: createViteRendererIndex(userDepsVirtualModuleId),
38
42
  map: null,
@@ -1,17 +1,17 @@
1
1
  import { findNextAvailablePort, } from 'react-cosmos';
2
- import { createViteCosmosConfig } from './createViteCosmosConfig.js';
2
+ import { getCosmosVitePort } from './createCosmosViteConfig.js';
3
3
  export async function viteConfigPlugin({ cosmosConfig, }) {
4
4
  const { rendererUrl } = cosmosConfig;
5
5
  if (rendererUrl) {
6
6
  return cosmosConfig;
7
7
  }
8
- const viteCosmosConfig = createViteCosmosConfig(cosmosConfig);
9
- const port = await findNextAvailablePort(viteCosmosConfig.port, cosmosConfig.portRetries);
8
+ const cosmosViteConfig = cosmosConfig.vite || {};
9
+ const port = await findNextAvailablePort(getCosmosVitePort(cosmosViteConfig), cosmosConfig.portRetries);
10
10
  return {
11
11
  ...cosmosConfig,
12
12
  rendererUrl: `http://localhost:${port}`,
13
13
  vite: {
14
- ...viteCosmosConfig,
14
+ ...cosmosViteConfig,
15
15
  port: port,
16
16
  },
17
17
  };
@@ -1,6 +1,6 @@
1
- import viteReactPlugin from '@vitejs/plugin-react';
2
1
  import { startFixtureWatcher } from 'react-cosmos';
3
2
  import { createServer } from 'vite';
3
+ import { createCosmosViteConfig } from './createCosmosViteConfig.js';
4
4
  import { reactCosmosViteRollupPlugin, userDepsResolvedModuleId, } from './reactCosmosViteRollupPlugin.js';
5
5
  export async function viteDevServerPlugin({ platformType, cosmosConfig, }) {
6
6
  if (platformType !== 'web')
@@ -9,18 +9,21 @@ export async function viteDevServerPlugin({ platformType, cosmosConfig, }) {
9
9
  if (!rendererUrl) {
10
10
  throw new Error('Vite plugin requires cosmosConfig.rendererUrl to be set (eg. "http://localhost:5050")');
11
11
  }
12
+ const { rootDir } = cosmosConfig;
13
+ const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
12
14
  const server = await createServer({
13
- configFile: false,
14
- root: cosmosConfig.rootDir,
15
+ // Last time I checked the config file is merged with this inline config:
16
+ // https://github.com/vitejs/vite/blob/07bd6d14e545d05c6a29cf341f117fcfe9536ba4/packages/vite/src/node/config.ts#L418
17
+ configFile: cosmosViteConfig.configPath,
18
+ root: rootDir,
15
19
  server: {
16
20
  // https://vitejs.dev/config/server-options.html#server-host
17
21
  host: '0.0.0.0',
18
22
  port: parseInt(new URL(rendererUrl).port, 10),
19
23
  },
20
- plugins: [viteReactPlugin(), reactCosmosViteRollupPlugin(cosmosConfig)],
24
+ plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
21
25
  });
22
26
  await server.listen();
23
- server.printUrls();
24
27
  const watcher = await startFixtureWatcher(cosmosConfig, 'add', () => {
25
28
  const module = server.moduleGraph.getModuleById(userDepsResolvedModuleId);
26
29
  if (!module) {
@@ -1,22 +1,23 @@
1
- import viteReactPlugin from '@vitejs/plugin-react';
2
1
  import { rename } from 'node:fs/promises';
3
2
  import path from 'path';
4
3
  import { RENDERER_FILENAME } from 'react-cosmos';
5
4
  import { removeLeadingSlash } from 'react-cosmos-core';
6
5
  import { build } from 'vite';
6
+ import { createCosmosViteConfig } from './createCosmosViteConfig.js';
7
7
  import { reactCosmosViteRollupPlugin } from './reactCosmosViteRollupPlugin.js';
8
8
  export async function viteExportPlugin({ cosmosConfig }) {
9
- const { exportPath, publicUrl } = cosmosConfig;
9
+ const { rootDir, exportPath, publicUrl } = cosmosConfig;
10
+ const cosmosViteConfig = createCosmosViteConfig(cosmosConfig);
10
11
  await build({
11
- configFile: false,
12
- root: cosmosConfig.rootDir,
12
+ configFile: cosmosViteConfig.configPath,
13
+ root: rootDir,
13
14
  base: publicUrl,
14
15
  build: {
15
16
  outDir: path.resolve(exportPath, removeLeadingSlash(publicUrl)),
16
17
  emptyOutDir: false,
17
18
  minify: false,
18
19
  },
19
- plugins: [viteReactPlugin(), reactCosmosViteRollupPlugin(cosmosConfig)],
20
+ plugins: [reactCosmosViteRollupPlugin(cosmosConfig, cosmosViteConfig)],
20
21
  });
21
22
  // Make way for the Playground's index.html
22
23
  await rename(path.resolve(exportPath, 'index.html'), path.resolve(exportPath, RENDERER_FILENAME));
package/package.json CHANGED
@@ -1,18 +1,17 @@
1
1
  {
2
2
  "name": "react-cosmos-plugin-vite",
3
- "version": "6.0.0-alpha.13",
3
+ "version": "6.0.0-alpha.15",
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
- "@vitejs/plugin-react": "^3.1.0",
11
- "react-cosmos-core": "6.0.0-alpha.13",
12
- "react-cosmos-dom": "6.0.0-alpha.13"
10
+ "react-cosmos-core": "6.0.0-alpha.14",
11
+ "react-cosmos-dom": "6.0.0-alpha.14"
13
12
  },
14
13
  "peerDependencies": {
15
14
  "vite": "*"
16
15
  },
17
- "gitHead": "cf95e67ce942f6a242f023ecebe45d6916d5b753"
16
+ "gitHead": "1de8663ae24b3e8c6b24590583718222d31b24c8"
18
17
  }
@@ -1,6 +0,0 @@
1
- import { CosmosConfig } from 'react-cosmos';
2
- type ViteCosmosConfig = {
3
- port: number;
4
- };
5
- export declare function createViteCosmosConfig(cosmosConfig: CosmosConfig): ViteCosmosConfig;
6
- export {};
@@ -1,6 +0,0 @@
1
- export function createViteCosmosConfig(cosmosConfig) {
2
- const configInput = cosmosConfig.vite || {};
3
- return {
4
- port: configInput.port || 5050,
5
- };
6
- }