[Vuejs]-Vuejs how to compile single file component to js object

0👍

You can use vue-template-compiler package:

Install it:

npm i vue-template-compiler

Use it (test.vue is a component from your example):

const fs = require('fs');
const compiler = require('vue-template-compiler');

const content = fs.readFileSync('./test.vue', 'utf-8');

const res = compiler.parseComponent(content);
const template = res.template.content.trim();

const result = { template };
console.log(result);

Result:

{ template: '<div>\n  Hello!\n</div>' }

Don’t forget to add error checks to your code.

Leave a comment