Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting "TypeError: t is not a function" when trying to replicate script import issue #209

Open
SMen1 opened this issue Nov 14, 2024 · 1 comment
Labels
question Further information is requested

Comments

@SMen1
Copy link

SMen1 commented Nov 14, 2024

I was getting the following error when trying to use vue.js composables and import a script js file.

'import' and 'export' may appear only with 'sourceType: "module"' (1:0)

I have tried to replicate the solutions here:
#44 and here
#14 (comment)

Specifically this solution:

async getFile(url) {
const res = await fetch(url);
if (!res.ok) throw Object.assign(new Error(url + " " + res.statusText), { res });
let content = await res.text();
content = url.endsWith("/test.js") ? { content: content, type: ".mjs" } : content;
return content;
},

Where the test.js file is :

export const user = {
userName: "root",
password: "zfs1111"
}

is giving me the error: TypeError: t is not a function in the console. I get the same error trying to import a non-test js file.

Is this an old solution that is no longer viable? What is the new solution if yes?

using Vue3.js

Versions

  • Browser (name & version):
  • vue3-sfc-loader:

Additional context

@SMen1 SMen1 added bug Something isn't working unconfirmed Unconfirmed issue labels Nov 14, 2024
@FranckFreiburger
Copy link
Owner

FranckFreiburger commented Nov 22, 2024

hello SMen1,

yes, the solution you are talking about is quite old
{ content: content, type: ".mjs" } is not valid any more.

In order to handle binary files (ex. png) I managed to change 'content' property into
getContentData: (asBinary: Boolean) => Promise<ContentData>

Now, your option.getFile function should look like this:

      async getFile(url) {
        
        const res = await fetch(url);
        if ( !res.ok )
          throw Object.assign(new Error(res.statusText + ' ' + url), { res });

        // make test.js be treated as an mjs file
        if ( url.endsWith("/test.js") ) {
          return {
              getContentData: async (asBinary) => { // asBinary is unused here, we know it is a text file
                
                return await res.text();
              },
              type: ".mjs",
          }          
        }

        return {
          getContentData: asBinary => asBinary ? res.arrayBuffer() : res.text(),
        }
      },

@FranckFreiburger FranckFreiburger added question Further information is requested and removed bug Something isn't working unconfirmed Unconfirmed issue labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants