Page MenuHomePhabricator

Enable use of Codex Less mixins inside of MediaWiki
Closed, ResolvedPublic5 Estimated Story Points

Description

Codex ships the Link Component as a Less mixin as opposed to a Vue component. In the future other components may be implemented in this way as well.

Unfortunately, this mixin cannot currently be used inside other Less files that are processed using MediaWiki's ResourceLoader feature. This is because the link.less file beings with the following @import rule:

@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';

This import rule assumes that the Codex Design Tokens package has been installed using Node.js; ResourceLoader is not aware of these paths and doesn't know where to find the token files.

A similar problem affects the css-icon.less file: it imports from @wikimedia/codex-design-tokens/..., and it also imports @wikimedia/codex-icons/dist/codex-icon-paths.less, which ResourceLoader also won't be able to find.

Additionally, Less files in MW need to be able to import the mixin file itself. Our options for this are:

  1. Use a direct file path. This would look ugly: in extensions it'd look something like @import '../../../../../resources/lib/codex-design-tokens/mixins/link.less';
  2. Add resources/lib/codex-design-tokens/mixins to the list of default import paths, so that @import 'link.less'; works. However, this makes it unclear that it's being imported from Codex, and the names are so generic that they might be shadowed by files in the same directory that happen to have the same name.
  3. Make @import ( reference ) '@wikimedia/codex/mixins/link.less'; work. This has the benefit that the Codex docs would be accurate for MW use too.

I think we should do #3.

Solution

The fact that the public mixin files import the tokens is a problem for a different reason too: consumers of these mixins should be able to choose which theme to use, but the import statement in these mixin files hard-codes a theme name, forcing that theme to be used even if the consumer chose a different one (and overriding the consumer's theme choice for everything that comes below its import of the mixin file!). We can solve both this problem and the "MediaWiki doesn't know how to import from @wikimedia/codex-design-tokens" problem by removing these token imports from the public mixin files, and instead making the consumer responsible for importing the tokens.

Example usage in MediaWiki:

// Import the Codex tokens from the correct theme for the current skin, once T325237 is done.
@import ( reference ) 'mediawiki.skin.variables.less'; 
@import ( reference ) '@wikimedia/codex/mixins/link.less';

Example usage outside of MediaWiki:

// Import the Codex tokens from whichever theme the caller wants.
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '@wikimedia/codex/mixins/link.less';

To make the import path of the mixins themselves work, we should alias @wikimedia/codex/ to resources/lib/codex. Less.php doesn't support aliases out of the box, but it's relatively straightforward to implement this by passing in a function to SetImportDirs. Using the same technique, we should alias @wikimedia/codex-icons/ to resources/lib/codex-icons, so that the import from the icons package in css-icon.less will work.

We can also use the import callback function to intercept imports from @wikimedia/codex-design-tokens/. These imports already won't work and will throw an error, but it would be nice if the error message could be something like "you can't import from @wikimedia/codex-design-tokens, import mediawiki.skin.variables.less if you want the tokens" as opposed to a generic and more confusing error message like "file not found".


Acceptance criteria

After the next Codex release:

Event Timeline

egardner triaged this task as Medium priority.Feb 1 2023, 7:55 PM
Restricted Application raised the priority of this task from Medium to High. · View Herald TranscriptFeb 2 2023, 12:57 AM

I don't like the idea of having a MediaWiki-specific import in Codex code, even if it's an optional one.

However, now that I think about it, it's kind of weird and possibly unhelpful that we're even importing the tokens in these mixin files at all. Doing that also prevents non-MW consumers from selecting a different theme: link.less imports the wikimedia-ui theme even if you don't want that theme, and that overrides your own theme selection for the link mixin and all of your styles that come after it.

Instead, maybe we could make importing the tokens the responsibility of the caller? That would both allow non-MW consumers to select a different theme if they wish, and allow MW to use a different path for its token import. For example, using the link mixin in a standard (non-MW) environment would look like this:

@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less'; /* This line is new */
@import ( reference ) '@wikimedia/codex/mixins/link.less';

.my-link {
	.cdx-mixin-link();
}

And in MediaWiki it would look like this:

@import ( reference ) 'mediawiki.skin.variables'; /* This line is different */
@import ( reference ) '@wikimedia/codex/mixins/link.less';

a {
	.cdx-mixin-link();
}

The other issue is that the import path @wikimedia/codex/mixins/link.less won't work in MW. We could tell people to instead import from a different path, but I think it'd be more promising to instead make the standard (NPM-style) import paths work where possible. See T325237#8583392 or an idea for how we could do that.

egardner set the point value for this task to 3.Feb 7 2023, 9:46 PM

Change 890145 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] [WIP] ResourceLoader: Add path remapping for Less imports

