Skip to content

Commit 21a372d

Browse files
committed
fix: 修复 windows 下入口路径匹配问题
1 parent 9bafc26 commit 21a372d

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

lib/commands/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var connect = require('connect'),
1515

1616
var Manager = require('../modules/manager.js');
1717
var UtilFs = require('../utils/fs.js');
18+
var UtilPath = require('../utils/path.js');
1819

1920
exports.usage = '开发服务';
2021
exports.abbr = 's';
@@ -196,9 +197,10 @@ exports.run = function (options) {
196197

197198
// 将入口的 .scss/.less 后缀替换为.css
198199
var cssReg = new RegExp('\\' + config.entryExtNames.css.join('|\\'));
199-
entryPath = entryPath.replace(cssReg, '.css');
200+
entryPath = UtilPath.normalize(entryPath.replace(cssReg, '.css'));
200201

201202
// 如果是 ykit 处理过的样式文件,将其变为正常的请求路径(../.ykit_cache/main/index.css.js => main/index.css)
203+
202204
if (entryPath.indexOf('.css.js') && entryPath.indexOf('.ykit_cache/') > 1) {
203205
entryPath = entryPath.split('.ykit_cache/')[1].replace('.css.js', '.css');
204206
}

lib/models/Project.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Config = require('./Config.js'),
1717
ExtractTextPlugin = require('extract-text-webpack-plugin');
1818

1919
var UtilFs = require('../utils/fs.js');
20+
var UtilPath = require('../utils/path.js');
2021

2122
var ENVS = {
2223
LOCAL: 'local',
@@ -196,8 +197,6 @@ var Project = function () {
196197
}, {
197198
key: 'fixCss',
198199
value: function fixCss() {
199-
var _this2 = this;
200-
201200
var config = this.config.getConfig(),
202201
entries = config.entry,
203202
cssExtNames = config.entryExtNames.css,
@@ -233,12 +232,12 @@ var Project = function () {
233232

234233
entryItem.forEach(function (cssPath) {
235234
var originCssPath = sysPath.join(config.context, cssPath);
236-
var requiredPath = _this2._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
235+
var requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
237236
fs.appendFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
238237
});
239238
} else {
240239
var originCssPath = sysPath.join(config.context, entry);
241-
var requiredPath = _this2._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
240+
var requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
242241
fs.writeFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
243242
}
244243

@@ -251,7 +250,7 @@ var Project = function () {
251250
}, {
252251
key: 'lint',
253252
value: function lint(dir, callback) {
254-
var _this3 = this;
253+
var _this2 = this;
255254

256255
warn('Linting JS Files ...');
257256

@@ -263,7 +262,7 @@ var Project = function () {
263262
}
264263

265264
var files = ['.js', '.yaml', '.yml', '.json', ''].map(function (ext) {
266-
return path.join(_this3.cwd, '.eslintrc' + ext);
265+
return path.join(_this2.cwd, '.eslintrc' + ext);
267266
});
268267
var config = UtilFs.readFileAny(files);
269268

@@ -288,7 +287,7 @@ var Project = function () {
288287
}, {
289288
key: 'pack',
290289
value: function pack(opt, callback) {
291-
var _this4 = this;
290+
var _this3 = this;
292291

293292
var config = this.config.getConfig();
294293
UtilFs.deleteFolderRecursive(this.cachePath);
@@ -322,7 +321,7 @@ var Project = function () {
322321
config.output = config.output.dev;
323322
}
324323

325-
_this4.fixCss();
324+
_this3.fixCss();
326325

327326
if (opt.clean) {
328327
try {
@@ -369,7 +368,7 @@ var Project = function () {
369368
}
370369
});
371370
info();
372-
_this4.packCallbacks.forEach(function (cb) {
371+
_this3.packCallbacks.forEach(function (cb) {
373372
return cb(opt, stats);
374373
});
375374
}
@@ -380,7 +379,7 @@ var Project = function () {
380379

381380
if (opt.lint) {
382381
async.series([function (callback) {
383-
return _this4.lint(callback);
382+
return _this3.lint(callback);
384383
}], function (err, results) {
385384
if (!err) {
386385
if (results[0] && results[1]) {
@@ -440,18 +439,6 @@ var Project = function () {
440439
return sysPath.resolve(context, lintPathItem);
441440
});
442441
}
443-
}, {
444-
key: '_normalizePath',
445-
value: function _normalizePath(str, stripTrailing) {
446-
if (typeof str !== 'string') {
447-
throw new TypeError('expected a string');
448-
}
449-
str = str.replace(/[\\\/]+/g, '/');
450-
if (stripTrailing !== false) {
451-
str = str.replace(/\/$/, '');
452-
}
453-
return str;
454-
}
455442
}, {
456443
key: '_requireUncached',
457444
value: function _requireUncached(module) {

lib/utils/path.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
exports.normalize = function (str, stripTrailing) {
4+
if (typeof str !== 'string') {
5+
throw new TypeError('expected a string');
6+
}
7+
str = str.replace(/[\\\/]+/g, '/');
8+
if (stripTrailing !== false) {
9+
str = str.replace(/\/$/, '');
10+
}
11+
return str;
12+
};

src/commands/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let connect = require('connect'),
1313

1414
let Manager = require('../modules/manager.js');
1515
let UtilFs = require('../utils/fs.js');
16+
let UtilPath = require('../utils/path.js');
1617

1718
exports.usage = '开发服务';
1819
exports.abbr = 's';
@@ -191,9 +192,10 @@ exports.run = (options) => {
191192

192193
// 将入口的 .scss/.less 后缀替换为.css
193194
const cssReg = new RegExp('\\' + config.entryExtNames.css.join('|\\'));
194-
entryPath = entryPath.replace(cssReg, '.css');
195+
entryPath = UtilPath.normalize(entryPath.replace(cssReg, '.css'));
195196

196197
// 如果是 ykit 处理过的样式文件,将其变为正常的请求路径(../.ykit_cache/main/index.css.js => main/index.css)
198+
197199
if(entryPath.indexOf('.css.js') && entryPath.indexOf('.ykit_cache/') > 1) {
198200
entryPath = entryPath.split('.ykit_cache/')[1].replace('.css.js', '.css');
199201
}

src/models/Project.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let Config = require('./Config.js'),
1111
ExtractTextPlugin = require('extract-text-webpack-plugin');
1212

1313
let UtilFs = require('../utils/fs.js');
14+
let UtilPath = require('../utils/path.js');
1415

1516
const ENVS = {
1617
LOCAL: 'local',
@@ -207,7 +208,7 @@ class Project {
207208

208209
entryItem.forEach((cssPath) => {
209210
const originCssPath = sysPath.join(config.context, cssPath);
210-
const requiredPath = this._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
211+
const requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
211212
fs.appendFileSync(
212213
cacheFilePath,
213214
'require("' + requiredPath + '");',
@@ -216,7 +217,7 @@ class Project {
216217
});
217218
} else {
218219
const originCssPath = sysPath.join(config.context, entry);
219-
const requiredPath = this._normalizePath(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
220+
const requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
220221
fs.writeFileSync(
221222
cacheFilePath,
222223
'require("' + requiredPath + '");',
@@ -416,17 +417,6 @@ class Project {
416417
});
417418
}
418419

419-
_normalizePath(str, stripTrailing) {
420-
if (typeof str !== 'string') {
421-
throw new TypeError('expected a string');
422-
}
423-
str = str.replace(/[\\\/]+/g, '/');
424-
if (stripTrailing !== false) {
425-
str = str.replace(/\/$/, '');
426-
}
427-
return str;
428-
}
429-
430420
_requireUncached(module) {
431421
delete require.cache[require.resolve(module)];
432422
return require(module);

src/utils/path.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
exports.normalize = function(str, stripTrailing){
4+
if (typeof str !== 'string') {
5+
throw new TypeError('expected a string');
6+
}
7+
str = str.replace(/[\\\/]+/g, '/');
8+
if (stripTrailing !== false) {
9+
str = str.replace(/\/$/, '');
10+
}
11+
return str;
12+
};

0 commit comments

Comments
 (0)