Skip to content
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

Add transform and extract APIs #4469

Merged
merged 2 commits into from May 26, 2021
Merged

Add transform and extract APIs #4469

merged 2 commits into from May 26, 2021

Conversation

bradlc
Copy link
Contributor

@bradlc bradlc commented May 26, 2021

This PR adds purge.transform and purge.extract configuration options.

purge.extract

This is an alternative way to define class extractors:

purge: {
  extract: (content) => { /* ... */ },
}

// same as:
purge: {
  options: {
    defaultExtractor: (content) => { /* ... */ },
  },
}
purge: {
  extract: {
    DEFAULT: (content) => { /* ... */ },
    html: (content) => { /* ... */ },
  },
}

// same as:
purge: {
  options: {
    defaultExtractor: (content) => { /* ... */ },
    extractors: [
      {
        extensions: ['html'],
        extractor: (content) => { /* ... */ },
      },
    ],
  },
}

purge.transform

purge.transform is a new option that allows you to specify transformation functions that run on file contents before it is passed to the class extractor. One use-case for this is converting markdown to HTML:

// One transformer for every file
purge: {
  transform: (content) => { /* ... */ },
}

// Transformer for .md files, and a (optional) default transformer for all other file types
purge: {
  transform: {
    DEFAULT: (content) => { /* ... */ },
    md: markdownToHtml,
  },
}

Notes

  • The Svelte transform (content.replace(/(?:^|\s)class:/g, ' ')) is now implemented as a transformer.
  • purge.options.defaultExtractor and purge.options.extractors still work and override any extractors defined using purge.extract. One thing that I noticed about this is that when using a custom defaultExtractor the purge.preserveHtmlElements option is still respected, but this isn't the case with custom extractors defined in purge.extractors. This behaviour has been maintained but I'm wondering if this was intentional?

@adamwathan adamwathan merged commit 7fc4690 into refactorstuff May 26, 2021
@adamwathan adamwathan deleted the transformers branch May 26, 2021 13:04
@adamwathan
Copy link
Member

Thanks man! I think the preserveHtmlElements thing is intentional (I vaguely remember an issue about it). We can reconsider how that should work for v3 though if it feels weird.

adamwathan pushed a commit that referenced this pull request May 26, 2021
* add `transform` and `extract` APIs

* make svelte transform part of the transformer stuff
adamwathan pushed a commit that referenced this pull request May 26, 2021
* add `transform` and `extract` APIs

* make svelte transform part of the transformer stuff
adamwathan pushed a commit that referenced this pull request May 26, 2021
* add `transform` and `extract` APIs

* make svelte transform part of the transformer stuff
adamwathan pushed a commit that referenced this pull request May 26, 2021
* add `transform` and `extract` APIs

* make svelte transform part of the transformer stuff
adamwathan pushed a commit that referenced this pull request May 26, 2021
* add `transform` and `extract` APIs

* make svelte transform part of the transformer stuff
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.

None yet

2 participants