Skip to content

Commit cf3a3a7

Browse files
fix: reduce runtime (#1378)
1 parent 7c2cede commit cf3a3a7

17 files changed

+1011
-429
lines changed

.eslintrc.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
module.exports = {
22
root: true,
33
extends: ["@webpack-contrib/eslint-config-webpack", "prettier"],
4+
overrides: [
5+
{
6+
files: "src/runtime/**/*",
7+
env: {
8+
browser: true,
9+
},
10+
rules: {
11+
"prefer-destructuring": "off",
12+
"no-param-reassign": "off",
13+
"no-continue": "off",
14+
"no-underscore-dangle": "off",
15+
},
16+
},
17+
],
418
};

src/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,13 @@ export default async function loader(content, map, meta) {
207207
imports.unshift({
208208
type: "api_sourcemap_import",
209209
importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
210-
url: stringifyRequest(
211-
this,
212-
require.resolve("./runtime/cssWithMappingToString")
213-
),
210+
url: stringifyRequest(this, require.resolve("./runtime/noSourceMaps")),
211+
});
212+
} else {
213+
imports.unshift({
214+
type: "api_sourcemap_import",
215+
importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
216+
url: stringifyRequest(this, require.resolve("./runtime/noSourceMaps")),
214217
});
215218
}
216219
}

src/runtime/api.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
// css base code, injected by the css-loader
6-
// eslint-disable-next-line func-names
7-
module.exports = function (cssWithMappingToString) {
5+
module.exports = (cssWithMappingToString) => {
86
const list = [];
97

108
// return the list of modules as css string
@@ -21,18 +19,15 @@ module.exports = function (cssWithMappingToString) {
2119
};
2220

2321
// import a list of modules into the list
24-
// eslint-disable-next-line func-names
25-
list.i = function (modules, mediaQuery, dedupe) {
22+
list.i = function i(modules, mediaQuery, dedupe) {
2623
if (typeof modules === "string") {
27-
// eslint-disable-next-line no-param-reassign
2824
modules = [[null, modules, ""]];
2925
}
3026

3127
const alreadyImportedModules = {};
3228

3329
if (dedupe) {
3430
for (let i = 0; i < this.length; i++) {
35-
// eslint-disable-next-line prefer-destructuring
3631
const id = this[i][0];
3732

3833
if (id != null) {
@@ -45,7 +40,6 @@ module.exports = function (cssWithMappingToString) {
4540
const item = [].concat(modules[i]);
4641

4742
if (dedupe && alreadyImportedModules[item[0]]) {
48-
// eslint-disable-next-line no-continue
4943
continue;
5044
}
5145

src/runtime/getUrl.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
module.exports = (url, options) => {
22
if (!options) {
3-
// eslint-disable-next-line no-param-reassign
43
options = {};
54
}
65

76
if (!url) {
87
return url;
98
}
109

11-
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
1210
url = String(url.__esModule ? url.default : url);
1311

1412
// If url is already wrapped in quotes, remove them
1513
if (/^['"].*['"]$/.test(url)) {
16-
// eslint-disable-next-line no-param-reassign
1714
url = url.slice(1, -1);
1815
}
1916

2017
if (options.hash) {
21-
// eslint-disable-next-line no-param-reassign
2218
url += options.hash;
2319
}
2420

src/runtime/noSourceMaps.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (i) => i[1];

src/runtime/cssWithMappingToString.js src/runtime/sourceMaps.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
module.exports = function cssWithMappingToString(item) {
2-
const [, content, , cssMapping] = item;
1+
module.exports = (item) => {
2+
const content = item[1];
3+
const cssMapping = item[3];
34

45
if (!cssMapping) {
56
return content;
67
}
78

89
if (typeof btoa === "function") {
9-
// eslint-disable-next-line no-undef
1010
const base64 = btoa(
1111
unescape(encodeURIComponent(JSON.stringify(cssMapping)))
1212
);

src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
935935
let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${
936936
options.sourceMap
937937
? "___CSS_LOADER_API_SOURCEMAP_IMPORT___"
938-
: "function(i){return i[1]}"
938+
: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___"
939939
});\n`;
940940

941941
for (const item of api) {

test/__snapshots__/esModule-option.test.js.snap

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ exports[`"esModule" option should work when not specified: errors 1`] = `Array [
44

55
exports[`"esModule" option should work when not specified: module 1`] = `
66
"// Imports
7+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\";
78
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
89
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\";
910
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
1011
var ___CSS_LOADER_URL_IMPORT_0___ = new URL(\\"./img.png\\", import.meta.url);
11-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
12+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
1213
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
1314
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
1415
// Module
@@ -50,11 +51,12 @@ exports[`"esModule" option should work with a value equal to "false": errors 1`]
5051

5152
exports[`"esModule" option should work with a value equal to "false": module 1`] = `
5253
"// Imports
54+
var ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ = require(\\"../../../src/runtime/noSourceMaps.js\\");
5355
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../src/runtime/api.js\\");
5456
var ___CSS_LOADER_AT_RULE_IMPORT_0___ = require(\\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\");
5557
var ___CSS_LOADER_GET_URL_IMPORT___ = require(\\"../../../src/runtime/getUrl.js\\");
5658
var ___CSS_LOADER_URL_IMPORT_0___ = require(\\"./img.png\\");
57-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
59+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
5860
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
5961
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
6062
// Module
@@ -96,11 +98,12 @@ exports[`"esModule" option should work with a value equal to "true" and the "mod
9698

9799
exports[`"esModule" option should work with a value equal to "true" and the "mode" value equal to "global": module 1`] = `
98100
"// Imports
101+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\";
99102
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
100103
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\";
101104
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
102105
var ___CSS_LOADER_URL_IMPORT_0___ = new URL(\\"./img.png\\", import.meta.url);
103-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
106+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
104107
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
105108
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
106109
// Module
@@ -143,11 +146,12 @@ exports[`"esModule" option should work with a value equal to "true" and the "mod
143146

144147
exports[`"esModule" option should work with a value equal to "true" and the "mode" value equal to "local": module 1`] = `
145148
"// Imports
149+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\";
146150
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
147151
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\";
148152
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
149153
var ___CSS_LOADER_URL_IMPORT_0___ = new URL(\\"./img.png\\", import.meta.url);
150-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
154+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
151155
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
152156
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
153157
// Module
@@ -192,11 +196,12 @@ exports[`"esModule" option should work with a value equal to "true" and the "mod
192196

193197
exports[`"esModule" option should work with a value equal to "true" and the "mode" value equal to "pure": module 1`] = `
194198
"// Imports
199+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\";
195200
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
196201
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\";
197202
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
198203
var ___CSS_LOADER_URL_IMPORT_0___ = new URL(\\"./img.png\\", import.meta.url);
199-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
204+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
200205
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
201206
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
202207
// Module
@@ -241,11 +246,12 @@ exports[`"esModule" option should work with a value equal to "true": errors 1`]
241246

242247
exports[`"esModule" option should work with a value equal to "true": module 1`] = `
243248
"// Imports
249+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\";
244250
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
245251
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./imported.css\\";
246252
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../../src/runtime/getUrl.js\\";
247253
var ___CSS_LOADER_URL_IMPORT_0___ = new URL(\\"./img.png\\", import.meta.url);
248-
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
254+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
249255
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
250256
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
251257
// Module

0 commit comments

Comments
 (0)