aribouius/asset-require-hook

Support ES6 modules

Closed this issue · 5 comments

First of all, thank you for this wonderful thing.

It works perfectly with const logo = require('./logo.png') but does not seem to work with import logo from './logo.png'

I am using it with typescript where es6 imports are standard thing.

This is not an issue with this project, you need to use babel to transpile your code for node.js execution. Take a look at babel-register (https://babeljs.io/docs/en/babel-register)

Typescript already transpiles the code into an es5 compatible javascript. There should be no reason to bring babel as well.

If your node.js version does not support es5 import syntax then you will need babel to transpile import syntax to require syntax

Hey @artemave this library was originally written to complement webpack when doing server-side rendering without a separate server build. I'm not a typescript user so my knowledge of how it might interact with this library is thin, but if you are already transpiling your code there is likely a better option out there for handling non JS imports.

This library depends on a deprecated node feature, which I vaguely recall being not supported for native ES imports, so this may simply not be possible. Happy to look into it though.

Turns out this my issue had nothing to do with this library, but everything to do with the way (or multiple ways) typescript generates javascript.

In the end, adding "esModuleInterop": true to tsconfig.json solved it.

Thank you for your help!