Skip to content

Commit 4d6a6bb

Browse files
committed
test: test full dynamic import
1 parent 4a9307c commit 4d6a6bb

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

packages/playground/dynamic-import/__tests__/dynamic-import.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ test('should load literal dynamic import', async () => {
1111
await page.click('.baz')
1212
await untilUpdated(() => page.textContent('.view'), 'Baz view')
1313
})
14+
15+
test('should load full dynamic import from public', async () => {
16+
await page.click('.qux')
17+
await untilUpdated(() => page.textContent('.view'), 'Qux view')
18+
})

packages/playground/dynamic-import/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<button class="foo">Foo</button>
22
<button class="bar">Bar</button>
33
<button class="baz">Baz</button>
4+
<button class="qux">Qux</button>
45

56
<div class="view"></div>
67

packages/playground/dynamic-import/nested/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ async function setView(view) {
77
document.querySelector(`.${id}`).addEventListener('click', () => setView(id))
88
})
99

10+
// literal dynamic
1011
document.querySelector('.baz').addEventListener('click', async () => {
11-
// literal dynamic
1212
const { msg } = await import('../views/baz.js')
1313
text('.view', msg)
1414
})
1515

16+
// full dynamic
17+
const arr = ['qux.js']
18+
const view = `/${arr[0]}`
19+
document.querySelector('.qux').addEventListener('click', async () => {
20+
const { msg } = await import(/*@vite-ignore*/ view)
21+
text('.view', msg)
22+
})
23+
1624
function text(el, text) {
1725
document.querySelector(el).textContent = text
1826
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = 'Qux view'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
module.exports = {
5+
plugins: [
6+
{
7+
name: 'copy',
8+
writeBundle() {
9+
fs.copyFileSync(
10+
path.resolve(__dirname, 'qux.js'),
11+
path.resolve(__dirname, 'dist/qux.js')
12+
)
13+
}
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)