Skip to content

Commit c5a390b

Browse files
author
Zhicheng Wang
committed
bugfix: Rewrite to the pitch function, so that it can work together with the css-loader
1 parent 547828e commit c5a390b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "css-with-mapping-loader",
33
"description": "css to string with source mapping loader for webpack",
4-
"version": "0.0.3",
4+
"version": "0.0.5",
55
"main": "./src/css-with-mapping.js",
66
"author": {
77
"name": "Zhicheng Wang",

src/css-with-mapping.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
/**
22
* @see https://webpack.github.io/docs/loaders.html
33
*/
4-
module.exports = function (script) {
5-
var content = this.exec(script, __filename);
6-
if (!content || content.length !== 1) {
7-
throw new Error('The css-with-mapping-loader must be chained after the css-loader\n' + content);
8-
}
4+
module.exports = function () {};
5+
6+
/**
7+
* @see https://webpack.github.io/docs/loaders.html#pitching-loader
8+
*/
9+
module.exports.pitch = function (remainingRequest) {
10+
this.cacheable && this.cacheable();
11+
12+
var loaderUtils = require('loader-utils');
13+
return encode.toString() + '\n' +
14+
'var result = require(' + loaderUtils.stringifyRequest(this, "!!" + remainingRequest) + ');\n' +
15+
'if (typeof result === "string") {\n' +
16+
'module.exports = result;\n' +
17+
'} else {\n' +
18+
'module.exports = encode(result);\n' +
19+
'}\n';
20+
};
21+
22+
function encode(content) {
923
var cssObject = content[0];
1024
var cssContent = cssObject[1] || '';
1125
var cssMapping = cssObject[3];
@@ -17,5 +31,5 @@ module.exports = function (script) {
1731
var sourceURLs = cssMapping.sources.map(function (source) {
1832
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'
1933
});
20-
return 'module.exports = ' + JSON.stringify([cssContent].concat(sourceURLs).concat([sourceMapping]).join('\n'));
21-
};
34+
return [cssContent].concat(sourceURLs).concat([sourceMapping]).join('\n');
35+
}

0 commit comments

Comments
 (0)