-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest.js
More file actions
147 lines (119 loc) · 4.24 KB
/
test.js
File metadata and controls
147 lines (119 loc) · 4.24 KB
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
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import Vinyl from 'vinyl';
import sourceMaps from 'gulp-sourcemaps';
import {pEvent} from 'p-event';
import autoprefixer from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('autoprefix CSS', async t => {
const stream = autoprefixer();
const data = pEvent(stream, 'data');
stream.end(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture', 'fixture.css'),
contents: Buffer.from('::placeholder {\n\tcolor: gray;\n}'),
}));
const file = await data;
t.regex(file.contents.toString(), /-/);
t.is(file.relative, 'fixture.css');
});
test('generate source maps', async t => {
const init = sourceMaps.init();
const write = sourceMaps.write();
const data = pEvent(write, 'data');
init
.pipe(autoprefixer({
overrideBrowserslist: ['Firefox ESR'],
}))
.pipe(write);
init.end(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture', 'fixture.css'),
contents: Buffer.from('a {\n\tdisplay: flex;\n}'),
sourceMap: '',
}));
const file = await data;
t.is(file.sourceMap.mappings, 'AAAA;CACC,aAAa;AACd');
const contents = file.contents.toString();
t.regex(contents, /flex/);
t.regex(contents, /sourceMappingURL=data:application\/json;charset=utf8;base64/);
});
test('read upstream source maps', async t => {
const stream = autoprefixer();
const finalStream = stream.pipe(sourceMaps.write());
const data = pEvent(finalStream, 'data');
const testFile = new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture', 'fixture.css'),
contents: Buffer.from('a {\n\tdisplay: flex;\n}\n'),
});
testFile.sourceMap = {
version: 3,
sources: ['imported.less'],
names: [],
mappings: 'AAAA;EACC,aAAA',
file: 'fixture.css',
sourcesContent: ['a {\n display: flex;\n}\n'],
};
stream.end(testFile);
const file = await data;
const sourcesContent = [
'a {\n display: flex;\n}\n',
'a {\n\tdisplay: flex;\n}\n',
];
t.is(file.sourceMap.sourcesContent[0], sourcesContent[0]);
t.is(file.sourceMap.sourcesContent[1], sourcesContent[1]);
});
test('preserve original source paths in sourcemaps', async t => {
const stream = autoprefixer();
const finalStream = stream.pipe(sourceMaps.write());
const data = pEvent(finalStream, 'data');
const testFile = new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture', 'output.css'),
contents: Buffer.from('a {\n\tdisplay: flex;\n}\n.button {\n\tcolor: red;\n}\n'),
});
// Simulate a sourcemap from multiple source files (like SASS imports)
testFile.sourceMap = {
version: 3,
sources: ['../scss/main.scss', '../scss/components/button.scss'],
names: [],
mappings: 'AAAA;EACC,aAAA;ACDD;EACC,UAAU;',
file: 'output.css',
sourcesContent: ['a {\n display: flex;\n}\n', '.button {\n color: red;\n}\n'],
};
stream.end(testFile);
const file = await data;
// Verify original sources are preserved
t.true(file.sourceMap.sources.includes('../scss/main.scss'));
t.true(file.sourceMap.sources.includes('../scss/components/button.scss'));
});
test('uses browserslist config from package.json', async t => {
const {promises: fs} = await import('node:fs');
const packageJson = JSON.parse(await fs.readFile(path.join(__dirname, 'package.json'), 'utf8'));
const originalBrowserslist = packageJson.browserslist;
packageJson.browserslist = ['last 1 Chrome version'];
await fs.writeFile(path.join(__dirname, 'package.json'), JSON.stringify(packageJson, null, '\t') + '\n');
const stream = autoprefixer();
const data = pEvent(stream, 'data');
stream.end(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture', 'fixture.css'),
contents: Buffer.from('a {\n\tdisplay: flex;\n}'),
}));
const file = await data;
t.regex(file.contents.toString(), /display:\s*flex/);
if (originalBrowserslist) {
packageJson.browserslist = originalBrowserslist;
} else {
delete packageJson.browserslist;
}
await fs.writeFile(path.join(__dirname, 'package.json'), JSON.stringify(packageJson, null, '\t') + '\n');
});