UNPKG

3.41 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
6
7const path = require('path');
8
9const Packager = require('./Packager');
10
11const lineCounter = require('../utils/lineCounter');
12
13const urlJoin = require('../utils/urlJoin');
14
15class CSSPackager extends Packager {
16 start() {
17 var _this = this;
18
19 return (0, _asyncToGenerator2.default)(function* () {
20 _this.lineOffset = 0;
21 _this.columnOffset = 0;
22 })();
23 }
24
25 addAsset(asset) {
26 var _this2 = this;
27
28 return (0, _asyncToGenerator2.default)(function* () {
29 let css = asset.generated.css || ''; // Figure out which media types this asset was imported with.
30 // We only want to import the asset once, so group them all together.
31
32 let media = [];
33 var _iteratorNormalCompletion = true;
34 var _didIteratorError = false;
35 var _iteratorError = undefined;
36
37 try {
38 for (var _iterator = asset.parentDeps[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
39 let dep = _step.value;
40
41 if (!dep.media) {
42 // Asset was imported without a media type. Don't wrap in @media.
43 media.length = 0;
44 break;
45 } else {
46 media.push(dep.media);
47 }
48 } // If any, wrap in an @media block
49
50 } catch (err) {
51 _didIteratorError = true;
52 _iteratorError = err;
53 } finally {
54 try {
55 if (!_iteratorNormalCompletion && _iterator.return != null) {
56 _iterator.return();
57 }
58 } finally {
59 if (_didIteratorError) {
60 throw _iteratorError;
61 }
62 }
63 }
64
65 if (media.length) {
66 css = `@media ${media.join(', ')} {\n${css.trim()}\n}\n`;
67 }
68
69 if (asset.options.sourceMaps) {
70 let lineCount = lineCounter(css);
71
72 if (lineCount == 1) {
73 _this2.bundle.addOffset(asset, _this2.lineOffset, _this2.columnOffset);
74
75 yield _this2.write(css);
76 _this2.columnOffset += css.length;
77 } else {
78 const lines = css.split('\n');
79
80 if (_this2.columnOffset == 0) {
81 _this2.bundle.addOffset(asset, _this2.lineOffset, 0);
82
83 yield _this2.write(css + '\n');
84 } else {
85 _this2.columnOffset = 0;
86
87 _this2.bundle.addOffset(asset, _this2.lineOffset + 1, 0);
88
89 _this2.columnOffset = lines[lines.length - 1].length;
90 yield _this2.write('\n' + css);
91 }
92
93 _this2.lineOffset += lineCount;
94 }
95 } else {
96 yield _this2.write(css);
97 }
98 })();
99 }
100
101 end() {
102 var _this3 = this,
103 _superprop_callEnd = (..._args) => super.end(..._args);
104
105 return (0, _asyncToGenerator2.default)(function* () {
106 if (_this3.options.sourceMaps) {
107 // Add source map url if a map bundle exists
108 let mapBundle = _this3.bundle.siblingBundlesMap.get('map');
109
110 if (mapBundle) {
111 let mapUrl = urlJoin(_this3.options.publicURL, path.basename(mapBundle.name));
112 yield _this3.write(`\n/*# sourceMappingURL=${mapUrl} */`);
113 }
114 }
115
116 yield _superprop_callEnd();
117 })();
118 }
119
120}
121
122module.exports = CSSPackager;
\No newline at end of file