Closed Bug 1804969 Opened 2 years ago Closed 2 years ago

Create a webpack plugin to re-write chrome JS URLs for Storybook

Categories

(Firefox :: General, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
110 Branch
Tracking Status
firefox110 --- fixed

People

(Reporter: hjones, Assigned: mstriemer)

References

(Blocks 1 open bug)

Details

(Whiteboard: [recomp])

Attachments

(1 file)

Storybook can't handle chrome: urls, and as a result we've had to add code like this to our newer components:

// Use a relative URL in storybook to get faster reloads on style changes.
static stylesheetUrl = window.IS_STORYBOOK
    ? "./moz-toggle/moz-toggle.css"
    : "chrome://global/content/elements/moz-toggle.css";

...

<link rel="stylesheet" href=${this.constructor.stylesheetUrl} />

This works well, but it would be nice to handle the CSS URL logic in a single place so we can remove this Storybook specific code from our different components.

One way to do this is to write a custom Webpack loader and use it in .storybook/main.js. I got the following code that rewrites chrome URLs to a relative path working as a POC:

// rowser/components/storybook/.storybook/replace-chrome-css.js

// eslint-disable-next-line
module.exports = function(source) {
  // My regex skills are pretty much non-existent, so there's almost 
  // certainly a better way to do all of this.
  const chromeCSSRegex = /chrome.+\.css/g;
  return source.replace(chromeCSSRegex, function(match) {
    const start = match.lastIndexOf("/") + 1;
    const end = match.lastIndexOf(".");
    const fileName = match.slice(start, end);
    // Hack to avoid replacing the path for in-content/common.css for now
    if (fileName === "common") {
      return match;
    }
    return `./${fileName}/${fileName}.css`;
  });
};
// browser/components/storybook/.storybook/main.js
...
config.module.rules.push({
    test: /\.mjs$/,
    loader: path.resolve(__dirname, "./replace-chrome-css.js"),
});
...

We would want to tweak this a bit, maybe adding something like the following so that we can specify what path to use in place of the chrome URL:

const rewrites = {
  "chrome://browser/skin/migration/migration-wizard.css": "./migration/migration-wizard.css",
};
Summary: Create a webpack plugin to re-write chrome CSS URls for Storybook → Create a webpack plugin to re-write chrome CSS URLs for Storybook

The Bugbug bot thinks this bug should belong to the 'Core::CSS Parsing and Computation' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: General → CSS Parsing and Computation
Product: Firefox → Core
Component: CSS Parsing and Computation → XUL Widgets
Product: Core → Toolkit
Severity: -- → S3
Component: XUL Widgets → General
Priority: -- → P3
Product: Toolkit → Firefox

This patch will rewrite all chrome:// URLs in .mjs files, but it isn't
emitting proper URLs for assets. This means that JS imports will map
correctly, but any img/css references won't have a valid path outside of
local development and CSS files that use @import will not resolve imports
correctly.

To reference images and CSS files you will still need to ensure those files
are in the Storybook static path and use a separate URL to reference them
in the window.IS_STORYBOOK case.

Assignee: nobody → mstriemer
Status: NEW → ASSIGNED
Pushed by mstriemer@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/9fa0c0b8ac49 Rewrite chrome:// JS imports in Storybook r=mconley,hjones
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 110 Branch
Summary: Create a webpack plugin to re-write chrome CSS URLs for Storybook → Create a webpack plugin to re-write chrome JS URLs for Storybook
Whiteboard: [fidefe-reusable-components-ms1] → [recomp]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: