Skip to content

Commit

Permalink
chore: release v0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Nov 10, 2023
1 parent 8272728 commit 51a9d2b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.15.0

### Features

- Add `rs-module-lexer` wasm bindings.

## 0.14.1

### Patches
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-cdn2",
"version": "0.14.1",
"version": "0.15.0",
"description": "A Vite plugin that allowed you replace module with CDN",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -79,8 +79,9 @@
"dependencies": {
"@babel/core": "^7.22.5",
"@rollup/pluginutils": "^5.0.2",
"@xn-sakina/rml-wasm": "^2.1.1",
"debug": "^4.3.4",
"rs-module-lexer": "^2.0.1"
"rs-module-lexer": "^2.1.1"
},
"ava": {
"files": [
Expand Down
30 changes: 26 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createFilter } from '@rollup/pluginutils'
import type { Plugin } from 'vite'
import { parse as esModuleLexer } from 'rs-module-lexer'
import _debug from 'debug'
import { createScanner, getPackageExports, serializationExportsFields } from './scanner'
import { createInjectScript } from './inject'
Expand All @@ -11,11 +10,28 @@ import { transformWithBabel } from './transform'

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

// 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.
// WASM support at least node15

async function createRsModuleLexer() {
try {
const { parse } = (await import('rs-module-lexer')).default
return parse
} catch (error) {
const { parse } = await import('@xn-sakina/rml-wasm').catch(() => {
throw new Error('rs-module-lexer can\'t work on you current machine.')
})
return parse
}
}

function createDependency() {
const dependency: Record<string, ModuleInfo> = {}

const filter = (code: string, id: string, dependencyWithAlias: Record<string, string>) => {
const { output } = esModuleLexer({ input: [{ filename: id, code }] })
const filter = (code: string, id: string, dependencyWithAlias: Record<string, string>, lex: Awaited<ReturnType<typeof createRsModuleLexer>>) => {
const { output } = lex({ input: [{ filename: id, code }] })
if (!len(output)) return false
const { imports } = output[0]
const modules = Array.from(new Set([...imports.map(i => i.n)]))
Expand All @@ -28,6 +44,7 @@ function createDependency() {

return {
dependency,
lex: null,
get dependencyWithAlias() {
const traverse = (aliases: string[], name: string) => aliases.reduce((acc, cur) => ({ ...acc, [cur]: name }), {})
return Object.values(this.dependency).reduce((acc, cur) => {
Expand All @@ -36,7 +53,7 @@ function createDependency() {
}, {})
},
filter: function (code: string, id: string) {
return filter(code, id, this.dependencyWithAlias)
return filter(code, id, this.dependencyWithAlias, this.lex)
}
}
}
Expand Down Expand Up @@ -80,6 +97,9 @@ function cdn(opts: CDNPluginOptions = {}): Plugin[] {
const [isSupport, version] = isSupportThreads()
try {
if (!isSupport) throw new Error(`vite-plugin-cdn2 can't work with nodejs ${version}.`)
const esModuleLexer = await createRsModuleLexer()
api.dependency.lex = esModuleLexer
console.log(api.dependency, esModuleLexer)
const defaultWd = config.root
scanner.setDefaultWd(defaultWd)
debug('start scanning')
Expand Down Expand Up @@ -126,6 +146,8 @@ function external(opts: ExternalPluginOptions = {}): Plugin {
name: 'vite-plugin-external',
async buildStart() {
try {
const esModuleLexer = await createRsModuleLexer()
dependency.lex = esModuleLexer
debug('start check modules')
for (const module of modules) {
if (!module.global) throw new Error(`vite-plugin-external: missing global for module ${module.name}`)
Expand Down
92 changes: 50 additions & 42 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1635,65 +1635,72 @@ __metadata:
languageName: node
linkType: hard

"@xn-sakina/rml-darwin-arm64@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-darwin-arm64@npm:2.0.1"
"@xn-sakina/rml-darwin-arm64@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-darwin-arm64@npm:2.1.1"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard

"@xn-sakina/rml-darwin-x64@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-darwin-x64@npm:2.0.1"
"@xn-sakina/rml-darwin-x64@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-darwin-x64@npm:2.1.1"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@xn-sakina/rml-linux-arm-gnueabihf@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-linux-arm-gnueabihf@npm:2.0.1"
"@xn-sakina/rml-linux-arm-gnueabihf@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-linux-arm-gnueabihf@npm:2.1.1"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard

"@xn-sakina/rml-linux-arm64-gnu@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-linux-arm64-gnu@npm:2.0.1"
"@xn-sakina/rml-linux-arm64-gnu@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-linux-arm64-gnu@npm:2.1.1"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"@xn-sakina/rml-linux-arm64-musl@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-linux-arm64-musl@npm:2.0.1"
"@xn-sakina/rml-linux-arm64-musl@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-linux-arm64-musl@npm:2.1.1"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"@xn-sakina/rml-linux-x64-gnu@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-linux-x64-gnu@npm:2.0.1"
"@xn-sakina/rml-linux-x64-gnu@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-linux-x64-gnu@npm:2.1.1"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"@xn-sakina/rml-linux-x64-musl@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-linux-x64-musl@npm:2.0.1"
"@xn-sakina/rml-linux-x64-musl@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-linux-x64-musl@npm:2.1.1"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"@xn-sakina/rml-win32-arm64-msvc@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-win32-arm64-msvc@npm:2.0.1"
"@xn-sakina/rml-wasm@npm:^2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-wasm@npm:2.1.1"
checksum: d589fa3936277f7250e43c626758721b245d72b227c78ab149bcb5f6f4fc67a572bfcfc6ebed38e8ea6fedcfafa3b1c58970b6d1a0906d8df58435511e9c2b9f
languageName: node
linkType: hard

"@xn-sakina/rml-win32-arm64-msvc@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-win32-arm64-msvc@npm:2.1.1"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard

"@xn-sakina/rml-win32-x64-msvc@npm:2.0.1":
version: 2.0.1
resolution: "@xn-sakina/rml-win32-x64-msvc@npm:2.0.1"
"@xn-sakina/rml-win32-x64-msvc@npm:2.1.1":
version: 2.1.1
resolution: "@xn-sakina/rml-win32-x64-msvc@npm:2.1.1"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard
Expand Down Expand Up @@ -5913,19 +5920,19 @@ __metadata:
languageName: node
linkType: hard

"rs-module-lexer@npm:^2.0.1":
version: 2.0.1
resolution: "rs-module-lexer@npm:2.0.1"
dependencies:
"@xn-sakina/rml-darwin-arm64": 2.0.1
"@xn-sakina/rml-darwin-x64": 2.0.1
"@xn-sakina/rml-linux-arm-gnueabihf": 2.0.1
"@xn-sakina/rml-linux-arm64-gnu": 2.0.1
"@xn-sakina/rml-linux-arm64-musl": 2.0.1
"@xn-sakina/rml-linux-x64-gnu": 2.0.1
"@xn-sakina/rml-linux-x64-musl": 2.0.1
"@xn-sakina/rml-win32-arm64-msvc": 2.0.1
"@xn-sakina/rml-win32-x64-msvc": 2.0.1
"rs-module-lexer@npm:^2.1.1":
version: 2.1.1
resolution: "rs-module-lexer@npm:2.1.1"
dependencies:
"@xn-sakina/rml-darwin-arm64": 2.1.1
"@xn-sakina/rml-darwin-x64": 2.1.1
"@xn-sakina/rml-linux-arm-gnueabihf": 2.1.1
"@xn-sakina/rml-linux-arm64-gnu": 2.1.1
"@xn-sakina/rml-linux-arm64-musl": 2.1.1
"@xn-sakina/rml-linux-x64-gnu": 2.1.1
"@xn-sakina/rml-linux-x64-musl": 2.1.1
"@xn-sakina/rml-win32-arm64-msvc": 2.1.1
"@xn-sakina/rml-win32-x64-msvc": 2.1.1
dependenciesMeta:
"@xn-sakina/rml-darwin-arm64":
optional: true
Expand All @@ -5945,7 +5952,7 @@ __metadata:
optional: true
"@xn-sakina/rml-win32-x64-msvc":
optional: true
checksum: 68c6ec4b8d25c1730dc15954cbfcb19120427503e62ad267f023bb23337587135c4786d1c290e2ea54aefe2b2e4a725b811191535eca1592bff39f9326602d4d
checksum: e203c3db6642b58765d1b275b588c3f0b7b8ebadca62f7bd610bfbff2772a17b5303699343c886cda0f3fd6606de74b459d596c3debeb3f192fc2c2919f647a2
languageName: node
linkType: hard

Expand Down Expand Up @@ -6756,14 +6763,15 @@ __metadata:
"@types/debug": ^4.1.8
"@types/node": ^20.5.0
"@types/react": ^18.2.36
"@xn-sakina/rml-wasm": ^2.1.1
ava: ^5.2.0
c8: ^7.12.0
debug: ^4.3.4
eslint: ^8.51.0
eslint-config-kagura: ^2.0.1
playwright: ^1.35.1
react: ^18.2.0
rs-module-lexer: ^2.0.1
rs-module-lexer: ^2.1.1
tsup: ^7.1.0
tsx: ^3.14.0
typescript: ^4.8.3
Expand Down

0 comments on commit 51a9d2b

Please sign in to comment.