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

Angular 13 compatibility #247

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Conversation

bartholomej
Copy link

What is in this PR?

  • Building UMD

Why?

Because it no longer works with Angular 13. Issue: #246

Notes

  • Possible breaking changes!
  • Tested with:
    • Angular 12.0.0 (with ivy), nodejs14, nodejs16
    • Angular 13.0.0 (with ivy), nodejs14, nodejs16

More testing would be appreciated :)

@venraij
Copy link

venraij commented Nov 15, 2021

@bartholomej I've tested it with Angular 13 NodeJS 16 as well but still doesn't work. Gives me the same error as before your changes.

@tiberiuzuld
Copy link

tiberiuzuld commented Nov 15, 2021

Can confirm that this changes do not work

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '../dist/cli/cli'
Require stack:
- <path>/node_modules/@biesbjerg/ngx-translate-extract/bin/cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)

Angular 13 NodeJS 16

@venraij
Copy link

venraij commented Nov 15, 2021

@bartholomej i've opened a pull request in your repo to merge my changes into your branch. The new changes fix the issues.

@tiberiuzuld
Copy link

tiberiuzuld commented Nov 15, 2021

@venraij
Still same issue as above.
Installed your version with "@biesbjerg/ngx-translate-extract": "github:bartholomej/ngx-translate-extract#ng13"
Or it requires a build ... ? Will try that now.

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '../dist/cli/cli.js'
Require stack:
-<path>\node_modules\@biesbjerg\ngx-translate-extract\bin\cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)

@tiberiuzuld
Copy link

Ok made the build and copied the dist folder but still not working

node:internal/modules/cjs/loader:979
    throw new ERR_REQUIRE_ESM(filename, true);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module <path>\node_modules\@angular\compile
r\fesm2015\compiler.mjs not supported.
Instead change the require of <path>\node_modules\@angular\compiler\fesm2015\compile
r.mjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (<path>\node_modules\@biesbjerg\ngx-translate-extract\dist
\parsers\pipe.parser.js:3:20)
    at Object.<anonymous> <path>\node_modules\@biesbjerg\ngx-translate-extract\dist
\cli\cli.js:25:23)
    at Object.<anonymous> (<path>\node_modules\@biesbjerg\ngx-translate-extract\bin\
cli.js:3:1) {
  code: 'ERR_REQUIRE_ESM'
}

@venraij
Copy link

venraij commented Nov 15, 2021

@tiberiuzuld give this a whirl https://www.npmjs.com/package/@misternick/ngx-translate-extract it is the packaged version. It uses the code in this branche of my repo https://github.com/venraij/ngx-translate-extract/tree/ng13

@tiberiuzuld
Copy link

Still the same issue

node:internal/modules/cjs/loader:979
    throw new ERR_REQUIRE_ESM(filename, true);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module <path>\node_modules\@angular\compile
r\fesm2015\compiler.mjs not supported.
Instead change the require of <path>\node_modules\@angular\compiler\fesm2015\compile
r.mjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (<path>\node_modules\@misternick\ngx-translate-extract\dis
t\parsers\pipe.parser.js:3:20)
    at Object.<anonymous> (<path>\node_modules\@misternick\ngx-translate-extract\dis
t\cli\cli.js:25:23)
    at Object.<anonymous> (<path>\node_modules\@misternick\ngx-translate-extract\bin
\cli.js:3:1) {
  code: 'ERR_REQUIRE_ESM'
}

Maybe the issue in my case is the fact that I use npm i --legacy-peer-deps so I still download the @angular/compiler@13 which does not like to be loaded with require commonJS style.

@tiberiuzuld
Copy link

So after some more trial and error arrived at this changes:
https://github.com/biesbjerg/ngx-translate-extract/compare/master...tiberiuzuld:ng13?expand=1

My initial issue starts from the change in angular packages which now default to ESM so all the libraries depending on angular need to make the switch angular/angular#43833

My changes still don't work completely at least for my project.
It starts extracting and some point I get the error:

An error occurred: TypeError: Cannot read properties of null (reading 'name')

Issues that I encountered along the way that required changes in the code changes linked above:

  1. ESM imports in NodeJS need to specify .js extension to make them work.
    Typescript will emit .js extension only in 4.5 but currently we are stuck to 4.4 so we need to set the extension.

  2. Yargs 16 was not working, upgraded to 17, removed @types/yargs and yargs().option
    imported yargs is a constructor now, needs to be instantiated.

  3. Commonjs packages not working with named exports:

    // before
    import * as glob from 'glob';
    glob.sync(..)
    // after
    import pkg from 'glob';
    const { sync } = pkg;
    sync(..)
    
    //before
    import { flatten } from 'flat';
    // after
    import pkg from 'flat';
    const { flatten } = pkg;
    
    //before
    import pkg from 'gettext-parser';
    // after
    const { po } = pkg;
    
    // after
    import pkg from 'typescript';
    const { SyntaxKind, isStringLiteralLike, isArrayLiteralExpression, isBinaryExpression, isConditionalExpression } = pkg;
  4. angular/compiler MethodCall not present anymore, changed to Call

  5. __dirname not available anymore in ESM changes:

