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

A way to ignore prefix/suffix in module ID, then resolve the remaining ID as usual #37319

Open
5 tasks done
jakearchibald opened this issue Mar 10, 2020 · 2 comments
Open
5 tasks done
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@jakearchibald
Copy link

Search Terms

wildcard module declarations ignore prefix suffix relative path

Suggestion

import url, { other, exported, values } from 'css:./whatever.css';

TL;DR: I'm looking for a way to say "for all modules starting css:, ignore the css: bit, and resolve the remaining identifier as normal.

As identified by wildcard module declarations, some build tools use prefixes/suffixes to influence how a file is imported.

You can provide types for these:

declare module "css:*" {
  const value: string;
  export default value;
  export const other: string;
  export const exported: string;
  export const values: string;
}

The above allows developers to say "all modules starting css: look like this…".

However, what if the exports from each CSS files differs? This is true for CSS modules, where the class names in a CSS file are 'exported'.

In these cases it's typical to generate a .css.d.ts file alongside your CSS that defines the types. However, if you're using a prefix like css:, TypeScript can't find the location of the .css.d.ts file.

I'm looking for a way to say "for all modules starting css:, ignore the css: bit, and resolve the remaining identifier as normal.

Use Cases

  • Build tools that use prefixes/suffixes to indicate how to process files.
  • Files that may export different things per file (like CSS modules)

Examples

It isn't clear to me if this should be done in tsconfig or in a type definition file.

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh
Copy link
Member

What module loaders support this? We already support ! as an "ignore things after this" character

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 11, 2020
@jakearchibald
Copy link
Author

What module loaders support this?

Parcel, but you can also do it easily in Rollup too.

We already support ! as an "ignore things after this" character

Interesting! I wasn't aware of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants