Skip to content

Commit 09e21c1

Browse files
refactor: runtime code
1 parent 04c3d16 commit 09e21c1

File tree

6 files changed

+103
-88
lines changed

6 files changed

+103
-88
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ rules:
4141
consistent-this: error
4242
curly: 'off'
4343
default-case: 'off'
44-
dot-location: error
44+
dot-location: 'off'
4545
dot-notation:
4646
- error
4747
- allowKeywords: true

lib/css-base.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

lib/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = function(content, map) {
126126
// embed runtime
127127
callback(null, urlEscapeHelper +
128128
"exports = module.exports = require(" +
129-
loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) +
129+
loaderUtils.stringifyRequest(this, require.resolve("./runtime.js")) +
130130
")(" + sourceMap + ");\n" +
131131
"// imports\n" +
132132
importJs + "\n\n" +

lib/runtime.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
// CSS (Loader) Runtime
6+
module.exports = function(useSourceMap) {
7+
const list = [];
8+
9+
// return the list of modules as css string
10+
list.toString = function toString() {
11+
return this.map(function(item) {
12+
const content = cssWithMappingToString(item, useSourceMap);
13+
14+
if (item[2]) {
15+
return "@media " + item[2] + "{" + content + "}";
16+
}
17+
18+
return content;
19+
}).join("");
20+
};
21+
22+
// import a list of modules into the list
23+
list.i = function(modules, mediaQuery) {
24+
if (typeof modules === "string") {
25+
modules = [[null, modules, ""]];
26+
}
27+
28+
const isImported = {};
29+
30+
for (let i = 0; i < this.length; i++) {
31+
const id = this[i][0];
32+
33+
if (typeof id === "number") {
34+
isImported[id] = true;
35+
}
36+
}
37+
38+
for (let i = 0; i < modules.length; i++) {
39+
const item = modules[i];
40+
41+
// skip already imported module
42+
// this implementation is not 100% perfect for weird media query combinations
43+
// when a module is imported multiple times with different media queries.
44+
// I hope this will never occur (Hey this way we have smaller bundles)
45+
if (typeof item[0] !== "number" || !isImported[item[0]]) {
46+
if (mediaQuery && !item[2]) {
47+
item[2] = mediaQuery;
48+
} else if (mediaQuery) {
49+
item[2] = "(" + item[2] + ") and (" + mediaQuery + ")";
50+
}
51+
52+
list.push(item);
53+
}
54+
}
55+
};
56+
57+
return list;
58+
};
59+
60+
function cssWithMappingToString(item, useSourceMap) {
61+
const content = item[1] || "";
62+
const sourceMap = item[3];
63+
64+
if (!sourceMap) {
65+
return content;
66+
}
67+
68+
if (useSourceMap && typeof btoa === "function") {
69+
const sourceMapping = toComment(sourceMap);
70+
const sourceURLs = sourceMap.sources.map(function(source) {
71+
return "/*# sourceURL=" + sourceMap.sourceRoot + source + " */";
72+
});
73+
74+
return [content]
75+
.concat(sourceURLs)
76+
.concat([sourceMapping])
77+
.join("\n");
78+
}
79+
80+
return [content].join("\n");
81+
}
82+
83+
// Adapted from convert-source-map (MIT)
84+
function toComment(sourceMap) {
85+
// eslint-disable-next-line no-undef
86+
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
87+
const data =
88+
"sourceMappingURL=data:application/json;charset=utf-8;base64," + base64;
89+
90+
return "/*# " + data + " */";
91+
}

test/cssBaseTest.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*eslint-env mocha*/
22

3-
var base = require("../lib/css-base");
3+
var runtime = require("../lib/runtime");
44

5-
describe("css-base", function() {
5+
describe("runtime", function() {
66
before(function() {
77
global.btoa = function btoa(str) {
88
var buffer = null;
@@ -22,23 +22,23 @@ describe("css-base", function() {
2222
})
2323

2424
it("should toString a single module", function() {
25-
var m = base();
25+
var m = runtime();
2626
m.push([1, "body { a: 1; }", ""]);
2727
m.toString().should.be.eql("body { a: 1; }");
2828
});
2929
it("should toString multiple modules", function() {
30-
var m = base();
30+
var m = runtime();
3131
m.push([2, "body { b: 2; }", ""]);
3232
m.push([1, "body { a: 1; }", ""]);
3333
m.toString().should.be.eql("body { b: 2; }body { a: 1; }");
3434
});
3535
it("should toString with media query", function() {
36-
var m = base();
36+
var m = runtime();
3737
m.push([1, "body { a: 1; }", "screen"]);
3838
m.toString().should.be.eql("@media screen{body { a: 1; }}");
3939
});
4040
it("should import modules", function() {
41-
var m = base();
41+
var m = runtime();
4242
var m1 = [1, "body { a: 1; }", "screen"];
4343
var m2 = [2, "body { b: 2; }", ""];
4444
var m3 = [3, "body { c: 3; }", ""];
@@ -53,7 +53,7 @@ describe("css-base", function() {
5353
"@media screen{body { a: 1; }}");
5454
});
5555
it("should toString with source mapping", function() {
56-
var m = base(true);
56+
var m = runtime(true);
5757
m.push([1, "body { a: 1; }", "", {
5858
file: "test.scss",
5959
sources: [
@@ -66,7 +66,7 @@ describe("css-base", function() {
6666
});
6767
it("should toString without source mapping if btoa not avalibale", function() {
6868
global.btoa = null;
69-
var m = base(true);
69+
var m = runtime(true);
7070
m.push([1, "body { a: 1; }", "", {
7171
file: "test.scss",
7272
sources: [

test/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function getEvaluated(output, modules) {
99
var fn = vm.runInThisContext("(function(module, exports, require) {" + output + "})", "testcase.js");
1010
var m = { exports: {}, id: 1 };
1111
fn(m, m.exports, function(module) {
12-
if(module.indexOf("css-base") >= 0)
13-
return require("../lib/css-base");
12+
if(module.indexOf("runtime") >= 0)
13+
return require("../lib/runtime");
1414
if(module.indexOf("url/escape") >= 0)
1515
return require("../lib/url/escape");
1616
if(module.indexOf("-!/path/css-loader!") === 0)

0 commit comments

Comments
 (0)