-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix: lightningcss fails with html-proxy #13776
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d23733b
chore: add broken test
Marabyte 8ae1c9c
fix: checks if filepath has css extension and src code exists
Marabyte 1d8115c
Add comment
ArnaudBarre cadf40b
Merge remote-tracking branch 'origin/main' into fix/lightning-css-proxy
ArnaudBarre ddb01f5
Fix version
ArnaudBarre 8bb8f5a
Remove useless optional chaining
ArnaudBarre 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
13 changes: 13 additions & 0 deletions
13
playground/css-lightningcss-proxy/__tests__/css-lightningcss-proxy.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,13 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { port } from './serve' | ||
import { getColor, page } from '~utils' | ||
|
||
const url = `http://localhost:${port}` | ||
|
||
describe('injected inline style', () => { | ||
test('injected inline style is present', async () => { | ||
await page.goto(url) | ||
const el = await page.$('.ssr-proxy') | ||
expect(await getColor(el)).toBe('coral') | ||
}) | ||
}) |
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,38 @@ | ||
// this is automatically detected by playground/vitestSetup.ts and will replace | ||
// the default e2e test serve behavior | ||
|
||
import path from 'node:path' | ||
import kill from 'kill-port' | ||
import { hmrPorts, ports, rootDir } from '~utils' | ||
|
||
export const port = ports['css/lightningcss-proxy'] | ||
|
||
export async function serve(): Promise<{ close(): Promise<void> }> { | ||
await kill(port) | ||
|
||
const { createServer } = await import(path.resolve(rootDir, 'server.js')) | ||
const { app, vite } = await createServer( | ||
rootDir, | ||
hmrPorts['css/lightningcss-proxy'], | ||
) | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const server = app.listen(port, () => { | ||
resolve({ | ||
// for test teardown | ||
async close() { | ||
await new Promise((resolve) => { | ||
server.close(resolve) | ||
}) | ||
if (vite) { | ||
await vite.close() | ||
} | ||
}, | ||
}) | ||
}) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} |
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,5 @@ | ||
<!--[inline-css]--> | ||
<div class="wrapper"> | ||
<p>Injected inline style with SSR Proxy</p> | ||
<p class="ssr-proxy">This should be coral</p> | ||
</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,15 @@ | ||
{ | ||
"name": "@vitejs/test-css-lightningcss-proxy", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "node server", | ||
"serve": "NODE_ENV=production node server", | ||
"debug": "node --inspect-brk server", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"lightningcss": "^1.21.5", | ||
"express": "^4.18.2" | ||
} | ||
} |
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,89 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import express from 'express' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
const isTest = process.env.VITEST | ||
|
||
const DYNAMIC_STYLES = ` | ||
<style> | ||
.ssr-proxy { | ||
color: coral; | ||
} | ||
</style> | ||
` | ||
|
||
export async function createServer(root = process.cwd(), hmrPort) { | ||
const resolve = (p) => path.resolve(__dirname, p) | ||
|
||
const app = express() | ||
|
||
/** | ||
* @type {import('vite').ViteDevServer} | ||
*/ | ||
const vite = await ( | ||
await import('vite') | ||
).createServer({ | ||
root, | ||
logLevel: isTest ? 'error' : 'info', | ||
css: { | ||
transformer: 'lightningcss', | ||
lightningcss: { | ||
drafts: { nesting: true }, | ||
}, | ||
}, | ||
server: { | ||
middlewareMode: true, | ||
watch: { | ||
// During tests we edit the files too fast and sometimes chokidar | ||
// misses change events, so enforce polling for consistency | ||
usePolling: true, | ||
interval: 100, | ||
}, | ||
hmr: { | ||
port: hmrPort, | ||
}, | ||
}, | ||
appType: 'custom', | ||
}) | ||
// use vite's connect instance as middleware | ||
app.use(vite.middlewares) | ||
|
||
app.use('*', async (req, res, next) => { | ||
try { | ||
let [url] = req.originalUrl.split('?') | ||
if (url.endsWith('/')) url += 'index.html' | ||
|
||
if (url.startsWith('/favicon.ico')) { | ||
return res.status(404).end('404') | ||
} | ||
|
||
const htmlLoc = resolve(`.${url}`) | ||
let template = fs.readFileSync(htmlLoc, 'utf-8') | ||
|
||
template = template.replace('<!--[inline-css]-->', DYNAMIC_STYLES) | ||
|
||
// Force calling transformIndexHtml with url === '/', to simulate | ||
// usage by ecosystem that was recommended in the SSR documentation | ||
// as `const url = req.originalUrl` | ||
const html = await vite.transformIndexHtml('/', template) | ||
|
||
res.status(200).set({ 'Content-Type': 'text/html' }).end(html) | ||
} catch (e) { | ||
vite && vite.ssrFixStacktrace(e) | ||
console.log(e.stack) | ||
res.status(500).end(e.stack) | ||
} | ||
}) | ||
|
||
return { app, vite } | ||
} | ||
|
||
if (!isTest) { | ||
createServer().then(({ app }) => | ||
app.listen(5173, () => { | ||
console.log('http://localhost:5173') | ||
}), | ||
) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
When running the playground, I got
""
(empty string) as a value offilePath
. I think this doesn't make sense and it's a bug in Lightning CSS. But I think it's fine to merge this as a workaround for now.