-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement
supports
conditions (#548)
* implement supports conditions * apply suggestion from code review * media combine * layer : add dedicated test for duplicate anonymous imports
- Loading branch information
1 parent
3d51fe5
commit c262a30
Showing
43 changed files
with
390 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"use strict" | ||
|
||
const base64EncodedConditionalImport = require("./base64-encoded-import") | ||
|
||
module.exports = function applyConditions(bundle, atRule) { | ||
bundle.forEach(stmt => { | ||
if ( | ||
stmt.type === "charset" || | ||
stmt.type === "warning" || | ||
!stmt.conditions?.length | ||
) { | ||
return | ||
} | ||
|
||
if (stmt.type === "import") { | ||
stmt.node.params = base64EncodedConditionalImport( | ||
stmt.fullUri, | ||
stmt.conditions | ||
) | ||
return | ||
} | ||
|
||
const { nodes } = stmt | ||
const { parent } = nodes[0] | ||
|
||
const atRules = [] | ||
|
||
// Convert conditions to at-rules | ||
for (const condition of stmt.conditions) { | ||
if (typeof condition.media !== "undefined") { | ||
const mediaNode = atRule({ | ||
name: "media", | ||
params: condition.media, | ||
source: parent.source, | ||
}) | ||
|
||
atRules.push(mediaNode) | ||
} | ||
|
||
if (typeof condition.supports !== "undefined") { | ||
const supportsNode = atRule({ | ||
name: "supports", | ||
params: `(${condition.supports})`, | ||
source: parent.source, | ||
}) | ||
|
||
atRules.push(supportsNode) | ||
} | ||
|
||
if (typeof condition.layer !== "undefined") { | ||
const layerNode = atRule({ | ||
name: "layer", | ||
params: condition.layer, | ||
source: parent.source, | ||
}) | ||
|
||
atRules.push(layerNode) | ||
} | ||
} | ||
|
||
// Add nodes to AST | ||
const outerAtRule = atRules.shift() | ||
const innerAtRule = atRules.reduce((previous, next) => { | ||
previous.append(next) | ||
return next | ||
}, outerAtRule) | ||
|
||
parent.insertBefore(nodes[0], outerAtRule) | ||
|
||
// remove nodes | ||
nodes.forEach(node => { | ||
node.parent = undefined | ||
}) | ||
|
||
// better output | ||
nodes[0].raws.before = nodes[0].raws.before || "\n" | ||
|
||
// wrap new rules with media query and/or layer at rule | ||
innerAtRule.append(nodes) | ||
|
||
stmt.type = "nodes" | ||
stmt.nodes = [outerAtRule] | ||
delete stmt.node | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use strict" | ||
|
||
const formatImportPrelude = require("./format-import-prelude") | ||
|
||
// Base64 encode an import with conditions | ||
// The order of conditions is important and is interleaved with cascade layer declarations | ||
// Each group of conditions and cascade layers needs to be interpreted in order | ||
// To achieve this we create a list of base64 encoded imports, where each import contains a stylesheet with another import. | ||
// Each import can define a single group of conditions and a single cascade layer. | ||
module.exports = function base64EncodedConditionalImport(prelude, conditions) { | ||
conditions.reverse() | ||
const first = conditions.pop() | ||
let params = `${prelude} ${formatImportPrelude( | ||
first.layer, | ||
first.media, | ||
first.supports | ||
)}` | ||
|
||
for (const condition of conditions) { | ||
params = `'data:text/css;base64,${Buffer.from(`@import ${params}`).toString( | ||
"base64" | ||
)}' ${formatImportPrelude( | ||
condition.layer, | ||
condition.media, | ||
condition.supports | ||
)}` | ||
} | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use strict" | ||
|
||
module.exports = function formatImportPrelude(layer, media, supports) { | ||
const parts = [] | ||
|
||
if (typeof layer !== "undefined") { | ||
let layerParams = "layer" | ||
if (layer) { | ||
layerParams = `layer(${layer})` | ||
} | ||
|
||
parts.push(layerParams) | ||
} | ||
|
||
if (typeof supports !== "undefined") { | ||
parts.push(`supports(${supports})`) | ||
} | ||
|
||
if (typeof media !== "undefined") { | ||
parts.push(media) | ||
} | ||
|
||
return parts.join(" ") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.