// before
.version(require(__dirname + '/../../package.json').version)
// after
.version(process.env.npm_package_version)

The changes are complex to upgrade to ng13 and the last error I hit when running the changes in my project doesn't offer any clues where to take it from there.
In case someone wants to pick up the work

@tiberiuzuld
Copy link

Update:
Was able to fix all issues and to be able to run the tests under ESM except for node 12

The issues:
An error occurred: TypeError: Cannot read properties of null (reading 'exp')
and
An error occurred: TypeError: Cannot read properties of null (reading 'name')

Where from:

	protected expressionIsOrHasBindingPipe(exp: any): exp is BindingPipe {
		if (exp && exp.name && exp.name === TRANSLATE_PIPE_NAME) {
			return true;
		}
		if (exp && exp.exp && exp.exp instanceof BindingPipe) {
			return this.expressionIsOrHasBindingPipe(exp?.exp);
		}
		return false;
	}

Seems exp can be null now.

Made a new PR with my changes which work with ng13: #248

Also made a branch on my fork with the dist folder for who wants to use it directly in package.json :
https://github.com/tiberiuzuld/ngx-translate-extract/tree/ng13-dist

"@biesbjerg/ngx-translate-extract": "github:tiberiuzuld/ngx-translate-extract#ng13-dist",

@venraij
Copy link

venraij commented Nov 15, 2021

@tiberiuzuld I've made some further changes which fixed the issue even via npm for me. Tried it in 2 different projects of ng13. https://github.com/venraij/ngx-translate-extract/tree/angular-13

Unfortunately it breaks compatibility with lower angular versions because of the removal of MethodCall

@adnanomerovic
Copy link

I have the same problem as @enchorb

@enchorb
Copy link

enchorb commented Jan 8, 2022

@adnanomerovic Temporary solution using patch-package and this fix:

diff --git a/node_modules/@bartholomej/ngx-translate-extract/dist/cli/tasks/extract.task.js b/node_modules/@bartholomej/ngx-translate-extract/dist/cli/tasks/extract.task.js
index 23f06c6..d408e97 100644
--- a/node_modules/@bartholomej/ngx-translate-extract/dist/cli/tasks/extract.task.js
+++ b/node_modules/@bartholomej/ngx-translate-extract/dist/cli/tasks/extract.task.js
@@ -103,7 +103,7 @@ export class ExtractTask {
     save(output, collection) {
         const dir = path.dirname(output);
         if (!fs.existsSync(dir)) {
-            mkdirp.sync(dir);
+            fs.mkdirSync(dir, { recursive: true });
         }
         fs.writeFileSync(output, this.compiler.compile(collection));
     }

@tarlepp
Copy link

tarlepp commented Jan 9, 2022

@biesbjerg could we get some feedback from you?

@zeljkoantich
Copy link

It works on:

Angular v13.0.2
node v16.13.1
npm v8.1.2

@pongells
Copy link

It works on:

Angular v13.0.2
node v16.13.1
npm v8.1.2

does not work for me

@bartholomej
Copy link
Author

@enchorb @adnanomerovic @orestisioakeimidis You're right.
I was able to replicate this and it was a case where the output folder did not exist before the extraction began.

This should be fixed in version v8.0.2

npm i @bartholomej/ngx-translate-extract --save-dev

Let me know how it works.

@bartholomej
Copy link
Author

It works on:

Angular v13.0.2
node v16.13.1
npm v8.1.2

does not work for me

@pongells Can you be a little more specific, please? What doesn't work? Any error messages? Are you using latest version of ngx-translate-extract v8.0.2?

@adnanomerovic
Copy link

@enchorb @adnanomerovic @orestisioakeimidis You're right. I was able to replicate this and it was a case where the output folder did not exist before the extraction began.

This should be fixed in version v8.0.2

npm i @bartholomej/ngx-translate-extract --save-dev

Let me know how it works.

It's works! :D

@pongells
Copy link

It works on:

Angular v13.0.2
node v16.13.1
npm v8.1.2

does not work for me

@pongells Can you be a little more specific, please? What doesn't work? Any error messages? Are you using latest version of ngx-translate-extract v8.0.2?

8.0.2 was not out at the time :) works with latest version, thank you

@zeljkoantich
Copy link

If there are no problems any more, maybe this PR can me merged?

@bartholomej
Copy link
Author

