Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite integration #1406

Merged
merged 41 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3c6599b
Create multiple examples
ovidiuch Dec 19, 2022
a07d107
Create vite folder
ovidiuch Dec 19, 2022
c589fa9
Update startDevServer.ts
ovidiuch Dec 19, 2022
3d8d128
Add basic Vite integration
ovidiuch Dec 19, 2022
53a96cc
Fix test
ovidiuch Dec 19, 2022
32110a7
Add setCosmosConfig method to dev server plugin args
ovidiuch Jan 23, 2023
ab3d8f9
s/experimentalRendererUrl/rendererUrl
ovidiuch Jan 23, 2023
7d84deb
Rename plugins
ovidiuch Jan 23, 2023
b5004e7
Add shared example components
ovidiuch Jan 23, 2023
bab2600
Update fixtures.test.ts
ovidiuch Jan 23, 2023
0bbf7ed
Update native.ts
ovidiuch Jan 23, 2023
47cfea2
Update startDevServer.ts
ovidiuch Jan 23, 2023
25c5cad
Simplify Vite integration
ovidiuch Feb 3, 2023
2f825af
Add playgroundUrl to renderer config
ovidiuch Feb 17, 2023
33cf903
Update native.ts
ovidiuch Feb 17, 2023
cd53a38
Extract fixture watcher
ovidiuch Feb 17, 2023
5b87351
Update userDepsFile.tsx
ovidiuch Feb 17, 2023
4910dd8
Create Rollup plugin for Vite integration
ovidiuch Feb 17, 2023
5314b40
Remove url-parse
ovidiuch Feb 18, 2023
4bebd6c
Improve Rollup plugin
ovidiuch Feb 18, 2023
86b23ca
No longer generate user deps file when rendererUrl is set
ovidiuch Feb 18, 2023
17aae1c
Refactor
ovidiuch Feb 18, 2023
cddc93e
Remove url usage
ovidiuch Feb 18, 2023
ec3c0d5
Always use relative renderer URL in static exports
ovidiuch Feb 18, 2023
4047715
Export Playground source map
ovidiuch Feb 18, 2023
7d54321
Implement export plugin for Vite
ovidiuch Feb 18, 2023
e11a962
Extract react-cosmos-plugin-vite package
ovidiuch Feb 18, 2023
6ba115f
Update findCosmosPluginConfigs.test.ts
ovidiuch Feb 18, 2023
5240930
Update resolveUrlPath.ts
ovidiuch Feb 18, 2023
126a24a
Update generateExport.ts
ovidiuch Feb 18, 2023
b571d9f
Move file
ovidiuch Feb 18, 2023
5ea6db5
Clean stuff up
ovidiuch Feb 18, 2023
7b7451a
Update userDepsTemplate.test.ts
ovidiuch Feb 18, 2023
0bf98fc
Update userDeps.ts
ovidiuch Feb 18, 2023
9ffd3c7
Update types.ts
ovidiuch Feb 18, 2023
2d7d027
Revert unneeded dev server changes
ovidiuch Feb 18, 2023
4811963
Update reactCosmosViteRollupPlugin.ts
ovidiuch Feb 18, 2023
d053363
Update index.ts
ovidiuch Feb 18, 2023
b68a3ec
Update comment
ovidiuch Feb 18, 2023
3bd6e85
Update config.schema.json
ovidiuch Feb 18, 2023
44fa1e2
Update config.schema.json
ovidiuch Feb 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules

packages/*/dist
example/cosmos-export
examples/*/cosmos-export
website/dist

