-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: allow running .ts in node_modules if private #55385
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const { | |
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
ERR_INVALID_TYPESCRIPT_SYNTAX, | ||
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING, | ||
} = require('internal/errors').codes; | ||
const { BuiltinModule } = require('internal/bootstrap/realm'); | ||
|
||
|
@@ -28,7 +29,7 @@ const assert = require('internal/assert'); | |
|
||
const { Buffer } = require('buffer'); | ||
const { getOptionValue } = require('internal/options'); | ||
const { assertTypeScript, setOwnProperty, getLazy } = require('internal/util'); | ||
const { assertTypeScript, setOwnProperty, getLazy, isUnderNodeModules } = require('internal/util'); | ||
const { inspect } = require('internal/util/inspect'); | ||
|
||
const lazyTmpdir = getLazy(() => require('os').tmpdir()); | ||
|
@@ -347,6 +348,21 @@ function parseTypeScript(source, options) { | |
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} filename | ||
* @returns {boolean} Returns true if the nearest package.json is private. | ||
*/ | ||
function isPrivateNodeModule(filename) { | ||
const packageJsonReader = require('internal/modules/package_json_reader'); | ||
const resolved = StringPrototypeStartsWith(filename, 'file://') ? filename : pathToFileURL(filename).href; | ||
const packageConfig = packageJsonReader.getPackageScopeConfig(resolved); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we cache already analized paths? |
||
if (!packageConfig?.exists) { | ||
return false; | ||
} | ||
return packageConfig.isPrivate === true; | ||
} | ||
|
||
/** | ||
* @typedef {object} TransformOutput | ||
* @property {string} code The compiled code. | ||
|
@@ -355,9 +371,16 @@ function parseTypeScript(source, options) { | |
* Performs type-stripping to TypeScript source code. | ||
* @param {string} source TypeScript code to parse. | ||
* @param {string} filename The filename of the source code. | ||
* @param {string} isPackageJsonPrivate Whether the nearest package.json is private. | ||
* @returns {TransformOutput} The stripped TypeScript code. | ||
*/ | ||
function stripTypeScriptTypes(source, filename) { | ||
function stripTypeScriptTypes(source, filename, isPackageJsonPrivate = false) { | ||
// If the package.json is private, we can | ||
// allow the stripping of types because it is | ||
// not possible to publish it on npm. | ||
if (isUnderNodeModules(filename) && isPackageJsonPrivate !== true && !isPrivateNodeModule(filename)) { | ||
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); | ||
} | ||
assert(typeof source === 'string'); | ||
const options = { | ||
__proto__: null, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { foo } from 'private'; | ||
|
||
interface Bar { }; | ||
|
||
console.log(foo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance it can be missing?