Skip to content

Commit 0039258

Browse files
committed
feat: source-ref support rollup
1 parent b8e2c2b commit 0039258

File tree

9 files changed

+173
-27
lines changed

9 files changed

+173
-27
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"pnpm": {
2121
"peerDependencyRules": {
2222
"ignoreMissing": [
23-
"acorn"
23+
"acorn",
24+
"acorn-dynamic-import"
2425
]
2526
}
2627
},

packages/plugin-declaration-generator/src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import traverse from '@babel/traverse';
33
import generate from '@babel/generator';
44
import template from '@babel/template';
55
import type { Comment } from '@babel/types';
6+
import { program } from '@babel/types';
67
import fs from 'fs-extra';
78
import _ from 'lodash';
89

910
const babelPlugins: ParserPlugin[] = ['jsx', 'typescript'];
1011
const buildNamedExport = template('export function %%name%%(): any', {
11-
plugins: babelPlugins as any,
12+
plugins: babelPlugins,
1213
});
1314

1415
interface Options {
@@ -26,10 +27,7 @@ export async function generateFunctionDeclare(options: Options) {
2627
});
2728
});
2829

29-
const code = generate({
30-
type: 'Program',
31-
body: _.flatten(astList),
32-
} as any).code;
30+
const code = generate(program(_.flatten(astList))).code;
3331

3432
return code;
3533
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## source-ref for webpack loader
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "rollup-plugin-source-ref",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "dist/index.js",
6+
"files": [
7+
"dist"
8+
],
9+
"scripts": {
10+
"build": "tsc",
11+
"prepare": "tsc"
12+
},
13+
"keywords": [
14+
"sourcecode",
15+
"pointer",
16+
"webpack"
17+
],
18+
"author": "moonrailgun",
19+
"license": "MIT",
20+
"devDependencies": {
21+
"@types/babel__generator": "^7.6.4",
22+
"@types/babel__traverse": "^7.17.1",
23+
"@types/node": "^15.12.5",
24+
"@types/webpack": "^5.28.0",
25+
"rollup": "^2.75.0",
26+
"ts-node": "^10.0.0",
27+
"typescript": "^4.5.2",
28+
"webpack": "^5.72.0",
29+
"webpack-test-utils": "^1.1.0"
30+
},
31+
"dependencies": {
32+
"@babel/generator": "^7.17.7",
33+
"@babel/parser": "^7.17.7",
34+
"@babel/traverse": "^7.17.3",
35+
"@babel/types": "^7.17.10"
36+
}
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Plugin } from 'rollup';
2+
import { parse } from '@babel/parser';
3+
import traverse from '@babel/traverse';
4+
import generate from '@babel/generator';
5+
import { jsxAttribute, jsxIdentifier, stringLiteral } from '@babel/types';
6+
7+
const TRACE_ID = 'data-source';
8+
9+
export default function sourceRef(): Plugin {
10+
return {
11+
name: 'source-ref',
12+
transform(code, id) {
13+
const filepath = id;
14+
15+
const ast = parse(code, {
16+
sourceType: 'module',
17+
plugins: ['jsx', 'typescript'],
18+
});
19+
20+
traverse(ast, {
21+
JSXOpeningElement(path) {
22+
const location = path.node.loc;
23+
if (!location) {
24+
return;
25+
}
26+
27+
if (Array.isArray(location)) {
28+
return;
29+
}
30+
31+
const line = location.start.line;
32+
const col = location.start.column;
33+
34+
const attrs = path.node.attributes;
35+
for (let i = 0; i < attrs.length; i++) {
36+
const attr = attrs[i];
37+
if (attr.type === 'JSXAttribute' && attr.name.name === TRACE_ID) {
38+
// existed
39+
return;
40+
}
41+
}
42+
43+
const traceId = `${filepath}:${line}:${col}`;
44+
45+
attrs.push(
46+
jsxAttribute(jsxIdentifier(TRACE_ID), stringLiteral(traceId))
47+
);
48+
},
49+
});
50+
51+
const res = generate(ast);
52+
53+
return { code: res.code, map: res.map };
54+
},
55+
};
56+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"module": "commonjs",
5+
"target": "ES5",
6+
"lib": ["ESNext"],
7+
"declaration": true,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": true,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true
12+
},
13+
"include": ["src"]
14+
}

pnpm-lock.yaml

Lines changed: 45 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)