Skip to content

Ensure content globs defined in @config files are relative to that file#14314

Merged
philipp-spiess merged 7 commits intonextfrom
fix/ensure-globs-in-config-are-relative-to-that-file
Sep 3, 2024
Merged

Ensure content globs defined in @config files are relative to that file#14314
philipp-spiess merged 7 commits intonextfrom
fix/ensure-globs-in-config-are-relative-to-that-file

Conversation

@philipp-spiess
Copy link
Contributor

When you configure custom content globs inside an @config file, we want to tread these globs as being relative to that config file and not the CSS file that requires the content file. A config can be used by multiple CSS configs.

base: inputBasePath, // Globs are relative to the input.css file
sources: compiler.globs.map(({ base, pattern }) => ({
// Globs are relative to the input.css file
base: base ? path.dirname(path.resolve(inputBasePath, base)) : inputBasePath,
Copy link
Contributor Author

@philipp-spiess philipp-spiess Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if we shouldn't call this something else? We can't resolve the base inside tailwindcss core (if we do, we need to make it aware of paths etc. which feels wrong if it doesn't do IO?) so instead, we have to translate the paths that are relative to the input CSS file here to the proper base dirname.

My proposal is to call it origin instead of base. That would also make it clearer why origin can be null (i.e it's origin is the input CSS file)

I went with base for consistency with the other places where we use globs.

Update: I went ahead and called it origin for now. Still curious what you think here, though!

cc @thecrypticace @RobinMalfait @adamwathan

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like origin if it’s a full file name and not the directory, is that the case? The full file path feels more correct to me with that name (“this is the file this source came from”) if it doesn’t introduce any other problems.

Copy link
Contributor Author

@philipp-spiess philipp-spiess Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like origin if it’s a full file name and not the directory, is that the case?

Yeah that's the case, it's basically just keeping track of whatever string is after @plugin or @config right now, it's only transformed if it's relative inside @imports (using the postcss plugin) but core doesn't do any other manipulation. So it's a file path that is relative to the input base path

@philipp-spiess philipp-spiess force-pushed the fix/ensure-globs-in-config-are-relative-to-that-file branch from 9d1bc73 to 7065edf Compare September 3, 2024 09:24
@thecrypticace
Copy link
Contributor

I would almost rather pass filename / filepath or something as an option and keep the name base. It feels weird to me that there's a disconnect in the naming for this one API.

'project-a/src/index.css': css`
@import 'tailwindcss/utilities';
@source '../../project-b/src/**/*.js';
@config '../tailwind.config.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any value in testing @source and @config here instead of just switching to @config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. I was initially thinking no but since we do return origin of undefined for globs defined inside the css file, it does behave differently (and that is implemented in each client right now). I wanted to avoid having to split all tests again to support that though, perhaps we can at least include both cases in the same test (so it doesn't mean we double all our basic integration tests right away).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added back the @source tests

sources: compiler.globs.map((pattern) => ({
base: inputBasePath, // Globs are relative to the input.css file
sources: compiler.globs.map(({ origin, pattern }) => ({
// Globs are relative to the input.css file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this comment was already there but I wonder if it's misleading to say input.css here since the file could be named anything? And origin comes from a config file, not a CSS file.

Maybe these should be more like:

Globs are relative to the CSS file or config file where they are specified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it there because the origin is also relative to the input CSS file right now, but yeah this is confusing. Maybe:

Globs need to be relative to the CSS file or config file where they are specified. An eventual origin is relative to the CSS file.

But this is really confusing now 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Ensure the glob is relative to the input CSS file or the config file where they are specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it to the latest one

@philipp-spiess
Copy link
Contributor Author

@thecrypticace

I would almost rather pass filename / filepath or something as an option and keep the name base.

Do you have an example of what you mean? I think base is confusing too because it refers to the folder name of which a pattern is relative to in Oxide but it would mean the javascript file relative to the input css that defined the glob in this case, so striving for naming consistency could be more confusing than we want it to.

@thecrypticace
Copy link
Contributor

@thecrypticace

I would almost rather pass filename / filepath or something as an option and keep the name base.

Do you have an example of what you mean? I think base is confusing too because it refers to the folder name of which a pattern is relative to in Oxide but it would mean the javascript file relative to the input css that defined the glob in this case, so striving for naming consistency could be more confusing than we want it to.

Right and we don't want to call path.dirname in core. yeah okay I'm not gonna fight to hard for this — just thinking out loud. origin is fine.

@philipp-spiess philipp-spiess merged commit a1d56d8 into next Sep 3, 2024
@philipp-spiess philipp-spiess deleted the fix/ensure-globs-in-config-are-relative-to-that-file branch September 3, 2024 14:54
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.

3 participants