/
copy.js
157 lines (136 loc) · 4.57 KB
/
copy.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
compareFixtures(
'skip-urls-with-tilde',
'should skip URLs with tilde'
);
describe('copy without assetsPath', () => {
const opts = {
url: 'copy'
};
compareFixtures(
'cant-copy',
'shouldn\'t copy assets if not info available',
opts
);
const postcssOpts = {
from: 'test/fixtures/index.css',
to: 'test/fixtures/build/index.css'
};
testCopy(opts, postcssOpts);
});
describe('copy with assetsPath', () => {
const opts = {
url: 'copy',
assetsPath: 'assets'
};
compareFixtures(
'cant-copy',
'shouldn\'t copy assets if not info available',
opts
);
const postcssOpts = {
from: 'test/fixtures/index.css',
to: 'test/fixtures/build/index.css'
};
testCopy(opts, postcssOpts);
});
describe('copy with assetsPath and without "to"', () => {
const opts = {
url: 'copy',
assetsPath: 'test/fixtures/build/assets'
};
const postcssOpts = {
from: 'test/fixtures/index.css'
};
testCopy(opts, postcssOpts);
});
describe('copy when inline fallback', () => {
const opts = {
url: 'inline',
maxSize: 0.0001,
fallback: 'copy',
assetsPath: 'assets'
};
compareFixtures(
'cant-copy',
'shouldn\'t copy assets if not info available',
opts
);
const postcssOpts = {
from: 'test/fixtures/index.css',
to: 'test/fixtures/build/index.css'
};
testCopy(opts, postcssOpts);
});
function testCopy(opts, postcssOpts) {
const optsWithHash = Object.assign({}, opts, { useHash: true });
const optsWithAppendHash = Object.assign({}, opts, { useHash: true, hashOptions: { append: true } });
const assetsPath = opts.assetsPath ? `${opts.assetsPath}\/` : '';
const patterns = {
copyPixelPng: new RegExp(`"${assetsPath}imported\/pixel\.png"`),
copyPixelGif: new RegExp(`"${assetsPath}pixel\\.gif"`),
copyParamsPixelPngHash: new RegExp(`"${assetsPath}imported\/pixel\\.png\\?\#iefix"`),
copyParamsPixelPngParam: new RegExp(`"${assetsPath}imported\/pixel\\.png\\?foo=bar"`),
copyParamsPixelGif: new RegExp(`"${assetsPath}pixel\\.gif\\#el"`),
copyXXHashPixel8: new RegExp(`"${assetsPath}[a-z0-9]{8}\\.png"`),
copyXXHashParamsPixel8: new RegExp(`"${assetsPath}[a-z0-9]{8}\\.png\\?v=1\\.1\\#iefix"`),
copyXXHashPrependPixel8: new RegExp(`"${assetsPath}pixel_[a-z0-9]{8}\\.png"`)
};
const matchAll = (css, patternsKeys) =>
assert.ok(patternsKeys.every((pat) => css.match(patterns[pat])));
describe('should copy asset from the source (`from`) to the assets destination (`to` + `assetsPath`)', () => {
it('rebase the url', (done) => {
processedCss('fixtures/copy', opts, postcssOpts)
.then((css) => {
matchAll(css, ['copyPixelPng', 'copyPixelGif']);
done();
})
.catch((err) => {
done(err);
});
});
it('rebase the url keeping parameters', (done) => {
processedCss('fixtures/copy-parameters', opts, postcssOpts)
.then((css) => {
matchAll(css, [
'copyParamsPixelPngHash',
'copyParamsPixelPngParam',
'copyParamsPixelGif'
]);
done();
});
});
it('rebase the url using a hash name', (done) => {
processedCss(
'fixtures/copy-hash',
optsWithHash,
postcssOpts
)
.then((css) => {
matchAll(css, ['copyXXHashPixel8']);
done();
});
});
it('rebase the url using a hash name keeping parameters', (done) => {
processedCss(
'fixtures/copy-hash-parameters',
optsWithHash,
postcssOpts
)
.then((css) => {
matchAll(css, ['copyXXHashParamsPixel8']);
done();
});
});
it('rebase the url using a hash and prepending the original filename', (done) => {
processedCss(
'fixtures/copy-hash',
optsWithAppendHash,
postcssOpts
)
.then((css) => {
matchAll(css, ['copyXXHashPrependPixel8']);
done();
});
});
});
}