Skip to content

Commit

Permalink
fix(plugin): use require.resolve to prevent conflict with @vercel/n…
Browse files Browse the repository at this point in the history
…ft (#4863)
  • Loading branch information
SukkaW authored Jan 9, 2022
1 parent 9d064d2 commit 4c4bd22
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const Locals = require('./locals');
const defaultConfig = require('./default_config');
const loadDatabase = require('./load_database');
const multiConfigPath = require('./multi_config_path');
const { sync } = require('resolve');
const { deepMerge, full_url_for } = require('hexo-util');
let resolveSync; // = require('resolve');

const libDir = dirname(__dirname);
const dbVersion = 1;
Expand Down Expand Up @@ -180,7 +180,7 @@ class Hexo extends EventEmitter {
const query = {};

if (!this.config.future) {
query.date = {$lte: Date.now()};
query.date = { $lte: Date.now() };
}

if (!this._showDrafts()) {
Expand All @@ -194,7 +194,7 @@ class Hexo extends EventEmitter {
const query = {};

if (!this.config.future) {
query.date = {$lte: Date.now()};
query.date = { $lte: Date.now() };
}

return db.model('Page').find(query);
Expand Down Expand Up @@ -241,7 +241,7 @@ class Hexo extends EventEmitter {
'load_config', // Load config
'load_theme_config', // Load alternate theme config
'load_plugins' // Load external plugins & scripts
], name => require(`./${name}`)(this)).then(() => this.execFilter('after_init', null, {context: this})).then(() => {
], name => require(`./${name}`)(this)).then(() => this.execFilter('after_init', null, { context: this })).then(() => {
// Ready to go!
this.emit('ready');
});
Expand All @@ -265,12 +265,19 @@ class Hexo extends EventEmitter {

resolvePlugin(name, basedir) {
try {
// Try to resolve the plugin with the resolve.sync.
return sync(name, { basedir });
// Try to resolve the plugin with the Node.js's built-in require.resolve.
return require.resolve(name, { paths: [basedir] });
} catch (err) {
// There was an error (likely the plugin wasn't found), so return a possibly
// non-existing path that a later part of the resolution process will check.
return join(basedir, 'node_modules', name);
try {
// There was an error (likely the node_modules is corrupt or from early version of npm)
// Use Hexo prior 6.0.0's behavior (resolve.sync) to resolve the plugin.
resolveSync = resolveSync || require('resolve').sync;
return resolveSync(name, { basedir });
} catch (err) {
// There was an error (likely the plugin wasn't found), so return a possibly
// non-existing path that a later part of the resolution process will check.
return join(basedir, 'node_modules', name);
}
}
}

Expand Down Expand Up @@ -314,7 +321,7 @@ class Hexo extends EventEmitter {
]);
}).then(() => {
mergeCtxThemeConfig(this);
return this._generate({cache: false});
return this._generate({ cache: false });
}).asCallback(callback);
}

Expand All @@ -329,7 +336,7 @@ class Hexo extends EventEmitter {
// enable cache when run hexo server
useCache = true;
}
this._watchBox = debounce(() => this._generate({cache: useCache}), 100);
this._watchBox = debounce(() => this._generate({ cache: useCache }), 100);

return loadDatabase(this).then(() => {
this.log.info('Start processing');
Expand All @@ -347,7 +354,7 @@ class Hexo extends EventEmitter {
mergeCtxThemeConfig(this);
});

return this._generate({cache: useCache});
return this._generate({ cache: useCache });
}).asCallback(callback);
}

Expand Down Expand Up @@ -421,7 +428,7 @@ class Hexo extends EventEmitter {
return path;
}

return this.execFilter('template_locals', new Locals(path, data), {context: this})
return this.execFilter('template_locals', new Locals(path, data), { context: this })
.then(locals => { route.set(path, createLoadThemeRoute(generatorResult, locals, this)); })
.thenReturn(path);
}).then(newRouteList => {
Expand All @@ -446,12 +453,12 @@ class Hexo extends EventEmitter {
this.emit('generateBefore');

// Run before_generate filters
return this.execFilter('before_generate', this.locals.get('data'), {context: this})
return this.execFilter('before_generate', this.locals.get('data'), { context: this })
.then(() => this._routerReflesh(this._runGenerators(), useCache)).then(() => {
this.emit('generateAfter');

// Run after_generate filters
return this.execFilter('after_generate', null, {context: this});
return this.execFilter('after_generate', null, { context: this });
}).finally(() => {
this._isGenerating = false;
});
Expand All @@ -460,13 +467,13 @@ class Hexo extends EventEmitter {
exit(err) {
if (err) {
this.log.fatal(
{err},
{ err },
'Something\'s wrong. Maybe you can find the solution here: %s',
underline('https://hexo.io/docs/troubleshooting.html')
);
}

return this.execFilter('before_exit', null, {context: this}).then(() => {
return this.execFilter('before_exit', null, { context: this }).then(() => {
this.emit('exit', err);
});
}
Expand Down

1 comment on commit 4c4bd22

@SoraKasvgano
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像是提交失败???

Please sign in to comment.