FranckFreiburger / vue3-sfc-loader

Single File Component loader for Vue2 and Vue3. Load .vue files directly from your HTML. No node.js environment, no build step.
MIT License
1.03k stars 116 forks source link

Php file in place of Vue file #22

Closed shaan1974 closed 3 years ago

shaan1974 commented 3 years ago

Hello,

First thanks for this great tool.

My problem. I just want to use a .php file in place of a .vue file into the loadModule function but it's not working. it told me : "TypeError: Unable to handle .php files (js/vue/components/hello_world/test.php), see additionalModuleHandlers"

i'v try to add in config:

additionalModuleHandlers: { '.php': function(source, path, options) { return source; } }

But it's not working at all. May be i'm doing some wrong ?

Thanks

FranckFreiburger commented 3 years ago

Hi shaan1974,

To handle this situation, your getFile() should inform vue3-sfc-loader that these .php files generate .vue content :

    async getFile(url) {

      const res = await fetch(url);
      if ( !res.ok )
        throw Object.assign(new Error(url + " " + res.statusText), { res });
      const content = await res.text();
      return /\/vue\/components\/.*?\.php/.test(url) ? { content: content, extname: ".vue" } : content;
    },

Think extname like a kind of mimetype. Here we tell vue3-sfc-loader that vue/components/**/*.php generate SFC content.

shaan1974 commented 3 years ago

Thanks a lot for you help you save my live ;) By the way nice job, because i'm not a big fan of Cli tools.

FranckFreiburger commented 3 years ago

I'm closing this as answered. Discussion can continue if needed.