@builderx/babel-plugin
Advanced tools
Comparing version
@@ -1,125 +0,40 @@ | ||
const babelParser = require('@babel/parser'); | ||
const nodepath = require('path'); | ||
//const fs = require('fs'); | ||
module.exports = function ({ | ||
types: t | ||
}) { | ||
let entryFile; | ||
return { | ||
pre(state) { | ||
// Store the entry file path | ||
if (!entryFile) { | ||
entryFile = state.opts.entryFile; | ||
} | ||
}, | ||
visitor: { | ||
JSXElement(path, state) { | ||
const { | ||
node | ||
} = path; | ||
if (node.openingElement.name.name !== 'script') { | ||
const { | ||
filename | ||
} = state.file.opts; | ||
const { | ||
start | ||
} = node.loc; | ||
// Construct data-path value | ||
const dataPathValue = `${filename}:${start.line}:${start.column}`; | ||
// Create a new JSX attribute | ||
const dataPathAttribute = t.jsxAttribute(t.jsxIdentifier('data-path'), t.stringLiteral(dataPathValue)); | ||
// Add data-path attribute to the element | ||
if (node.openingElement.attributes) { | ||
node.openingElement.attributes.push(dataPathAttribute); | ||
// node.openingElement.attributes.push(dataSetAttribute); | ||
} else { | ||
// node.openingElement.attributes = [dataPathAttribute, dataSetAttribute]; | ||
} | ||
} | ||
}, | ||
Program(path, state) { | ||
const isEntryFile = filename => { | ||
// if (filename.includes('node_modules')) { | ||
// return false; | ||
// } | ||
const basename = nodepath.basename(filename); | ||
return basename === '_app.js' || basename === '_app.tsx' || basename === 'layout.js' || basename === 'layout.jsx' || basename === 'layout.tsx' || basename === '_layout.js' || basename === '_layout.jsx' || basename === '_layout.tsx' || basename === 'index.js' || basename === 'index.tsx'; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const babelPlugin = function ({ types: t }, options) { | ||
let entryFile; | ||
let metadata = options?.metadata || {}; | ||
return { | ||
pre(state) { | ||
if (!entryFile) { | ||
entryFile = state.opts.entryFile; | ||
} | ||
}, | ||
visitor: { | ||
JSXElement(path, state) { | ||
const { node } = path; | ||
if (node.openingElement.name.name !== 'script') { | ||
const filename = state.file.opts.filename || metadata.fileName; | ||
const { start } = node.loc; | ||
const dataPathValue = `${filename}:${start.line}:${start.column}`; | ||
const dataPathAttribute = t.jsxAttribute(t.jsxIdentifier('data-bx-path'), t.stringLiteral(dataPathValue)); | ||
if (node.openingElement.attributes) { | ||
node.openingElement.attributes.push(dataPathAttribute); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
if (!entryFile && isEntryFile(state.file.opts.filename)) { | ||
entryFile = state.file.opts.filename; | ||
} | ||
}; | ||
exports.default = babelPlugin; | ||
// Check if InspectorTool is already injected | ||
let inspectorToolInjected = false; | ||
const filename = state.file.opts.filename; | ||
}); | ||
// if (filename) { | ||
// // console.log(`Processing file: ${filename}`); | ||
// // Check if this is the entry file | ||
// // You might need additional logic to determine if it's an entry file | ||
// if (filename === entryFile) { | ||
// const fileToInject = nodepath.join( | ||
// __dirname, | ||
// './builderx-tools.js' | ||
// ); | ||
// // Read the content of the file you want to inject | ||
// const fileContent = fs.readFileSync(fileToInject, 'utf8'); | ||
// // import in file | ||
// // Parse the file content to AST nodes using Babel's parser | ||
// const injectedAST = babelParser.parse(fileContent, { | ||
// sourceType: 'module', | ||
// plugins: ['jsx'], | ||
// }); | ||
// path.node.body.unshift(...injectedAST.program.body); | ||
// // Dangerously set innerHTML inside body as a fallback for RSC components | ||
// path.traverse({ | ||
// JSXElement(elementPath) { | ||
// if (elementPath.node.openingElement.name.name === 'body') { | ||
// // Create a new script element | ||
// const scriptElement = t.jsxElement( | ||
// t.jsxOpeningElement( | ||
// t.jsxIdentifier('script'), | ||
// [ | ||
// t.jsxAttribute( | ||
// t.jsxIdentifier('id'), | ||
// t.stringLiteral('my-script') | ||
// ), | ||
// t.jsxAttribute( | ||
// t.jsxIdentifier('dangerouslySetInnerHTML'), | ||
// t.jsxExpressionContainer( | ||
// t.objectExpression([ | ||
// t.objectProperty( | ||
// t.identifier('__html'), | ||
// t.stringLiteral(fileContent) | ||
// ), | ||
// ]) | ||
// ) | ||
// ), | ||
// ], | ||
// true | ||
// ), | ||
// null, | ||
// [] | ||
// ); | ||
// // Add the script element as the last child of the html element | ||
// // elementPath.node.children.unshift(scriptElement); | ||
// } | ||
// }, | ||
// }); | ||
// } | ||
// } | ||
} | ||
} | ||
}; | ||
}; |
{ | ||
"name": "@builderx/babel-plugin", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A Babel plugin for builderx", | ||
@@ -9,3 +9,3 @@ "keywords": [ | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"module": "dist/mindex.js", | ||
"source": "dist/index.js", | ||
@@ -19,4 +19,5 @@ "exports": { | ||
], | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "babel src --out-dir dist --copy-files" | ||
"build": "tsc" | ||
}, | ||
@@ -38,4 +39,5 @@ "license": "ISC", | ||
"@babel/traverse": "^7.20.5", | ||
"lodash.merge": "^4.6.2" | ||
"lodash.merge": "^4.6.2", | ||
"typescript": "^4.x.x" | ||
} | ||
} |
4
33.33%3101
-43.98%8
14.29%62
-44.14%+ Added
+ Added