-
Notifications
You must be signed in to change notification settings - Fork 137
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
[bugfix] Ensured that normalizeFileExt ignores .css.d.ts files #1450
[bugfix] Ensured that normalizeFileExt ignores .css.d.ts files #1450
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this makes sense. Typescript caused a lot of headaches when they picked .d.ts
to mean a different thing than .ts
.
@@ -5,7 +5,14 @@ import minimatch from 'minimatch'; | |||
import type { Plugin } from 'rollup'; | |||
|
|||
function normalizeFileExt(fileName: string) { | |||
return fileName.replace(/\.ts|\.hbs|\.gts|\.gjs$/, '.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please check your use case and see if this implementation also solves it?
return fileName.replace(/(?<!\.d)\.ts|\.hbs|\.gts|\.gjs$/, '.js');
The idea here is "negative lookbehind assertions".
If this works, we can stick with only a single regex replace instead of nesting them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I checked that your solution works in ijlee2/embroider-css-modules#46.
…single regex replace
Background
I created
sample-v2-addon
to investigate how we can introduce CSS modules to v2 addons (while supporting Glint). The key is, for each CSS file*.css
, to create the corresponding declaration file*.css.d.ts
.When the addon's
publicEntrypoints
andappReexports
include the "normal" wildcard patterncomponents/**/*.js
, the resultingdist
will include files that are not supposed to be present:I worked around the problem by providing the pattern
components/**/!(*.css.d).js
, but find the additional complexity in regular expression to be a tech risk (not easy to understand—what is.css.d.js
?—and maintain):Proposed solution
I suspect, the current implementation of
normalizeFileExt
didn't consider file extensions that include.ts
as a substring.By making an early exit, we can consider
*.ts
cases and define a more specific rule that ignores.css.d.ts
.Admittedly, the regular expression
/(^.?|\.[^d]|[^.]d|[^.][^d])\.ts$/
, copied from StackOverflow, is complex and forms another tech risk. Ideally, unit tests fornormalizeFileExt
would be present to document its input and output. (I didn't know in which package such tests can be written.)