diff --git a/packages/@vue/cli-service/__tests__/buildLib.spec.js b/packages/@vue/cli-service/__tests__/buildLib.spec.js index 2b94caece7..177b39511f 100644 --- a/packages/@vue/cli-service/__tests__/buildLib.spec.js +++ b/packages/@vue/cli-service/__tests__/buildLib.spec.js @@ -1,4 +1,4 @@ -jest.setTimeout(30000) +jest.setTimeout(40000) const path = require('path') const portfinder = require('portfinder') @@ -8,6 +8,12 @@ const create = require('@vue/cli-test-utils/createTestProject') const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer') let server, browser, page + +afterEach(async () => { + await browser.close() + server.close() +}) + test('build as lib', async () => { const project = await create('build-lib', defaultPreset) @@ -45,7 +51,40 @@ test('build as lib', async () => { expect(h3Text).toMatch('Installed CLI Plugins') }) -afterAll(async () => { - await browser.close() - server.close() +test('build as lib (js)', async () => { + const project = await create('build-lib-js', defaultPreset) + await project.write('src/main.js', ` + export default { foo: 1 } + export const bar = 2 + `) + const { stdout } = await project.run('vue-cli-service build --target lib --name testLib src/main.js') + expect(stdout).toMatch('Build complete.') + + expect(project.has('dist/demo.html')).toBe(true) + expect(project.has('dist/testLib.common.js')).toBe(true) + expect(project.has('dist/testLib.umd.js')).toBe(true) + expect(project.has('dist/testLib.umd.min.js')).toBe(true) + + const port = await portfinder.getPortPromise() + server = createServer({ root: path.join(project.dir, 'dist') }) + + await new Promise((resolve, reject) => { + server.listen(port, err => { + if (err) return reject(err) + resolve() + }) + }) + + const launched = await launchPuppeteer(`http://localhost:${port}/demo.html`) + browser = launched.browser + page = launched.page + + // should expose a module with default and named exports + expect(await page.evaluate(() => { + return window.testLib.default.foo + })).toBe(1) + + expect(await page.evaluate(() => { + return window.testLib.bar + })).toBe(2) }) diff --git a/packages/@vue/cli-service/lib/commands/build/demo-lib-js.html b/packages/@vue/cli-service/lib/commands/build/demo-lib-js.html new file mode 100644 index 0000000000..368b0d02dd --- /dev/null +++ b/packages/@vue/cli-service/lib/commands/build/demo-lib-js.html @@ -0,0 +1,7 @@ +