[Vuejs]-Export modules to objects, shell script

0๐Ÿ‘

โœ…

Iโ€™d use a here-doc as a template, rather than using sed.

allcomponents=()
for component in components/*
do
    component=${component##*/} # remove directory prefix
    allcomponents+=("$component")
    cat <<EOS
const $component = {}
const req$component = require.context('@/components/$component', true, /\.vue\$/)
req$component.keys().forEach(fileName => {
  const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '\$1')
  atoms[componentName] = req$component(fileName).default
})

EOS
done > exports.js
echo "export default {" >> exports.js
printf "  %s,\n" "${allcomponents[@]}" >> exports.js
echo "}" >> exports.js


Leave a comment