Just as we can do:
# app.coffee
require 'coffeescript/register'
{ slice } = require './pizza.coffee'
or
// app.js
require('babel/register');
const { slice } = require('./pizza.js');
we should be able to achieve the equivalent in ESM, e.g. import a CoffeeScript file or import a JavaScript file that requires transpilation. References: CoffeeScript register, babel-register.
Use case 4. Related to #82.
I鈥檓 not sure the first example works - you have to require the register module before you鈥檙e able to require or run coffeescript.
@ljharb I didn鈥檛 flesh it out, but the first example works if run via coffee, as in coffee app.coffee. But it turns out that using that method means you don鈥檛 need register, so I guess you鈥檙e right in that sense. If the file were transpiled into app.js and then run via node app.js, you would need the register:
// app.js, transpiled from app.coffee above
var slice;
require('coffeescript/register');
({slice} = require('./pizza.coffee'));
@GeoffreyBooth to clarify, does this request require you to mutate the loader pipeline at runtime? If the loader needs to be declared somewhere outside of evaluation that does seem to fit this use case on a glance, but differs in workflow from your example (easier to analyze though).
@bmeck from what I recall - loaders (declared via the CLI flag or package.json) are entirely sufficient in order to implement the feature requested here.
I'm not saying they are the solution we should adopt necessarily. Just that importing CoffeeScript or TypeScript could work this way.
Also, if such loaders (as declared via the CLI flag or package.json) existed, wouldn't that mean you could write a loader that allows mutating the loader pipeline at runtime?
does this request require you to mutate the loader pipeline at runtime?
@bmeck I don鈥檛 think that necessarily needs to happen at runtime, if the advent of loaders creates a new phase of code to be executed before app code and dependencies are evaluated. I guess ideally we would support either scenario? But I鈥檓 not sure it makes a difference.
It also depends on how loaders are defined and configured to be loaded. Currently, require('coffeescript/register') registers a file extension handler on CommonJS whenever that require line happens, and so any require lines for .coffee files need to happen afterward. That鈥檚 fine. I鈥檓 not sure how loaders would be configured or how Node would be told to load them; package.json and/or CLI flags? I don鈥檛 think it matters so much as the effect would be the same.
Most helpful comment
@ljharb I didn鈥檛 flesh it out, but the first example works if run via
coffee, as incoffee app.coffee. But it turns out that using that method means you don鈥檛 needregister, so I guess you鈥檙e right in that sense. If the file were transpiled intoapp.jsand then run vianode app.js, you would need theregister: