-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
ssr.noExternal = true
option (#4490)
- Loading branch information
Showing
12 changed files
with
164 additions
and
9 deletions.
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
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
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,48 @@ | ||
// @ts-check | ||
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace | ||
// the default e2e test serve behavior | ||
|
||
const path = require('path') | ||
|
||
const port = (exports.port = 9528) | ||
|
||
/** | ||
* @param {string} root | ||
* @param {boolean} isProd | ||
*/ | ||
exports.serve = async function serve(root, isProd) { | ||
// we build first, regardless of whether it's prod/build mode | ||
// because Vite doesn't support the concept of a "webworker server" | ||
const { build } = require('vite') | ||
|
||
// worker build | ||
await build({ | ||
root, | ||
logLevel: 'silent', | ||
build: { | ||
target: 'esnext', | ||
ssr: 'src/entry-worker.jsx', | ||
outDir: 'dist/worker' | ||
} | ||
}) | ||
|
||
const { createServer } = require(path.resolve(root, 'worker.js')) | ||
const { app } = await createServer(root, isProd) | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const server = app.listen(port, () => { | ||
resolve({ | ||
// for test teardown | ||
async close() { | ||
await new Promise((resolve) => { | ||
server.close(resolve) | ||
}) | ||
} | ||
}) | ||
}) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/playground/ssr-webworker/__tests__/ssr-webworker.spec.ts
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 @@ | ||
import { port } from './serve' | ||
|
||
const url = `http://localhost:${port}` | ||
|
||
test('/', async () => { | ||
await page.goto(url + '/') | ||
expect(await page.textContent('h1')).toMatch('hello from webworker') | ||
expect(await page.textContent('.linked')).toMatch('dep from upper directory') | ||
expect(await page.textContent('.external')).toMatch('object') | ||
}) |
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,15 @@ | ||
{ | ||
"name": "test-ssr-webworker", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "DEV=1 node worker", | ||
"build:worker": "vite build --ssr src/entry-worker.jsx --outDir dist/worker" | ||
}, | ||
"dependencies": { | ||
"react": "^17.0.2" | ||
}, | ||
"devDependencies": { | ||
"miniflare": "^1.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,19 @@ | ||
import { msg as linkedMsg } from 'resolve-linked' | ||
import React from 'react' | ||
|
||
addEventListener('fetch', function (event) { | ||
return event.respondWith( | ||
new Response( | ||
` | ||
<h1>hello from webworker</h1> | ||
<p class="linked">${linkedMsg}</p> | ||
<p class="external">${typeof React}</p> | ||
`, | ||
{ | ||
headers: { | ||
'content-type': 'text/html' | ||
} | ||
} | ||
) | ||
) | ||
}) |
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,12 @@ | ||
/** | ||
* @type {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
build: { | ||
minify: false | ||
}, | ||
ssr: { | ||
target: 'webworker', | ||
noExternal: true | ||
} | ||
} |
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,26 @@ | ||
// @ts-check | ||
const path = require('path') | ||
const { Miniflare } = require('miniflare') | ||
|
||
const isDev = process.env.DEV | ||
|
||
async function createServer(root = process.cwd()) { | ||
const mf = new Miniflare({ | ||
scriptPath: path.resolve(root, 'dist/worker/entry-worker.js') | ||
}) | ||
|
||
const app = mf.createServer() | ||
|
||
return { app } | ||
} | ||
|
||
if (isDev) { | ||
createServer().then(({ app }) => | ||
app.listen(3000, () => { | ||
console.log('http://localhost:3000') | ||
}) | ||
) | ||
} | ||
|
||
// for test use | ||
exports.createServer = createServer |
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
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
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
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