Skip to content

Feature Request: add a way to register dependencies by plugins #877

@sapphi-red

Description

@sapphi-red

Plugins may rely on a different file but there's no way to tell that information to lightningcss.

For example, the plugin below depends on .json placed next to the input file.

export function fooVisitor() {
  /** @type {string[]} */
  let currentStyleSheetSources
  return {
    StyleSheet(stylesheet) {
      currentStyleSheetSources = stylesheet.sources
    },
    Rule: {
      unknown: {
        foo(rule) {
          const from = currentStyleSheetSources[rule.loc.source_index]
          const importedFile = JSON.parse(fs.readFileSync(from + '.json', 'utf8'))
          const cssAst = convertJsonToAst(importedFile) // convert json to lightningcss somehow
          return cssAst
        },
      },
    },
  }
}

To make the re-transform happen when the json file is changed, the user that calls lightningcss.bundle needs to know which css file depends on which json file.

Simply having a convention (e.g. expose dependencies property from the visitor) would work if composeVisitors supports it, but having the information in TransformResult.dependency feels natural.

For reference postcss, achieves this via messages.
https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#31-use-messages-to-specify-dependencies
Maybe it makes sense to have a way to emit warnings from visitors as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions