Skip to content

Commit 4951587

Browse files
committed
transform: Add ability to pass ast instead of source string and return ast as well
Eventually, vite will gain support for being a little smarter with working with js-side AST, so let's prepare for that.
1 parent b3362ea commit 4951587

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

transform/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,20 @@ export const checkImport = (node, regex) => {
783783
};
784784

785785
export const createTransform = transformBabelAST => (source, options) => {
786-
const ast = parser.parse(source, {sourceType: 'module', ...options, code: false, ast: true});
786+
let ast;
787+
if (typeof source === 'string') {
788+
ast = parser.parse(source, {sourceType: 'module', ...options, code: false, ast: true});
789+
} else {
790+
ast = source;
791+
}
787792

788793
transformBabelAST(ast, options);
789794

790-
return generate.default(ast, {
795+
const output = generate.default(ast, {
791796
sourceMaps: true,
792797
...options,
793798
}, source);
799+
800+
output.ast = ast;
801+
return output;
794802
};

0 commit comments

Comments
 (0)