Skip to content

Commit

Permalink
Migrated to faster PostCSS v8 API - thanks @ai
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Nov 18, 2020
1 parent 63a5b44 commit 508ea75
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/core/declProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ function buildResult(newUrl, matched, before, after) {
return `${before}${newUrl}${after}`
}

// Tracks visited declarations
const processTracker = new Set()

export const declProcessor = (from, to, options, result, decl) => {
if (processTracker.has(decl)) {
return
}

const dir = { from, to, file: getDirDeclFile(decl) }
const pattern = /(url\(\s*["']?)([^"')]+)(["']?\s*\))/g

Expand Down Expand Up @@ -120,6 +127,7 @@ export const declProcessor = (from, to, options, result, decl) => {
return null
})
).then((values) => {
processTracker.add(decl)
decl.value = decl.value.replace(pattern, (match) => {
const replacement = values.shift()
return replacement == null ? match : replacement
Expand Down
26 changes: 10 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import path from "path"

import { Once } from "postcss"
import { Declaration } from "postcss"

import { declProcessor } from "./core/declProcessor"

export default (options = {}) => ({
postcssPlugin: "postcss-dark-theme-class",
Once(root, { result }) {
const opts = result.opts
const from = opts.from ? path.dirname(opts.from) : "."
const to = opts.to ? path.dirname(opts.to) : from
postcssPlugin: "postcss-smart-asset",
Declaration(decl, { result }) {
const opts = result.opts
const from = opts.from ? path.dirname(opts.from) : "."
const to = opts.to ? path.dirname(opts.to) : from

const promises = []
return declProcessor(from, to, options, result, decl)
}
})

root.walkDecls((decl) => {
promises.push(declProcessor(from, to, options, result, decl))
})

return Promise.all(promises)
}
})

// PostCSS v8
// PostCSS v8 marker
export const postcss = true
5 changes: 2 additions & 3 deletions src/mode/rebase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { compareFixtures, processedCss } from "../../test/setup"
describe("rebase", () => {
const opts = {}

test("rebase with empty options", () => {
processedCss("copy-hash", undefined, { from: "test/fixtures/here" })
})
test("rebase with empty options", () =>
processedCss("copy-hash", undefined, { from: "test/fixtures/here" }))

compareFixtures("cant-rebase", "shouldn't rebase url if not info available")
compareFixtures("rebase-to-from", "should rebase url to dirname(from)", opts, {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"include": ["src"],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 508ea75

Please sign in to comment.