-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: dynamic import retry plugin [KM-865] #2
Open
2eha0
wants to merge
12
commits into
main
Choose a base branch
from
feat/dynmamic-import-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7cc82d4
feat: dynamic import retry plugin [KM-865]
2eha0 b693bae
feat: default exclude node_modules
2eha0 4f4a0e2
test: vue async component
2eha0 d9d2e53
fix: lint warn
2eha0 4142f22
fix: remove unused file
2eha0 2cfa2ad
test: transitive and dynamic-import-vars
2eha0 ef8837b
Merge remote-tracking branch 'origin/main' into feat/dynmamic-import-…
2eha0 00a7462
fix: playwright cache
2eha0 55c1300
fix: playwright cache
2eha0 385164f
fix: playwright cache
2eha0 74a06e6
ci: test cache
2eha0 7b3806e
fix: cache key
2eha0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,19 @@ | ||
## Directory Overview | ||
|
||
This directory serves as a testing environment for plugins. Each subdirectory corresponds to a specific plugin, identified by its name. | ||
|
||
* `vitestGlobalSetup.ts`: This is the global setup file for Vitest, executed once before all tests. It initializes a headless browser environment. | ||
* `vitestSetup.ts`: This is the per-test setup file for Vitest, executed before each individual test. It provides references to the browser instance and the page context for use during testing. | ||
|
||
## Steps to Test Your Plugin | ||
1. Create a new subdirectory within the playground directory, using your plugin’s name as the folder name. | ||
2. Within this folder, set up one or more Vite projects for testing. | ||
3. Write test files with the `.spec.ts` extension to validate your plugin’s functionality. | ||
4. If browser-based testing is required, you can import `page` or `browser` from the `vitestSetup.ts` file to interact with the headless browser environment. | ||
|
||
## Run playground project manually | ||
```bash | ||
pnpm vite dev ./playground/<plugin-name>/<project-name> | ||
pnpm vite build ./playground/<plugin-name>/<project-name> | ||
pnpm vite preview ./playground/<plugin-name>/<project-name> | ||
``` |
58 changes: 58 additions & 0 deletions
58
playground/dynamic-import-retry/dynamic-import-vars/dynamic-import-vars.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,58 @@ | ||
import { expect, test } from 'vitest' | ||
import { build, preview } from 'vite' | ||
import { resolve } from 'node:path' | ||
|
||
import { browserLogs, page } from '../../vitestSetup' | ||
|
||
import type { InlineConfig } from 'vite' | ||
import { loadConfigFromFile } from 'vite' | ||
import { beforeAll } from 'vitest' | ||
|
||
let viteTestUrl: string | ||
|
||
beforeAll(async () => { | ||
const res = await loadConfigFromFile( | ||
{ | ||
command: 'build', | ||
mode: 'production', | ||
}, | ||
undefined, | ||
resolve(__dirname), | ||
) | ||
if (!res) throw new Error('Failed to load config') | ||
|
||
const testConfig: InlineConfig = { | ||
...res.config, | ||
logLevel: 'silent', | ||
configFile: false, | ||
} | ||
|
||
await build(testConfig) | ||
const previewServer = await preview(testConfig) | ||
|
||
viteTestUrl = previewServer.resolvedUrls!.local[0] | ||
await page.goto(viteTestUrl) | ||
|
||
return async () => { | ||
previewServer.close() | ||
} | ||
}) | ||
|
||
test('should work with @rollup/plugin-dynamic-import-vars', async () => { | ||
let loadCount = 0 | ||
await page.route( | ||
(url) => url.pathname.includes('en-') && url.pathname.includes('.js'), | ||
route => { | ||
loadCount++ | ||
if (loadCount < 2) { | ||
return route.fulfill({ status: 404, body: 'Not Found' }) | ||
} | ||
return route.continue() | ||
}, | ||
) | ||
await page.click('#btn-vars') | ||
await page.waitForTimeout(1000) | ||
const log = browserLogs.find(log => log.includes('{title: Hello World}')) | ||
expect(loadCount).toBe(2) | ||
expect(log).toBeDefined() | ||
}) |
12 changes: 12 additions & 0 deletions
12
playground/dynamic-import-retry/dynamic-import-vars/index.html
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,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Dynamic import vars</title> | ||
</head> | ||
<body> | ||
<button id="btn-vars">vars</button> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
3 changes: 3 additions & 0 deletions
3
playground/dynamic-import-retry/dynamic-import-vars/src/locales/en.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,3 @@ | ||
export default { | ||
title: 'Hello World', | ||
} |
7 changes: 7 additions & 0 deletions
7
playground/dynamic-import-retry/dynamic-import-vars/src/main.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,7 @@ | ||
const btn = document.querySelector<HTMLButtonElement>('#btn-vars')! | ||
|
||
btn.addEventListener('click', async () => { | ||
const lang = 'en' | ||
const res = await import(`./locales/${lang}.ts`) | ||
console.log(res.default) | ||
}) |
11 changes: 11 additions & 0 deletions
11
playground/dynamic-import-retry/dynamic-import-vars/tsconfig.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,11 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "dist", | ||
"declaration": false, | ||
"declarationDir": null, | ||
"types": ["vite/client"] | ||
}, | ||
"include": ["vite.config.ts.bak"] | ||
} |
21 changes: 21 additions & 0 deletions
21
playground/dynamic-import-retry/dynamic-import-vars/vite.config.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,21 @@ | ||
import { defineConfig } from 'vite' | ||
import { resolve } from 'node:path' | ||
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars' | ||
|
||
import { DynamicImportRetryPlugin } from '../../../src/plugin-dynamic-import-retry' | ||
|
||
export default defineConfig({ | ||
root: resolve(__dirname), | ||
build: { | ||
minify: false, | ||
outDir: 'dist', | ||
target: 'esnext', | ||
emptyOutDir: true, | ||
rollupOptions: { | ||
plugins: [ | ||
dynamicImportVars(), | ||
DynamicImportRetryPlugin(), | ||
], | ||
}, | ||
}, | ||
}) |
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" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Transitive</title> | ||
</head> | ||
<body> | ||
<button id="btn-simple">simple module</button> | ||
<button id="btn-transitive">transitive module</button> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
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 @@ | ||
const btnA = document.querySelector<HTMLButtonElement>('#btn-simple')! | ||
const btnB = document.querySelector<HTMLButtonElement>('#btn-transitive')! | ||
|
||
btnA.addEventListener('click', async () => { | ||
const { value } = await import('./simple') | ||
console.log(value) | ||
}) | ||
|
||
btnB.addEventListener('click', async () => { | ||
const { value } = await import('./transitive') | ||
console.log(value) | ||
}) | ||
|
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 @@ | ||
export const value = 'a' |
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,2 @@ | ||
import { value as valueA } from './simple' | ||
export const value = valueA + 'b' |
64 changes: 64 additions & 0 deletions
64
playground/dynamic-import-retry/transitive/transitive.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,64 @@ | ||
import { expect, test } from 'vitest' | ||
import { build, preview } from 'vite' | ||
import { resolve } from 'node:path' | ||
|
||
import { browserErrors, page } from '../../vitestSetup' | ||
|
||
import type { InlineConfig } from 'vite' | ||
import { loadConfigFromFile } from 'vite' | ||
import { beforeAll } from 'vitest' | ||
|
||
let viteTestUrl: string | ||
|
||
beforeAll(async () => { | ||
const res = await loadConfigFromFile( | ||
{ | ||
command: 'build', | ||
mode: 'production', | ||
}, | ||
undefined, | ||
resolve(__dirname), | ||
) | ||
if (!res) throw new Error('Failed to load config') | ||
|
||
const testConfig: InlineConfig = { | ||
...res.config, | ||
logLevel: 'silent', | ||
configFile: false, | ||
} | ||
|
||
await build(testConfig) | ||
const previewServer = await preview(testConfig) | ||
|
||
viteTestUrl = previewServer.resolvedUrls!.local[0] | ||
await page.goto(viteTestUrl) | ||
|
||
return async () => { | ||
previewServer.close() | ||
} | ||
}) | ||
|
||
test('should not work on transitive import', async () => { | ||
let subModuleLoadCount = 0 | ||
let entryModuleLoadCount = 0 | ||
await page.route( | ||
(url) => url.pathname.includes('simple') && url.pathname.includes('.js'), | ||
route => { | ||
subModuleLoadCount++ | ||
return route.fulfill({ status: 404, body: 'Not Found' }) | ||
}, | ||
) | ||
await page.route( | ||
(url) => url.pathname.includes('transitive') && url.pathname.includes('.js'), | ||
route => { | ||
entryModuleLoadCount++ | ||
return route.continue() | ||
}, | ||
) | ||
await page.click('#btn-transitive') | ||
await page.waitForTimeout(2000) | ||
expect(entryModuleLoadCount).toBe(4) | ||
expect(subModuleLoadCount).toBe(1) | ||
const e = browserErrors.find(e => e.message.includes('[dynamic-import-retry]') && e.message.includes('transitive')) | ||
expect(e).toBeDefined() | ||
}) |
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,11 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "dist", | ||
"declaration": false, | ||
"declarationDir": null, | ||
"types": ["vite/client"] | ||
}, | ||
"include": ["vite.config.ts.bak"] | ||
} |
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 @@ | ||
import { defineConfig } from 'vite' | ||
import { resolve } from 'node:path' | ||
|
||
import { DynamicImportRetryPlugin } from '../../../src/plugin-dynamic-import-retry' | ||
|
||
export default defineConfig({ | ||
root: resolve(__dirname), | ||
plugins: [ | ||
DynamicImportRetryPlugin(), | ||
], | ||
build: { | ||
minify: false, | ||
outDir: 'dist', | ||
target: 'esnext', | ||
emptyOutDir: true, | ||
}, | ||
}) |
12 changes: 12 additions & 0 deletions
12
playground/dynamic-import-retry/vue-async-component/index.html
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,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + Vue + TS</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to pass the PLAYWRIGHT_BROWSERS_PATH here as an env variable