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

Set up TypeScript #19559

Merged
merged 30 commits into from
Sep 16, 2022
Merged

Set up TypeScript #19559

merged 30 commits into from
Sep 16, 2022

Conversation

emilpaw
Copy link
Contributor

@emilpaw emilpaw commented Aug 28, 2022

Initial set up of TypeScript.


Please make sure the below checklist is followed for Pull Requests.

When you are still working on the PR, consider converting it to Draft (below reviewers) and adding skip-ci label, you can still see CI build result at your branch.

@github-actions github-actions bot added the theme: dependencies Pull requests that update a dependency file label Aug 28, 2022
@emilpaw emilpaw mentioned this pull request Aug 28, 2022
1 task
.eslintrc.json Outdated Show resolved Hide resolved
@Tcharl
Copy link
Contributor

Tcharl commented Sep 5, 2022

Not an expert, but I would love to start contributing to jhipster in typescript!

.gitignore Show resolved Hide resolved
@emilpaw
Copy link
Contributor Author

emilpaw commented Sep 11, 2022

At this point running npm run build compiles all the JavaScript code files and moves the needed assets to the build directory dist.

Running it does not work yet though. First linking with npm link and then running jhipster info throws AssertionError [ERR_ASSERTION]: Error on the registered namespace dist:app, make sure your folder is called generator-jhipster.

@mshima Could you help me understand what the error exactly means and what the issue is?

@mshima
Copy link
Member

mshima commented Sep 11, 2022

At this point running npm run build compiles all the JavaScript code files and moves the needed assets to the build directory dist.

Why copy every js file to dist?
We can keep old structure with additional dist and src folders for now.

Running it does not work yet though. First linking with npm link and then running jhipster info throws AssertionError [ERR_ASSERTION]: Error on the registered namespace dist:app, make sure your folder is called generator-jhipster.

@mshima Could you help me understand what the error exactly means and what the issue is?

This is related to the generators lookup process.
We need to customize the lookup process when moving generators to dist.
IMO we should do this when they are migrated to typescript.

@emilpaw
Copy link
Contributor Author

emilpaw commented Sep 11, 2022

Why copy every js file to dist?

TypeScript compiles all source files, JavaScript and TypeScript ones. It's not just copying the files. Not sure what the migration to TS could look like if we only compile a selected part of the program and leave the rest uncompiled.

I was thinking about letting TypeScript compile the whole program and then piece after piece migrate the JavaScript files to TypeScript.

What exactly did you have in mind?

This is related to the generators lookup process. We need to customize the lookup process when moving generators to dist.

I am still not sure what the code does but I just naively replace .. with ../.. in

