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

New vite based application runtime #1881

Merged
merged 107 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
33b18f8
WIP
Janpot Apr 6, 2023
09343cf
Merge branch 'master' into vite-experiment
Janpot Apr 7, 2023
1de9540
updates
Janpot Apr 7, 2023
519bef9
more
Janpot Apr 7, 2023
301a305
eqwrqe
Janpot Apr 9, 2023
90131ce
WIP
Janpot Apr 12, 2023
a283b74
Merge branch 'master' into vite-experiment
Janpot Apr 12, 2023
54cc0df
Merge branch 'master' into vite-experiment
Janpot Apr 12, 2023
f872e0a
Merge branch 'master' into vite-experiment
Janpot Apr 12, 2023
9725366
prod mode
Janpot Apr 12, 2023
fc7d3cc
canvas mode
Janpot Apr 12, 2023
c5ed547
canvas ipl
Janpot Apr 12, 2023
2f0c8e9
jfdhsgg
Janpot Apr 12, 2023
e8cb4b4
Fix command
Janpot Apr 12, 2023
2f8dd21
Merge branch 'master' into vite-experiment
Janpot Apr 12, 2023
7af282d
index
Janpot Apr 12, 2023
39aa40d
fix lint
Janpot Apr 12, 2023
e74b63d
fix lint for some reason
Janpot Apr 12, 2023
3664336
frger
Janpot Apr 12, 2023
4dab728
fsfwe
Janpot Apr 12, 2023
c8b0bc7
Merge branch 'master' into vite-experiment
Janpot Apr 12, 2023
8560a32
Update toolpadAppBuilder.ts
Janpot Apr 12, 2023
4beea0c
dsfew
Janpot Apr 13, 2023
228bc58
dev
Janpot Apr 13, 2023
a78fe13
ewtwe
Janpot Apr 13, 2023
f14309f
nother
Janpot Apr 13, 2023
fc41846
Merge branch 'master' into vite-experiment
Janpot Apr 13, 2023
fc282ec
fwfwe
Janpot Apr 13, 2023
799d2e5
Merge branch 'master' into vite-experiment
Janpot Apr 13, 2023
ff6f991
not this
Janpot Apr 13, 2023
ef1e458
Update tsup.config.ts
Janpot Apr 13, 2023
4e09eba
dewfr
Janpot Apr 13, 2023
53e683a
Merge branch 'master' into vite-experiment
Janpot Apr 14, 2023
3039957
remove optimized deps folder on start of tests
Janpot Apr 14, 2023
b423f04
fwf
Janpot Apr 14, 2023
6277ccc
fix resolve
Janpot Apr 14, 2023
3aa1148
try
Janpot Apr 14, 2023
e54b664
fsfs
Janpot Apr 14, 2023
c88f5ef
fwert
Janpot Apr 14, 2023
9ddac22
fwrfew
Janpot Apr 14, 2023
39bc9a2
frwgwr
Janpot Apr 14, 2023
2f9e540
oops
Janpot Apr 14, 2023
b0ca104
fsfgsd
Janpot Apr 14, 2023
a0434a1
try this
Janpot Apr 14, 2023
73ca353
dwefwe
Janpot Apr 14, 2023
0e3e2d7
sfr
Janpot Apr 14, 2023
5352ca0
Update toolpadAppServer.ts
Janpot Apr 14, 2023
75a7160
dsfs
Janpot Apr 14, 2023
b27b92e
ddwef
Janpot Apr 14, 2023
375a815
ewfwef
Janpot Apr 14, 2023
310b9f2
frger
Janpot Apr 14, 2023
e39ffae
dwefwe
Janpot Apr 14, 2023
6c0d7aa
navigation fix for /pages
Janpot Apr 15, 2023
ea40860
dew
Janpot Apr 15, 2023
a64b3ac
tracing playwright
Janpot Apr 15, 2023
361b498
dewrwe
Janpot Apr 15, 2023
cd8052b
other test
Janpot Apr 15, 2023
e7ed98d
ewrfwe
Janpot Apr 15, 2023
b0596ff
fwefw
Janpot Apr 15, 2023
2ed4233
dewr
Janpot Apr 15, 2023
c7f8afd
fix test
Janpot Apr 15, 2023
3ede0bb
dedfwf
Janpot Apr 15, 2023
0c4a1f7
Merge branch 'master' into vite-experiment
Janpot Apr 16, 2023
27ec873
frefer
Janpot Apr 16, 2023
25f0acf
dsfwef
Janpot Apr 16, 2023
a6bc025
snapshot
Janpot Apr 16, 2023
19dac05
fwf
Janpot Apr 16, 2023
f8e5101
defwe
Janpot Apr 16, 2023
df33ad8
fwfw
Janpot Apr 16, 2023
10ce76c
Revert "fwfw"
Janpot Apr 16, 2023
260b8c1
efwr
Janpot Apr 16, 2023
aa86d4d
dsfds
Janpot Apr 16, 2023
ea2fd77
ewrewq
Janpot Apr 16, 2023
14720a8
dewfe
Janpot Apr 16, 2023
4c0decc
WIPok
Janpot Apr 18, 2023
1fda15f
react
Janpot Apr 18, 2023
33002f8
try this
Janpot Apr 18, 2023
0c15b08
improve exit
Janpot Apr 18, 2023
8b5c554
Merge branch 'master' into vite-experiment
Janpot Apr 18, 2023
f21c94b
gdfgdhf
Janpot Apr 18, 2023
a5e146f
Test
Janpot Apr 19, 2023
ede5ac8
yep
Janpot Apr 19, 2023
e0441a5
jsxRuntime
Janpot Apr 19, 2023
4601f66
import fix
Janpot Apr 19, 2023
0bb5905
dfew
Janpot Apr 19, 2023
850342b
drf
Janpot Apr 19, 2023
4fa1c39
Update playwright.config.ts
Janpot Apr 19, 2023
259615a
fwe
Janpot Apr 19, 2023
1f4ac79
dewf
Janpot Apr 19, 2023
4a35d7f
dewrwe
Janpot Apr 19, 2023
803549b
more
Janpot Apr 19, 2023
ea581d8
betas
Janpot Apr 19, 2023
85b6772
more optimizeddeps
Janpot Apr 19, 2023
afa0676
update comment
Janpot Apr 19, 2023
4305822
Merge branch 'master' into vite-experiment
Janpot Apr 19, 2023
63caacc
Add flag
Janpot Apr 19, 2023
03f3387
remove unintended
Janpot Apr 19, 2023
5c356c0
Merge branch 'master' into vite-experiment
Janpot Apr 19, 2023
cbabd96
try old way
Janpot Apr 20, 2023
22576d6
switch canvas
Janpot Apr 20, 2023
b05cde7
fix
Janpot Apr 20, 2023
2efaa42
rename
Janpot Apr 20, 2023
5fe44e9
poll
Janpot Apr 20, 2023
726da25
reenable vite tests
Janpot Apr 20, 2023
492cd20
setup tests folder
Janpot Apr 20, 2023
26cba9c
linty mclintface
Janpot Apr 20, 2023
fdc75e6
Merge branch 'master' into vite-experiment
Janpot Apr 20, 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
8 changes: 6 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
- image: mcr.microsoft.com/playwright:v1.32.3-focal
environment:
NODE_ENV: test
TOOLPAD_TEST_RETRIES: 1
- image: kennethreitz/httpbin:latest

