standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Possibility to ignore certain files or file-endings #835

Closed cjh9 closed 4 years ago

cjh9 commented 5 years ago

I'm creating a large isomorphic js-application with node on the backend. Esm is in one instance ending up in some frontend code and is trying to import a svg-file. It is normally imported and inlined by a webpack-svg-loader in the frontend.

import svgicon from 'app/img/svg/icon-svg.svg'

Error message:

/myisomorphicapplication/app/img/svg/icon-svg.svg:1
<svg id="icon-svg" 
^

SyntaxError: Unexpected token <
    at Object.compileFunction (vm.js:383:10)
    at Generator.next (<anonymous>)
    at /myisomorphicapplication/components/ux/Icon.js:1
    at Generator.next (<anonymous>)
Waiting for the debugger to disconnect...
[nodemon] app crashed - waiting for file changes before starting...

Is it possible to tell esm to ignore some files? I tried to hook into require by doing:

var original_require = require;
require = function(file){
    console.log('here')
    console.log(file)
    // Check filename and ignore 
    return original_require(file);
}; 

But it doesn't work, esm is already taking care of that. I would like to be able to still use the second option in "Getting started" in the readme-file, nodemon -w server -w common -r esm server/main.js and not create an additional index.js file as explained in the first option in "Getting started".

index.js

require = function(file){
   // Check name and ignore etc..
  return require("esm")(module/*, options*/)
}
module.exports = require("./main.js")

Is there any possibility to do this?

dnalborczyk commented 4 years ago

hey @cjh9

esm is in one instance ending up in some frontend code and is trying to import a svg-file.

that shouldn't happen, as esm is meant to support es6 modules for node.js only. your front end app should not hook into the node.js (commonjs bridge) entry file, but the es6 file the bridge is loading.

to illustrate:

// something like this is the entry point for node.js only!
// index.js
require = require("esm")(module/*, options*/)
module.exports = require("./main.js")
// something like this should be referenced by your front end app
// main.js
export {}