Skip to content

Commit 6dbb901

Browse files
test: check
1 parent b5e011b commit 6dbb901

File tree

4 files changed

+235
-81
lines changed

4 files changed

+235
-81
lines changed

src/index.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55
import validateOptions from 'schema-utils';
6-
import RequestShortener from 'webpack/lib/RequestShortener';
76
import postcss from 'postcss';
87
import postcssPkg from 'postcss/package.json';
98

@@ -12,7 +11,6 @@ import { getOptions, isUrlRequest } from 'loader-utils';
1211
import schema from './options.json';
1312
import { importParser, icssParser, urlParser } from './plugins';
1413
import {
15-
normalizeSourceMap,
1614
getModulesPlugins,
1715
getFilter,
1816
getImportCode,
@@ -76,14 +74,7 @@ export default function loader(content, map, meta) {
7674
to: this.currentRequest.split('!').pop(),
7775
map: options.sourceMap
7876
? {
79-
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
80-
prev:
81-
sourceMap && map
82-
? normalizeSourceMap(
83-
map,
84-
new RequestShortener(this.rootContext)
85-
)
86-
: null,
77+
prev: sourceMap && map ? map : null,
8778
inline: false,
8879
annotation: false,
8980
}

src/utils.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import localByDefault from 'postcss-modules-local-by-default';
1616
import extractImports from 'postcss-modules-extract-imports';
1717
import modulesScope from 'postcss-modules-scope';
1818
import camelCase from 'camelcase';
19+
import RequestShortener from 'webpack/lib/RequestShortener';
1920

2021
const whitespace = '[\\x20\\t\\r\\n\\f]';
2122
const unescapeRegExp = new RegExp(
@@ -156,15 +157,9 @@ function getModulesPlugins(options, loaderContext) {
156157
];
157158
}
158159

159-
function normalizeSourceMap(map, requestShortener) {
160-
let newMap = map;
161-
162-
// Some loader emit source map as string
163-
if (typeof newMap === 'string') {
164-
newMap = JSON.parse(newMap);
165-
}
166-
167-
console.log(newMap);
160+
function normalizeSourceMap(map, rootContext) {
161+
const newMap = map.toJSON();
162+
const requestShortener = new RequestShortener(rootContext);
168163

169164
// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
170165
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
@@ -183,9 +178,7 @@ function normalizeSourceMap(map, requestShortener) {
183178
);
184179
}
185180

186-
console.log(newMap);
187-
188-
return newMap;
181+
return JSON.stringify(newMap);
189182
}
190183

191184
function getImportPrefix(loaderContext, importLoaders) {
@@ -377,7 +370,10 @@ function getModuleCode(
377370
}
378371

379372
const { css, map } = result;
380-
const sourceMapValue = sourceMap && map ? `,${map}` : '';
373+
const sourceMapValue =
374+
sourceMap && map
375+
? `,${normalizeSourceMap(map, loaderContext.rootContext)}`
376+
: '';
381377

382378
let cssCode = JSON.stringify(css);
383379

@@ -506,7 +502,6 @@ export {
506502
normalizeUrl,
507503
getFilter,
508504
getModulesPlugins,
509-
normalizeSourceMap,
510505
getImportCode,
511506
getModuleCode,
512507
getExportCode,
+218-51
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,228 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`loader should have same "contenthash" with "css-loader" and with source maps: errors 1`] = `Array []`;
4-
5-
exports[`loader should have same "contenthash" with "css-loader" and with source maps: module 1`] = `
6-
Array [
7-
"main.54b9712c1981e48c12c4.bundle.js",
8-
]
9-
`;
10-
11-
exports[`loader should have same "contenthash" with "css-loader" and with source maps: warnings 1`] = `Array []`;
12-
13-
exports[`loader should have same "contenthash" with "css-loader" and without source maps: errors 1`] = `Array []`;
14-
15-
exports[`loader should have same "contenthash" with "css-loader" and without source maps: module 1`] = `
16-
Array [
17-
"main.b38d5f87c88c55a4258e.bundle.js",
18-
]
19-
`;
20-
21-
exports[`loader should have same "contenthash" with "css-loader" and without source maps: warnings 1`] = `Array []`;
22-
23-
exports[`loader should have same "contenthash" with "postcss-loader" and with source maps: errors 1`] = `Array []`;
24-
25-
exports[`loader should have same "contenthash" with "postcss-loader" and with source maps: module 1`] = `
26-
Array [
27-
"main.d77dd6564bc6e6297cd4.bundle.js",
28-
]
29-
`;
30-
31-
exports[`loader should have same "contenthash" with "postcss-loader" and with source maps: warnings 1`] = `Array []`;
32-
33-
exports[`loader should have same "contenthash" with "postcss-loader" and without source maps: errors 1`] = `Array []`;
34-
35-
exports[`loader should have same "contenthash" with "postcss-loader" and without source maps: module 1`] = `
36-
Array [
37-
"main.f8e62206a43c13393798.bundle.js",
38-
]
39-
`;
40-
41-
exports[`loader should have same "contenthash" with "postcss-loader" and without source maps: warnings 1`] = `Array []`;
42-
433
exports[`loader should have same "contenthash" with "sass-loader" and with source maps: errors 1`] = `Array []`;
444

455
exports[`loader should have same "contenthash" with "sass-loader" and with source maps: module 1`] = `
46-
Array [
47-
"main.151589c7e12ebfd445e7.bundle.js",
48-
]
49-
`;
6+
Object {
7+
"main.a12ab765493c74c80be2.bundle.js": "/******/ (function(modules) { // webpackBootstrap
8+
/******/ // The module cache
9+
/******/ var installedModules = {};
10+
/******/
11+
/******/ // The require function
12+
/******/ function __webpack_require__(moduleId) {
13+
/******/
14+
/******/ // Check if module is in cache
15+
/******/ if(installedModules[moduleId]) {
16+
/******/ return installedModules[moduleId].exports;
17+
/******/ }
18+
/******/ // Create a new module (and put it into the cache)
19+
/******/ var module = installedModules[moduleId] = {
20+
/******/ i: moduleId,
21+
/******/ l: false,
22+
/******/ exports: {}
23+
/******/ };
24+
/******/
25+
/******/ // Execute the module function
26+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
27+
/******/
28+
/******/ // Flag the module as loaded
29+
/******/ module.l = true;
30+
/******/
31+
/******/ // Return the exports of the module
32+
/******/ return module.exports;
33+
/******/ }
34+
/******/
35+
/******/
36+
/******/ // expose the modules object (__webpack_modules__)
37+
/******/ __webpack_require__.m = modules;
38+
/******/
39+
/******/ // expose the module cache
40+
/******/ __webpack_require__.c = installedModules;
41+
/******/
42+
/******/ // define getter function for harmony exports
43+
/******/ __webpack_require__.d = function(exports, name, getter) {
44+
/******/ if(!__webpack_require__.o(exports, name)) {
45+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
46+
/******/ }
47+
/******/ };
48+
/******/
49+
/******/ // define __esModule on exports
50+
/******/ __webpack_require__.r = function(exports) {
51+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
52+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
53+
/******/ }
54+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
55+
/******/ };
56+
/******/
57+
/******/ // create a fake namespace object
58+
/******/ // mode & 1: value is a module id, require it
59+
/******/ // mode & 2: merge all properties of value into the ns
60+
/******/ // mode & 4: return value when already ns object
61+
/******/ // mode & 8|1: behave like require
62+
/******/ __webpack_require__.t = function(value, mode) {
63+
/******/ if(mode & 1) value = __webpack_require__(value);
64+
/******/ if(mode & 8) return value;
65+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
66+
/******/ var ns = Object.create(null);
67+
/******/ __webpack_require__.r(ns);
68+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
69+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
70+
/******/ return ns;
71+
/******/ };
72+
/******/
73+
/******/ // getDefaultExport function for compatibility with non-harmony modules
74+
/******/ __webpack_require__.n = function(module) {
75+
/******/ var getter = module && module.__esModule ?
76+
/******/ function getDefault() { return module['default']; } :
77+
/******/ function getModuleExports() { return module; };
78+
/******/ __webpack_require__.d(getter, 'a', getter);
79+
/******/ return getter;
80+
/******/ };
81+
/******/
82+
/******/ // Object.prototype.hasOwnProperty.call
83+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
84+
/******/
85+
/******/ // __webpack_public_path__
86+
/******/ __webpack_require__.p = \\"/webpack/public/path/\\";
87+
/******/
88+
/******/
89+
/******/ // Load entry module and return exports
90+
/******/ return __webpack_require__(__webpack_require__.s = \\"./contenthash/basic-sass.js\\");
91+
/******/ })
92+
/************************************************************************/
93+
/******/ ({
94+
95+
/***/ \\"../../src/runtime/api.js\\":
96+
/*!**********************************************************************!*\\\\
97+
!*** /home/evilebottnawi/IdeaProjects/css-loader/src/runtime/api.js ***!
98+
\\\\**********************************************************************/
99+
/*! no static exports found */
100+
/***/ (function(module, exports) {
101+
102+
/*
103+
MIT License http://www.opensource.org/licenses/mit-license.php
104+
Author Tobias Koppers @sokra
105+
*/
106+
// css base code, injected by the css-loader
107+
// eslint-disable-next-line func-names
108+
module.exports = function(useSourceMap) {
109+
const list = [];
110+
111+
// return the list of modules as css string
112+
list.toString = function toString() {
113+
return this.map((item) => {
114+
const content = cssWithMappingToString(item, useSourceMap);
115+
116+
if (item[2]) {
117+
return \`@media \${item[2]} {\${content}}\`;
118+
}
119+
120+
return content;
121+
}).join('');
122+
};
123+
124+
// import a list of modules into the list
125+
// eslint-disable-next-line func-names
126+
list.i = function(modules, mediaQuery) {
127+
if (typeof modules === 'string') {
128+
// eslint-disable-next-line no-param-reassign
129+
modules = [[null, modules, '']];
130+
}
131+
132+
for (let i = 0; i < modules.length; i++) {
133+
const item = [].concat(modules[i]);
134+
135+
if (mediaQuery) {
136+
if (!item[2]) {
137+
item[2] = mediaQuery;
138+
} else {
139+
item[2] = \`\${mediaQuery} and \${item[2]}\`;
140+
}
141+
}
142+
143+
list.push(item);
144+
}
145+
};
146+
147+
return list;
148+
};
149+
150+
function cssWithMappingToString(item, useSourceMap) {
151+
const content = item[1] || '';
152+
// eslint-disable-next-line prefer-destructuring
153+
const cssMapping = item[3];
154+
155+
if (!cssMapping) {
156+
return content;
157+
}
158+
159+
if (useSourceMap && typeof btoa === 'function') {
160+
const sourceMapping = toComment(cssMapping);
161+
const sourceURLs = cssMapping.sources.map(
162+
(source) => \`/*# sourceURL=\${cssMapping.sourceRoot}\${source} */\`
163+
);
164+
165+
return [content]
166+
.concat(sourceURLs)
167+
.concat([sourceMapping])
168+
.join('\\\\n');
169+
}
170+
171+
return [content].join('\\\\n');
172+
}
173+
174+
// Adapted from convert-source-map (MIT)
175+
function toComment(sourceMap) {
176+
// eslint-disable-next-line no-undef
177+
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
178+
const data = \`sourceMappingURL=data:application/json;charset=utf-8;base64,\${base64}\`;
179+
180+
return \`/*# \${data} */\`;
181+
}
182+
183+
184+
/***/ }),
185+
186+
/***/ \\"./contenthash/basic-sass.js\\":
187+
/*!***********************************!*\\\\
188+
!*** ./contenthash/basic-sass.js ***!
189+
\\\\***********************************/
190+
/*! exports provided: default */
191+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
192+
193+
\\"use strict\\";
194+
__webpack_require__.r(__webpack_exports__);
195+
/* harmony import */ var _basic_scss__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./basic.scss */ \\"./contenthash/basic.scss\\");
196+
/* harmony import */ var _basic_scss__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_basic_scss__WEBPACK_IMPORTED_MODULE_0__);
197+
198+
199+
__export__ = _basic_scss__WEBPACK_IMPORTED_MODULE_0___default.a;
200+
201+
/* harmony default export */ __webpack_exports__[\\"default\\"] = (_basic_scss__WEBPACK_IMPORTED_MODULE_0___default.a);
202+
203+
204+
/***/ }),
205+
206+
/***/ \\"./contenthash/basic.scss\\":
207+
/*!********************************!*\\\\
208+
!*** ./contenthash/basic.scss ***!
209+
\\\\********************************/
210+
/*! no static exports found */
211+
/***/ (function(module, exports, __webpack_require__) {
50212
51-
exports[`loader should have same "contenthash" with "sass-loader" and with source maps: warnings 1`] = `Array []`;
213+
// Imports
214+
var ___CSS_LOADER_API_IMPORT___ = __webpack_require__(/*! ../../../src/runtime/api.js */ \\"../../src/runtime/api.js\\");
215+
exports = ___CSS_LOADER_API_IMPORT___(true);
216+
// Module
217+
exports.push([module.i, \\"a {\\\\n color: red;\\\\n}\\\\na:hover {\\\\n color: red;\\\\n}\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"./contenthash/basic.scss\\",\\"basic.scss\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;EACE,UAHI;ACEN;ADGE;EACE,UANE;ACKN\\",\\"file\\":\\"basic.scss\\",\\"sourcesContent\\":[\\"$var: red;\\\\n\\\\na {\\\\n color: $var;\\\\n\\\\n &:hover {\\\\n color: $var;\\\\n }\\\\n}\\\\n\\",\\"a {\\\\n color: red;\\\\n}\\\\na:hover {\\\\n color: red;\\\\n}\\"]}]);
218+
// Exports
219+
module.exports = exports;
52220
53-
exports[`loader should have same "contenthash" with "sass-loader" and without source maps: errors 1`] = `Array []`;
54221
55-
exports[`loader should have same "contenthash" with "sass-loader" and without source maps: module 1`] = `
56-
Array [
57-
"main.9ae036ace8bbbebbd185.bundle.js",
58-
]
222+
/***/ })
223+
224+
/******/ });",
225+
}
59226
`;
60227
61-
exports[`loader should have same "contenthash" with "sass-loader" and without source maps: warnings 1`] = `Array []`;
228+
exports[`loader should have same "contenthash" with "sass-loader" and with source maps: warnings 1`] = `Array []`;

0 commit comments

Comments
 (0)