Skip to content

Instantly share code, notes, and snippets.

@isaacs

isaacs/c.cjs Secret

Created May 25, 2023 21:40
Show Gist options
  • Save isaacs/a9aacc1efeca7d565f2f7828467d9e6e to your computer and use it in GitHub Desktop.
Save isaacs/a9aacc1efeca7d565f2f7828467d9e6e to your computer and use it in GitHub Desktop.
console.log('c.cjs', {
require,
__filename,
__dirname,
module,
isMain: require.main === module,
})
require('./d.cjs')
console.log('d.cjs', {
require,
__filename,
__dirname,
module,
isMain: require.main === module,
})
// ensure cycles are caught
require('./c.cjs')
// does not continue with default load if return next(spec, context)
// require.main is not set
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
export const load = async (spec, context) => {
console.log('LOAD', spec, context);
const { format } = context;
const source = await readFile(fileURLToPath(spec), 'utf8');
console.error('source=', source);
// this does not do the default load() apparently?
// return next(spec, context)
return {
format,
source,
shortCircuit: true,
};
};
export const resolve = async (spec, context, next) => {
console.log('RESOLVE', spec, context);
// this only works for the main file, not modules loaded via require()
return next(spec, context);
};
@isaacs
Copy link
Author

isaacs commented May 25, 2023

Expect:

$ ./node c.cjs
c.cjs {
  require: [Function: require] {
    resolve: [Function: resolve] { paths: [Function: paths] },
    main: Module {
      id: '.',
      path: '/Users/isaacs/dev/isaacs/node-main',
      exports: {},
      filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
      loaded: false,
      children: [],
      paths: [Array]
    },
    extensions: [Object: null prototype] {
      '.js': [Function (anonymous)],
      '.json': [Function (anonymous)],
      '.node': [Function (anonymous)]
    },
    cache: [Object: null prototype] {
      '/Users/isaacs/dev/isaacs/node-main/c.cjs': [Module]
    }
  },
  __filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
  __dirname: '/Users/isaacs/dev/isaacs/node-main',
  module: Module {
    id: '.',
    path: '/Users/isaacs/dev/isaacs/node-main',
    exports: {},
    filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
    loaded: false,
    children: [],
    paths: [
      '/Users/isaacs/dev/isaacs/node-main/node_modules',
      '/Users/isaacs/dev/isaacs/node_modules',
      '/Users/isaacs/dev/node_modules',
      '/Users/isaacs/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ]
  },
  isMain: true
}
d.cjs {
  require: [Function: require] {
    resolve: [Function: resolve] { paths: [Function: paths] },
    main: Module {
      id: '.',
      path: '/Users/isaacs/dev/isaacs/node-main',
      exports: {},
      filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
      loaded: false,
      children: [Array],
      paths: [Array]
    },
    extensions: [Object: null prototype] {
      '.js': [Function (anonymous)],
      '.json': [Function (anonymous)],
      '.node': [Function (anonymous)]
    },
    cache: [Object: null prototype] {
      '/Users/isaacs/dev/isaacs/node-main/c.cjs': [Module],
      '/Users/isaacs/dev/isaacs/node-main/d.cjs': [Module]
    }
  },
  __filename: '/Users/isaacs/dev/isaacs/node-main/d.cjs',
  __dirname: '/Users/isaacs/dev/isaacs/node-main',
  module: Module {
    id: '/Users/isaacs/dev/isaacs/node-main/d.cjs',
    path: '/Users/isaacs/dev/isaacs/node-main',
    exports: {},
    filename: '/Users/isaacs/dev/isaacs/node-main/d.cjs',
    loaded: false,
    children: [],
    paths: [
      '/Users/isaacs/dev/isaacs/node-main/node_modules',
      '/Users/isaacs/dev/isaacs/node_modules',
      '/Users/isaacs/dev/node_modules',
      '/Users/isaacs/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ]
  },
  isMain: false
}

Actual:

$ ./node --loader=./loader.mjs ./c.cjs
(node:64883) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
RESOLVE file:///Users/isaacs/dev/isaacs/node-main/c.cjs {
  conditions: [ 'node', 'import', 'node-addons' ],
  importAssertions: {},
  parentURL: undefined
}
LOAD file:///Users/isaacs/dev/isaacs/node-main/c.cjs { format: 'commonjs', importAssertions: {} }
source= console.log('c.cjs', {
  require,
  __filename,
  __dirname,
  module,
  isMain: require.main === module,
})
require('./d.cjs')

c.cjs {
  require: [Function: require] {
    resolve: [Function: resolve],
    cache: [Object: null prototype] {
      '/Users/isaacs/dev/isaacs/node-main/c.cjs': [Module]
    },
    main: undefined
  },
  __filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
  __dirname: '/Users/isaacs/dev/isaacs/node-main',
  module: Module {
    id: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
    path: '/Users/isaacs/dev/isaacs/node-main',
    exports: {},
    filename: '/Users/isaacs/dev/isaacs/node-main/c.cjs',
    loaded: false,
    children: [],
    paths: [
      '/Users/isaacs/dev/isaacs/node-main/node_modules',
      '/Users/isaacs/dev/isaacs/node_modules',
      '/Users/isaacs/dev/node_modules',
      '/Users/isaacs/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ]
  },
  isMain: false
}

node:internal/process/esm_loader:42
      internalBinding('errors').triggerUncaughtException(
                                ^
TypeError [ERR_INVALID_ARG_TYPE]: The "./loader.mjs 'resolve' hook's nextResolve() specifier" property must be of type string. Received an instance of Object
    at new NodeError (node:internal/errors:405:5)
    at validateString (node:internal/validators:162:11)
    at validateArgs (node:internal/modules/esm/hooks:226:7)
    at nextResolve (node:internal/modules/esm/hooks:724:7)
    at Hooks.resolve (node:internal/modules/esm/hooks:245:30)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:173:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v21.0.0-pre

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment