fix(misc): pass full path to tsconfig when registering transpiler (#19451)
This commit is contained in:
parent
e97bbc040b
commit
6946f65059
@ -140,7 +140,7 @@ Nx provides a helper function that you can import within your setup/teardown fil
|
|||||||
|
|
||||||
```typescript {% fileName="global-setup.ts" %}
|
```typescript {% fileName="global-setup.ts" %}
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
|
const cleanupRegisteredPaths = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
import { yourFancyFunction } from '@some-org/my-util-library';
|
import { yourFancyFunction } from '@some-org/my-util-library';
|
||||||
export default async function () {
|
export default async function () {
|
||||||
@ -184,7 +184,7 @@ export default {
|
|||||||
|
|
||||||
```typescript {% fileName="global-setup-swc.ts" %}
|
```typescript {% fileName="global-setup-swc.ts" %}
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
|
const cleanupRegisteredPaths = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
export default async function () {
|
export default async function () {
|
||||||
// swc will hoist all imports, and we need to make sure the register happens first
|
// swc will hoist all imports, and we need to make sure the register happens first
|
||||||
|
|||||||
@ -140,7 +140,7 @@ Nx provides a helper function that you can import within your setup/teardown fil
|
|||||||
|
|
||||||
```typescript {% fileName="global-setup.ts" %}
|
```typescript {% fileName="global-setup.ts" %}
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
|
const cleanupRegisteredPaths = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
import { yourFancyFunction } from '@some-org/my-util-library';
|
import { yourFancyFunction } from '@some-org/my-util-library';
|
||||||
export default async function () {
|
export default async function () {
|
||||||
@ -184,7 +184,7 @@ export default {
|
|||||||
|
|
||||||
```typescript {% fileName="global-setup-swc.ts" %}
|
```typescript {% fileName="global-setup-swc.ts" %}
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
|
const cleanupRegisteredPaths = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
export default async function () {
|
export default async function () {
|
||||||
// swc will hoist all imports, and we need to make sure the register happens first
|
// swc will hoist all imports, and we need to make sure the register happens first
|
||||||
|
|||||||
@ -53,7 +53,7 @@ describe('Jest', () => {
|
|||||||
`libs/${mylib}/setup.ts`,
|
`libs/${mylib}/setup.ts`,
|
||||||
stripIndents`
|
stripIndents`
|
||||||
const { registerTsProject } = require('@nx/js/src/internal');
|
const { registerTsProject } = require('@nx/js/src/internal');
|
||||||
const cleanup = registerTsProject('.', 'tsconfig.base.json');
|
const cleanup = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
import {setup} from '@global-fun/globals';
|
import {setup} from '@global-fun/globals';
|
||||||
export default async function() {setup();}
|
export default async function() {setup();}
|
||||||
@ -66,7 +66,7 @@ describe('Jest', () => {
|
|||||||
`libs/${mylib}/teardown.ts`,
|
`libs/${mylib}/teardown.ts`,
|
||||||
stripIndents`
|
stripIndents`
|
||||||
const { registerTsProject } = require('@nx/js/src/internal');
|
const { registerTsProject } = require('@nx/js/src/internal');
|
||||||
const cleanup = registerTsProject('.', 'tsconfig.base.json');
|
const cleanup = registerTsProject('./tsconfig.base.json');
|
||||||
|
|
||||||
import {teardown} from '@global-fun/globals';
|
import {teardown} from '@global-fun/globals';
|
||||||
export default async function() {teardown();}
|
export default async function() {teardown();}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { join } from 'path';
|
import { basename, dirname, join } from 'path';
|
||||||
import { existsSync, readFileSync } from 'fs';
|
import { existsSync, readFileSync } from 'fs';
|
||||||
import { logger, ProjectConfiguration } from '@nx/devkit';
|
import { logger, ProjectConfiguration } from '@nx/devkit';
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
@ -92,10 +92,7 @@ function getModuleFederationConfig(
|
|||||||
|
|
||||||
let cleanupTranspiler = () => {};
|
let cleanupTranspiler = () => {};
|
||||||
if (existsSync(moduleFederationConfigPathTS)) {
|
if (existsSync(moduleFederationConfigPathTS)) {
|
||||||
cleanupTranspiler = registerTsProject(
|
cleanupTranspiler = registerTsProject(join(workspaceRoot, tsconfigPath));
|
||||||
moduleFederationConfigPathTS,
|
|
||||||
tsconfigPath
|
|
||||||
);
|
|
||||||
moduleFederationConfigPath = moduleFederationConfigPathTS;
|
moduleFederationConfigPath = moduleFederationConfigPathTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import { merge } from 'webpack-merge';
|
import { merge } from 'webpack-merge';
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
|
import { workspaceRoot } from '@nx/devkit';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
export async function mergeCustomWebpackConfig(
|
export async function mergeCustomWebpackConfig(
|
||||||
baseWebpackConfig: any,
|
baseWebpackConfig: any,
|
||||||
@ -9,7 +11,7 @@ export async function mergeCustomWebpackConfig(
|
|||||||
) {
|
) {
|
||||||
const customWebpackConfiguration = resolveCustomWebpackConfig(
|
const customWebpackConfiguration = resolveCustomWebpackConfig(
|
||||||
pathToWebpackConfig,
|
pathToWebpackConfig,
|
||||||
options.tsConfig
|
join(workspaceRoot, options.tsConfig)
|
||||||
);
|
);
|
||||||
// The extra Webpack configuration file can also export a Promise, for instance:
|
// The extra Webpack configuration file can also export a Promise, for instance:
|
||||||
// `module.exports = new Promise(...)`. If it exports a single object, but not a Promise,
|
// `module.exports = new Promise(...)`. If it exports a single object, but not a Promise,
|
||||||
@ -26,7 +28,7 @@ export async function mergeCustomWebpackConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
|
export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
|
||||||
const cleanupTranspiler = registerTsProject(path, tsConfig);
|
const cleanupTranspiler = registerTsProject(tsConfig);
|
||||||
const customWebpackConfig = require(path);
|
const customWebpackConfig = require(path);
|
||||||
cleanupTranspiler();
|
cleanupTranspiler();
|
||||||
// If the user provides a configuration in TS file
|
// If the user provides a configuration in TS file
|
||||||
@ -42,7 +44,7 @@ export function resolveIndexHtmlTransformer(
|
|||||||
tsConfig: string,
|
tsConfig: string,
|
||||||
target: import('@angular-devkit/architect').Target
|
target: import('@angular-devkit/architect').Target
|
||||||
) {
|
) {
|
||||||
const cleanupTranspiler = registerTsProject(path, tsConfig);
|
const cleanupTranspiler = registerTsProject(tsConfig);
|
||||||
const indexTransformer = require(path);
|
const indexTransformer = require(path);
|
||||||
cleanupTranspiler();
|
cleanupTranspiler();
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { existsSync } from 'fs';
|
|||||||
import { readNxJson } from 'nx/src/config/configuration';
|
import { readNxJson } from 'nx/src/config/configuration';
|
||||||
import { isNpmProject } from 'nx/src/project-graph/operators';
|
import { isNpmProject } from 'nx/src/project-graph/operators';
|
||||||
import { getDependencyConfigs } from 'nx/src/tasks-runner/utils';
|
import { getDependencyConfigs } from 'nx/src/tasks-runner/utils';
|
||||||
|
import { join } from 'path';
|
||||||
import { from, Observable } from 'rxjs';
|
import { from, Observable } from 'rxjs';
|
||||||
import { switchMap } from 'rxjs/operators';
|
import { switchMap } from 'rxjs/operators';
|
||||||
import { createTmpTsConfigForBuildableLibs } from '../utilities/buildable-libs';
|
import { createTmpTsConfigForBuildableLibs } from '../utilities/buildable-libs';
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import { createTmpTsConfigForBuildableLibs } from '../utilities/buildable-libs';
|
|||||||
import { from } from 'rxjs';
|
import { from } from 'rxjs';
|
||||||
import { switchMap } from 'rxjs/operators';
|
import { switchMap } from 'rxjs/operators';
|
||||||
import { getRootTsConfigPath } from '@nx/js';
|
import { getRootTsConfigPath } from '@nx/js';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
type BuildTargetOptions = {
|
type BuildTargetOptions = {
|
||||||
tsConfig: string;
|
tsConfig: string;
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import type { TSESLint } from '@typescript-eslint/utils';
|
|||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
import { WORKSPACE_PLUGIN_DIR, WORKSPACE_RULE_NAMESPACE } from './constants';
|
import { WORKSPACE_PLUGIN_DIR, WORKSPACE_RULE_NAMESPACE } from './constants';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
type ESLintRules = Record<string, TSESLint.RuleModule<string, unknown[]>>;
|
type ESLintRules = Record<string, TSESLint.RuleModule<string, unknown[]>>;
|
||||||
|
|
||||||
@ -11,7 +12,9 @@ export const workspaceRules = ((): ESLintRules => {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
// Register `tools/eslint-rules` for TS transpilation
|
// Register `tools/eslint-rules` for TS transpilation
|
||||||
const registrationCleanup = registerTsProject(WORKSPACE_PLUGIN_DIR);
|
const registrationCleanup = registerTsProject(
|
||||||
|
join(WORKSPACE_PLUGIN_DIR, 'tsconfig.json')
|
||||||
|
);
|
||||||
try {
|
try {
|
||||||
/**
|
/**
|
||||||
* Currently we only support applying the rules from the user's workspace plugin object
|
* Currently we only support applying the rules from the user's workspace plugin object
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import * as path from 'path';
|
|||||||
import { createESLintRule } from '../utils/create-eslint-rule';
|
import { createESLintRule } from '../utils/create-eslint-rule';
|
||||||
import { readProjectGraph } from '../utils/project-graph-utils';
|
import { readProjectGraph } from '../utils/project-graph-utils';
|
||||||
import { valid } from 'semver';
|
import { valid } from 'semver';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
type Options = [
|
type Options = [
|
||||||
{
|
{
|
||||||
@ -147,7 +148,7 @@ export default createESLintRule<Options, MessageIds>({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(global as any).tsProjectRegistered) {
|
if (!(global as any).tsProjectRegistered) {
|
||||||
registerTsProject(workspaceRoot, 'tsconfig.base.json');
|
registerTsProject(join(workspaceRoot, 'tsconfig.base.json'));
|
||||||
(global as any).tsProjectRegistered = true;
|
(global as any).tsProjectRegistered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,11 +18,24 @@ let ts: typeof import('typescript');
|
|||||||
*
|
*
|
||||||
* @returns cleanup function
|
* @returns cleanup function
|
||||||
*/
|
*/
|
||||||
export const registerTsProject = (
|
export function registerTsProject(tsConfigPath: string): () => void;
|
||||||
|
/**
|
||||||
|
* Optionally, if swc-node and tsconfig-paths are available in the current workspace, apply the require
|
||||||
|
* register hooks so that .ts files can be used for writing custom workspace projects.
|
||||||
|
*
|
||||||
|
* If ts-node and tsconfig-paths are not available, the user can still provide an index.js file in
|
||||||
|
* the root of their project and the fundamentals will still work (but
|
||||||
|
* workspace path mapping will not, for example).
|
||||||
|
*
|
||||||
|
* @returns cleanup function
|
||||||
|
* @deprecated This signature will be removed in Nx v18. You should pass the full path to the tsconfig in the first argument.
|
||||||
|
*/
|
||||||
|
export function registerTsProject(path: string, configFilename: string);
|
||||||
|
export function registerTsProject(
|
||||||
path: string,
|
path: string,
|
||||||
configFilename = 'tsconfig.json'
|
configFilename?: string
|
||||||
): (() => void) => {
|
): () => void {
|
||||||
const tsConfigPath = join(path, configFilename);
|
const tsConfigPath = configFilename ? join(path, configFilename) : path;
|
||||||
const compilerOptions: CompilerOptions = readCompilerOptions(tsConfigPath);
|
const compilerOptions: CompilerOptions = readCompilerOptions(tsConfigPath);
|
||||||
|
|
||||||
const cleanupFunctions: ((...args: unknown[]) => unknown)[] = [
|
const cleanupFunctions: ((...args: unknown[]) => unknown)[] = [
|
||||||
@ -35,7 +48,7 @@ export const registerTsProject = (
|
|||||||
fn();
|
fn();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
export function getSwcTranspiler(
|
export function getSwcTranspiler(
|
||||||
compilerOptions: CompilerOptions
|
compilerOptions: CompilerOptions
|
||||||
@ -132,7 +145,7 @@ export function registerTsConfigPaths(tsConfigPath): () => void {
|
|||||||
/**
|
/**
|
||||||
* Load the ts config from the source project
|
* Load the ts config from the source project
|
||||||
*/
|
*/
|
||||||
const tsconfigPaths: typeof import('tsconfig-paths') = require('tsconfig-paths');
|
const tsconfigPaths = loadTsConfigPaths();
|
||||||
const tsConfigResult = tsconfigPaths.loadConfig(tsConfigPath);
|
const tsConfigResult = tsconfigPaths.loadConfig(tsConfigPath);
|
||||||
/**
|
/**
|
||||||
* Register the custom workspace path mappings with node so that workspace libraries
|
* Register the custom workspace path mappings with node so that workspace libraries
|
||||||
@ -145,9 +158,11 @@ export function registerTsConfigPaths(tsConfigPath): () => void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
warnNoTsconfigPaths();
|
if (err instanceof Error) {
|
||||||
|
throw new Error(`Unable to load ${tsConfigPath}: ` + err.message);
|
||||||
}
|
}
|
||||||
return () => {};
|
}
|
||||||
|
throw new Error(`Unable to load ${tsConfigPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readCompilerOptions(tsConfigPath): CompilerOptions {
|
function readCompilerOptions(tsConfigPath): CompilerOptions {
|
||||||
@ -178,6 +193,14 @@ function readCompilerOptionsWithTypescript(tsConfigPath) {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadTsConfigPaths(): typeof import('tsconfig-paths') | null {
|
||||||
|
try {
|
||||||
|
return require('tsconfig-paths');
|
||||||
|
} catch {
|
||||||
|
warnNoTsconfigPaths();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function warnTsNodeUsage() {
|
function warnTsNodeUsage() {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
stripIndent(`${NX_PREFIX} Falling back to ts-node for local typescript execution. This may be a little slower.
|
stripIndent(`${NX_PREFIX} Falling back to ts-node for local typescript execution. This may be a little slower.
|
||||||
|
|||||||
@ -56,10 +56,7 @@ function getModuleFederationConfig(
|
|||||||
// create a no-op so this can be called with issue
|
// create a no-op so this can be called with issue
|
||||||
let cleanupTranspiler = () => {};
|
let cleanupTranspiler = () => {};
|
||||||
if (existsSync(moduleFederationConfigPathTS)) {
|
if (existsSync(moduleFederationConfigPathTS)) {
|
||||||
cleanupTranspiler = registerTsProject(
|
cleanupTranspiler = registerTsProject(join(workspaceRoot, tsconfigPath));
|
||||||
moduleFederationConfigPathTS,
|
|
||||||
tsconfigPath
|
|
||||||
);
|
|
||||||
moduleFederationConfigPath = moduleFederationConfigPathTS;
|
moduleFederationConfigPath = moduleFederationConfigPathTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import { resolveCustomWebpackConfig } from '../../utils/webpack/custom-webpack';
|
|||||||
import { normalizeOptions } from '../webpack/lib/normalize-options';
|
import { normalizeOptions } from '../webpack/lib/normalize-options';
|
||||||
import { WebpackExecutorOptions } from '../webpack/schema';
|
import { WebpackExecutorOptions } from '../webpack/schema';
|
||||||
import { WebDevServerOptions } from './schema';
|
import { WebDevServerOptions } from './schema';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
export async function* devServerExecutor(
|
export async function* devServerExecutor(
|
||||||
serveOptions: WebDevServerOptions,
|
serveOptions: WebDevServerOptions,
|
||||||
@ -62,9 +63,12 @@ export async function* devServerExecutor(
|
|||||||
let config = getDevServerConfig(context, buildOptions, serveOptions);
|
let config = getDevServerConfig(context, buildOptions, serveOptions);
|
||||||
|
|
||||||
if (buildOptions.webpackConfig) {
|
if (buildOptions.webpackConfig) {
|
||||||
|
let tsconfigPath = buildOptions.tsConfig.startsWith(context.root)
|
||||||
|
? buildOptions.tsConfig
|
||||||
|
: join(context.root, buildOptions.tsConfig);
|
||||||
let customWebpack = resolveCustomWebpackConfig(
|
let customWebpack = resolveCustomWebpackConfig(
|
||||||
buildOptions.webpackConfig,
|
buildOptions.webpackConfig,
|
||||||
buildOptions.tsConfig
|
tsconfigPath
|
||||||
);
|
);
|
||||||
|
|
||||||
if (typeof customWebpack.then === 'function') {
|
if (typeof customWebpack.then === 'function') {
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
import { ExecutorContext, logger, stripIndents } from '@nx/devkit';
|
import {
|
||||||
|
ExecutorContext,
|
||||||
|
logger,
|
||||||
|
stripIndents,
|
||||||
|
workspaceRoot,
|
||||||
|
} from '@nx/devkit';
|
||||||
import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
|
import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
|
||||||
import type { Configuration, Stats } from 'webpack';
|
import type { Configuration, Stats } from 'webpack';
|
||||||
import { from, of } from 'rxjs';
|
import { from, of } from 'rxjs';
|
||||||
@ -9,7 +14,7 @@ import {
|
|||||||
switchMap,
|
switchMap,
|
||||||
tap,
|
tap,
|
||||||
} from 'rxjs/operators';
|
} from 'rxjs/operators';
|
||||||
import { resolve } from 'path';
|
import { join, resolve } from 'path';
|
||||||
import {
|
import {
|
||||||
calculateProjectBuildableDependencies,
|
calculateProjectBuildableDependencies,
|
||||||
createTmpTsConfig,
|
createTmpTsConfig,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { registerTsProject } from '@nx/js/src/internal';
|
import { registerTsProject } from '@nx/js/src/internal';
|
||||||
|
|
||||||
export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
|
export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
|
||||||
const cleanupTranspiler = registerTsProject(path, tsConfig);
|
const cleanupTranspiler = registerTsProject(tsConfig);
|
||||||
const customWebpackConfig = require(path);
|
const customWebpackConfig = require(path);
|
||||||
cleanupTranspiler();
|
cleanupTranspiler();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user