.
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import * as Log from './output/log';
|
||||
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration';
|
||||
import { readFileSync } from 'fs';
|
||||
import isError from '../lib/is-error';
|
||||
import { hasNecessaryDependencies } from '../lib/has-necessary-dependencies';
|
||||
import { codeFrameColumns } from '../shared/lib/errors/code-frame';
|
||||
let TSCONFIG_WARNED = false;
|
||||
export function parseJsonFile(filePath) {
|
||||
const JSON5 = require('next/dist/compiled/json5');
|
||||
const contents = readFileSync(filePath, 'utf8');
|
||||
// Special case an empty file
|
||||
if (contents.trim() === '') {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return JSON5.parse(contents);
|
||||
} catch (err) {
|
||||
if (!isError(err)) throw err;
|
||||
const codeFrame = codeFrameColumns(String(contents), {
|
||||
start: {
|
||||
line: err.lineNumber || 1,
|
||||
column: err.columnNumber || undefined
|
||||
}
|
||||
}, {
|
||||
message: err.message,
|
||||
color: true
|
||||
});
|
||||
throw Object.defineProperty(new Error(`Failed to parse "${filePath}":\n${codeFrame ?? err.message}`), "__NEXT_ERROR_CODE", {
|
||||
value: "E1087",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
export default async function loadJsConfig(dir, config) {
|
||||
var _jsConfig_compilerOptions;
|
||||
let typeScriptPath;
|
||||
try {
|
||||
const deps = hasNecessaryDependencies(dir, [
|
||||
{
|
||||
pkg: 'typescript',
|
||||
file: 'typescript/lib/typescript.js',
|
||||
exportsRestrict: true
|
||||
}
|
||||
]);
|
||||
typeScriptPath = deps.resolved.get('typescript');
|
||||
} catch {}
|
||||
const tsConfigFileName = config.typescript.tsconfigPath || 'tsconfig.json';
|
||||
const tsConfigPath = path.join(dir, tsConfigFileName);
|
||||
const useTypeScript = Boolean(typeScriptPath && fs.existsSync(tsConfigPath));
|
||||
let implicitBaseurl;
|
||||
let jsConfig;
|
||||
// jsconfig is a subset of tsconfig
|
||||
if (useTypeScript) {
|
||||
if (tsConfigFileName !== 'tsconfig.json' && TSCONFIG_WARNED === false) {
|
||||
TSCONFIG_WARNED = true;
|
||||
Log.info(`Using tsconfig file: ${tsConfigFileName}`);
|
||||
}
|
||||
const ts = await Promise.resolve(require(typeScriptPath));
|
||||
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath, true);
|
||||
jsConfig = {
|
||||
compilerOptions: tsConfig.options
|
||||
};
|
||||
implicitBaseurl = path.dirname(tsConfigPath);
|
||||
}
|
||||
const jsConfigPath = path.join(dir, 'jsconfig.json');
|
||||
if (!useTypeScript && fs.existsSync(jsConfigPath)) {
|
||||
jsConfig = parseJsonFile(jsConfigPath);
|
||||
implicitBaseurl = path.dirname(jsConfigPath);
|
||||
}
|
||||
let resolvedBaseUrl;
|
||||
if (jsConfig == null ? void 0 : (_jsConfig_compilerOptions = jsConfig.compilerOptions) == null ? void 0 : _jsConfig_compilerOptions.baseUrl) {
|
||||
resolvedBaseUrl = {
|
||||
baseUrl: path.resolve(dir, jsConfig.compilerOptions.baseUrl),
|
||||
isImplicit: false
|
||||
};
|
||||
} else {
|
||||
if (implicitBaseurl) {
|
||||
resolvedBaseUrl = {
|
||||
baseUrl: implicitBaseurl,
|
||||
isImplicit: true
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
useTypeScript,
|
||||
jsConfig,
|
||||
resolvedBaseUrl,
|
||||
jsConfigPath: useTypeScript ? tsConfigPath : fs.existsSync(jsConfigPath) ? jsConfigPath : undefined
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=load-jsconfig.js.map
|
||||
Reference in New Issue
Block a user