From aac30b4ebd2c0b5fab72b4d5fc91285ece8be25c Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 12 Feb 2021 18:34:45 +0100 Subject: [PATCH] chore(scripts): make current tsc just task to work with ts aliases --- scripts/tasks/ts.ts | 63 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/scripts/tasks/ts.ts b/scripts/tasks/ts.ts index e3294a742be221..5d3fc74828d643 100644 --- a/scripts/tasks/ts.ts +++ b/scripts/tasks/ts.ts @@ -1,5 +1,11 @@ +import fs from 'fs'; import * as path from 'path'; -import { tscTask, argv } from 'just-scripts'; +import { tscTask, argv, TscTaskOptions, resolveCwd, logger } from 'just-scripts'; +import { Arguments } from 'yargs'; + +interface JustArgs extends Arguments { + production?: boolean; +} const libPath = path.resolve(process.cwd(), 'lib'); const srcPath = path.resolve(process.cwd(), 'src'); @@ -7,42 +13,77 @@ const srcPath = path.resolve(process.cwd(), 'src'); const useTsBuildInfo = /[\\/]packages[\\/]fluentui[\\/]/.test(process.cwd()) && path.basename(process.cwd()) !== 'perf-test'; -function getExtraTscParams(args) { +/** + * + * Explicitly set `baseUrl` to current package root for packages (converged packages) that use TS path aliases. + * > - This is a temporary workaround for current way of building packages. + * > - Without setting baseUrl we would get all aliased packages build within outDir + */ +function backportTsAliasedPackages(options: TscTaskOptions) { + const tsConfigFilePath = resolveCwd('./tsconfig.json'); + const tsConfig = JSON.parse(fs.readFileSync(tsConfigFilePath, 'utf-8')); + + const normalizedOptions = { ...options }; + + if (tsConfig.extends) { + logger.info(`package is using TS path aliases. Overriding baseUrl to package root.`); + normalizedOptions.baseUrl = '.'; + } + + return normalizedOptions; +} + +function getExtraTscParams(args: JustArgs) { return { - pretty: true, target: 'es5', // sourceMap must be true for inlineSources and sourceRoot to work ...(args.production && { inlineSources: true, sourceRoot: path.relative(libPath, srcPath), sourceMap: true }), }; } +function getJustArgv(): JustArgs { + return argv(); +} + export const ts = { commonjs: () => { - const extraOptions = getExtraTscParams(argv()); - return tscTask({ + const extraOptions = getExtraTscParams(getJustArgv()); + const options = backportTsAliasedPackages({ ...extraOptions, outDir: 'lib-commonjs', module: 'commonjs', ...(useTsBuildInfo && { tsBuildInfoFile: '.commonjs.tsbuildinfo' }), }); + + return tscTask(options); }, esm: () => { - const extraOptions = getExtraTscParams(argv()); + const extraOptions = getExtraTscParams(getJustArgv()); + const options = backportTsAliasedPackages({ + ...extraOptions, + outDir: 'lib', + module: 'esnext', + }); + // Use default tsbuildinfo for this variant - return tscTask({ ...extraOptions, outDir: 'lib', module: 'esnext' }); + return tscTask(options); }, amd: () => { - const extraOptions = getExtraTscParams(argv()); - return tscTask({ + const extraOptions = getExtraTscParams(getJustArgv()); + const options = backportTsAliasedPackages({ ...extraOptions, outDir: 'lib-amd', module: 'amd', ...(useTsBuildInfo && { tsBuildInfoFile: '.amd.tsbuildinfo' }), }); + + return tscTask(options); }, commonjsOnly: () => { - const extraOptions = getExtraTscParams(argv()); + const extraOptions = getExtraTscParams(getJustArgv()); // Use default tsbuildinfo for this variant (since it's the only variant) - return tscTask({ ...extraOptions, outDir: 'lib', module: 'commonjs' }); + const options = backportTsAliasedPackages({ ...extraOptions, outDir: 'lib', module: 'commonjs' }); + + return tscTask(options); }, };