module: use undefined if no main
If the package.json file does not have a "main" entry, return undefined rather than an empty string. This is to make more consistent behavior. For example, when package.json is a directory, "main" is undefined rather than an empty string. PR-URL: https://github.com/nodejs/node/pull/18593 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
2aa3e3b00f
commit
bd4773a043
@ -652,9 +652,8 @@ void Close(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
|
|
||||||
// Used to speed up module loading. Returns the contents of the file as
|
// Used to speed up module loading. Returns the contents of the file as
|
||||||
// a string or undefined when the file cannot be opened. Returns an empty
|
// a string or undefined when the file cannot be opened or "main" is not found
|
||||||
// string when the file does not contain the substring '"main"' because that
|
// in the file.
|
||||||
// is the property we care about.
|
|
||||||
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
|
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
|
||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
uv_loop_t* loop = env->event_loop();
|
uv_loop_t* loop = env->event_loop();
|
||||||
@ -708,7 +707,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
const size_t size = offset - start;
|
const size_t size = offset - start;
|
||||||
if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) {
|
if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) {
|
||||||
args.GetReturnValue().SetEmptyString();
|
return;
|
||||||
} else {
|
} else {
|
||||||
Local<String> chars_string =
|
Local<String> chars_string =
|
||||||
String::NewFromUtf8(env->isolate(),
|
String::NewFromUtf8(env->isolate(),
|
||||||
|
@ -6,8 +6,9 @@ const { readFileSync } = require('fs');
|
|||||||
const { strictEqual } = require('assert');
|
const { strictEqual } = require('assert');
|
||||||
|
|
||||||
strictEqual(internalModuleReadJSON('nosuchfile'), undefined);
|
strictEqual(internalModuleReadJSON('nosuchfile'), undefined);
|
||||||
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), '');
|
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), undefined);
|
||||||
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')), '');
|
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')),
|
||||||
|
undefined);
|
||||||
{
|
{
|
||||||
const filename = fixtures.path('require-bin/package.json');
|
const filename = fixtures.path('require-bin/package.json');
|
||||||
strictEqual(internalModuleReadJSON(filename), readFileSync(filename, 'utf8'));
|
strictEqual(internalModuleReadJSON(filename), readFileSync(filename, 'utf8'));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user