Skip to content

Commit

Permalink
Replace resolve with require.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Jan 16, 2023
1 parent 9ab4eb6 commit 35dbae4
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 32 deletions.
5 changes: 1 addition & 4 deletions lib/cfg.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { existsSync, readFileSync } = require('fs');
const { dirname, resolve } = require('path');

const resolveMain = require('./resolve-main');

const defaultConfig = {
clear: false,
debounce: 10,
Expand Down Expand Up @@ -30,8 +28,7 @@ function read(dir) {
}

function getConfig(script) {
const main = resolveMain(script);
const dir = main ? dirname(main) : '.';
const dir = resolve(dirname(script));

return Object.assign(
defaultConfig,
Expand Down
9 changes: 3 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const { fork } = require('child_process');
const filewatcher = require('filewatcher');
const { join } = require('path');
const semver = require('semver');
const { pathToFileURL } = require('url');

const { clearFactory } = require('./clear');
const { configureDeps, configureIgnore } = require('./ignore');
const ipc = require('./ipc');
const localPath = require('./local-path');
const logFactory = require('./log');
const notifyFactory = require('./notify');
const resolveMain = require('./resolve-main');

module.exports = function (
script,
Expand Down Expand Up @@ -56,7 +53,7 @@ module.exports = function (
const isTooDeep = configureDeps(deps);

// Run ./dedupe.js as preload script
if (dedupe) process.env.NODE_DEV_PRELOAD = localPath('dedupe');
if (dedupe) process.env.NODE_DEV_PRELOAD = require.resolve('./dedupe');

const watcher = filewatcher({ debounce, forcePolling, interval });
let isPaused = false;
Expand Down Expand Up @@ -94,11 +91,11 @@ module.exports = function (

const args = nodeArgs.slice();

args.push(`--require=${resolveMain(localPath('wrap'))}`);
args.push(`--require=${require.resolve('./wrap')}`);

const loaderName = semver.satisfies(process.version, '>=16.12.0') ? 'load' : 'get-format';

const loaderURL = pathToFileURL(resolveMain(localPath(join('loaders', `${loaderName}.mjs`))));
const loaderURL = pathToFileURL(require.resolve(`./loaders/${loaderName}.mjs`));

args.push(`--experimental-loader=${loaderURL.href}`);

Expand Down
2 changes: 0 additions & 2 deletions lib/local-path.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/notify.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const notifier = require('node-notifier');

const localPath = require('./local-path');

const iconLevelPath = level => localPath(`../icons/node_${level}.png`);
const iconLevelPath = level => require.resolve(`../icons/node_${level}.png`);

// Writes a message to the console and optionally displays a desktop notification.
module.exports = (notifyEnabled, log) => {
Expand Down
7 changes: 0 additions & 7 deletions lib/resolve-main.js

This file was deleted.

12 changes: 4 additions & 8 deletions lib/wrap.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { dirname, extname } = require('path');
const childProcess = require('child_process');
const { sync: resolve } = require('resolve');
const { isMainThread } = require('worker_threads');

const { getConfig } = require('./cfg');
const hook = require('./hook');
const { relay, send } = require('./ipc');
const resolveMain = require('./resolve-main');
const suppressExperimentalWarnings = require('./suppress-experimental-warnings');

// Experimental warnings need to be suppressed in worker threads as well, since
Expand All @@ -18,7 +16,7 @@ suppressExperimentalWarnings(process);
// on the main thread.
if (!isMainThread) return;

const script = process.argv[1];
const script = require.resolve(process.argv[1]);
const { extensions, fork, vm } = getConfig(script);

if (process.env.NODE_DEV_PRELOAD) {
Expand Down Expand Up @@ -58,18 +56,16 @@ process.on('uncaughtException', err => {
hook(vm, required => send({ required }));

// Check if a module is registered for this extension
const main = resolveMain(script);
const ext = extname(main).slice(1);
const ext = extname(script).slice(1);
const mod = extensions[ext];
const basedir = dirname(main);

// Support extensions where 'require' returns a function that accepts options
if (typeof mod === 'object' && mod.name) {
const fn = require(resolve(mod.name, { basedir }));
const fn = require(require.resolve(mod.name, { paths: [dirname(script)] }));
if (typeof fn === 'function' && mod.options) {
// require returned a function, call it with options
fn(mod.options);
}
} else if (typeof mod === 'string') {
require(resolve(mod, { basedir }));
require(require.resolve(mod, { paths: [dirname(script)] }));
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"get-package-type": "^0.1.0",
"minimist": "^1.2.6",
"node-notifier": "^8.0.1",
"resolve": "^1.22.0",
"semver": "^7.3.7"
},
"devDependencies": {
Expand All @@ -58,4 +57,4 @@
"*.{js,mjs}": "eslint --cache --fix",
"*.{js,md}": "prettier --write"
}
}
}

0 comments on commit 35dbae4

Please sign in to comment.