-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,224 additions
and
787 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,3 @@ | ||
import { Visitor } from 'estraverse'; | ||
import * as ts from 'typescript'; | ||
|
||
export { jsParser } from './js-parser'; | ||
export { tsParser } from './ts-parser'; | ||
export { jsParser, transformer, AstParserVisitors } from './js-parser'; | ||
export { tsParser, AstParserTsTransformers } from './ts-parser'; | ||
export { loadTsConfig, isFunction } from './utils'; | ||
|
||
export type AstParserVisitors = Visitor | Visitor[] | undefined; | ||
export type AstParserTsTransformers = | ||
| ts.TransformerFactory<ts.Node> | ||
| ts.TransformerFactory<ts.Node>[] | ||
| undefined; |
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 |
---|---|---|
@@ -1,44 +1,66 @@ | ||
import { Parser } from 'acorn'; | ||
import { generate } from 'astring'; | ||
import { replace, Visitor } from 'estraverse'; | ||
import { SourceMapGenerator } from 'source-map'; | ||
|
||
import { isFunction } from './utils'; | ||
|
||
import type { Node } from 'estree'; | ||
|
||
const isFunction = (v: unknown) => | ||
[ | ||
'[object Function]', | ||
'[object GeneratorFunction]', | ||
'[object AsyncFunction]', | ||
'[object Promise]', | ||
].includes(Object.prototype.toString.call(v)); | ||
export type AstParserVisitors = Visitor | Visitor[] | undefined; | ||
|
||
/** | ||
* Create an AST representation of the provided source and use the | ||
* visitor object to transform it if provided | ||
*/ | ||
export function jsParser(source: string, visitors?: Visitor | Visitor[]) { | ||
export function jsParser( | ||
source: string, | ||
file: string, | ||
visitors?: AstParserVisitors, | ||
) { | ||
if (!visitors) { | ||
/* eslint-disable-next-line no-console */ | ||
console.warn( | ||
'[esbuildPluginAst]: No javascript visitor provided, the plugin will have no effect.', | ||
); | ||
return source; | ||
return { code: source }; | ||
} | ||
|
||
const visitorArray = Array.isArray(visitors) ? visitors : [visitors]; | ||
|
||
let ast = Parser.parse(source, { | ||
const ast = Parser.parse(source, { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
}) as Node; | ||
|
||
return transformer({ ast, file, visitors }); | ||
} | ||
|
||
type TransformerOptions<N extends Node> = { | ||
ast: N; | ||
file: string; | ||
visitors?: AstParserVisitors; | ||
}; | ||
|
||
export function transformer<N extends Node>({ | ||
ast, | ||
file, | ||
visitors, | ||
}: TransformerOptions<N>) { | ||
const visitorArray = Array.isArray(visitors) ? visitors : [visitors]; | ||
|
||
let newAst = ast; | ||
|
||
for (const visitor of visitorArray) { | ||
if (!isFunction(visitor.enter) && !isFunction(visitor.leave)) { | ||
if ( | ||
typeof visitor === 'undefined' || | ||
(!isFunction(visitor?.enter) && !isFunction(visitor?.leave)) | ||
) { | ||
continue; | ||
} | ||
|
||
ast = replace(ast, visitor); | ||
newAst = replace(newAst, visitor) as N; | ||
} | ||
|
||
return generate(ast); | ||
const map = new SourceMapGenerator({ file }); | ||
|
||
return { code: generate(newAst, { sourceMap: map }), ast: newAst, map }; | ||
} |
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
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,9 @@ | ||
# rollup-plugin-ast-trasform | ||
|
||
Plugin to transform JavaScript and TypeScript AST with rollup | ||
|
||
## Installation | ||
|
||
``` | ||
npm i -D @liip/rollup-plugin-ast-transform | ||
``` |
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 @@ | ||
import base from '../../jest.base.config'; | ||
|
||
const id = 'rollup-plugin-ast-transform'; | ||
const root = `<rootDir>/packages/${id}`; | ||
|
||
module.exports = { | ||
...base, | ||
id, | ||
displayName: id, | ||
rootDir: '../..', | ||
roots: [root], | ||
}; |
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,46 @@ | ||
{ | ||
"name": "@liip/rollup-plugin-ast-transform", | ||
"version": "0.1.0", | ||
"description": "Plugin to transform JavaScript and TypeScript AST with rollup", | ||
"keywords": [ | ||
"rollup", | ||
"plugin", | ||
"ast", | ||
"transform" | ||
], | ||
"author": { | ||
"name": "Liip", | ||
"url": "https://www.liip.ch/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/liip/class-prefixer.git", | ||
"directory": "packages/rollup-plugin-ast-transform" | ||
}, | ||
"publishConfig": { | ||
"@liip:registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"main": "dist/plugin.js", | ||
"files": [ | ||
"/dist" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "tsc -w -p ./tsconfig.build.json", | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"lint": "eslint \"src/**/*.ts\"", | ||
"lint:fix": "npm run lint -- --fix", | ||
"format": "prettier '**/!(dist)/*.@(ts|json|md)' --check", | ||
"format:fix": "npm run format -- --write", | ||
"test": "NODE_ENV=test jest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@liip/ast-parsers": "^0.1.0", | ||
"@rollup/pluginutils": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/rollup": "^0.51.4" | ||
} | ||
} |
Oops, something went wrong.