Skip to content

Commit

Permalink
test: add vite5 e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Dec 14, 2023
1 parent a39ab93 commit 9c19846
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 13 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,26 @@ jobs:
- name: install dependices
run: yarn

- name: build project
run: yarn build
- name: test 2.x to 4.x
run: yarn ava e2e/vite[2-4]/**/*.spec.ts
run-stable-e2e-test:
strategy:
matrix:
version: [18,20]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
node-version: ${{ matrix.version }}
- name: Install berry
run: corepack enable

- name: install dependices
run: yarn

- name: test 5.x
run: yarn ava e2e/vite5/**/*.spec.ts


- name: run e2e test
run: yarn e2e

2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.12.1
v20.4.0
5 changes: 3 additions & 2 deletions e2e/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { cdn } from '../dist'
import type { Vite2Instance } from './vite2/interface'
import type { Vite3Instance } from './vite3/interface'
import type { Vite4Instance } from './vite4/interface'
import type { Vite5Instance } from './vite5/interface'

type ViteInstance = Vite2Instance | Vite3Instance | Vite4Instance
type ViteInstance = Vite2Instance | Vite3Instance | Vite4Instance | Vite5Instance

export interface TestOptions {
vite: ViteInstance
Expand Down Expand Up @@ -72,7 +73,7 @@ function createServer(taskName: string) {
const readStream = fs.createReadStream(fullPath)
res.setHeader('Content-Type', contentType)
res.statusCode = 200
readStream.pipe(res)
readStream.pipe(res)
}
} catch (error) {
res.statusCode = 404
Expand Down
14 changes: 14 additions & 0 deletions e2e/vite5/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import vue from '@vitejs/plugin-vue'
import { runTest } from '../e2e'
import { name } from './package.json'

export default (async function () {
const vite = await import('vite')
runTest(name, {
vite,
pluginOption: {
modules: ['vue']
},
plugins: [vue()]
})
})()
1 change: 1 addition & 0 deletions e2e/vite5/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Vite5Instance = typeof import('vite')
7 changes: 7 additions & 0 deletions e2e/vite5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vite-plugin-cdn2-e2e-vite5",
"dependencies": {
"@vitejs/plugin-vue": "^4.5.2",
"vite": "^5"
}
}
19 changes: 13 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { transformWithBabel } from './transform'

const debug = _debug('vite-plugin-cdn2')

const NODE_MODULES = 'node_modules'
// rs-module-lexer can't cover all platforms.
// But it provide a wasm bindings. So we provide
// a wrapper func to cover most of scence.
Expand Down Expand Up @@ -63,17 +64,24 @@ interface ExternalPluginAPI {
dependency: ReturnType<typeof createDependency>
}

function transformPresetModule(transform: Plugin['transform']): Plugin {
function transformPresetModule(api: ExternalPluginAPI): Plugin {
return {
name: 'vite-plugin-cdn2:presetModule',
transform
transform(code, id) {
if (!api.filter(id)) return
if (id.includes(NODE_MODULES)) {
const result = transformCJSRequire(code, api.dependency.dependency)
if (api.dependency.filter(code, id)) return transformWithBabel(code, api.dependency)
return result
}
}
}
}

function cdn(opts: CDNPluginOptions = {}): Plugin[] {
const { modules = [], url = jsdelivr, include = /\.(mjs|js|ts|vue|jsx|tsx)(\?.*|)$/, exclude, logLevel = 'warn', resolve: resolver, apply = 'build' } = opts
const scanner = createScanner(modules)
const { api: _api, transform } = external({ modules: [], include, exclude })
const { api: _api } = external({ modules: [], include, exclude })
const api = _api as ExternalPluginAPI
const transformPlugin = (): Plugin => {
return {
Expand Down Expand Up @@ -123,12 +131,11 @@ function cdn(opts: CDNPluginOptions = {}): Plugin[] {
}
}
}
return [{ ...transformPlugin(), apply }, { ...transformPresetModule(transform), apply }]
return [{ ...transformPlugin(), apply }, { ...transformPresetModule(api), apply }]
}

function external(opts: ExternalPluginOptions = {}): Plugin {
// Inspired by vite-plugin-external
const nodeModules = 'node_modules'
const debug = _debug('vite-plugin-external')
const { modules = [], include, exclude } = opts
const filter = createFilter(include, exclude)
Expand Down Expand Up @@ -158,7 +165,7 @@ function external(opts: ExternalPluginOptions = {}): Plugin {
},
transform(code, id) {
if (!filter(id)) return
if (id.includes(nodeModules)) {
if (id.includes(NODE_MODULES)) {
const result = transformCJSRequire(code, dependency.dependency)
if (dependency.filter(code, id)) return transformWithBabel(code, dependency)
return result
Expand Down
Loading

0 comments on commit 9c19846

Please sign in to comment.