diff --git a/packages/playground/lib/__tests__/lib.spec.ts b/packages/playground/lib/__tests__/lib.spec.ts index 3bc9f903a08912..9978d62ac777ad 100644 --- a/packages/playground/lib/__tests__/lib.spec.ts +++ b/packages/playground/lib/__tests__/lib.spec.ts @@ -1,4 +1,6 @@ -import { isBuild } from 'testUtils' +import { isBuild, findAssetFile, testDir } from 'testUtils' +import path from 'path' +import fs from 'fs' if (isBuild) { test('es', async () => { @@ -8,6 +10,15 @@ if (isBuild) { test('umd', async () => { expect(await page.textContent('.umd')).toBe('It works') }) + + test('Library mode does not include `preload`', async () => { + expect(await page.textContent('.dynamic-import-message')).toBe('hello vite') + const code = fs.readFileSync( + path.join(testDir, 'dist/lib/dynamic-import-message.js'), + 'utf-8' + ) + expect(code).not.toMatch('__vitePreload') + }) } else { test('dev', async () => { expect(await page.textContent('.demo')).toBe('It works') diff --git a/packages/playground/lib/__tests__/serve.js b/packages/playground/lib/__tests__/serve.js new file mode 100644 index 00000000000000..15c64de40276d1 --- /dev/null +++ b/packages/playground/lib/__tests__/serve.js @@ -0,0 +1,90 @@ +// @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 port = (exports.port = 9527) + +/** + * @param {string} root + * @param {boolean} isBuildTest + */ +exports.serve = async function serve(root, isBuildTest) { + // build first + + 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') + }) + + await build({ + root, + logLevel: 'silent', + configFile: path.resolve(__dirname, '../vite.dyimport.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) + } + }) + } +} diff --git a/packages/playground/lib/index.dist.html b/packages/playground/lib/index.dist.html index eddf3006a66aec..b10b55e0fd913d 100644 --- a/packages/playground/lib/index.dist.html +++ b/packages/playground/lib/index.dist.html @@ -1,6 +1,7 @@
+
+ +