**/__fsmocks__/**
Expand Down
7 changes: 3 additions & 4 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
.vscode
coverage
example/cosmos-export
example/cosmos.userdeps.js
example/package.json
example/*/dist
examples/*/cosmos-export
examples/*/cosmos.userdeps.js
examples/*/package.json
lerna.json
package.json
packages/*/dist
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/testBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function homepageTests() {
context('homepage', () => {
it('has document title', () => {
cy.title().should('include', 'react-cosmos-example');
cy.title().should('include', 'example-webpack-babel');
});

it('displays welcome message', () => {
Expand Down
11 changes: 7 additions & 4 deletions cypress/tests/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Native', () => {

context('homepage', () => {
it('has document title', () => {
cy.title().should('include', 'react-cosmos-example');
cy.title().should('include', 'example-webpack-babel');
});

it('displays pending renderer message', () => {
Expand All @@ -16,12 +16,15 @@ describe('Native', () => {

context('user deps file', () => {
it('has port option', () => {
getUserDepsFile().should('contain', `"port": 5002`);
getUserDepsFile().should(
'contain',
`"playgroundUrl": "http://localhost:5002"`
);
});

it('has fixture paths', () => {
userDepsContainsModule('src/__fixtures__/HelloWorld.ts');
userDepsContainsModule('src/Counter/Counter.fixture.tsx');
userDepsContainsModule('src/Counter.fixture.tsx');
userDepsContainsModule('src/WelcomeMessage/WelcomeMessage.fixture.tsx');
});

Expand All @@ -32,7 +35,7 @@ describe('Native', () => {
});

function getUserDepsFile() {
return cy.readFile('example/cosmos.userdeps.js');
return cy.readFile('examples/webpack-babel/cosmos.userdeps.js');
}

function userDepsContainsModule(modulePath: string) {
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export default ({ children }) => <Provider store={store}>{children}</Provider>;

### Migrating _proxies_

Migrating Cosmos Classic proxies to React Cosmos 5 is not intuitive. _Sorry for that!_ Check out the [nested decorators example](../example/src/NestedDecorators) and join the `#proxies-upgrade` [Slack](https://react-cosmos.slack.com/join/shared_invite/zt-g9rsalqq-clCoV7DWttVvzO5FAAmVAw) channel to learn more about this and to get help with your migration.
Migrating Cosmos Classic proxies to React Cosmos 5 is not intuitive. _Sorry for that!_ Check out the [nested decorators example](../examples/webpack-babel/src/NestedDecorators) and join the `#proxies-upgrade` [Slack](https://react-cosmos.slack.com/join/shared_invite/zt-g9rsalqq-clCoV7DWttVvzO5FAAmVAw) channel to learn more about this and to get help with your migration.

### Redux state mock

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { CounterButton } from '../CounterButton/CounterButton.js';
import { CounterButton } from './CounterButton.js';

type Props = {
suffix: string;
Expand Down
11 changes: 11 additions & 0 deletions examples/shared/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "examples-shared",
"version": "5.7.2",
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/examples/shared",
"license": "MIT",
"type": "module",
"scripts": {},
"dependencies": {},
"devDependencies": {}
}
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/vite/cosmos.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": [
"react-cosmos-plugin-boolean-input",
"react-cosmos-plugin-open-fixture",
"react-cosmos-plugin-vite"
],
"rendererUrl": "http://localhost:5050"
}
12 changes: 12 additions & 0 deletions examples/vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./index.tsx"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions examples/vite/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root')!);
root.render(<h1>Hello, world!</h1>);
20 changes: 20 additions & 0 deletions examples/vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"name": "example-vite",
"version": "5.7.2",
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/examples/vite",
"license": "MIT",
"type": "module",
"scripts": {
"start": "cosmos"
},
"dependencies": {
"examples-shared": "^5.7.2",
"react-cosmos": "^5.7.2",
"react-cosmos-plugin-boolean-input": "^5.7.2",
"react-cosmos-plugin-open-fixture": "^5.7.2",
"react-cosmos-plugin-vite": "^5.7.2",
"vite": "^4.1.2"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StateMock } from '@react-mock/state';
import { Counter } from 'examples-shared/components/Counter.js';
import React from 'react';
import { Counter } from './Counter.js';

export default {
default: <Counter suffix="times" />,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CounterButton } from 'examples-shared/components/CounterButton.js';
import React from 'react';
import { useValue } from 'react-cosmos-core';
import { CounterButton } from './CounterButton.js';

export default () => {
const [count, setCount] = useValue<number>('count', { defaultValue: 0 });
Expand Down
2 changes: 2 additions & 0 deletions examples/webpack-babel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cosmos-export
cosmos.userdeps.js
2 changes: 1 addition & 1 deletion example/README.md → examples/webpack-babel/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
> Run `yarn` and `yarn build` in the root directory first. Then you can run `yarn start` inside this example directory.

This example is tied to the Cosmos [monorepo](../CONTRIBUTING.md#monorepo), which makes it useful for internal development, but not as useful as a starter kit because some dependencies are implicit.
This example is tied to the Cosmos [monorepo](../../CONTRIBUTING.md#monorepo), which makes it useful for internal development, but not as useful as a starter kit because some dependencies are implicit.

Here are some external examples that didn't sit comfortably in this monorepo:

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"globalImports": ["src/global.css"],
"staticPath": "static",
"plugins": [
"react-cosmos-plugin-boolean-input",
"react-cosmos-plugin-open-fixture",
"react-cosmos-plugin-webpack",
"react-cosmos-plugin-boolean-input"
"react-cosmos-plugin-webpack"
]
}
5 changes: 3 additions & 2 deletions example/package.json → examples/webpack-babel/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"private": true,
"name": "react-cosmos-example",
"name": "example-webpack-babel",
"version": "5.7.2",
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/example",
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/examples/webpack-babel",
"license": "MIT",
"type": "module",
"scripts": {
"start": "cosmos"
},
"dependencies": {
"examples-shared": "^5.7.2",
"react-cosmos": "^5.7.2",
"react-cosmos-plugin-boolean-input": "^5.7.2",
"react-cosmos-plugin-open-fixture": "^5.7.2",
Expand Down
19 changes: 19 additions & 0 deletions examples/webpack-babel/src/Counter.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { StateMock } from '@react-mock/state';
import { Counter } from 'examples-shared/components/Counter.js';
import React from 'react';

export default {
default: <Counter suffix="times" />,

'small number': (
<StateMock state={{ count: 5 }}>
<Counter suffix="times" />
</StateMock>
),

'large number': (
<StateMock state={{ count: 555555555 }}>
<Counter suffix="times" />
</StateMock>
),
};
14 changes: 14 additions & 0 deletions examples/webpack-babel/src/CounterButton.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CounterButton } from 'examples-shared/components/CounterButton.js';
import React from 'react';
import { useValue } from 'react-cosmos-core';

export default () => {
const [count, setCount] = useValue<number>('count', { defaultValue: 0 });
return (
<CounterButton
suffix="times"
count={count}
increment={() => setCount(prevCount => prevCount + 1)}
/>
);
};
1 change: 1 addition & 0 deletions examples/webpack-babel/src/__fixtures__/HelloWorld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Hello World!';
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,55 @@ it('returns fixture info', async () => {
treePath: ['Controls'],
},
{
absoluteFilePath: `${rootDir}/src/Counter/Counter.fixture.tsx`,
absoluteFilePath: `${rootDir}/src/Counter.fixture.tsx`,
fileName: 'Counter',
getElement: expect.any(Function),
name: 'default',
parents: [],
playgroundUrl:
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22default%22%7D',
relativeFilePath: 'src/Counter/Counter.fixture.tsx',
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22default%22%7D',
relativeFilePath: 'src/Counter.fixture.tsx',
rendererUrl:
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22default%22%7D',
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22default%22%7D',
treePath: ['Counter', 'default'],
},
{
absoluteFilePath: `${rootDir}/src/Counter/Counter.fixture.tsx`,
absoluteFilePath: `${rootDir}/src/Counter.fixture.tsx`,
fileName: 'Counter',
getElement: expect.any(Function),
name: 'small number',
parents: [],
playgroundUrl:
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22small+number%22%7D',
relativeFilePath: 'src/Counter/Counter.fixture.tsx',
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22small+number%22%7D',
relativeFilePath: 'src/Counter.fixture.tsx',
rendererUrl:
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22small+number%22%7D',
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22small+number%22%7D',
treePath: ['Counter', 'small number'],
},
{
absoluteFilePath: `${rootDir}/src/Counter/Counter.fixture.tsx`,
absoluteFilePath: `${rootDir}/src/Counter.fixture.tsx`,
fileName: 'Counter',
getElement: expect.any(Function),
name: 'large number',
parents: [],
playgroundUrl:
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22large+number%22%7D',
relativeFilePath: 'src/Counter/Counter.fixture.tsx',
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22large+number%22%7D',
relativeFilePath: 'src/Counter.fixture.tsx',
rendererUrl:
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter%2FCounter.fixture.tsx%22%2C%22name%22%3A%22large+number%22%7D',
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounter.fixture.tsx%22%2C%22name%22%3A%22large+number%22%7D',
treePath: ['Counter', 'large number'],
},
{
absoluteFilePath: `${rootDir}/src/CounterButton/CounterButton.fixture.tsx`,
absoluteFilePath: `${rootDir}/src/CounterButton.fixture.tsx`,
fileName: 'CounterButton',
getElement: expect.any(Function),
name: null,
parents: [],
playgroundUrl:
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounterButton%2FCounterButton.fixture.tsx%22%7D',
relativeFilePath: 'src/CounterButton/CounterButton.fixture.tsx',
'http://localhost:5000/?fixtureId=%7B%22path%22%3A%22src%2FCounterButton.fixture.tsx%22%7D',
relativeFilePath: 'src/CounterButton.fixture.tsx',
rendererUrl:
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounterButton%2FCounterButton.fixture.tsx%22%7D',
'http://localhost:5000/_renderer.html?_fixtureId=%7B%22path%22%3A%22src%2FCounterButton.fixture.tsx%22%7D',
treePath: ['CounterButton'],
},
{
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"workspaces": [
"packages/*",
"example",
"examples/*",
"website"
],
"repository": "https://github.com/react-cosmos/react-cosmos",
Expand All @@ -22,10 +22,13 @@
"dist": "yarn link-entries dist",
"build:clear": "ts-node --esm ./scripts/clearBuild.ts",
"build": "ts-node --esm ./scripts/build.ts",
"start": "yarn cosmos --root-dir example",
"export": "yarn cosmos-export --root-dir example",
"start:export": "http-server -p 5001 ./example/cosmos-export",
"start:native": "yarn cosmos-native --root-dir example --port 5002",
"start": "yarn cosmos --root-dir examples/webpack-babel",
"start:vite": "yarn cosmos --root-dir examples/vite",
"export": "yarn cosmos-export --root-dir examples/webpack-babel",
"export:vite": "yarn cosmos-export --root-dir examples/vite",
"export:vite:serve": "http-server -p 5001 ./examples/vite/cosmos-export",
"start:export": "http-server -p 5001 ./examples/webpack-babel/cosmos-export",
"start:native": "yarn cosmos-native --root-dir examples/webpack-babel --port 5002",
"start:playground": "yarn cosmos --root-dir packages/react-cosmos-ui",
"website:start": "yarn cosmos --root-dir website",
"website:build": "ts-node --esm ./scripts/buildWebsite.ts",
Expand Down Expand Up @@ -54,7 +57,6 @@
"@types/react-native": "^0.70.8",
"@types/react-test-renderer": "^18.0.0",
"@types/styled-components": "^5.1.26",
"@types/url-parse": "^1.4.8",
"@types/util.promisify": "1.0.4",
"@types/webpack": "5.28.0",
"@types/webpack-hot-middleware": "^2.25.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-cosmos-core/src/renderer/rendererConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type RemoteRendererConfig = {
port: number;
export type RendererConfig = {
playgroundUrl: string;
};
Loading