Repository navigation
Description
🔍 Search Terms
content mapper
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
I would like to be able to opt out of lint-like TypeScript errors for content-mappers. For example, I might want to use ESM syntax in a CJS context, even if verbatimModuleSyntax is allowed. Or I would like to use export =, even is module is set to esnext.
A context where these rules don’t apply, are declaration files.
📃 Motivating Example
Content mapper authors can now return a .d.ts, .d.cts, or .d.mts extension as part of the transform response.
💻 Use Cases
I realized you can sort of categorize content mappers into two categories.
The first category is files with embedded JavaScript/TypeScript content. This includes Astro, Ember, MDX, Svelte, and Vue. This is the typical use case for content mappers. In this case it makes sense for the format to be opiniated about the module format.
The second category is static data that can be imported, but has a known shape. I just published css-modules-content-mapper. This always exports a value whose type matches Record<string, string>, but the keys are known. Other formats in this category are JSON5, TOML, YAML, XML, etc. These formats can always be written using type declarations only.
For this second category, I would really like to not care about the module format used. I want to use export = and tell TypeScript to treat it as a .d.${ext}.cts file.
A workaround is to add an @ts-ignore comment to the export = line, but that seems less ideal.
Activity
remcohaszing commentedon Sep 7, 2026
I just noticed TypeScript does allow CJS syntax in
.ctsfiles ifmoduleis set toesnext. But it doesn’t allow it in content-mapped file which specify the.ctsextension in the transform result. This makes it a content-mapper bug, not a missing feature. I’m guessing.ctsis treated as.ts.For a reproduction, see https://github.com/remcohaszing/css-modules-content-mapper/tree/main/fixtures/example, and change the
moduleoption toesnext.cc Andrew Branch (@andrewbranch)
andrewbranch commentedon Nov 12, 2026
The module format bug was fixed in #65432, but it doesn't let you do whatever you want with module format, because that would violate the law of declaration file substitutability. If you claim to be a
.ctsfile in a Transform response, but you emit a declaration file ofComponent.d.mdx.tsorComponent.d.ts(via anoutputExtensionsmapping) and you're in a"type": "module"context, then the program interprets your file as CJS while compiling, but emits an ESM declaration file. So, whatever file extension you claim to be has to agree with the module format that will be assigned to your declaration file output. (This rule applies even if you don't have declaration emit turned on, because I believe TypeScript shouldn't let you get into a situation where you have to fundamentally restructure your app to enable declaration emit, since it's a prerequisite ofcompositeandincremental.)It's possible to let you return declarations from Transform, but there are a few problems that would have to be solved, and it seems like the bug fix plus the module format agreement restriction removes most of the motivation for this feature request.
remcohaszing commentedon Nov 12, 2026
I’m a bit confused now. I thought claiming to be
.cts, would cause TypeScript to emitComponent.d.mdx.cts(unlessoutputExtensionsis involved). MDX is not a real example, butstyles.d.css.ctsis.Never mind about this. I jumped to conclusions too early when I suggested this.
andrewbranch commentedon Nov 12, 2026
styles.d.css.ctsisn't a recognized extension format. I proposed doing exactly that at an earlier point while investigating this issue, but it causes problems for project references and incremental compilation to not know the declaration filename ofstyles.csswithout spawning the mapper. It also makes module resolution messy to have a one-to-many mapping of source-to-declaration extensions.remcohaszing commentedon Nov 12, 2026
Hmm ok, so then
MappedOutput['extension']essentially can’t be.mjs,.cjs,.mts, or.cts, unless theoutputExtensionsis used, right? (With the exception of supplemental.) If this is the case, I’ll look into changingcss-modules-content-mapperto emit ESM content instead of CJS.andrewbranch commentedon Nov 12, 2026
It can be
.mtsor.mjsif you're in"type": "module", and vice versa. You just need detected format agreement on the inputs and outputs; whether the format decision comes from the extension or package.json doesn't matter.remcohaszing commentedon Nov 13, 2026
The user can configure the following:
extensions.outputExtensions. This requires the user to specifyrewriteRelativeImportExtensions.The content mapper can specify the following in
package.json:outputExtensions, even if the content mapper can’t know the extensions that are specified by the user. This requires the user to specifyrewriteRelativeImportExtensions.In the transform RPC:
verbatimModuleSyntaxand TypeScript interprets the file as CJS.package.json.I see two additions that would help with this problem:
TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'from virtual code produced by content mappers. Socss-modules-content-mappercan writeexport default styleseven if the file is CJS and the project usesverbatimModuleSyntax. It looks like this is already happening for unused locals and parameters.transformparams.