From decc7d885d108a3e96ffc82abcb4057f54aa6db5 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Tue, 20 Jul 2021 03:37:13 +0800 Subject: [PATCH] feat: library mode does not include preload (#4097) --- packages/playground/lib/__tests__/lib.spec.ts | 13 ++- packages/playground/lib/__tests__/serve.js | 90 +++++++++++++++++++ packages/playground/lib/index.dist.html | 7 ++ packages/playground/lib/src/main2.js | 4 + packages/playground/lib/src/message.js | 1 + .../playground/lib/vite.dyimport.config.js | 18 ++++ packages/vite/src/node/importGlob.ts | 4 +- .../src/node/plugins/importAnalysisBuild.ts | 7 +- 8 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 packages/playground/lib/__tests__/serve.js create mode 100644 packages/playground/lib/src/main2.js create mode 100644 packages/playground/lib/src/message.js create mode 100644 packages/playground/lib/vite.dyimport.config.js 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 @@
+
+ +