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

Rewrite composes rules inside @imported stylesheets #389

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion index.js
Expand Up @@ -160,7 +160,7 @@ function parseStyles(result, styles, options, state, media) {
if (stmt.children) {
stmt.children.forEach((child, index) => {
if (child.type === "import") imports.push(child)
else bundle.push(child)
else bundle.push(rewriteComposes(child, path.dirname(stmt.uri)))
// For better output
if (index === 0) child.parent = stmt
})
Expand All @@ -174,6 +174,21 @@ function parseStyles(result, styles, options, state, media) {
})
}

function rewriteComposes(stmt, dirname) {
if (stmt.nodes) {
stmt.nodes.forEach(child => {
rewriteComposes(child, dirname)
})
}
if (stmt.type === "decl" && stmt.prop === "composes") {
stmt.value = stmt.value.replace(
/(from\s*["'])(\.\.?\/[^'"]*)/,
(m, m1, m2) => m1 + path.normalize(dirname+'/'+m2)
)
}
return stmt
}

function resolveImportId(result, stmt, options, state) {
const atRule = stmt.node
let sourceFile
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/composes-second.css
@@ -0,0 +1 @@
.a {}
1 change: 1 addition & 0 deletions test/fixtures/imports/composes-first.css
@@ -0,0 +1 @@
.a { composes: a from '../composes-second.css'; }
15 changes: 14 additions & 1 deletion test/import.js
@@ -1,5 +1,5 @@
// builtin tooling
import { readFileSync } from "fs"
import { existsSync, readFileSync } from "fs"
import path from "path"

// external tooling
Expand Down Expand Up @@ -98,3 +98,16 @@ test("should work with no styles without throwing an error", t => {
t.is(result.warnings().length, 0)
})
})

test("should rewrite relative paths in `composes` inside @imported stylesheet", t => {
return postcss()
.use(atImport())
.process("@import 'test/fixtures/imports/composes-first.css';", {
from: undefined,
})
.then(result => {
const path = (/from '(.*)'/.exec(result.css) || [])[1]
t.is(existsSync(path), true)
t.is(result.warnings().length, 0)
})
})