https://gerrit.wikimedia.org/r/890145

Change 890146 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] [WIP] ResourceLoader: Descriptive error for Less imports from codex-design-tokens

https://gerrit.wikimedia.org/r/890146

Change 890147 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] [WIP] mixins: Remove tokens import

https://gerrit.wikimedia.org/r/890147

Change 894132 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] [WIP] foreign-resources: Also fetch Less mixin files from Codex

https://gerrit.wikimedia.org/r/894132

Change 896197 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] css-icon: Make Less code compatible with MediaWiki's older Less compiler

https://gerrit.wikimedia.org/r/896197

Change 896201 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] mixins: Add a file that imports all mixins

https://gerrit.wikimedia.org/r/896201

Change 896202 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] mediawiki.skin.defaults.less: Import all Codex mixins

https://gerrit.wikimedia.org/r/896202

Change 896197 merged by jenkins-bot:

[design/codex@main] css-icon: Make Less code compatible with MediaWiki's older Less compiler

https://gerrit.wikimedia.org/r/896197

Change 896420 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] mediawiki.less: Add wrapper file to make codex-icon-paths import work

https://gerrit.wikimedia.org/r/896420

Change 890147 merged by jenkins-bot:

[design/codex@main] mixins: Remove tokens import

https://gerrit.wikimedia.org/r/890147

Change 898895 had a related patch set uploaded (by Eric Gardner; author: Eric Gardner):

[mediawiki/core@master] Update Codex from v0.6.2 to v0.7.0

https://gerrit.wikimedia.org/r/898895

Change 896201 merged by jenkins-bot:

[design/codex@main] mixins: Add a file that imports all mixins

https://gerrit.wikimedia.org/r/896201

Volker_E renamed this task from Enable use of Codex LESS mixins inside of MediaWiki to Enable use of Codex Less mixins inside of MediaWiki.Mar 16 2023, 12:23 PM
Volker_E updated the task description. (Show Details)
Volker_E updated the task description. (Show Details)

Change 894132 merged by jenkins-bot:

[mediawiki/core@master] foreign-resources: Also fetch Less mixin files from Codex

https://gerrit.wikimedia.org/r/894132

Change 896420 merged by jenkins-bot:

[mediawiki/core@master] mediawiki.less: Add wrapper file to make codex-icon-paths import work

https://gerrit.wikimedia.org/r/896420

ldelench_wmf changed the point value for this task from 3 to 5.Mar 27 2023, 4:27 PM

Change 903343 had a related patch set uploaded (by Catrope; author: Catrope):

[design/codex@main] build: Remove 'dist/' from import path for codex-icon-paths.less

https://gerrit.wikimedia.org/r/903343

Change 903343 merged by jenkins-bot:

[design/codex@main] build: Remove 'dist/' from import path for codex-icon-paths.less

https://gerrit.wikimedia.org/r/903343

Change 903778 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] Update Codex from v0.7.0 to v0.8.0

https://gerrit.wikimedia.org/r/903778

Change 903778 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v0.7.0 to v0.8.0

https://gerrit.wikimedia.org/r/903778

Change 890145 merged by jenkins-bot:

[mediawiki/core@master] ResourceLoader: Add path remapping for Less imports

https://gerrit.wikimedia.org/r/890145

Change 896202 merged by jenkins-bot:

[mediawiki/core@master] mediawiki.skin.defaults.less: Import all Codex mixins

https://gerrit.wikimedia.org/r/896202

Change 904633 had a related patch set uploaded (by Jdlrobson; author: Jdlrobson):

[mediawiki/extensions/MobileFrontend@master] Fix storybook integration

https://gerrit.wikimedia.org/r/904633

Change 904633 merged by jenkins-bot:

[mediawiki/extensions/MobileFrontend@master] Fix storybook integration

https://gerrit.wikimedia.org/r/904633

AnneT updated the task description. (Show Details)

Change 907558 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/core@master] ResourceLoader: Avoid new use of MWException

https://gerrit.wikimedia.org/r/907558

Change 890146 merged by jenkins-bot:

[mediawiki/core@master] ResourceLoader: Descriptive error for Less imports from codex-design-tokens

https://gerrit.wikimedia.org/r/890146

Change 907558 merged by jenkins-bot:

[mediawiki/core@master] ResourceLoader: Avoid new use of MWException

https://gerrit.wikimedia.org/r/907558

Catrope updated the task description. (Show Details)

Change #1031889 had a related patch set uploaded (by Reedy; author: Catrope):

[mediawiki/core@REL1_39] foreign-resources: Also fetch Less mixin files from Codex

https://gerrit.wikimedia.org/r/1031889