Skip to content

Commit

Permalink
improve jsParser
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jul 15, 2014
1 parent 9ad3afa commit aebc1dd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 78 deletions.
30 changes: 2 additions & 28 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ var isRelative = util.isRelative;
var hideExt = util.hideExt;
var addExt = util.addExt;
var rename = util.rename;
var winPath = util.winPath;

/*
exports
*/

exports.transportId = transportId;
exports.transportDeps = transportDeps;
exports.generateId = generateId;
exports.generateDeps = generateDeps;
exports.getFileInfo = getFileInfo;
exports.createStream = createStream;
exports.getStyleId = getStyleId;
Expand Down Expand Up @@ -63,7 +62,7 @@ function transportId(filepath, pkg, options) {
filepath += '.js';
}

var id = join(prefix, filepath).replace(/\\/g, '/');
var id = winPath(join(prefix, filepath));
debug('transport id(%s) of pakcage %s', id, pkg.id);
return id;
}
Expand Down Expand Up @@ -135,31 +134,6 @@ function transportDeps(filepath, pkg, options) {
}
}

/*
Generate cmd id from vinyl object
required options: pkg, idleading, rename
*/

function generateId(file, options) {
var fInfo = getFileInfo(file, options.pkg);
return transportId(fInfo.filepath, fInfo.pkg, options);
}

/*
Generate cmd dependency from vinyl object
required options: pkg, idleading, rename, ignore
*/

function generateDeps(file, options) {
var fInfo = getFileInfo(file, options.pkg);
return transportDeps(fInfo.filepath, fInfo.pkg, options)
.map(function(item) {
return '"' + item + '"';
}).join(',');
}

/*
Get filepath and pkg from vinyl object, attempt to find
from dependent package if current package don't match.
Expand Down
54 changes: 28 additions & 26 deletions lib/plugin/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ var requires = require('searequire');
var util = require('../util');
var rename = util.rename;
var template = util.template;
var isRelative = util.isRelative;
var hideExt = util.hideExt;
var resolvePath = util.resolvePath;
var common = require('../common');
var generateId = common.generateId;
var generateDeps = common.generateDeps;
var createStream = common.createStream;
var transportDeps = common.transportDeps;
var transportId = common.transportId;
var getFileInfo = common.getFileInfo;
var getStyleId = common.getStyleId;
var debug = require('debug')('transport:js');

module.exports = transport;
transport.wrap = wrap;
transport.replace = replace;
module.exports = jsParser;

function transport(options) {
function jsParser(options) {
return createStream(options, 'js', parser);
}

Expand All @@ -27,8 +27,8 @@ var footerTpl = '\n});\n';

function parser(file, options) {
debug('filepath:%s', file.path);
file.contents = replace(file, options);
file.contents = wrap(file, options);

file.contents = new Buffer(transport(file, options));

//replace filename with suffix
file.originPath = file.originPath || file.path;
Expand All @@ -37,33 +37,29 @@ function parser(file, options) {
return file;
}

function wrap(file, options) {
var id = generateId(file, options);
var deps = generateDeps(file, options);
function transport(file, options) {
var code = file.contents.toString();
var fInfo = getFileInfo(file, options.pkg);
var id = transportId(fInfo.filepath, fInfo.pkg, options);
var deps = transportDeps(fInfo.filepath, fInfo.pkg, options);

return Buffer.concat([
new Buffer(util.template(headerTpl, {id: id, deps: deps})),
file.contents,
getStyleBox(file, options),
new Buffer(footerTpl)
]);
return template(headerTpl, {id: id, deps: arr2str(deps)}) +
replace(code, fInfo, options) +
getStyleBox(file, options) +
footerTpl;
}

function replace(file, options) {
var fInfo = getFileInfo(file, options.pkg);
var code = file.contents.toString();
code = requires(code, function(require) {
function replace(code, fInfo, options) {
return requires(code, function(require) {
var id = replaceId(require.path, fInfo.pkg, options, fInfo.filepath);
return template('require("{{id}}")', {id: id});
});
return new Buffer(code);
}

function replaceId(id, pkg, options, base) {
id = util.hideExt(id);

if (util.isRelative(id)) {
var file = util.resolvePath(id, base);
if (isRelative(id)) {
id = hideExt(id);
var file = resolvePath(id, base);
file = findFile(file, pkg.files);
return transportId(file, pkg, options);
}
Expand Down Expand Up @@ -107,3 +103,9 @@ function findFile(file, files) {
}
return file;
}

function arr2str(arr) {
return arr.map(function(item) {
return '"' + item + '"';
}).join(',');
}
1 change: 1 addition & 0 deletions make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
call node_modules/.bin/mocha -R spec -t 20000
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"istanbul": "^0.2.6",
"mocha": "^1.17.1",
"should": "^3.1.3",
"utility": "*"
"utility": "*",
"totoro": "1"
},
"repository": {
"type": "git",
Expand Down
23 changes: 0 additions & 23 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,6 @@ describe('Common', function() {

});


it('generateId', function() {
var pkg = getPackage('js-require-css');
var fakePath = join(base, 'js-require-css/sea-modules/b/1.0.0/index.css');
var fakeFile = new gutil.File({
contents: fs.readFileSync(fakePath),
path: fakePath
});
common.generateId(fakeFile, {pkg: pkg})
.should.eql('b/1.0.0/index.css.js');
});

it('generateDeps', function() {
var pkg = getPackage('js-require-css');
var fakePath = join(base, 'js-require-css/index.js');
var fakeFile = new gutil.File({
contents: fs.readFileSync(fakePath),
path: fakePath
});
common.generateDeps(fakeFile, {pkg: pkg})
.should.eql('"d/1.0.0/index","import-style/1.0.0/index"');
});

describe('getFileInfo', function() {

it('getFileInfo', function() {
Expand Down

0 comments on commit aebc1dd

Please sign in to comment.