next
Version:
The React Framework
269 lines (268 loc) • 13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
verifyAndRunTypeScript: null,
verifyAndRunTypeScriptInWorker: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
verifyAndRunTypeScript: function() {
return verifyAndRunTypeScript;
},
verifyAndRunTypeScriptInWorker: function() {
return verifyAndRunTypeScriptInWorker;
}
});
const _picocolors = require("./picocolors");
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
const _hasnecessarydependencies = require("./has-necessary-dependencies");
const _semver = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/semver"));
const _compileerror = require("./compile-error");
const _log = /*#__PURE__*/ _interop_require_wildcard(require("../build/output/log"));
const _getTypeScriptIntent = require("./typescript/getTypeScriptIntent");
const _writeAppTypeDeclarations = require("./typescript/writeAppTypeDeclarations");
const _writeConfigurationDefaults = require("./typescript/writeConfigurationDefaults");
const _installdependencies = require("./install-dependencies");
const _ciinfo = require("../server/ci-info");
const _missingDependencyError = require("./typescript/missingDependencyError");
const _runTypeScriptCli = require("./typescript/runTypeScriptCli");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {
__proto__: null
};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
const typescriptApiPackage = {
file: 'typescript/lib/typescript.js',
pkg: 'typescript',
install: 'typescript@^6.0.0',
exportsRestrict: true
};
const typescriptCliPackage = {
file: 'typescript/bin/tsc',
pkg: 'typescript',
exportsRestrict: true
};
const requiredTypePackages = [
{
file: '@types/react/index.d.ts',
pkg: '@types/react',
exportsRestrict: true
},
{
file: '@types/node/index.d.ts',
pkg: '@types/node',
exportsRestrict: true
}
];
async function verifyAndRunTypeScript({ dir, distDir, cacheDir, strictRouteTypes, tsconfigPath, shouldRunTypeCheck, typedRoutes, disableStaticImages, hasAppDir, hasPagesDir, appDir, pagesDir, debugBuildPaths, useTypeScriptCli = false, onFirstCliOutput }) {
const tsConfigFileName = tsconfigPath || 'tsconfig.json';
const resolvedTsConfigPath = _path.default.join(dir, tsConfigFileName);
const typeCheckMode = useTypeScriptCli ? 'typescript-cli' : 'typescript-api';
// Construct intentDirs from appDir and pagesDir for getTypeScriptIntent
const intentDirs = [
pagesDir,
appDir
].filter(Boolean);
try {
var _deps_missing, _deps_missing1;
// Check if the project uses TypeScript:
const intent = await (0, _getTypeScriptIntent.getTypeScriptIntent)(dir, intentDirs, tsConfigFileName);
if (!intent) {
return {
version: null,
typeCheckMode
};
}
// Check if @typescript/native-preview is installed as an alternative
const hasNativePreview = (0, _runTypeScriptCli.hasNativeTypeScriptPreview)(dir);
const installedTypeScript = (0, _runTypeScriptCli.getTypeScriptPackageInfo)(dir);
if (!useTypeScriptCli && !hasNativePreview && installedTypeScript && !installedTypeScript.apiPath) {
throw (0, _runTypeScriptCli.getTypeScriptApiMissingError)(installedTypeScript.version);
}
const requiredPackages = [
useTypeScriptCli ? typescriptCliPackage : typescriptApiPackage,
...requiredTypePackages
];
// Ensure TypeScript and necessary `@types/*` are installed:
let deps = (0, _hasnecessarydependencies.hasNecessaryDependencies)(dir, requiredPackages);
// If @typescript/native-preview is installed and only the typescript package is missing,
// we can skip auto-installing typescript since the native preview provides TS compilation.
// However, we still need @types/react and @types/node for type checking.
if (!useTypeScriptCli && hasNativePreview && ((_deps_missing = deps.missing) == null ? void 0 : _deps_missing.length) > 0) {
const missingWithoutTypescript = deps.missing.filter((dep)=>dep.pkg !== 'typescript');
const onlyTypescriptMissing = deps.missing.length === 1 && deps.missing[0].pkg === 'typescript';
if (onlyTypescriptMissing) {
// @typescript/native-preview is installed and only typescript is missing
// Skip installation and return early - the project can use the native preview
_log.info(`Detected ${(0, _picocolors.bold)('@typescript/native-preview')} as TypeScript compiler. ` + `Some Next.js TypeScript features (like type checking during build) require the standard ${(0, _picocolors.bold)('typescript')} package.`);
// Still write type declarations since they don't require the typescript package
await (0, _writeAppTypeDeclarations.writeAppTypeDeclarations)({
baseDir: dir,
distDir,
imageImportsEnabled: !disableStaticImages,
hasPagesDir,
hasAppDir,
strictRouteTypes,
typedRoutes
});
return {
version: null,
typeCheckMode
};
}
// If there are other missing deps besides typescript, only install those
if (missingWithoutTypescript.length > 0 && missingWithoutTypescript.length < deps.missing.length) {
deps.missing = missingWithoutTypescript;
}
}
if (((_deps_missing1 = deps.missing) == null ? void 0 : _deps_missing1.length) > 0) {
if (_ciinfo.isCI) {
// we don't attempt auto install in CI to avoid side-effects
// and instead log the error for installing needed packages
(0, _missingDependencyError.missingDepsError)(dir, deps.missing);
}
console.log((0, _picocolors.bold)((0, _picocolors.yellow)(`It looks like you're trying to use TypeScript but do not have the required package(s) installed.`)) + '\n' + 'Installing dependencies' + '\n\n' + (0, _picocolors.bold)('If you are not trying to use TypeScript, please remove the ' + (0, _picocolors.cyan)('tsconfig.json') + ' file from your package root (and any TypeScript files in your app and pages directories).') + '\n');
await (0, _installdependencies.installDependencies)(dir, deps.missing, true).catch((err)=>{
if (err && typeof err === 'object' && 'command' in err) {
console.error(`Failed to install required TypeScript dependencies, please install them manually to continue:\n` + err.command + '\n');
}
throw err;
});
deps = (0, _hasnecessarydependencies.hasNecessaryDependencies)(dir, requiredPackages);
}
const typeScriptPackage = (0, _runTypeScriptCli.getTypeScriptPackageInfo)(dir);
const typeScriptPath = useTypeScriptCli ? typeScriptPackage == null ? void 0 : typeScriptPackage.tscPath : typeScriptPackage == null ? void 0 : typeScriptPackage.apiPath;
if (!typeScriptPackage || !typeScriptPath) {
(0, _missingDependencyError.missingDepsError)(dir, deps.missing.length > 0 ? deps.missing : [
useTypeScriptCli ? typescriptCliPackage : typescriptApiPackage
]);
}
const typescriptVersion = typeScriptPackage.version;
if (_semver.default.lt(typescriptVersion, '5.1.0')) {
_log.warn(`Minimum recommended TypeScript version is v5.1.0, older versions can potentially be incompatible with Next.js. Detected: ${typescriptVersion}`);
}
// Reconfigure (or create) the user's `tsconfig.json` for them:
await (0, _writeConfigurationDefaults.writeConfigurationDefaults)(typescriptVersion, resolvedTsConfigPath, intent.firstTimeSetup, hasAppDir, distDir, hasPagesDir, strictRouteTypes);
// Write out the necessary `next-env.d.ts` file to correctly register
// Next.js' types:
await (0, _writeAppTypeDeclarations.writeAppTypeDeclarations)({
baseDir: dir,
distDir,
imageImportsEnabled: !disableStaticImages,
hasPagesDir,
hasAppDir,
strictRouteTypes,
typedRoutes
});
let result;
if (shouldRunTypeCheck) {
if (useTypeScriptCli) {
if (debugBuildPaths) {
_log.warn('`experimental.useTypeScriptCli` checks the complete TypeScript project; `--debug-build-paths` does not limit type checking.');
}
const { runTypeCheckCli } = require('./typescript/runTypeCheckCli');
result = await runTypeCheckCli({
baseDir: dir,
tsConfigPath: resolvedTsConfigPath,
tscPath: typeScriptPath,
cacheDir,
onFirstOutput: onFirstCliOutput
});
} else {
const { runTypeCheck } = require('./typescript/runTypeCheck');
// Install native bindings so that code frame rendering works in the worker
const { installBindings } = require('../build/swc/install-bindings');
await installBindings();
const typescript = await Promise.resolve(require(typeScriptPath));
// Verify the project passes type-checking before we go to webpack phase:
result = await runTypeCheck(typescript, dir, distDir, resolvedTsConfigPath, cacheDir, hasAppDir, {
app: appDir,
pages: pagesDir
}, debugBuildPaths);
}
}
return {
result,
version: typescriptVersion,
typeCheckMode
};
} catch (err) {
// Print the user-facing message here and rethrow. This function runs both
// in-process (next dev / next test / next typegen, and the CLI type-check
// during build) and inside a jest worker (the TypeScript-API type-check
// during build). A thrown error does not survive the worker boundary — the
// parent only sees `Call retries were exceeded` — so the message must be
// printed on this side. The caller decides what to do with the throw (exit,
// or tolerate it as `next dev` does on re-verification).
if (err instanceof _compileerror.CompileError) {
// The checker already printed its diagnostics.
console.error((0, _picocolors.red)('Failed to type check.\n'));
if (err.message) {
console.error(err.message);
}
} else if (err instanceof Error) {
console.error(err.message);
} else {
console.error(err);
}
throw err;
}
}
async function verifyAndRunTypeScriptInWorker(options) {
try {
return await verifyAndRunTypeScript(options);
} catch {
// The error was already printed by `verifyAndRunTypeScript`.
// Kill the worker with a non-zero exit code.
process.exit(1);
}
}
//# sourceMappingURL=verify-typescript-setup.js.map