admin管理员组文章数量:1353228
I was trying to insert a ImportDeclaration into a snippets of JavaScript code with Babel.js:
const babel = require('babel-core')
const t = babel.types
const traverse = babel.traverse
const template = babel.template
const generate = require('babel-generator').default
const babylon = require('babylon')
const code = [
"import A from 'a'",
"import B from 'b'",
"export default {",
" ponents: {",
" },",
" methods: {",
" init () {",
" }",
" }",
"}"
].join("\n")
console.log(code)
const ast = babylon.parse(code, {
sourceType: 'module'
})
var n = []
traverse(ast, {
ImportDeclaration: {
exit(path) {
n.push(path)
}
}
})
const name = 'UserDialog',
src = './user-dialog.vue'
if (n.length) {
const importCode = "import " + name + " from '" + src + "'"
console.log(importCode)
const importAst = template(importCode, {
sourceType: 'module'
})()
// append to last import statement
n[n.length - 1].insertAfter(importAst);
console.log(generate(ast).code)
}
I was trying to insert a ImportDeclaration into a snippets of JavaScript code with Babel.js:
const babel = require('babel-core')
const t = babel.types
const traverse = babel.traverse
const template = babel.template
const generate = require('babel-generator').default
const babylon = require('babylon')
const code = [
"import A from 'a'",
"import B from 'b'",
"export default {",
" ponents: {",
" },",
" methods: {",
" init () {",
" }",
" }",
"}"
].join("\n")
console.log(code)
const ast = babylon.parse(code, {
sourceType: 'module'
})
var n = []
traverse(ast, {
ImportDeclaration: {
exit(path) {
n.push(path)
}
}
})
const name = 'UserDialog',
src = './user-dialog.vue'
if (n.length) {
const importCode = "import " + name + " from '" + src + "'"
console.log(importCode)
const importAst = template(importCode, {
sourceType: 'module'
})()
// append to last import statement
n[n.length - 1].insertAfter(importAst);
console.log(generate(ast).code)
}
But I got the following error
What's the proper way to do this?
FYI:
You can get above code from git clone https://github./aztack/babel-test.git
-
I also tried to
path.insertAfter(importAst)
inexit
method. But It will cause thetraverse
go into an infinite loop, since I just inserted aImportDeclaration
... – aztack Commented Dec 27, 2017 at 14:06
1 Answer
Reset to default 13You'd be best off writing this as a Babel plugin, e.g.
const babel = require('babel-core');
const code = [
"import A from 'a'",
"import B from 'b'",
"export default {",
" ponents: {",
" },",
" methods: {",
" init () {",
" }",
" }",
"}"
].join("\n");
const result = babel.transform(code, {
plugins: [myImportInjector]
});
console.log(result.code);
function myImportInjector({ types, template }) {
const myImport = template(`import UserDialog from "./user-dialog";`, {sourceType: "module"});
return {
visitor: {
Program(path, state) {
const lastImport = path.get("body").filter(p => p.isImportDeclaration()).pop();
if (lastImport) lastImport.insertAfter(myImport());
},
},
};
}
本文标签: javascriptHow to insert import statement into AST with BabeljsStack Overflow
版权声明:本文标题:javascript - How to insert import statement into AST with Babel.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743853427a2550429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论