chore: source-ref add React.Fragment support

ignore it
pull/81/head
moonrailgun 3 years ago
parent 8d6db59aee
commit 01b3c11bb9

@ -2,9 +2,9 @@
"name": "rollup-plugin-source-ref", "name": "rollup-plugin-source-ref",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "dist/index.js", "main": "lib/index.js",
"files": [ "files": [
"dist" "lib"
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",

@ -2,7 +2,13 @@ import type { Plugin } from 'rollup';
import { parse } from '@babel/parser'; import { parse } from '@babel/parser';
import traverse from '@babel/traverse'; import traverse from '@babel/traverse';
import generate from '@babel/generator'; import generate from '@babel/generator';
import { jsxAttribute, jsxIdentifier, stringLiteral } from '@babel/types'; import {
isJSXIdentifier,
isJSXMemberExpression,
jsxAttribute,
jsxIdentifier,
stringLiteral,
} from '@babel/types';
const TRACE_ID = 'data-source'; const TRACE_ID = 'data-source';
@ -28,6 +34,17 @@ export default function sourceRef(): Plugin {
return; return;
} }
const name = path.node.name;
if (isJSXIdentifier(name) && name.name === 'Fragment') {
return;
}
if (
isJSXMemberExpression(name) &&
name.property.name === 'Fragment'
) {
return;
}
const line = location.start.line; const line = location.start.line;
const col = location.start.column; const col = location.start.column;

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "dist", "outDir": "lib",
"module": "commonjs", "module": "commonjs",
"target": "ES5", "target": "ES5",
"lib": ["ESNext"], "lib": ["ESNext"],

@ -2,9 +2,9 @@
"name": "source-ref-loader", "name": "source-ref-loader",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "dist/index.js", "main": "lib/index.js",
"files": [ "files": [
"dist" "lib"
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",

@ -2,7 +2,13 @@ import type { LoaderContext } from 'webpack';
import { parse } from '@babel/parser'; import { parse } from '@babel/parser';
import traverse from '@babel/traverse'; import traverse from '@babel/traverse';
import generate from '@babel/generator'; import generate from '@babel/generator';
import { jsxAttribute, jsxIdentifier, stringLiteral } from '@babel/types'; import {
isJSXIdentifier,
isJSXMemberExpression,
jsxAttribute,
jsxIdentifier,
stringLiteral,
} from '@babel/types';
const TRACE_ID = 'data-source'; const TRACE_ID = 'data-source';
@ -37,6 +43,14 @@ async function loader(this: LoaderContext<any>, source: string): Promise<void> {
return; return;
} }
const name = path.node.name;
if (isJSXIdentifier(name) && name.name === 'Fragment') {
return;
}
if (isJSXMemberExpression(name) && name.property.name === 'Fragment') {
return;
}
const line = location.start.line; const line = location.start.line;
const col = location.start.column; const col = location.start.column;

@ -9,6 +9,8 @@ export const tsx = {
'/src/foo.tsx': ` '/src/foo.tsx': `
export const HelloWorld = <><div>hello world</div></>; export const HelloWorld = <><div>hello world</div></>;
export const HelloWorld2 = <React.Fragment><div>hello world</div></React.Fragment>;
export const HelloWorld3 = <Fragment><div>hello world</div></Fragment>;
export default class Foo { export default class Foo {
render() { render() {
return <div className="class-name">content</div> return <div className="class-name">content</div>

@ -5,12 +5,13 @@ import { configureLoader } from './utils';
async function test() { async function test() {
const built = await build(fixtures.tsx, (config) => { const built = await build(fixtures.tsx, (config) => {
configureLoader(config); configureLoader(config);
});
console.log(built.stats.endTime - built.stats.startTime); console.log('configureLoader(config);', config.module.rules);
});
console.log(built.stats.compilation.errors.length); console.log('usage:', built.stats.endTime - built.stats.startTime, 'ms');
console.log(built.stats.compilation.errors); console.log('errors:', built.stats.compilation.errors.length);
// console.log(built.stats.compilation.errors);
} }
test(); test();

@ -1,14 +1,18 @@
import type { Configuration } from 'webpack'; import type { Configuration } from 'webpack';
import type { DefaultWebpackConfig } from 'webpack-test-utils';
const loaderPath = require.resolve('../src/index.ts'); const loaderPath = require.resolve('../src/index.ts');
export function configureLoader(config: Configuration) { export function configureLoader(config: DefaultWebpackConfig & Configuration) {
config.resolveLoader.alias = { config.resolveLoader.alias = {
'source-pointer-loader': loaderPath, 'source-ref-loader': loaderPath,
}; };
config.module.rules.push({ config.module.rules.push({
test: /\.tsx$/, test: /\.tsx$/,
loader: 'source-pointer-loader', loader: 'source-ref-loader',
options: {
available: true,
},
}); });
} }

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "dist", "outDir": "lib",
"module": "commonjs", "module": "commonjs",
"target": "ES2018", "target": "ES2018",

Loading…
Cancel
Save