Skip to content

Instantly share code, notes, and snippets.

@degt degt/convert.js
Last active Sep 3, 2018

Embed
What would you like to do?
Convert .VUE files to .js
const compiler = require('vue-template-compiler');
const babel = require("@babel/core");
let content = '';
process.stdin.resume();
process.stdin.on('data', buf => {
content += buf.toString();
});
process.stdin.on('end', () => {
const parsed = compiler.parseComponent(content);
const template = parsed.template ? parsed.template.content : '';
const script = parsed.script ? parsed.script.content : '';
const templateEscaped = template.trim().replace(/`/g, '\\`');
const scriptWithTemplate = script.match(/export default ?\{/)
? script.replace(/export default ?\{/, `$&\n\ttemplate: \`\n${templateEscaped}\`,`)
: `${script}\n export default {\n\ttemplate: \`\n${templateEscaped}\`};`;
const result = babel.transform(scriptWithTemplate, {
compact: false,
presets: ["@babel/env"],
plugins: ["transform-es2015-template-literals", "transform-es2015-literals"]
});
process.stdout.write(result.code);
// process.stdout.write(scriptWithTemplate);
});
find './src/components' -name '*.vue' -exec bash -c 'node convert.js < "{}" > "{}.js"' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.