Skip to content

Instantly share code, notes, and snippets.

@thomhines
Last active February 14, 2021 19:07
Show Gist options
  • Save thomhines/3d49f2791073162b4eb3237127d2639a to your computer and use it in GitHub Desktop.
Save thomhines/3d49f2791073162b4eb3237127d2639a to your computer and use it in GitHub Desktop.
Shell script to convert Vue single-file component files (.vue) into vanilla JS Vue components
# In shell, run `sh vue_sfc.sh inputfile.vue outputfile.js`
# HTML template and JS conversion only (doesn't do CSS), but it *can* do multiple components in one file.
# Note: you need to wrap your components in a non-standard tag `<component name="component_name">...</component>`
cat $1 | \
perl -00 -pe 's|\n\n|\n|gs' | \
perl -00 -pe 's|<component name="(.*?)">|Vue.component("\1", {\n\ttemplate:|gs' | \
perl -00 -pe 's|<template>(.*?)<\/template>|`\1`,|gs' | \
perl -00 -pe 's|<script>.*?{| |gs' | \
perl -00 -pe 's|<\/script>.*?</component>|);\n|gs' | \
perl -00 -pe 's|<!--.*?-->||gs' \
> $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment