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

[BUG] cds-typer does not generate ES module syntax with targetModuleType set to auto #432

Closed
1 task done
AnsgarLichter opened this issue Dec 9, 2024 · 2 comments · Fixed by #435
Closed
1 task done
Labels
bug Something isn't working new

Comments

@AnsgarLichter
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Nature of Your Project

JavaScript

Current Behavior

After initializing my project with the esm, the JS files of cds typer still use CJS syntax, though the package.json contains type: module:

// This is an automatically generated file. Please do not change its contents manually!
const cds = require('@sap/cds')
const csn = cds.entities('AdminService')
// service
const AdminService = { name: 'AdminService' }
module.exports = AdminService
module.exports.AdminService = AdminService
// Books
module.exports.Book = { is_singular: true, __proto__: csn.Books }
module.exports.Books = csn.Books
// Authors
module.exports.Author = { is_singular: true, __proto__: csn.Authors }
module.exports.Authors = csn.Authors
// Languages
module.exports.Language = { is_singular: true, __proto__: csn.Languages }
module.exports.Languages = csn.Languages
// Genres
module.exports.Genre = { is_singular: true, __proto__: csn.Genres }
module.exports.Genres = csn.Genres
// Currencies
module.exports.Currency = { is_singular: true, __proto__: csn.Currencies }
module.exports.Currencies = csn.Currencies
// events
// actions
// enums

This is my package.json:

{
  "name": "override",
  "version": "1.0.0",
  "description": "A simple CAP project.",
  "repository": "<Add your repository here>",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "@sap/cds": "^8",
    "express": "^4"
  },
  "devDependencies": {
    "@cap-js/sqlite": "^1",
    "@cap-js/cds-types": "^0.8.0",
    "@cap-js/cds-typer": ">=0.1",
    "@types/node": "^22.0.0",
    "typescript": "^5"
  },
  "scripts": {
    "start": "cds-serve",
    "watch": "cds-tsx w"
  },
  "type": "module",
  "sapux": [
    "app/admin-books",
    "app/browse"
  ],
  "imports": {
    "#cds-models/*": "./@cds-models/*/index.js"
  }
}

Once I add the following section to package.json, ES modules are used successfully:

"cds": {
    "typer": {
      "target_module_type": "esm"
    }
  }

Expected Behavior

When using the default setting auto for the targetModuleType, I expect es modules to be generated by cds-typer.

Steps To Reproduce

  1. cds init esm-test --add esm,typer,sample
  2. cd esm-test
  3. npm i
  4. Execute a change in your data model and check the resulting JS files in @cds-models

Environment

- **OS**: macOS 15
- **Node**: 22.7.0
- **npm**: 10.9.0
- **cds-typer**: 0.30.0 
- **cds**: 8.5.1

Repository Containing a Minimal Reproducible Example

No response

Anything else?

I read the documentation and checked the PR #398 for further guidance but I didn't find anything that was helpful to me.
Thank you in advance for your support!

@AnsgarLichter AnsgarLichter added bug Something isn't working new labels Dec 9, 2024
@daogrady
Copy link
Contributor

Hi Ansgar,

good catch! I have prepared a fix.

Best,
Daniel

@AnsgarLichter
Copy link
Author

Hi Daniel,

thanks for the quick response!
I tested your branch locally and I solves my issue.

Best,
Ansgar

daogrady added a commit that referenced this issue Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants