Skip to content

Commit

Permalink
💥 Resupport both CJS and ESM vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
kingyue737 committed Sep 14, 2023
1 parent e7a6e5d commit fa3deee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ last 2 versions
not dead
```

2. Add plugin to `vite.config.m*`:
> Since v0.2.0, this plugin only supports ESM as `browserslist-useragent-regexp` [dropped support for CJS](https://github.com/browserslist/browserslist-useragent-regexp/commit/41456bc22b789fee57384a00abb64e0690ded08a). If you want to use CJS version, install v0.1.0 instead.
2. Add plugin to `vite.config.*`:

```ts
// vite.config.mts
// vite.config.ts
import SupportedBrowsers from 'vite-plugin-browserslist-useragent'
export default defineConfig({
plugins: [SupportedBrowsers(/* options */)],
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.2.2",
"description": "A utility vite plugin to compile browserslist query to a RegExp to test browser useragent.",
"author": "Yue JIN <[email protected]>",
"packageManager": "pnpm@7.14.2",
"packageManager": "pnpm@8.7.5",
"engines": {
"node": ">=14"
},
Expand All @@ -15,7 +15,7 @@
"vite-plugin",
"useragent"
],
"main": "dist/index.js",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -24,6 +24,7 @@
],
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
Expand All @@ -38,8 +39,8 @@
"url": "https://github.com/kingyue737/vite-plugin-browserslist-useragent/issues"
},
"scripts": {
"build": "tsup src/index.ts --dts --format esm",
"prepublishOnly": "npm run build",
"build": "tsup src/index.ts --dts --format cjs,esm",
"prepublishOnly": "pnpm run build",
"release": "bumpp --execute=\"pnpm changelog\" --commit \"🔖 Release v\" --push --tag --all && npm publish --registry=\"https://registry.npmjs.org/\"",
"changelog": "gitmoji-changelog"
},
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
getUserAgentRegex,
getUserAgentRegexes,
} from 'browserslist-useragent-regexp'
import type { getUserAgentRegex } from 'browserslist-useragent-regexp'
import type { Plugin } from 'vite'

export default function (
options?: Parameters<typeof getUserAgentRegex>[0]
options?: Parameters<typeof getUserAgentRegex>[0],
): Plugin {
const virtualModuleId = 'virtual:supported-browsers'
const resolvedVirtualModuleId = '\0' + virtualModuleId
Expand All @@ -17,10 +14,15 @@ export default function (
return resolvedVirtualModuleId
}
},
load(id) {
async load(id) {
if (id === resolvedVirtualModuleId) {
const { getUserAgentRegex, getUserAgentRegexes } = await import(
'browserslist-useragent-regexp'
)
return `export const browsersRegex = ${getUserAgentRegex(options)}
export const browsersRegexes = ${JSON.stringify(getUserAgentRegexes(options))}`
export const browsersRegexes = ${JSON.stringify(
getUserAgentRegexes(options),
)}`
}
},
}
Expand Down

0 comments on commit fa3deee

Please sign in to comment.