forked from postcss/postcss-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.js
80 lines (68 loc) · 2.13 KB
/
layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict"
// external tooling
const test = require("ava")
const postcss = require("postcss")
const crypto = require("crypto")
const cwd = process.cwd()
// plugin
const atImport = require("..")
// internal tooling
const checkFixture = require("./helpers/check-fixture")
test("should resolve layers of import statements", checkFixture, "layer")
test(
"should correctly wrap imported at rules in layers",
checkFixture,
"layer-import-atrules",
{
nameLayer: hashLayerName,
}
)
test(
"should correctly wrap imported at rules in anonymous layers",
checkFixture,
"layer-import-atrules-anonymous",
{
nameLayer: hashLayerName,
}
)
test("should group rules", checkFixture, "layer-rule-grouping", {
nameLayer: hashLayerName,
})
test("should pass the root file name to the nameLayer function", t => {
return postcss()
.use(atImport({ path: "test/fixtures/imports", nameLayer: hashLayerName }))
.process('@import "foo.css" layer;', { from: "layer.css" })
.then(result => {
t.is(result.css, `@layer import-anon-layer-52ff1597784c{\nfoo{}\n}`)
})
})
test("should error when value is not a function", t => {
return postcss()
.use(atImport({ nameLayer: "not a function" }))
.process("", { from: undefined })
.catch(error => t.is(error.message, "nameLayer option must be a function"))
})
test("should throw when using anonymous layers without the nameLayer plugin option", t => {
return postcss()
.use(atImport({ path: "test/fixtures/imports" }))
.process('@import "foo.css" layer;', { from: undefined })
.catch(err => {
t.is(
err.message,
'postcss-import: <css input>:1:1: When using anonymous layers in @import you must also set the "nameLayer" plugin option'
)
})
})
function hashLayerName(index, rootFilename) {
if (!rootFilename) {
return `import-anon-layer-${index}`
}
// A stable, deterministic and unique layer name:
// - layer index
// - relative rootFilename to current working directory
return `import-anon-layer-${crypto
.createHash("sha256")
.update(`${index}-${rootFilename.split(cwd)[1]}`)
.digest("hex")
.slice(0, 12)}`
}