this.env.lookup({ packagePaths: [path.join(__dirname, '..')], lookups: ['generators'] }).forEach(generator => {
and so far it seems to work. At least generating a project works fine.

IMO we should do this when they are migrated to typescript.

If there is a way to only compile parts of the programm and we decide we prefer that over letting TypeScript compile all source files we could do this. Otherwise this is needed to be done within the scope of this PR.

@mraible
Copy link
Contributor

mraible commented Sep 11, 2022

@mshima
Copy link
Member

mshima commented Sep 11, 2022

Why copy every js file to dist?

TypeScript compiles all source files, JavaScript and TypeScript ones. It's not just copying the files. Not sure what the migration to TS could look like if we only compile a selected part of the program and leave the rest uncompiled.

I was thinking about letting TypeScript compile the whole program and then piece after piece migrate the JavaScript files to TypeScript.

What exactly did you have in mind?

I was thinking to migrate jdl first to reduce the complexity of the migration.

This is related to the generators lookup process. We need to customize the lookup process when moving generators to dist.

I am still not sure what the code does but I just naively replace .. with ../.. in

this.env.lookup({ packagePaths: [path.join(__dirname, '..')], lookups: ['generators'] }).forEach(generator => {

The lookups should be changed to dist/generators too.

and so far it seems to work. At least generating a project works fine.

IMO we should do this when they are migrated to typescript.

If there is a way to only compile parts of the programm and we decide we prefer that over letting TypeScript compile all source files we could do this. Otherwise this is needed to be done within the scope of this PR.

IFAIK jdl doesn't uses other folder, so compiling the jdl folder and moving it to src/jdl should be enough.
And change jdl imports to use dist/jdl.

const { createImporterFromContent, createImporterFromFiles } = require('../jdl/jdl-importer');

Anyway, feel free to compile everything if it works.

Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

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

We will support node >=16, target and module can be bumped.

package.json Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
tsconfig.json Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@mshima
Copy link
Member

mshima commented Sep 13, 2022

What do you want to do? Do you want the tests to stay written in JavaScript and not migrated to TypeScript?

If we copy tests to dist, how do we update snapshots?
We should move every directory to src before building with typescript. This will make import package.json work in both dist and src directories.
We could use mocha + ts-node to test ts files or other alternative for in place ts tests.

@emilpaw
Copy link
Contributor Author

emilpaw commented Sep 14, 2022

If we copy tests to dist, how do we update snapshots?

I haven't yet looked into how snapshots work, so I don't know.

We should move every directory to src before building with typescript. This will make import package.json work in both dist and src directories.

You mean to move cli, generators, jdl and lib into a newly created directory src and then use the path ../../package.json instead of ../package.json? If you think that's better than copying the package.json we can do that.

We could use mocha + ts-node to test ts files or other alternative for in place ts tests.

Yes, I also looked into that but wasn't sure if that's the right approach.

The generator lookup seems to not work correctly yet. At least that's my guess based on the error message in the failed tests. So just changing the paths did not work. I am still looking into that.

@mshima
Copy link
Member

mshima commented Sep 14, 2022

If we copy tests to dist, how do we update snapshots?

I haven't yet looked into how snapshots work, so I don't know.

npm run update-snapshots runs tests updating snapshots located near tests.

We should move every directory to src before building with typescript. This will make import package.json work in both dist and src directories.

You mean to move cli, generators, jdl and lib into a newly created directory src and then use the path ../../package.json instead of ../package.json? If you think that's better than copying the package.json we can do that.

That's not the only reason, having more than one root source folder can be confusing.
Improving the source tree is planned for v8.
My idea was to have cli, lib and generators folder for v8. Moving utils and jdl inside lib.
Migrating everything to typescript, we can have something like:

  • src
    • cli
    • jdl
    • generators
    • lib
      • utils

We could use mocha + ts-node to test ts files or other alternative for in place ts tests.

Yes, I also looked into that but wasn't sure if that's the right approach.

The generator lookup seems to not work correctly yet. At least that's my guess based on the error message in the failed tests. So just changing the paths did not work. I am still looking into that.

The lookup is working fine, all integration tests are working.
But tests are been executed in place, so the lookup is looking in the wrong place when executing tests due to the change.

Moving sources to src should fix the lookup too.

cli/environment-builder.js Outdated Show resolved Hide resolved
@emilpaw emilpaw mentioned this pull request Sep 15, 2022
6 tasks
@emilpaw emilpaw force-pushed the setup-typescript branch 2 times, most recently from 6136ea8 to c44818b Compare September 15, 2022 16:51
emilpaw and others added 5 commits September 15, 2022 18:58
Don't copy files ending with .snap and folders starting with __ (currently __snapshots__ & __workflow)  to build output. These are files needed for tests or CI which have no use in the build output.
@emilpaw emilpaw force-pushed the setup-typescript branch 2 times, most recently from e9640be to 931bc39 Compare September 15, 2022 18:40
@emilpaw
Copy link
Contributor Author

emilpaw commented Sep 15, 2022

All the tests were passing. While rebasing I made a mistake but now I restored it to the pre-rebase mistake state. @mshima You could force push your local branch if commits are missing but I think it's good now.

I also started to work on moving the source files to an src folder. See: #19750

In both PRs the pipelines don't seem to start anymore though. Any idea why?

@mshima
Copy link
Member

mshima commented Sep 15, 2022

In both PRs the pipelines don't seem to start anymore though. Any idea why?

Due to conflicts, since this tests of this PR is succeeding, we can finish it and merge.
The directory rearrangement can be done after.

@emilpaw
Copy link
Contributor Author

emilpaw commented Sep 15, 2022

Due to conflicts

I thought that also the pipeline in this PR was not starting but it just took a very long time.

Adapting the package.json exports to the new build output was not yet done. I changed the paths and then some tests broke. Currently, I am working on that. Other than that I think this PR is done.

@emilpaw emilpaw marked this pull request as ready for review September 16, 2022 08:52
Comment on lines 48 to 52
"./esm/cli": "./cli/index.mjs",
"./esm/generators/*": "./generators/*/esm.mjs",
"./esm/generators": "./lib/constants/generators.mjs",
"./esm/priorities": "./lib/constants/priorities.mjs",
"./esm/support": "./lib/support/index.cjs"
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for not update?

Suggested change
"./esm/cli": "./cli/index.mjs",
"./esm/generators/*": "./generators/*/esm.mjs",
"./esm/generators": "./lib/constants/generators.mjs",
"./esm/priorities": "./lib/constants/priorities.mjs",
"./esm/support": "./lib/support/index.cjs"
"./esm/cli": "./dist/cli/index.mjs",
"./esm/generators/*": "./dist/generators/*/esm.mjs",
"./esm/generators": "./dist/lib/constants/generators.mjs",
"./esm/priorities": "./dist/lib/constants/priorities.mjs",
"./esm/support": "./dist/lib/support/index.cjs"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I explained it in the commit: 5263183

package.json Outdated Show resolved Hide resolved
Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

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

I took a look at prepare script and it seems to work for this case:
Adding "prepare": "npm run build", script to package.json and build should be executed every time npm install is executed.
Can you check please?

Comment on lines 56 to 57
npm ci --prod
npm run build
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
npm ci --prod
npm run build
npm install

Comment on lines 73 to 74
npm ci --prod
npm run build
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
npm ci --prod
npm run build
npm install

tsconfig.json Outdated
"include": ["cli/**/*", "generators/**/*", "jdl/**/*", "lib/**/*", "test/**/*"],
"exclude": ["**/*.spec.*"],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove commented configs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

When using the build output for these exports the tests which import
these modules as part of the test case fail.

This has two reasons:
1. In CI/CD npm run build is not run before the tests so the
   modules fail to be imported. This could be solved by first building
   the package.
2. In the tests the modules are imported via ES Modules syntax and the
   import function and the result is compared for referential equality.
   When the built file is imported through the import function and
   it's compared to the source file the reference is always
   not the same.

When this part of the code is converted to TypeScript these issues must
be addressed.
This will build the project when it's installed.
This should be done by the prepare npm script now.
@mshima mshima merged commit 71b7e73 into jhipster:main Sep 16, 2022
@emilpaw emilpaw deleted the setup-typescript branch September 16, 2022 17:06
This was referenced Sep 16, 2022
@DanielFran DanielFran added this to the 8.0.0-beta.1 milestone Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: cli theme: dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants