forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add resolve.preserveSymlinks option (vitejs#4708)
- Loading branch information
1 parent
5d99fa2
commit 0ebb290
Showing
15 changed files
with
307 additions
and
32 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
9 changes: 9 additions & 0 deletions
9
packages/playground/preserve-symlinks/__tests__/preload.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,9 @@ | ||
test('should have no 404s', () => { | ||
browserLogs.forEach((msg) => { | ||
expect(msg).not.toMatch('404') | ||
}) | ||
}) | ||
|
||
test('not-preserve-symlinks', async () => { | ||
expect(await page.textContent('#root')).toBe('hello vite') | ||
}) |
108 changes: 108 additions & 0 deletions
108
packages/playground/preserve-symlinks/__tests__/serve.js
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,108 @@ | ||
// @ts-check | ||
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace | ||
// the default e2e test serve behavior | ||
|
||
const path = require('path') | ||
const http = require('http') | ||
const sirv = require('sirv') | ||
const fs = require('fs') | ||
|
||
const port = (exports.port = 9527) | ||
|
||
/** | ||
* @param {string} root | ||
* @param {boolean} isBuildTest | ||
*/ | ||
exports.serve = async function serve(root, isBuildTest) { | ||
const testDist = path.resolve(__dirname, '../moduleA/dist') | ||
|
||
if (fs.existsSync(testDist)) { | ||
emptyDir(testDist) | ||
} else { | ||
fs.mkdirSync(testDist, { recursive: true }) | ||
} | ||
|
||
fs.symlinkSync( | ||
path.resolve(testDist, '../src/index.js'), | ||
path.resolve(testDist, 'symlinks-moduleA.esm.js') | ||
) | ||
|
||
if (!isBuildTest) { | ||
const { createServer } = require('vite') | ||
process.env.VITE_INLINE = 'inline-serve' | ||
let viteServer = await ( | ||
await createServer({ | ||
root: root, | ||
logLevel: 'silent', | ||
server: { | ||
watch: { | ||
usePolling: true, | ||
interval: 100 | ||
}, | ||
host: true, | ||
fs: { | ||
strict: !isBuildTest | ||
} | ||
}, | ||
build: { | ||
target: 'esnext' | ||
} | ||
}) | ||
).listen() | ||
// use resolved port/base from server | ||
const base = viteServer.config.base === '/' ? '' : viteServer.config.base | ||
const url = | ||
(global.viteTestUrl = `http://localhost:${viteServer.config.server.port}${base}`) | ||
await page.goto(url) | ||
|
||
return viteServer | ||
} else { | ||
const { build } = require('vite') | ||
await build({ | ||
root, | ||
logLevel: 'silent', | ||
configFile: path.resolve(__dirname, '../vite.config.js') | ||
}) | ||
|
||
// start static file server | ||
const serve = sirv(path.resolve(root, 'dist')) | ||
const httpServer = http.createServer((req, res) => { | ||
if (req.url === '/ping') { | ||
res.statusCode = 200 | ||
res.end('pong') | ||
} else { | ||
serve(req, res) | ||
} | ||
}) | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
const server = httpServer.listen(port, async () => { | ||
await page.goto(`http://localhost:${port}`) | ||
resolve({ | ||
// for test teardown | ||
async close() { | ||
await new Promise((resolve) => { | ||
server.close(resolve) | ||
}) | ||
} | ||
}) | ||
}) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
function emptyDir(dir) { | ||
for (const file of fs.readdirSync(dir)) { | ||
const abs = path.resolve(dir, file) | ||
if (fs.lstatSync(abs).isDirectory()) { | ||
emptyDir(abs) | ||
fs.rmdirSync(abs) | ||
} else { | ||
fs.unlinkSync(abs) | ||
} | ||
} | ||
} |
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="src/favicon.svg" /> | ||
<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="/src/main.js"></script> | ||
</body> | ||
</html> |
17 changes: 17 additions & 0 deletions
17
packages/playground/preserve-symlinks/moduleA/package.json
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 @@ | ||
{ | ||
"name": "@symlinks/moduleA", | ||
"version": "0.0.0", | ||
"module": "dist/symlinks-moduleA.esm.js", | ||
"main": "dist/symlinks-moduleA.cjs.js", | ||
"preconstruct": { | ||
"entrypoints": [ | ||
"index.js" | ||
] | ||
}, | ||
"scripts": { | ||
"dev": "preconstruct dev" | ||
}, | ||
"devDependencies": { | ||
"@preconstruct/cli": "^2.0.6" | ||
} | ||
} |
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 @@ | ||
export const data = { | ||
msg: 'hello vite' | ||
} |
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 @@ | ||
import { data } from './data' | ||
|
||
export function sayHi() { | ||
return data | ||
} |
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 @@ | ||
{ | ||
"name": "preserveSymlinks", | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite --force", | ||
"build": "vite build", | ||
"serve": "vite preview" | ||
}, | ||
"workspaces": {}, | ||
"dependencies": { | ||
"@symlinks/moduleA": "link:./moduleA" | ||
} | ||
} |
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 @@ | ||
import { sayHi } from '@symlinks/moduleA' | ||
|
||
document.getElementById('root').innerText = sayHi().msg |
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,6 @@ | ||
// https://vitejs.dev/config/ | ||
module.exports = { | ||
resolve: { | ||
preserveSymlinks: false | ||
} | ||
} |
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.