Skip to content

Commit

Permalink
feat(rollup): Add rollup plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lgollut committed Aug 15, 2024
1 parent 03778bc commit c26aa06
Show file tree
Hide file tree
Showing 23 changed files with 1,224 additions and 787 deletions.
1,649 changes: 925 additions & 724 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@
"packages/class-prefixer-core",
"packages/esbuild-plugin-ast",
"packages/esbuild-plugin-ast-vue",
"packages/postcss-class-prefixer"
"packages/postcss-class-prefixer",
"packages/rollup-plugin-ast-transform"
],
"devDependencies": {
"@jest/types": "^29.6.3",
"@liip-lausanne/eslint-config": "^1.0.0",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"lerna": "^8.1.8",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"prettier": "^3.3.3",
"ts-jest": "^29.2.4",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
}
7 changes: 4 additions & 3 deletions packages/ast-parsers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"acorn": "^8.10.0",
"acorn": "^8.12.1",
"astring": "^1.8.6",
"estraverse": "^5.3.0"
"estraverse": "^5.3.0",
"source-map": "^0.6.1"
},
"devDependencies": {
"@types/estraverse": "^5.1.4"
"@types/estraverse": "^5.1.7"
}
}
10 changes: 7 additions & 3 deletions packages/ast-parsers/src/__tests__/js-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { jsParser } from '../js-parser';
describe('jsParser', () => {
const source = 'const value = 1;';
it('should return the source if no valid visitor is provided', () => {
expect(jsParser(source, {})).toContain(source);
expect(jsParser(source, 'test.js', {}).code).toContain(source);
});

it('should transform the source if visitor is a valid visitor object', () => {
Expand All @@ -23,7 +23,9 @@ describe('jsParser', () => {
},
};

expect(jsParser(source, visitor)).toMatch('const value = 2;');
expect(jsParser(source, 'test.js', visitor).code).toMatch(
'const value = 2;',
);
});

it('should transform the source if multiple visitor are passed', () => {
Expand Down Expand Up @@ -55,6 +57,8 @@ describe('jsParser', () => {
},
];

expect(jsParser(source, visitors)).toMatch('const expected = 2;');
expect(jsParser(source, 'test.js', visitors).code).toMatch(
'const expected = 2;',
);
});
});
13 changes: 2 additions & 11 deletions packages/ast-parsers/src/index.ts
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;
52 changes: 37 additions & 15 deletions packages/ast-parsers/src/js-parser.ts
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 };
}
5 changes: 5 additions & 0 deletions packages/ast-parsers/src/ts-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as ts from 'typescript';

import { isFunction } from './utils';

export type AstParserTsTransformers =
| ts.TransformerFactory<ts.Node>
| ts.TransformerFactory<ts.Node>[]
| undefined;

type TsParserOptions = {
path: string;
source: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/class-prefixer-ast-visitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@liip/class-prefixer-core": "^0.2.0"
},
"devDependencies": {
"@types/estree": "^1.0.2",
"@types/estree": "^1.0.5",
"estraverse": "^5.3.0"
}
}
2 changes: 1 addition & 1 deletion packages/class-prefixer-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/css-selector-tokenizer": "^0.7.2"
"@types/css-selector-tokenizer": "^0.7.4"
},
"dependencies": {
"css-selector-tokenizer": "^0.8.0"
Expand Down
8 changes: 4 additions & 4 deletions packages/esbuild-plugin-ast-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/convert-source-map": "^2.0.1",
"@types/hash-sum": "^1.0.0",
"esbuild": "^0.19.4"
"@types/convert-source-map": "^2.0.3",
"@types/hash-sum": "^1.0.2",
"esbuild": "^0.19.12"
},
"dependencies": {
"@liip/ast-parsers": "^0.1.0",
"@liip/esbuild-plugin-ast": "0.3.x",
"@vue/compiler-sfc": "^3.3.4",
"@vue/compiler-sfc": "^3.4.38",
"convert-source-map": "^2.0.0",
"hash-sum": "^2.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/esbuild-plugin-ast-vue/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function esbuildAstParserVue({
}

return {
contents: jsParser(code, availableVisitors),
contents: jsParser(code, args.path, availableVisitors).code,
errors: error,
resolveDir: dirname,
loader: 'js',
Expand All @@ -158,7 +158,7 @@ export function esbuildAstParserVue({
});

return {
contents: jsParser(code, templateVisitor),
contents: jsParser(code, args.path, templateVisitor).code,
pluginData: { code },
errors,
resolveDir: dirname,
Expand Down
5 changes: 2 additions & 3 deletions packages/esbuild-plugin-ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@liip/ast-parsers": "^0.1.0",
"@liip/class-prefixer-core": "^0.2.0"
"@liip/ast-parsers": "^0.1.0"
},
"devDependencies": {
"esbuild": "^0.19.4"
"esbuild": "^0.19.12"
},
"peerDependencies": {
"esbuild": "0.19.x"
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-ast/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function esbuildAstParser({
}

return {
contents: jsParser(source, visitors),
contents: jsParser(source, args.path, visitors).code,
loader: 'js',
};
});
Expand Down
10 changes: 5 additions & 5 deletions packages/postcss-class-prefixer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/clean-css": "^4.2.8",
"@types/css-selector-tokenizer": "^0.7.2",
"clean-css": "^5.3.2",
"postcss": "^8.4.31",
"postcss-nested": "^6.0.1"
"@types/clean-css": "^4.2.11",
"@types/css-selector-tokenizer": "^0.7.4",
"clean-css": "^5.3.3",
"postcss": "^8.4.41",
"postcss-nested": "^6.2.0"
},
"dependencies": {
"@liip/class-prefixer-core": "^0.2.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/rollup-plugin-ast-transform/README.md
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
```
12 changes: 12 additions & 0 deletions packages/rollup-plugin-ast-transform/jest.config.ts
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],
};
46 changes: 46 additions & 0 deletions packages/rollup-plugin-ast-transform/package.json
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"
}
}
Loading

0 comments on commit c26aa06

Please sign in to comment.