Skip to content

Commit

Permalink
feat!: named export on main entry, add rolldown entry
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 29, 2024
1 parent c13f9cd commit cca6c69
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 29 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@ jobs:
- run: npx changelogithub
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

publish-jsr:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4

- name: Setup pnpm
run: corepack enable

- name: Set node
uses: actions/setup-node@v4
with:
node-version: lts/*
cache: pnpm

- name: Install
run: pnpm i

- name: Publish package
run: npx jsr publish
22 changes: 22 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@unplugin/ast",
"version": "0.11.1",
"exports": {
"./index": "./src/index.ts",
"./esbuild": "./src/esbuild.ts",
"./rollup": "./src/rollup.ts",
"./rolldown": "./src/rolldown.ts",
"./vite": "./src/vite.ts",
"./webpack": "./src/webpack.ts",
"./rspack": "./src/rspack.ts"
},
"publish": {
"include": [
"src",
"package.json",
"jsr.json",
"README.md",
"LICENSE"
]
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
"require": "./dist/rollup.cjs",
"import": "./dist/rollup.js"
},
"./rolldown": {
"require": "./dist/rolldown.cjs",
"import": "./dist/rolldown.js"
},
"./esbuild": {
"require": "./dist/esbuild.cjs",
"import": "./dist/esbuild.js"
Expand Down Expand Up @@ -75,6 +79,7 @@
"build": "tsup",
"dev": "tsup --watch",
"test": "vitest",
"typecheck": "tsc --noEmit",
"release": "bumpp && pnpm publish",
"prepublishOnly": "pnpm run build"
},
Expand Down
7 changes: 5 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { NodeRef } from './types'
import type { Node } from '@babel/types'

export function useNodeRef() {
export function useNodeRef(): {
nodeRefs: Map<Node, NodeRef<Node | undefined>>
getNodeRef: (node: Node) => NodeRef<Node | undefined>
} {
const nodeRefs: Map<Node, NodeRef<Node | undefined>> = new Map()

function getNodeRef(node: Node) {
function getNodeRef(node: Node): NodeRef<Node | undefined> {
if (nodeRefs.has(node)) return nodeRefs.get(node)!
const ref: NodeRef<Node | undefined> = {
value: node,
Expand Down
23 changes: 21 additions & 2 deletions src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for esbuild plugin. Requires esbuild >= 0.15
*
* @module
*/

export default unplugin.esbuild
import { AST } from './index'

/**
* Esbuild plugin
*
* @example
* ```ts
* // esbuild.config.js
* import { build } from 'esbuild'
*
* build({
* plugins: [require('unplugin-ast/esbuild')()],
* })
* ```
*/
export default AST.esbuild as typeof AST.esbuild
34 changes: 18 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { createFilter } from '@rollup/pluginutils'
import { createUnplugin } from 'unplugin'
import { createUnplugin, type UnpluginInstance } from 'unplugin'
import { resolveOption, type Options } from './core/options'
import { transform } from './core/transform'

export default createUnplugin<Options>((options = {}) => {
const opt = resolveOption(options)
const filter = createFilter(opt.include, opt.exclude)
export const AST: UnpluginInstance<Options, false> = createUnplugin(
(options = {}) => {
const opt = resolveOption(options)
const filter = createFilter(opt.include, opt.exclude)

const name = 'unplugin-ast'
return {
name,
enforce: options.enforce,
const name = 'unplugin-ast'
return {
name,
enforce: options.enforce,

transformInclude(id) {
return filter(id)
},
transformInclude(id) {
return filter(id)
},

transform(code, id) {
return transform(code, id, opt)
},
}
})
transform(code, id) {
return transform(code, id, opt)
},
}
},
)

export * from './core/options'
export * from './core/transform'
Expand Down
22 changes: 22 additions & 0 deletions src/rolldown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This entry file is for Rolldown plugin.
*
* @module
*/

import { AST } from './index'

/**
* Rolldown plugin
*
* @example
* ```ts
* // rolldown.config.js
* import AST from 'unplugin-ast/rolldown'
*
* export default {
* plugins: [AST()],
* }
* ```
*/
export default AST.rolldown as typeof AST.rolldown
23 changes: 21 additions & 2 deletions src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for Rollup plugin.
*
* @module
*/

export default unplugin.rollup
import { AST } from './index'

/**
* Rollup plugin
*
* @example
* ```ts
* // rollup.config.js
* import AST from 'unplugin-ast/rollup'
*
* export default {
* plugins: [AST()],
* }
* ```
*/
export default AST.rollup as typeof AST.rollup
21 changes: 19 additions & 2 deletions src/rspack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import unplugin from '.'
/**
* This entry file is for Rspack plugin.
*
* @module
*/

export default unplugin.rspack
import { AST } from './index'

/**
* Rspack plugin
*
* @example
* ```ts
* // rspack.config.js
* module.exports = {
* plugins: [require('unplugin-ast/rspack')()],
* }
* ```
*/
export default AST.rspack as typeof AST.rspack
23 changes: 21 additions & 2 deletions src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for Vite plugin.
*
* @module
*/

export default unplugin.vite
import { AST } from './index'

/**
* Vite plugin
*
* @example
* ```ts
* // vite.config.ts
* import AST from 'unplugin-ast/vite'
*
* export default defineConfig({
* plugins: [AST()],
* })
* ```
*/
export default AST.vite as typeof AST.vite
21 changes: 19 additions & 2 deletions src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import unplugin from '.'
/**
* This entry file is for webpack plugin.
*
* @module
*/

export default unplugin.webpack
import { AST } from './index'

/**
* Webpack plugin
*
* @example
* ```ts
* // webpack.config.js
* module.exports = {
* plugins: [require('unplugin-ast/webpack')()],
* }
* ```
*/
export default AST.webpack as typeof AST.webpack
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"types": ["node"],
"strict": true,
"noUnusedLocals": true,
"noEmit": true,
"declaration": true,
"isolatedDeclarations": true,
"esModuleInterop": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
},
"include": ["src", "tests"]
Expand Down

0 comments on commit cca6c69

Please sign in to comment.