storybook-onbook-plugin 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/component-loc/component-loc.d.ts +7 -0
- package/dist/component-loc/component-loc.d.ts.map +1 -0
- package/dist/component-loc/component-loc.js +58 -0
- package/dist/component-loc/index.d.ts +2 -0
- package/dist/component-loc/index.d.ts.map +1 -0
- package/dist/component-loc/index.js +1 -0
- package/dist/screenshot-service/types.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
type ComponentLocPluginOptions = {
|
|
3
|
+
include?: RegExp;
|
|
4
|
+
};
|
|
5
|
+
export declare function componentLocPlugin(options?: ComponentLocPluginOptions): Plugin;
|
|
6
|
+
export default componentLocPlugin;
|
|
7
|
+
//# sourceMappingURL=component-loc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component-loc.d.ts","sourceRoot":"","sources":["../../src/component-loc/component-loc.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,KAAK,yBAAyB,GAAG;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,yBAA8B,GAAG,MAAM,CAgFlF;AAED,eAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import generateModule from '@babel/generator';
|
|
3
|
+
import { parse } from '@babel/parser';
|
|
4
|
+
import traverseModule from '@babel/traverse';
|
|
5
|
+
import * as t from '@babel/types';
|
|
6
|
+
export function componentLocPlugin(options = {}) {
|
|
7
|
+
const include = options.include ?? /\.(jsx|tsx)$/;
|
|
8
|
+
// @babel/traverse and @babel/generator are CommonJS modules with default exports.
|
|
9
|
+
// When imported in an ESM context, the default export may be on `.default` or directly on the module.
|
|
10
|
+
// This workaround handles both cases to ensure compatibility across different bundler configurations.
|
|
11
|
+
const traverse = traverseModule.default ??
|
|
12
|
+
traverseModule;
|
|
13
|
+
const generate = generateModule.default ??
|
|
14
|
+
generateModule;
|
|
15
|
+
let root;
|
|
16
|
+
return {
|
|
17
|
+
name: 'onbook-component-loc',
|
|
18
|
+
enforce: 'pre',
|
|
19
|
+
apply: 'serve',
|
|
20
|
+
configResolved(config) {
|
|
21
|
+
root = config.root;
|
|
22
|
+
},
|
|
23
|
+
transform(code, id) {
|
|
24
|
+
const filepath = id.split('?', 1)[0];
|
|
25
|
+
if (!filepath || filepath.includes('node_modules'))
|
|
26
|
+
return null;
|
|
27
|
+
if (!include.test(filepath))
|
|
28
|
+
return null;
|
|
29
|
+
const ast = parse(code, {
|
|
30
|
+
sourceType: 'module',
|
|
31
|
+
plugins: ['jsx', 'typescript'],
|
|
32
|
+
sourceFilename: filepath,
|
|
33
|
+
});
|
|
34
|
+
let mutated = false;
|
|
35
|
+
// Get relative path from project root
|
|
36
|
+
const relativePath = path.relative(root, filepath);
|
|
37
|
+
traverse(ast, {
|
|
38
|
+
JSXElement(nodePath) {
|
|
39
|
+
const opening = nodePath.node.openingElement;
|
|
40
|
+
const element = nodePath.node;
|
|
41
|
+
if (!opening.loc || !element.loc)
|
|
42
|
+
return;
|
|
43
|
+
const alreadyTagged = opening.attributes.some((attribute) => t.isJSXAttribute(attribute) &&
|
|
44
|
+
attribute.name.name === 'data-component-file');
|
|
45
|
+
if (alreadyTagged)
|
|
46
|
+
return;
|
|
47
|
+
opening.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-component-file'), t.stringLiteral(relativePath)), t.jsxAttribute(t.jsxIdentifier('data-component-start'), t.stringLiteral(`${element.loc.start.line}:${element.loc.start.column}`)), t.jsxAttribute(t.jsxIdentifier('data-component-end'), t.stringLiteral(`${element.loc.end.line}:${element.loc.end.column}`)));
|
|
48
|
+
mutated = true;
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
if (!mutated)
|
|
52
|
+
return null;
|
|
53
|
+
const output = generate(ast, { retainLines: true, sourceMaps: true, sourceFileName: id }, code);
|
|
54
|
+
return { code: output.code, map: output.map };
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export default componentLocPlugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/component-loc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { componentLocPlugin } from './component-loc.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|