In current Node.js, specifiers starting with https:// are unsupported. The loader below registers hooks to enable rudimentary support for such specifiers. While this may seem like a significant improvement to Node.js core functionality, there are substantial downsides to actually using this loader: performance is much slower than loading files from disk, there is no caching, and there is no security.

    1. // https-loader.mjs
    2. import { get } from 'https';
    3. export function resolve(specifier, context, defaultResolve) {
    4. const { parentURL = null } = context;
    5. // Normally Node.js would error on specifiers starting with 'https://', so
    6. // this hook intercepts them and converts them into absolute URLs to be
    7. // passed along to the later hooks below.
    8. if (specifier.startsWith('https://')) {
    9. return {
    10. url: specifier
    11. };
    12. } else if (parentURL && parentURL.startsWith('https://')) {
    13. return {
    14. url: new URL(specifier, parentURL).href
    15. };
    16. }
    17. // Let Node.js handle all other specifiers.
    18. return defaultResolve(specifier, context, defaultResolve);
    19. }
    20. export function getFormat(url, context, defaultGetFormat) {
    21. // This loader assumes all network-provided JavaScript is ES module code.
    22. if (url.startsWith('https://')) {
    23. return {
    24. format: 'module'
    25. };
    26. }
    27. // Let Node.js handle all other URLs.
    28. return defaultGetFormat(url, context, defaultGetFormat);
    29. }
    30. export function getSource(url, context, defaultGetSource) {
    31. // For JavaScript to be loaded over the network, we need to fetch and
    32. // return it.
    33. if (url.startsWith('https://')) {
    34. return new Promise((resolve, reject) => {
    35. get(url, (res) => {
    36. let data = '';
    37. res.on('data', (chunk) => data += chunk);
    38. res.on('end', () => resolve({ source: data }));
    39. }).on('error', (err) => reject(err));
    40. });
    41. }
    42. // Let Node.js handle all other URLs.
    43. return defaultGetSource(url, context, defaultGetSource);
    44. }
    1. // main.mjs
    2. import { VERSION } from 'https://coffeescript.org/browser-compiler-modern/coffeescript.js';
    3. console.log(VERSION);

    With the preceding loader, running node --experimental-loader ./https-loader.mjs ./main.mjs prints the current version of CoffeeScript per the module at the URL in main.mjs.