Skip to content

Commit a8d90ff

Browse files
committed
fix(path): 兼容windows path
1 parent 385748d commit a8d90ff

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

lib/models/Project.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ var Project = function () {
147147
}, {
148148
key: 'fixCss',
149149
value: function fixCss() {
150+
var _this2 = this;
151+
150152
var config = this.config.getConfig(),
151153
entries = config.entry,
152154
cssExtNames = config.entryExtNames.css,
@@ -173,11 +175,13 @@ var Project = function () {
173175

174176
entryItem.forEach(function (cssPath, i) {
175177
var originCssPath = sysPath.join(config.context, cssPath);
176-
fs.appendFileSync(cacheFilePath, 'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");', 'utf-8');
178+
var requiredPath = _this2._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
179+
fs.appendFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
177180
});
178181
} else {
179182
var originCssPath = sysPath.join(config.context, entry);
180-
fs.writeFileSync(cacheFilePath, 'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");', 'utf-8');
183+
var requiredPath = _this2._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
184+
fs.writeFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
181185
}
182186

183187
fps.push(cacheFilePath);
@@ -232,7 +236,7 @@ var Project = function () {
232236
}, {
233237
key: 'pack',
234238
value: function pack(opt, callback) {
235-
var _this2 = this;
239+
var _this3 = this;
236240

237241
var config = this.config.getConfig();
238242

@@ -249,7 +253,7 @@ var Project = function () {
249253
config.output = config.output.dev;
250254
}
251255

252-
_this2.fixCss();
256+
_this3.fixCss();
253257

254258
if (opt.sourcemap) {
255259
config.devtool = opt.sourcemap;
@@ -294,7 +298,7 @@ var Project = function () {
294298
}
295299
});
296300
info();
297-
_this2.packCallbacks.forEach(function (cb) {
301+
_this3.packCallbacks.forEach(function (cb) {
298302
return cb(opt, stats);
299303
});
300304
}
@@ -305,9 +309,9 @@ var Project = function () {
305309

306310
if (opt.lint) {
307311
async.series([function (callback) {
308-
return _this2.lint(callback);
312+
return _this3.lint(callback);
309313
}, function (callback) {
310-
return _this2.lintCss(callback);
314+
return _this3.lintCss(callback);
311315
}], function (err, results) {
312316
if (!err) {
313317
if (results[0] && results[1]) {
@@ -361,6 +365,18 @@ var Project = function () {
361365
return sysPath.resolve(context, lintPathItem);
362366
});
363367
}
368+
}, {
369+
key: '_normalizePath',
370+
value: function _normalizePath(str, stripTrailing) {
371+
if (typeof str !== 'string') {
372+
throw new TypeError('expected a string');
373+
}
374+
str = str.replace(/[\\\/]+/g, '/');
375+
if (stripTrailing !== false) {
376+
str = str.replace(/\/$/, '');
377+
}
378+
return str;
379+
}
364380
}]);
365381

366382
return Project;

src/models/Project.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,22 @@ class Project {
142142
if(Array.isArray(entryItem)) {
143143
// clear
144144
fs.writeFileSync(cacheFilePath, '', 'utf-8');
145-
145+
146146
entryItem.forEach((cssPath, i) => {
147147
const originCssPath = sysPath.join(config.context, cssPath)
148+
const requiredPath = this._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath))
148149
fs.appendFileSync(
149150
cacheFilePath,
150-
'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");',
151+
'require("' + requiredPath + '");',
151152
'utf-8'
152153
);
153154
})
154155
} else {
155156
const originCssPath = sysPath.join(config.context, entry)
157+
const requiredPath = this._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath))
156158
fs.writeFileSync(
157159
cacheFilePath,
158-
'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");',
160+
'require("' + requiredPath + '");',
159161
'utf-8'
160162
);
161163
}
@@ -329,6 +331,17 @@ class Project {
329331
return sysPath.resolve(context, lintPathItem)
330332
})
331333
}
334+
335+
_normalizePath(str, stripTrailing) {
336+
if (typeof str !== 'string') {
337+
throw new TypeError('expected a string');
338+
}
339+
str = str.replace(/[\\\/]+/g, '/');
340+
if (stripTrailing !== false) {
341+
str = str.replace(/\/$/, '');
342+
}
343+
return str;
344+
}
332345
}
333346

334347
module.exports = Project;

0 commit comments

Comments
 (0)