@zeljkoantich Yes, that's what we all need 😺
What do you think @biesbjerg? 🙏

tkohr added a commit to geonetwork/geonetwork-ui that referenced this pull request Feb 24, 2022
… to solve angular 13 compatibility

`npm uninstall @biesbjerg/ngx-translate-extract`
`npm i @bartholomej/ngx-translate-extract`
see:
biesbjerg/ngx-translate-extract#246
biesbjerg/ngx-translate-extract#247
angular/angular#43833 (comment)
tkohr added a commit to geonetwork/geonetwork-ui that referenced this pull request Feb 24, 2022
… to solve angular 13 compatibility

`npm uninstall @biesbjerg/ngx-translate-extract`
`npm i @bartholomej/ngx-translate-extract`
see:
biesbjerg/ngx-translate-extract#246
biesbjerg/ngx-translate-extract#247
angular/angular#43833 (comment)
@sebastien-p
Copy link

Good job @bartholomej!

Come on @biesbjerg can we please have some feedback from you?

@masha-mariya4
Copy link

thanks @bartholomej!
also for me downgrade biesbjerg/ngx-translate-extract to version 5.0.0 helped with this error

@zeljkoantich
Copy link

@bartholomej Maybe you should you release your fork officially, then we would switch to that fork?

Another alternative would be to have you here as a contributor with all rights.
But I'm not sure if any contributor can provide you with those rights, or it has to be the owner.

The lib is used, and there are no official commits in over a year.
#257

@Jace67
Copy link

Jace67 commented Jun 3, 2022

@bartholomej your extractor is working on angular 13 but I am encountering a problem with output path using curly braces.
I am not sure where to write this issue so I am writing it here.

My out file path has this path in the end: "/{en,jp}.json". Running the extractor will create a file "{en,jp}.json" at my output folder. Checking the file, the extraction was a success. So the only problem is that its saving the output in an incorrect filename.

@oxydeon
Copy link

oxydeon commented Jun 3, 2022

@bartholomej your extractor is working on angular 13 but I am encountering a problem with output path using curly braces. I am not sure where to write this issue so I am writing it here.

My out file path has this path in the end: "/{en,jp}.json". Running the extractor will create a file "{en,jp}.json" at my output folder. Checking the file, the extraction was a success. So the only problem is that its saving the output in an incorrect filename.

Are you on Windows?

On Windows I must specify each output destination individually:
ngx-translate-extract --output ./src/assets/i18n/fr.json ./src/assets/i18n/en.json

@vadimwe
Copy link

vadimwe commented Jun 3, 2022

@enchorb @adnanomerovic @orestisioakeimidis You're right. I was able to replicate this and it was a case where the output folder did not exist before the extraction began.

This should be fixed in version v8.0.2

npm i @bartholomej/ngx-translate-extract --save-dev

Let me know how it works.

Works good! Thank you!

@Jace67
Copy link

Jace67 commented Jun 6, 2022

@bartholomej your extractor is working on angular 13 but I am encountering a problem with output path using curly braces. I am not sure where to write this issue so I am writing it here.
My out file path has this path in the end: "/{en,jp}.json". Running the extractor will create a file "{en,jp}.json" at my output folder. Checking the file, the extraction was a success. So the only problem is that its saving the output in an incorrect filename.

Are you on Windows?

On Windows I must specify each output destination individually: ngx-translate-extract --output ./src/assets/i18n/fr.json ./src/assets/i18n/en.json

Is this a new limitation? Yes I am on Windows and your suggestion solves my issue but I didn't encounter this issue while I was using version 7.0.4 on Windows.

@FrEaKmAn
Copy link

For me @bartholomej/ngx-translate-extract works nicely, but is anyone using also @biesbjerg/ngx-translate-extract-marker. Is there an alternative compatible with @bartholomej/ngx-translate-extract? Current it fails with

Error: Cannot find module '@biesbjerg/ngx-translate-extract/dist/cli/tasks/extract.task'

@stevengunneweg
Copy link

I currently use both @bartholomej/[email protected] and @biesbjerg/[email protected] without any issue, however I'm a little behind on some dependencies.

@bartholomej
Copy link
Author

@FrEaKmAn Yes, I am using both @bartholomej/[email protected] and @biesbjerg/[email protected] together with angular 13/14 and it works well.

Although marker is still legacy View Engine library (and probably will forever remain, since no one does updates either) 😿
And there's even pull request for marker IVY compatibility.

@timeimp
Copy link

timeimp commented Mar 8, 2023

Is there any reason for this to be held up any more? Everyone in the comments seems to be agreeing that things work?

Or is this repo considered deprecated and we should move to another offering?

@michaelbromley
Copy link

@timeimp This repo is unmaintained. After consultation with the interested parties, we took on maintainership of a fork. You can read the discussion here: #246 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.