-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
feat: Test bundling packages which provide environment depended code parts #484
Merged
dai-shi
merged 6 commits into
dai-shi:main
from
aheissenberger:add-test-build-target-bundling
Feb 14, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d13a4d8
feat: Test bundling npm packages which provide environment depended c…
aheissenberger f88cb83
add lock file
aheissenberger 5fb8764
fix formating
aheissenberger 05e2f0d
Update e2e/fixtures/ssr-target-bundle/package.json
dai-shi e9e4f91
fix: waku from monorepo, add test to gloabt tsconfig.e2e
aheissenberger c7f093c
fix lockfile
aheissenberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SSR Bundling for NPM packages with environment depended code parts | ||
|
||
Choosing the right code based on the `export` section of the `react-textarea-autosize` module based on the backend server deployment target ('node' | 'webworker') and the client browser. No browser only code should be bundled with server only code. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "ssr-target-bundle", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"dev": "waku dev --with-ssr", | ||
"build": "waku build --with-ssr", | ||
"start": "waku start --with-ssr" | ||
}, | ||
"dependencies": { | ||
"react": "18.3.0-canary-4b2a1115a-20240202", | ||
"react-dom": "18.3.0-canary-4b2a1115a-20240202", | ||
"react-server-dom-webpack": "18.3.0-canary-4b2a1115a-20240202", | ||
"react-textarea-autosize": "^8.5.3", | ||
"waku": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.2.55", | ||
"@types/react-dom": "18.2.19", | ||
"typescript": "5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Textarea } from './Textarea.js'; | ||
|
||
const App = ({ name }: { name: string }) => { | ||
return ( | ||
<div style={{ border: '3px red dashed', margin: '1em', padding: '1em' }}> | ||
<title>Waku example</title> | ||
<h1 data-testid="app-name">{name}</h1> | ||
<Textarea /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
10 changes: 10 additions & 0 deletions
10
e2e/fixtures/ssr-target-bundle/src/components/Textarea.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use client'; | ||
import TextareaAutosize from 'react-textarea-autosize'; | ||
|
||
export const Textarea = () => { | ||
return ( | ||
<div> | ||
<TextareaAutosize data-testid="textarea" defaultValue={'EMPTY'} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { lazy } from 'react'; | ||
import { defineEntries } from 'waku/server'; | ||
import { Slot } from 'waku/client'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error here because you import a package from monorepo. Solution is add it to |
||
|
||
const App = lazy(() => import('./components/App.js')); | ||
|
||
export default defineEntries( | ||
// renderEntries | ||
async (input) => { | ||
return { | ||
App: <App name={input || 'Waku'} />, | ||
}; | ||
}, | ||
// getBuildConfig | ||
async () => [{ pathname: '/', entries: [{ input: '' }] }], | ||
// getSsrConfig | ||
async (pathname) => { | ||
switch (pathname) { | ||
case '/': | ||
return { | ||
input: '', | ||
body: <Slot id="App" />, | ||
}; | ||
default: | ||
return null; | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StrictMode } from 'react'; | ||
import { createRoot, hydrateRoot } from 'react-dom/client'; | ||
import { Root, Slot } from 'waku/client'; | ||
|
||
const rootElement = ( | ||
<StrictMode> | ||
<Root> | ||
<Slot id="App" /> | ||
</Root> | ||
</StrictMode> | ||
); | ||
|
||
if (import.meta.env.WAKU_HYDRATE) { | ||
hydrateRoot(document.body, rootElement); | ||
} else { | ||
createRoot(document.body).render(rootElement); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"strict": true, | ||
"target": "esnext", | ||
"downlevelIteration": true, | ||
"esModuleInterop": true, | ||
"module": "nodenext", | ||
"skipLibCheck": true, | ||
"noUncheckedIndexedAccess": true, | ||
"exactOptionalPropertyTypes": true, | ||
"jsx": "react-jsx", | ||
"rootDir": "./src", | ||
"outDir": "./dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { expect } from '@playwright/test'; | ||
import { execSync, exec, ChildProcess } from 'node:child_process'; | ||
import { fileURLToPath } from 'node:url'; | ||
import waitPort from 'wait-port'; | ||
import { getFreePort, test } from './utils.js'; | ||
import { rm } from 'node:fs/promises'; | ||
|
||
const waku = fileURLToPath( | ||
new URL('../packages/waku/dist/cli.js', import.meta.url), | ||
); | ||
|
||
const commands = [ | ||
{ | ||
command: 'dev --with-ssr', | ||
}, | ||
{ | ||
build: 'build --with-ssr', | ||
command: 'start --with-ssr', | ||
}, | ||
]; | ||
|
||
const cwd = fileURLToPath( | ||
new URL('./fixtures/ssr-target-bundle', import.meta.url), | ||
); | ||
|
||
for (const { build, command } of commands) { | ||
test.describe(`ssr-target-bundle: ${command}`, () => { | ||
let cp: ChildProcess; | ||
let port: number; | ||
test.beforeAll('remove cache', async () => { | ||
await rm(`${cwd}/dist`, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
|
||
test.beforeAll(async () => { | ||
if (build) { | ||
execSync(`node ${waku} ${build}`, { | ||
cwd, | ||
}); | ||
} | ||
port = await getFreePort(); | ||
cp = exec(`node ${waku} ${command}`, { | ||
cwd, | ||
env: { | ||
...process.env, | ||
PORT: `${port}`, | ||
}, | ||
}); | ||
cp.stdout?.on('data', (data) => { | ||
console.log(`${port} stdout: `, `${data}`); | ||
}); | ||
cp.stderr?.on('data', (data) => { | ||
console.error(`${port} stderr: `, `${data}`); | ||
}); | ||
await waitPort({ | ||
port, | ||
}); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
cp.kill(); | ||
}); | ||
|
||
test('add text input', async ({ page }) => { | ||
await page.goto(`http://localhost:${port}/`); | ||
await expect(page.getByTestId('app-name')).toHaveText('Waku'); | ||
await expect(page.getByTestId('textarea')).toHaveValue('EMPTY'); | ||
const height = await page | ||
.getByTestId('textarea') | ||
.evaluate((el) => el.clientHeight); | ||
await page.getByTestId('textarea').fill('Line1\nLine2\nLine3'); | ||
const heightChanged = await page | ||
.getByTestId('textarea') | ||
.evaluate((el) => el.clientHeight); | ||
expect(heightChanged).toBeGreaterThan(height); | ||
}); | ||
|
||
test('no js environment should have first screen', async ({ browser }) => { | ||
const context = await browser.newContext({ | ||
javaScriptEnabled: false, | ||
}); | ||
const page = await context.newPage(); | ||
await page.goto(`http://localhost:${port}/`); | ||
await expect(page.getByTestId('app-name')).toHaveText('Waku'); | ||
await expect(page.getByTestId('textarea')).toHaveValue('EMPTY'); | ||
await page.close(); | ||
await context.close(); | ||
}); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, it's not our fault that when target=webworker, the bundler picks the browser version, instead of worker version. So, it's our temporarily workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing. Sorry for the confusion. It's just a note so that we are on the same page.