steps:
Expand All @@ -154,9 +155,12 @@ jobs:
- run:
name: Tests real browser
environment:
- LOCAL_MODE_TESTS: '1'
- HTTPBIN_BASEURL: 'http://localhost/'
HTTPBIN_BASEURL: 'http://localhost/'
TOOLPAD_VITE_RUNTIME: 1
command: yarn test:integration
- store_artifacts:
path: test-results
destination: playwright-test-results

workflows:
pipeline:
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
extends: ['plugin:@next/next/recommended'],
rules: {
'@next/next/no-html-link-for-pages': ['error', 'packages/toolpad-app/pages/'],
'@next/next/no-img-element': 'off',
},
},
{
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"release:publish-canary": "lerna publish from-package --force-publish --no-git-tag-version --no-push --no-git-reset --pre-dist-tag canary",
"release:changelog": "dotenv -- node ./scripts/releaseChangelog.mjs --repo mui-toolpad",
"test:build": "lerna run build --scope @mui/toolpad-core --scope @mui/toolpad-components --stream",
"test:integration": "playwright test --config ./test/integration/playwright.config.ts",
"test:integration": "rm -rf ./node_modules/.vite && playwright test --config ./test/integration/playwright.config.ts",
"test": "jest",
"check-types": "lerna run check-types",
"toolpad": "toolpad"
Expand All @@ -48,9 +48,11 @@
"@swc/helpers": "^0.5.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/archiver": "^5.3.2",
"@types/gtag.js": "^0.0.12",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@types/react-inspector": "^4.0.2",
"@types/rimraf": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
Expand All @@ -77,12 +79,15 @@
"markdownlint-cli2": "^0.6.0",
"prettier": "^2.8.7",
"pretty-quick": "^3.1.3",
"react-inspector": "^5.1.1",
"recharts": "^2.5.0",
"regenerator-runtime": "^0.13.11",
"rimraf": "^5.0.0",
"typescript": "^5.0.4",
"yarn-deduplicate": "^6.0.1"
},
"dependencies": {
"archiver": "^5.3.1",
"cross-env": "^7.0.3",
"dotenv-cli": "^7.2.1",
"inquirer": "^9.1.5",
Expand Down
19 changes: 19 additions & 0 deletions packages/toolpad-app/cli/appBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import invariant from 'invariant';
import { buildApp } from '../src/server/toolpadAppBuilder';

async function main() {
invariant(
process.env.NODE_ENV === 'production',
'The app builder must be run with NODE_ENV=production',
);
invariant(!!process.env.TOOLPAD_PROJECT_DIR, 'A project root must be defined');

const projectDir = process.env.TOOLPAD_PROJECT_DIR;

await buildApp({ root: projectDir, base: '/prod' });
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
101 changes: 101 additions & 0 deletions packages/toolpad-app/cli/appServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import invariant from 'invariant';
import { createServer, Plugin } from 'vite';
import {
getHtmlContent,
postProcessHtml,
createViteConfig,
resolvedComponentsId,
} from '../src/server/toolpadAppBuilder';
import config from '../src/config';
import { loadDomFromDisk } from '../src/server/localMode';

function devServerPlugin(): Plugin {
return {
name: 'toolpad-dev-server',

async configureServer(viteServer) {
return () => {
viteServer.middlewares.use('/', async (req, res, next) => {
invariant(req.url, 'request must have a url');
const url = new URL(req.url, 'http://x');
const canvas = url.searchParams.get('toolpad-display') === 'canvas';

try {
const dom = await loadDomFromDisk();

const template = getHtmlContent({ canvas });

let html = await viteServer.transformIndexHtml(req.url, template);

html = postProcessHtml(html, { config, dom });

res.setHeader('content-type', 'text/html').end(html);
} catch (e) {
next(e);
}
});
};
},
};
}

export interface ToolpadAppDevServerParams {
root: string;
base: string;
}

export async function createDevServer({ root, base }: ToolpadAppDevServerParams) {
const devServer = await createServer(
createViteConfig({
dev: true,
root,
base,
plugins: [devServerPlugin()],
}),
);

return devServer;
}

export type Command = {
kind: 'reload-components';
};

export type Event = {
kind: 'ready';
};

async function main() {
invariant(
process.env.NODE_ENV === 'development',
'The dev server must be run with NODE_ENV=development',
);
invariant(!!process.env.TOOLPAD_PROJECT_DIR, 'A project root must be defined');
invariant(!!process.env.TOOLPAD_PORT, 'A port must be defined');
invariant(process.send, 'Process must be spawned with an IPC channel');

const app = await createDevServer({
root: process.env.TOOLPAD_PROJECT_DIR,
base: '/preview',
});

await app.listen(Number(process.env.TOOLPAD_PORT));

process.on('message', (msg: Command) => {
if (msg.kind === 'reload-components') {
const mod = app.moduleGraph.getModuleById(resolvedComponentsId);
if (mod) {
app.reloadModule(mod);
}
}
});

process.send({});

process.send({ kind: 'ready' } satisfies Event);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
43 changes: 32 additions & 11 deletions packages/toolpad-app/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ interface RunOptions {
dir: string;
port?: number;
dev?: boolean;
viteRuntime: boolean;
}

async function runApp(cmd: Command, { port, dev = false, dir }: RunOptions) {
async function runApp(cmd: Command, { port, dev = false, dir, viteRuntime }: RunOptions) {
const projectDir = path.resolve(process.cwd(), dir);
const { execaNode } = await import('execa');
const { default: chalk } = await import('chalk');
Expand All @@ -63,7 +64,9 @@ async function runApp(cmd: Command, { port, dev = false, dir }: RunOptions) {
cwd: projectDir,
stdio: 'pipe',
env: {
NODE_ENV: dev ? 'development' : 'production',
NODE_ENV: process.env.NODE_ENV,
TOOLPAD_NEXT_DEV: dev ? '1' : '',
TOOLPAD_VITE_RUNTIME: viteRuntime ? '1' : '',
TOOLPAD_DIR: toolpadDir,
TOOLPAD_PROJECT_DIR: projectDir,
TOOLPAD_PORT: String(port),
Expand All @@ -73,10 +76,11 @@ async function runApp(cmd: Command, { port, dev = false, dir }: RunOptions) {
});

invariant(cp.stdout, 'child process must be started with "stdio: \'pipe\'"');
invariant(cp.stderr, 'child process must be started with "stdio: \'pipe\'"');

process.stdin.pipe(cp.stdin!);
cp.stdout?.pipe(process.stdout);
cp.stderr?.pipe(process.stderr);
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);

if (cmd === 'dev') {
// Poll stdout for "http://localhost:3000" first
Expand Down Expand Up @@ -108,19 +112,31 @@ async function devCommand(args: RunOptions) {

interface BuildOptions {
dir: string;
viteRuntime: boolean;
}

async function buildCommand({ dir }: BuildOptions) {
async function buildCommand({ dir, viteRuntime }: BuildOptions) {
const projectDir = path.resolve(process.cwd(), dir);
const { default: chalk } = await import('chalk');
// eslint-disable-next-line no-console
console.log(
`${chalk.blue('info')} - building Toolpad application at ${chalk.cyan(projectDir)}...`,
);
console.log(`${chalk.blue('info')} - building Toolpad application...`);

if (viteRuntime) {
const { execaNode } = await import('execa');

const builderPath = path.resolve(__dirname, './appBuilder.js');

await execaNode(builderPath, [], {
cwd: projectDir,
stdio: 'inherit',
env: {
NODE_ENV: 'production',
TOOLPAD_PROJECT_DIR: projectDir,
FORCE_COLOR: '1',
},
});
}

await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
// eslint-disable-next-line no-console
console.log(`${chalk.green('success')} - build done.`);
}
Expand All @@ -139,6 +155,11 @@ export default async function cli(argv: string[]) {
describe: 'Directory of the Toolpad application',
default: '.',
},
viteRuntime: {
type: 'boolean',
describe: 'Use the new experimental vite runtime',
default: false,
},
} as const;

const sharedRunOptions = {
Expand Down
Loading