Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
fixup: s/type/entry-type impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesBorins committed Mar 27, 2019
1 parent 39c31c3 commit 330ab11
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 58 deletions.
6 changes: 3 additions & 3 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ Enable FIPS-compliant crypto at startup.
Requires Node.js to be built with
.Sy ./configure --openssl-fips .
.
.It Fl -entry-type Ns = Ns Ar type
Set the top-level module resolution type.
.
.It Fl -experimental-modules
Enable experimental ES module support and caching modules.
.
Expand Down Expand Up @@ -280,9 +283,6 @@ Print stack traces for process warnings (including deprecations).
.It Fl -track-heap-objects
Track heap object allocations for heap snapshots.
.
.It Fl -type Ns = Ns Ar type
Set the top-level module resolution type.
.
.It Fl -use-bundled-ca , Fl -use-openssl-ca
Use bundled Mozilla CA store as supplied by current Node.js version or use OpenSSL's default CA store.
The default store is selectable at build-time.
Expand Down
11 changes: 3 additions & 8 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,6 @@ E('ERR_INVALID_PROTOCOL',
TypeError);
E('ERR_INVALID_REPL_EVAL_CONFIG',
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError);
E('ERR_INVALID_REPL_TYPE',
'Cannot specify --type for REPL', TypeError);
E('ERR_INVALID_RETURN_PROPERTY', (input, name, prop, value) => {
return `Expected a valid ${input} to be returned for the "${prop}" from the` +
` "${name}" function but got ${value}.`;
Expand Down Expand Up @@ -905,9 +903,6 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
TypeError);
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
E('ERR_INVALID_TYPE_FLAG',
'Type flag must be one of "module", "commonjs". Received --type=%s',
TypeError);
E('ERR_INVALID_URI', 'URI malformed', URIError);
E('ERR_INVALID_URL', function(input) {
this.input = input;
Expand Down Expand Up @@ -1055,12 +1050,12 @@ E('ERR_TRANSFORM_WITH_LENGTH_0',
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
E('ERR_TYPE_MISMATCH', (filename, ext, typeFlag, conflict) => {
const typeString =
typeFlag === 'module' ? '--type=module' : '--type=commonjs';
// --type mismatches file extension
typeFlag === 'module' ? '--entry-type=module' : '--entry-type=commonjs';
// --entry-type mismatches file extension
if (conflict === 'extension')
return `Extension ${ext} is not supported for ` +
`${typeString} loading ${filename}`;
// --type mismatches package.json "type"
// --entry-type mismatches package.json "type"
else if (conflict === 'scope')
return `Cannot use ${typeString} because nearest parent package.json ` +
((typeFlag === 'module') ?
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ function checkSyntax(source, filename) {
// Remove Shebang.
source = stripShebang(source);

const experimentalModules =
require('internal/options').getOptionValue('--experimental-modules');
const { getOptionValue } = require('internal/options');
const experimentalModules = getOptionValue('--experimental-modules');
if (experimentalModules) {
let isModule = false;
if (filename === '[stdin]' || filename === '[eval]') {
isModule = require('internal/process/esm_loader').typeFlag === 'module';
isModule = getOptionValue('--entry-type') === 'module';
} else {
const resolve = require('internal/modules/esm/default_resolve');
const { format } = resolve(pathToFileURL(filename).toString());
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/main/eval_stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ markBootstrapComplete();

readStdin((code) => {
process._eval = code;
if (require('internal/process/esm_loader').typeFlag === 'module')
if (require('internal/options').getOptionValue('--entry-type') === 'module')
evalModule(process._eval);
else
evalScript('[stdin]', process._eval, process._breakFirstLine);
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const {
const { evalModule, evalScript } = require('internal/process/execution');
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');

const source = require('internal/options').getOptionValue('--eval');
const { getOptionValue } = require('internal/options');
const source = getOptionValue('--eval');
prepareMainThreadExecution();
addBuiltinLibsToObject(global);
markBootstrapComplete();
if (require('internal/process/esm_loader').typeFlag === 'module')
if (getOptionValue('--entry-type') === 'module')
evalModule(source);
else
evalScript('[eval]', source, process._breakFirstLine);
9 changes: 4 additions & 5 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ const {
evalScript
} = require('internal/process/execution');

const { ERR_INVALID_REPL_TYPE } = require('internal/errors').codes;

prepareMainThreadExecution();

// --type flag not supported in REPL
if (require('internal/process/esm_loader').typeFlag) {
throw ERR_INVALID_REPL_TYPE();
// --entry-type flag not supported in REPL
if (require('internal/options').getOptionValue('--entry-type')) {
console.error('Cannot specify --entry-type for REPL');
process.exit(1);
}

const cliRepl = require('internal/repl');
Expand Down
24 changes: 12 additions & 12 deletions lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { getOptionValue } = require('internal/options');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
const typeFlag = getOptionValue('--entry-type');

const { resolve: moduleWrapResolve,
getPackageType } = internalBinding('module_wrap');
const { pathToFileURL, fileURLToPath } = require('internal/url');
const asyncESM = require('internal/process/esm_loader');
const { ERR_TYPE_MISMATCH,
ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;

Expand Down Expand Up @@ -80,26 +80,26 @@ function resolve(specifier, parentURL) {
type !== TYPE_MODULE ? legacyExtensionFormatMap : extensionFormatMap;
let format = extMap[ext];

if (isMain && asyncESM.typeFlag) {
// Conflict between explicit extension (.mjs, .cjs) and --type
if (ext === '.cjs' && asyncESM.typeFlag === 'module' ||
ext === '.mjs' && asyncESM.typeFlag === 'commonjs') {
if (isMain && typeFlag) {
// Conflict between explicit extension (.mjs, .cjs) and --entry-type
if (ext === '.cjs' && typeFlag === 'module' ||
ext === '.mjs' && typeFlag === 'commonjs') {
throw new ERR_TYPE_MISMATCH(
fileURLToPath(url), ext, asyncESM.typeFlag, 'extension');
fileURLToPath(url), ext, typeFlag, 'extension');
}

// Conflict between package scope type and --type
// Conflict between package scope type and --entry-type
if (ext === '.js') {
if (type === TYPE_MODULE && asyncESM.typeFlag === 'commonjs' ||
type === TYPE_COMMONJS && asyncESM.typeFlag === 'module') {
if (type === TYPE_MODULE && typeFlag === 'commonjs' ||
type === TYPE_COMMONJS && typeFlag === 'module') {
throw new ERR_TYPE_MISMATCH(
fileURLToPath(url), ext, asyncESM.typeFlag, 'scope');
fileURLToPath(url), ext, typeFlag, 'scope');
}
}
}
if (!format) {
if (isMain && asyncESM.typeFlag)
format = asyncESM.typeFlag;
if (isMain && typeFlag)
format = typeFlag;
else if (isMain)
format = type === TYPE_MODULE ? 'module' : 'commonjs';
else
Expand Down
6 changes: 0 additions & 6 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ const {
callbackMap,
} = internalBinding('module_wrap');
const {
ERR_INVALID_TYPE_FLAG,
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
} = require('internal/errors').codes;

const type = require('internal/options').getOptionValue('--type');
if (type && type !== 'commonjs' && type !== 'module')
throw new ERR_INVALID_TYPE_FLAG(type);
exports.typeFlag = type;

const { Loader } = require('internal/modules/esm/loader');
const { pathToFileURL } = require('internal/url');
const {
Expand Down
14 changes: 10 additions & 4 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,14 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
errors->push_back("--loader requires --experimental-modules be enabled");
}

if (!module_type.empty() && !experimental_modules) {
errors->push_back("--type requires --experimental-modules be enabled");
if (!module_type.empty()) {
if (!experimental_modules) {
errors->push_back("--entry-type requires "
"--experimental-modules to be enabled");
}
if (module_type != "commonjs" && module_type != "module") {
errors->push_back("--entry-type must \"module\" or \"commonjs\"");
}
}

if (experimental_json_modules && !experimental_modules) {
Expand Down Expand Up @@ -332,8 +338,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"show stack traces on process warnings",
&EnvironmentOptions::trace_warnings,
kAllowedInEnvironment);
AddOption("--type",
"top-level module type name",
AddOption("--entry-type",
"set module type name of the entry point",
&EnvironmentOptions::module_type,
kAllowedInEnvironment);

Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-no-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const entry = fixtures.path('/es-modules/noext-esm');

const child = spawn(process.execPath, [
'--experimental-modules',
'--type=module',
'--entry-type=module',
entry
]);

Expand Down
16 changes: 8 additions & 8 deletions test/es-module/test-esm-type-flag-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ expect('', packageTypeModuleMain, 'package-type-module');
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
expect('', packageWithoutTypeMain, 'package-without-type');

// Check that running with --type and no package.json "type" works
expect('--type=commonjs', packageWithoutTypeMain, 'package-without-type');
expect('--type=module', packageWithoutTypeMain, 'package-without-type');
// Check that running with --entry-type and no package.json "type" works
expect('--entry-type=commonjs', packageWithoutTypeMain, 'package-without-type');
expect('--entry-type=module', packageWithoutTypeMain, 'package-without-type');

// Check that running with conflicting --type flags throws errors
expect('--type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--type=commonjs', packageTypeModuleMain,
// Check that running with conflicting --entry-type flags throws errors
expect('--entry-type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--entry-type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--entry-type=commonjs', packageTypeModuleMain,
'ERR_TYPE_MISMATCH', true);
expect('--type=module', packageTypeCommonJsMain,
expect('--entry-type=module', packageTypeCommonJsMain,
'ERR_TYPE_MISMATCH', true);

function expect(opt = '', inputFile, want, wantsError = false) {
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-type-flag.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --experimental-modules --type=module
// Flags: --experimental-modules --entry-type=module
/* eslint-disable node-core/required-modules */
import cjs from '../fixtures/baz.js';
import '../common/index.mjs';
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cli-syntax-piped-bad.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ syntaxArgs.forEach(function(arg) {
assert.strictEqual(c.status, 1);
});

// Check --type=module
// Check --entry-type=module
syntaxArgs.forEach(function(arg) {
const stdin = 'export var p = 5; var foo bar;';
const c = spawnSync(
node,
['--experimental-modules', '--type=module', '--no-warnings', arg],
['--experimental-modules', '--entry-type=module', '--no-warnings', arg],
{ encoding: 'utf8', input: stdin }
);

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cli-syntax-piped-good.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ syntaxArgs.forEach(function(arg) {
assert.strictEqual(c.status, 0);
});

// Check --type=module
// Check --entry-type=module
syntaxArgs.forEach(function(arg) {
const stdin = 'export var p = 5; throw new Error("should not get run");';
const c = spawnSync(
node,
['--experimental-modules', '--no-warnings', '--type=module', arg],
['--experimental-modules', '--no-warnings', '--entry-type=module', arg],
{ encoding: 'utf8', input: stdin }
);

Expand Down

0 comments on commit 330ab11

Please sign in to comment.