Skip to content

Commit 75c6bee

Browse files
committed
fix: Can't resolve 'fs' when bundle with webpack #447
This is a webpack core level problem. Hackfixing for now. Closes #447.
1 parent 0eedd9f commit 75c6bee

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

lib/convert-source-map.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/* eslint-disable */
2+
'use strict';
3+
// XXXXX: This file should not exist. Working around a core level bug
4+
// that prevents using fs at loaders.
5+
//var fs = require('fs'); // XXX
6+
var path = require('path');
7+
8+
var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg;
9+
var mapFileCommentRx =
10+
//Example (Extra space between slashes added to solve Safari bug. Exclude space in production):
11+
// / /# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
12+
/(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg
13+
14+
function decodeBase64(base64) {
15+
return new Buffer(base64, 'base64').toString();
16+
}
17+
18+
function stripComment(sm) {
19+
return sm.split(',').pop();
20+
}
21+
22+
function readFromFileMap(sm, dir) {
23+
// NOTE: this will only work on the server since it attempts to read the map file
24+
25+
mapFileCommentRx.lastIndex = 0;
26+
var r = mapFileCommentRx.exec(sm);
27+
28+
// for some odd reason //# .. captures in 1 and /* .. */ in 2
29+
var filename = r[1] || r[2];
30+
var filepath = path.resolve(dir, filename);
31+
32+
try {
33+
return fs.readFileSync(filepath, 'utf8');
34+
} catch (e) {
35+
throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
36+
}
37+
}
38+
39+
function Converter (sm, opts) {
40+
opts = opts || {};
41+
42+
if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
43+
if (opts.hasComment) sm = stripComment(sm);
44+
if (opts.isEncoded) sm = decodeBase64(sm);
45+
if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
46+
47+
this.sourcemap = sm;
48+
}
49+
50+
Converter.prototype.toJSON = function (space) {
51+
return JSON.stringify(this.sourcemap, null, space);
52+
};
53+
54+
Converter.prototype.toBase64 = function () {
55+
var json = this.toJSON();
56+
return new Buffer(json).toString('base64');
57+
};
58+
59+
Converter.prototype.toComment = function (options) {
60+
var base64 = this.toBase64();
61+
var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
62+
return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
63+
};
64+
65+
// returns copy instead of original
66+
Converter.prototype.toObject = function () {
67+
return JSON.parse(this.toJSON());
68+
};
69+
70+
Converter.prototype.addProperty = function (key, value) {
71+
if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');
72+
return this.setProperty(key, value);
73+
};
74+
75+
Converter.prototype.setProperty = function (key, value) {
76+
this.sourcemap[key] = value;
77+
return this;
78+
};
79+
80+
Converter.prototype.getProperty = function (key) {
81+
return this.sourcemap[key];
82+
};
83+
84+
exports.fromObject = function (obj) {
85+
return new Converter(obj);
86+
};
87+
88+
exports.fromJSON = function (json) {
89+
return new Converter(json, { isJSON: true });
90+
};
91+
92+
exports.fromBase64 = function (base64) {
93+
return new Converter(base64, { isEncoded: true });
94+
};
95+
96+
exports.fromComment = function (comment) {
97+
comment = comment
98+
.replace(/^\/\*/g, '//')
99+
.replace(/\*\/$/g, '');
100+
101+
return new Converter(comment, { isEncoded: true, hasComment: true });
102+
};
103+
104+
exports.fromMapFileComment = function (comment, dir) {
105+
return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
106+
};
107+
108+
// Finds last sourcemap comment in file or returns null if none was found
109+
exports.fromSource = function (content) {
110+
var m = content.match(commentRx);
111+
return m ? exports.fromComment(m.pop()) : null;
112+
};
113+
114+
// Finds last sourcemap comment in file or returns null if none was found
115+
exports.fromMapFileSource = function (content, dir) {
116+
var m = content.match(mapFileCommentRx);
117+
return m ? exports.fromMapFileComment(m.pop(), dir) : null;
118+
};
119+
120+
exports.removeComments = function (src) {
121+
return src.replace(commentRx, '');
122+
};
123+
124+
exports.removeMapFileComments = function (src) {
125+
return src.replace(mapFileCommentRx, '');
126+
};
127+
128+
exports.generateMapFileComment = function (file, options) {
129+
var data = 'sourceMappingURL=' + file;
130+
return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
131+
};
132+
133+
Object.defineProperty(exports, 'commentRegex', {
134+
get: function getCommentRegex () {
135+
return commentRx;
136+
}
137+
});
138+
139+
Object.defineProperty(exports, 'mapFileCommentRegex', {
140+
get: function getMapFileCommentRegex () {
141+
return mapFileCommentRx;
142+
}
143+
});

lib/css-base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function cssWithMappingToString(item) {
5353
if (!cssMapping) {
5454
return content;
5555
}
56-
var convertSourceMap = require('convert-source-map');
56+
var convertSourceMap = require('./convert-source-map');
5757
var sourceMapping = convertSourceMap.fromObject(cssMapping).toComment({multiline: true});
5858
var sourceURLs = cssMapping.sources.map(function (source) {
5959
return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */'

0 commit comments

Comments
 (0)