Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: create a tsc prepare script, use it for angular framework, add ESM to angular dist #20516

Merged
merged 5 commits into from
Jan 6, 2023

Conversation

ndelangen
Copy link
Member

What I did

I created a script that mimicks the bundle script, but using tsc directly.

This is useful to have more control over the build process for packages that need to be compiled using tsc (angular).

This adds some improvements, such as:

  • supporting watch-mode
  • outputting ESM code output.

Known issues:

  • the ESM output hasn't been tested at all
  • the watch mode doesn;t output ESM

I converted the angular framework to use this new script.

@ndelangen ndelangen self-assigned this Jan 6, 2023
@ndelangen ndelangen added the build Internal-facing build tooling & test updates label Jan 6, 2023
Comment on lines +55 to +61
ts.createProgram({
rootNames: out.fileNames,
options: { ...compilerOptions, module: ts.ModuleKind.ES2020, declaration: false },
}).emit();

const files = glob.sync(join(process.cwd(), 'dist', '*.js'));
await Promise.all(files.map((file) => move(file, file.replace('.js', '.mjs'), {})));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm planning on improving this, the compiler host has the ability to override the writeFile method, which would allow me to write the file with a different extension than the one tsc wants to write... this would also be the way to get the watch-mode to work, most likely.

);

const compilerOptions = out.options;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you're thinking: why not simply use a child_process and spawn tsc CLI? That's going to be about a second slower, starting node.

But I'm considering doing it anyway, because typescript's API are all sync and blocking, thus generating both CJS and ESM, can only be done 1 after the other, not in parallel. So perhaps it balances out, and spawning 2 subprocesses is actually faster... :(


const reset = hasFlag(flags, 'reset');
const watch = hasFlag(flags, 'watch');
// const optimized = hasFlag(flags, 'optimized');
Copy link
Member Author

@ndelangen ndelangen Jan 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsc has no concept/feature of minification, so we'd have to use another tool on top, which might break stuff.. so I'm going to say let's just skip it for now, not take the risk.

@ndelangen
Copy link
Member Author

I found the documentation for how to use this here:
https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API

@ndelangen ndelangen added the ci:merged Run the CI jobs that normally run when merged. label Jan 6, 2023
@ndelangen ndelangen marked this pull request as ready for review January 6, 2023 12:41
@ndelangen ndelangen added maintenance User-facing maintenance tasks build Internal-facing build tooling & test updates and removed build Internal-facing build tooling & test updates labels Jan 6, 2023
@ndelangen ndelangen merged commit c30fa6d into next Jan 6, 2023
@ndelangen ndelangen deleted the norbert/tsc-script-for-angular branch January 6, 2023 18:27
@shilman shilman added build Internal-facing build tooling & test updates and removed maintenance User-facing maintenance tasks labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Internal-facing build tooling & test updates ci:merged Run the CI jobs that normally run when merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants