Last active
September 3, 2018 17:31
-
-
Save degt/6071de4d9b269476ebe48071eabf6eec to your computer and use it in GitHub Desktop.
Convert .VUE files to .js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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