/
CSSPackager.js
62 lines (54 loc) 路 1.68 KB
/
CSSPackager.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
const path = require('path');
const Packager = require('./Packager');
const lineCounter = require('../utils/lineCounter');
const urlJoin = require('../utils/urlJoin');
class CSSPackager extends Packager {
async start() {
this.lineOffset = 0;
this.columnOffset = 0;
}
async addAsset(asset) {
let css = asset.generated.css || '';
// Figure out which media types this asset was imported with.
// We only want to import the asset once, so group them all together.
let media = [];
for (let dep of asset.parentDeps) {
if (!dep.media) {
// Asset was imported without a media type. Don't wrap in @media.
media.length = 0;
break;
} else {
media.push(dep.media);
}
}
// If any, wrap in an @media block
if (media.length) {
css = `@media ${media.join(', ')} {\n${css.trim()}\n}\n`;
}
let lineCount = lineCounter(css) - 1;
if (lineCount == 0) {
this.bundle.addOffset(asset, this.lineOffset, this.columnOffset);
await this.write(css);
this.columnOffset = css.length;
} else {
this.bundle.addOffset(asset, this.lineOffset + 1, this.columnOffset);
await this.write('\n' + css);
this.columnOffset = 0;
this.lineOffset += lineCounter(css);
}
}
async end() {
if (this.options.sourceMaps) {
// Add source map url if a map bundle exists
let mapBundle = this.bundle.siblingBundlesMap.get('map');
if (mapBundle) {
let mapUrl = urlJoin(
this.options.publicURL,
path.basename(mapBundle.name)
);
await this.write(`\n/*# sourceMappingURL=${mapUrl}*/`);
}
}
}
}
module.exports = CSSPackager;