question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scripts with a src attribute that's not a known extension aren't recognized as js

See original GitHub issue

Describe the bug

I am writing a vite plugin for Imba and it works great. However, I cannot use <script type="module" src="/src/main.imba"></script> in the index.html.

The browser complains about

main.imba:1 
        
       Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Note that other imports work fine

Reproduction

https://stackblitz.com/edit/vitejs-vite-nxtcgv?file=vite.config.js,index.html,main.imba&terminal=dev

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 331.89 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.11.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Chrome Canary: 107.0.5275.0
    Safari: 15.5

Used Package Manager

npm

Logs

Click to expand!
       Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jonaskuskecommented, Sep 4, 2022

It would be nice if we could read the Sec-Fetch-Dest header as part of isJSRequest and apply transforms if it is set to script, but of course Safari doesn’t support it: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Dest

Edit: the webkit bug for the implementation was fixed earlier this year and it ships in the Safari nightly builds, according to bugzilla: https://bugs.webkit.org/show_bug.cgi?id=204744. Also, Vite already reads sec-fetch-dest in the indexHtml middleware for the spa fallback

@patak-dev, what do you think?

1reaction
jonaskuskecommented, Sep 2, 2022

This code is guarding the transform:

if (
  isJSRequest(url) ||
  isImportRequest(url) ||
  isCSSRequest(url) ||
  isHTMLProxy(url)
) {
// transform request, call plugins, ...
}

Since isJSRequest is not customizable, it returns false for .imba files. However, you can take advantage of the second condition and mark the request as an import by adding the ?import query param:

<script type="module" src="./main.imba?import"></script>

With this it works without any additional config! (besides the .imba file transform)

And as per https://github.com/vitejs/vite/pull/3828#issuecomment-875403352, maybe suggest adding .imba to the known JS type extensions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get the content of the file specified as the 'src' of a ...
I've looked to use this technique for client side error logging (of javascript exceptions) after getting "undefined variables" which aren't ...
Read more >
Working with JavaScript in Visual Studio Code
Working with JavaScript. This topic describes some of the advanced JavaScript features supported by Visual Studio Code. Using the TypeScript language ...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
Content Security Policy (CSP) Bypass - HackTricks
script -src: This directive specifies allowed sources for JavaScript. This includes not only URLs loaded directly into elements, but also things like inline ......
Read more >
What causes a Script error and how to solve them · Raygun Blog
Script errors occur when a client-side script that violates the same-origin policy of the user's browser by making an invalid cross-origin ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found