-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): update TS module resolution flow
This commit updates the implementation for resolving `.ts` files. Instead of registering the `ts-node` project only once, we now refrain from doing so since there might be multiple projects with different configurations. The current approach involves dynamically switching the implementation for registering and unregistering the project after the `.ts` file has been transpiled and resolved. This change addresses an issue where warnings were encountered when `ts-node` attempted to register with different configurations. The number of configurations is no longer a concern, as each time we need to read a `.ts` file, a new TS project is registered. This adjustment does not impact performance or other attributes because `ts-node` allows native project disabling. Part of the implementation has been adapted from what Nrwl Nx already has; we can find their implementation here: https://github.com/nrwl/nx/blob/master/packages/nx/src/plugins/js/utils/register.ts It's worth noting that their implementation is somewhat versatile, as it also supports SWC. Closes: #1197 Closes: #1213
- Loading branch information
Showing
16 changed files
with
160 additions
and
119 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
examples/custom-webpack/sanity-app-esm/custom-webpack.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import type { Configuration } from 'webpack'; | ||
|
||
import { WebpackEsmPlugin } from 'webpack-esm-plugin'; | ||
|
||
export default async (cfg: Configuration) => { | ||
const { default: configFromEsm } = await import('./custom-webpack.config.js'); | ||
|
||
// This is used to ensure we fixed the following issue: | ||
// https://github.com/just-jeb/angular-builders/issues/1213 | ||
cfg.plugins!.push(new WebpackEsmPlugin()); | ||
|
||
// Do some stuff with config and configFromEsm | ||
return { ...cfg, ...configFromEsm }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
examples/custom-webpack/sanity-app-esm/webpack-esm-plugin/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "webpack-esm-plugin", | ||
"version": "0.0.1", | ||
"module": "./webpack-esm-plugin.mjs", | ||
"typings": "./webpack-esm-plugin.d.ts", | ||
"exports": { | ||
"./package.json": { | ||
"default": "./package.json" | ||
}, | ||
".": { | ||
"types": "./webpack-esm-plugin.d.ts", | ||
"node": "./webpack-esm-plugin.mjs", | ||
"default": "./webpack-esm-plugin.mjs" | ||
} | ||
}, | ||
"sideEffects": false | ||
} |
5 changes: 5 additions & 0 deletions
5
examples/custom-webpack/sanity-app-esm/webpack-esm-plugin/webpack-esm-plugin.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as webpack from 'webpack'; | ||
|
||
export declare class WebpackEsmPlugin { | ||
apply(compiler: webpack.Compiler): void; | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/custom-webpack/sanity-app-esm/webpack-esm-plugin/webpack-esm-plugin.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class WebpackEsmPlugin { | ||
apply(compiler) { | ||
console.error('hello from the WebpackEsmPlugin'); | ||
} | ||
} | ||
|
||
export { WebpackEsmPlugin }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as path from 'node:path'; | ||
import type { CompilerOptions } from 'typescript'; | ||
|
||
let ts: typeof import('typescript'); | ||
let isTsEsmLoaderRegistered = false; | ||
|
||
export function registerTsProject(tsConfig: string) { | ||
const cleanupFunctions = [registerTsConfigPaths(tsConfig), registerTsNodeService(tsConfig)]; | ||
|
||
// Add ESM support for `.ts` files. | ||
// NOTE: There is no cleanup function for this, as it's not possible to unregister the loader. | ||
// Based on limited testing, it doesn't seem to matter if we register it multiple times, but just in | ||
// case let's keep a flag to prevent it. | ||
if (!isTsEsmLoaderRegistered) { | ||
const module = require('node:module'); | ||
if (module.register && packageIsInstalled('ts-node/esm')) { | ||
const url = require('node:url'); | ||
module.register(url.pathToFileURL(require.resolve('ts-node/esm'))); | ||
} | ||
isTsEsmLoaderRegistered = true; | ||
} | ||
|
||
return () => { | ||
cleanupFunctions.forEach(fn => fn()); | ||
}; | ||
} | ||
|
||
function registerTsNodeService(tsConfig: string): VoidFunction { | ||
const { register } = require('ts-node') as typeof import('ts-node'); | ||
|
||
const service = register({ | ||
project: tsConfig, | ||
compilerOptions: { | ||
module: 'CommonJS', | ||
types: [ | ||
'node', // NOTE: `node` is added because users scripts can also use pure node's packages as webpack or others | ||
], | ||
}, | ||
}); | ||
|
||
return () => { | ||
service.enabled(false); | ||
}; | ||
} | ||
|
||
function registerTsConfigPaths(tsConfig: string): VoidFunction { | ||
const tsConfigPaths = require('tsconfig-paths') as typeof import('tsconfig-paths'); | ||
const result = tsConfigPaths.loadConfig(tsConfig); | ||
if (result.resultType === 'success') { | ||
const { absoluteBaseUrl: baseUrl, paths } = result; | ||
if (baseUrl && paths) { | ||
// Returns a function to undo paths registration. | ||
return tsConfigPaths.register({ baseUrl, paths }); | ||
} | ||
} | ||
|
||
// We cannot return anything here if paths failed to be registered. | ||
// Additionally, I don't think we should perform any logging in this | ||
// context, considering that this is internal information not exposed | ||
// to the end user | ||
return () => {}; | ||
} | ||
|
||
function packageIsInstalled(m: string): boolean { | ||
try { | ||
require.resolve(m); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function readCompilerOptions(tsConfig: string): CompilerOptions { | ||
ts ??= require('typescript'); | ||
|
||
const jsonContent = ts.readConfigFile(tsConfig, ts.sys.readFile); | ||
const { options } = ts.parseJsonConfigFileContent( | ||
jsonContent.config, | ||
ts.sys, | ||
path.dirname(tsConfig) | ||
); | ||
|
||
// This property is returned in compiler options for some reason, but not part of the typings. | ||
// ts-node fails on unknown props, so we have to remove it. | ||
delete options.configFilePath; | ||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.