Is there a way to convert .mjs files to .js? Not all hosts support mjs files yet, so I'd like to convert mjs files to js files.
Background: Mozilla's PDFjs updated their code to use JavaScript modules (mjs), however I'm wrapping PDFjs in a WordPress plugin, which means anyone can install it, but most hosts don't support mjs files yet. Asking thousands of users to update their hosting to support .mjs files isn't a great option either.
MDN suggests there is no need to convert, but to simply use the .js
file extension as opposed to the .mjs
file extension. This will still use the correct mime type and allow the functionality to remain. The trade-off is that it bucks conventions in exchange for larger compatibility.
From "JavaScript modules"MDN :
Aside — .mjs versus .jsMDN
However, we decided to keep using .js, at least for the moment. To get modules to work correctly in a browser, you need to make sure that your server is serving them with a Content-Type header that contains a JavaScript MIME type such as text/javascript. If you don't, you'll get a strict MIME type checking error along the lines of "The server responded with a non-JavaScript MIME type" and the browser won't run your JavaScript. Most servers already set the correct type for .js files, but not yet for .mjs files. Servers that already serve .mjs files correctly include GitHub Pages and http-server for Node.js.