Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
compilerOptions.outDir = compilerOptions.outFile = undefined;
if (autoScriptTarget) {
const [nodeMajor] = process.versions.node.split('.'); // '12.0.0' => '12'
compilerOptions.target = nodeVersionToScriptTarget(Number(nodeMajor));
}
if (typeof cacheDirectoryPath !== 'string') {
// couldn't find a cache directory, so fall back to a non-caching implementation
return createTransformerExtension(
filePath =>
ts.transpileModule(readFileSync(filePath, 'utf8'), { fileName: filePath, compilerOptions }).outputText
);
}
const optionsScopedCachePath = join(cacheDirectoryPath, compilerOptionsToCacheName(compilerOptions));
try {
ensureDirectorySync(optionsScopedCachePath);
} catch {
/**/
}
if (installSourceMapSupport) {
sourceMapSupport.install({
environment: 'node',
retrieveSourceMap(filePath) {
const cacheFilePath = join(optionsScopedCachePath, filePathToCacheFileName(filePath));
const cachedOutput = readCacheFileSync(cacheFilePath);
if (cachedOutput && cachedOutput.mtime === statSync(filePath).mtime.getTime()) {
const { sourceMapText, outputText } = cachedOutput;
const map = sourceMapText || extractInlineSourceMap(outputText);
if (map) {
if (autoScriptTarget) {
const [nodeMajor] = process.versions.node.split('.'); // '12.0.0' => '12'
compilerOptions.target = nodeVersionToScriptTarget(Number(nodeMajor));
}
if (typeof cacheDirectoryPath !== 'string') {
// couldn't find a cache directory, so fall back to a non-caching implementation
return createTransformerExtension(
filePath =>
ts.transpileModule(readFileSync(filePath, 'utf8'), { fileName: filePath, compilerOptions }).outputText
);
}
const optionsScopedCachePath = join(cacheDirectoryPath, compilerOptionsToCacheName(compilerOptions));
try {
ensureDirectorySync(optionsScopedCachePath);
} catch {
/**/
}
if (installSourceMapSupport) {
sourceMapSupport.install({
environment: 'node',
retrieveSourceMap(filePath) {
const cacheFilePath = join(optionsScopedCachePath, filePathToCacheFileName(filePath));
const cachedOutput = readCacheFileSync(cacheFilePath);
if (cachedOutput && cachedOutput.mtime === statSync(filePath).mtime.getTime()) {
const { sourceMapText, outputText } = cachedOutput;
const map = sourceMapText || extractInlineSourceMap(outputText);
if (map) {
return { map, url: filePath };
}
// we dont accept any user overrides of sourcemap configuration
// instead, we force external sourcemaps (with inline sources) on/off based on webpack signals.
compilerOptions.sourceMap = compilerOptions.inlineSources = sourceMap;
compilerOptions.inlineSourceMap = compilerOptions.mapRoot = compilerOptions.sourceRoot = undefined;
// force declarations off, as we don't have .d.ts bundling.
// output locations are irrelevant, as we bundle. this ensures source maps have proper relative paths.
// noEmit will not give us any output, so force that off.
compilerOptions.declaration = compilerOptions.declarationMap = undefined;
compilerOptions.outDir = compilerOptions.out = compilerOptions.outFile = undefined;
compilerOptions.noEmit = compilerOptions.noEmitOnError = compilerOptions.emitDeclarationOnly = undefined;
// caching
const optionsScopedCachePath = cacheDirectoryPath
? join(cacheDirectoryPath, compilerOptionsToCacheName(compilerOptions))
: undefined;
if (optionsScopedCachePath && !ensuredDirectories.has(optionsScopedCachePath)) {
try {
ensureDirectorySync(optionsScopedCachePath);
} catch {
/**/
}
ensuredDirectories.add(optionsScopedCachePath);
}
const transpileOptions = {
fileName: resourcePath,
compilerOptions,
transformers,
reportDiagnostics: true
// force declarations off, as we don't have .d.ts bundling.
// output locations are irrelevant, as we bundle. this ensures source maps have proper relative paths.
// noEmit will not give us any output, so force that off.
compilerOptions.declaration = compilerOptions.declarationMap = undefined;
compilerOptions.outDir = compilerOptions.out = compilerOptions.outFile = undefined;
compilerOptions.noEmit = compilerOptions.noEmitOnError = compilerOptions.emitDeclarationOnly = undefined;
// caching
const optionsScopedCachePath = cacheDirectoryPath
? join(cacheDirectoryPath, compilerOptionsToCacheName(compilerOptions))
: undefined;
if (optionsScopedCachePath && !ensuredDirectories.has(optionsScopedCachePath)) {
try {
ensureDirectorySync(optionsScopedCachePath);
} catch {
/**/
}
ensuredDirectories.add(optionsScopedCachePath);
}
const transpileOptions = {
fileName: resourcePath,
compilerOptions,
transformers,
reportDiagnostics: true
};
// transpile
const { sourceMapText, outputText, diagnostics } = optionsScopedCachePath
? transpileCached({
configFilePath = configLookup ? ts.findConfigFile(contextPath, fileExists, configFileName) : undefined,
compilerOptions: noConfigOptions = defaultCompilerOptions,
cacheDirectoryPath = findCacheDirectory(contextPath),
installSourceMapSupport = !process.execArgv.includes('--enable-source-maps'),
autoScriptTarget = true
}: ICreateNodeExtensionOptions = {}): NodeExtension {
const formatDiagnosticsHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => contextPath,
getCanonicalFileName: getCanonicalPath,
getNewLine
};
const compilerOptions: ts.CompilerOptions = {};
if (typeof configFilePath === 'string') {
const { options, errors } = readAndParseConfigFile(configFilePath);
if (errors.length) {
throw new Error(ts.formatDiagnostics(errors, formatDiagnosticsHost));
}
Object.assign(compilerOptions, options);
if (compilerOptions.module !== ts.ModuleKind.CommonJS) {
// force commonjs, for node
compilerOptions.module = ts.ModuleKind.CommonJS;
}
} else {
Object.assign(compilerOptions, noConfigOptions);
}
// Ensure source maps get picked up by v8 inspector (vscode/chrome debuggers) and node's `--enable-source-maps`.
compilerOptions.inlineSourceMap = true;
compilerOptions.sourceMap = compilerOptions.inlineSources = undefined;
compilerOptions.mapRoot = compilerOptions.sourceRoot = undefined;
getNewLine,
transpileCached,
findCacheDirectory,
ensureDirectorySync,
compilerOptionsToCacheName,
createCachedFn
} from '@ts-tools/transpile';
const { fileExists } = ts.sys;
const identity = (value: string) => value;
const [cachedFindConfigFile] = createCachedFn(
ts.findConfigFile,
(searchPath, _, configName) => searchPath + delimiter + configName
);
const [cachedReadAndParseConfigFile] = createCachedFn(readAndParseConfigFile, identity);
const [cachedFindCacheDirectory] = createCachedFn(findCacheDirectory, identity);
const ensuredDirectories = new Set();
/**
* Loader options which can be provided via webpack configuration
* or a specific request query string
*/
export interface ITypeScriptLoaderOptions {
/**
* Keys to override in the `compilerOptions` section of the
* `tsconfig.json` file.
*/
compilerOptions?: object;
/**
* Turn persistent caching on/off.
transpileCached,
findCacheDirectory,
ensureDirectorySync,
compilerOptionsToCacheName,
createCachedFn
} from '@ts-tools/transpile';
const { fileExists } = ts.sys;
const identity = (value: string) => value;
const [cachedFindConfigFile] = createCachedFn(
ts.findConfigFile,
(searchPath, _, configName) => searchPath + delimiter + configName
);
const [cachedReadAndParseConfigFile] = createCachedFn(readAndParseConfigFile, identity);
const [cachedFindCacheDirectory] = createCachedFn(findCacheDirectory, identity);
const ensuredDirectories = new Set();
/**
* Loader options which can be provided via webpack configuration
* or a specific request query string
*/
export interface ITypeScriptLoaderOptions {
/**
* Keys to override in the `compilerOptions` section of the
* `tsconfig.json` file.
*/
compilerOptions?: object;
/**
* Turn persistent caching on/off.
*
import {
externalSourceMapPrefix,
readAndParseConfigFile,
getCanonicalPath,
getNewLine,
transpileCached,
findCacheDirectory,
ensureDirectorySync,
compilerOptionsToCacheName,
createCachedFn
} from '@ts-tools/transpile';
const { fileExists } = ts.sys;
const identity = (value: string) => value;
const [cachedFindConfigFile] = createCachedFn(
ts.findConfigFile,
(searchPath, _, configName) => searchPath + delimiter + configName
);
const [cachedReadAndParseConfigFile] = createCachedFn(readAndParseConfigFile, identity);
const [cachedFindCacheDirectory] = createCachedFn(findCacheDirectory, identity);
const ensuredDirectories = new Set();
/**
* Loader options which can be provided via webpack configuration
* or a specific request query string
*/
export interface ITypeScriptLoaderOptions {
/**
* Keys to override in the `compilerOptions` section of the
* `tsconfig.json` file.
*/
throw chalk.red(`Cannot find a ${configName} file for ${srcDirectoryPath}`);
}
const formatDiagnosticsHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory,
getCanonicalFileName: getCanonicalPath,
getNewLine: getNewLine
};
const { errors, fileNames, options: tsconfigOptions } = readAndParseConfigFile(tsConfigPath);
if (errors.length) {
throw ts.formatDiagnosticsWithColorAndContext(errors, formatDiagnosticsHost);
}
const canonicalSrcPath = getCanonicalPath(srcDirectoryPath);
const filesInSrcDirectory = fileNames
.map(filePath => ({ filePath, normalizedPath: normalize(filePath) }))
.filter(({ normalizedPath }) => getCanonicalPath(normalizedPath).startsWith(canonicalSrcPath));
const programs: Array<{ folderName: string; program: ts.Program }> = [];
for (const { folderName, getCompilerOptions } of formats) {
const compilerOptions: ts.CompilerOptions = {
...getCompilerOptions(tsconfigOptions),
outDir: undefined,
outFile: undefined,
out: undefined,
noEmit: false
};
programs.push({
.filter(({ normalizedPath }) => getCanonicalPath(normalizedPath).startsWith(canonicalSrcPath));