Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提高transport解析的速度 #89

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gruntfile.js
Expand Up @@ -30,6 +30,14 @@ module.exports = function(grunt) {
},

transport: {
/*
options: {
quickMode: {
baseUrl: 'test/cases/assets'
}
},
*/

expand: {
files: [{
expand: true,
Expand Down
107 changes: 89 additions & 18 deletions tasks/lib/script.js
Expand Up @@ -4,6 +4,7 @@ exports.init = function(grunt) {
var iduri = require('cmd-util').iduri;
var _ = grunt.util._;

var GLOBAL_PARSED_FILE_LIST = {};

var exports = {};

Expand Down Expand Up @@ -41,8 +42,17 @@ exports.init = function(grunt) {
}

// create .js file
var id;
if (!options.quickMode) {
id = meta.id ? meta.id : unixy(options.idleading + fileObj.name.replace(/\.js$/, ''));
} else {
// 快速模式 只储存绝对路径
id = unixy(options.idleading + fileObj.name.replace(/\.js$/, ''));
id = path.join(options.quickMode.baseUrl, id);
}

astCache = ast.modify(astCache, {
id: meta.id ? meta.id : unixy(options.idleading + fileObj.name.replace(/\.js$/, '')),
id: id,
dependencies: deps,
require: function(v) {
// ignore when deps is specified by developer
Expand Down Expand Up @@ -142,26 +152,53 @@ exports.init = function(grunt) {

parsed.forEach(function(meta) {
meta.dependencies.forEach(function(dep) {
dep = iduri.absolute(alias, dep);
if (!_.contains(deps, dep) && !_.contains(ids, dep) && !_.contains(ids, dep.replace(/\.js$/, ''))) {
deps.push(dep);
if (!options.quickMode) {
dep = iduri.absolute(alias, dep);
if (!_.contains(deps, dep) && !_.contains(ids, dep) && !_.contains(ids, dep.replace(/\.js$/, ''))) {
deps.push(dep);
}
} else {
if (dep.charAt(0) === '.') {
dep = path.join(fpath, dep);
} else {
var alias = iduri.parseAlias(options, dep);
var filename = iduri.appendext(alias);
var mpath;
options.paths.some(function(base) {
var filepath = path.join(base, filename);
if (grunt.file.exists(filepath)) {
grunt.log.verbose.writeln('find module "' + filepath + '"');
mpath = filepath;
return true;
}
});
dep = mpath;
}
if (dep && !_.contains(deps, dep) && !_.contains(ids, dep) && !_.contains(ids, dep.replace(/\.js$/, ''))) {
deps.push(dep);
}
}
});
});

return deps;
}

function parseDependencies(fpath, options) {
var rootpath = fpath;
var moduleDeps = {};

function relativeDependencies(fpath, options, basefile) {
if (basefile) {
fpath = path.join(path.dirname(basefile), fpath);
}
fpath = iduri.appendext(fpath);

if (GLOBAL_PARSED_FILE_LIST[fpath]) {
return GLOBAL_PARSED_FILE_LIST[fpath];
}

var deps = [];
var moduleDeps = {};

if (!grunt.file.exists(fpath)) {
if (!/\{\w+\}/.test(fpath)) {
Expand All @@ -183,28 +220,53 @@ exports.init = function(grunt) {
if (id.charAt(0) === '.') {
// fix nested relative dependencies
if (basefile) {
var altId = path.join(path.dirname(fpath), id).replace(/\\/g, '/');
var dirname = path.dirname(rootpath).replace(/\\/g, '/');
if ( dirname !== altId ) {
altId = path.relative(dirname, altId);
var altId = path.join(path.dirname(fpath), id);
if (options.quickMode) {
deps.push(altId);
} else {
// the same name between file and directory
altId = path.relative(dirname, altId + '.js').replace(/\.js$/, '');
}
altId = altId.replace(/\\/g, '/');
if (altId.charAt(0) !== '.') {
altId = './' + altId;
var dirname = path.dirname(rootpath);
if ( dirname !== altId ) {
altId = path.relative(dirname, altId);
} else {
// the same name between file and directory
altId = path.relative(dirname, altId + '.js').replace(/\.js$/, '');
}
altId = altId.replace(/\\/g, '/');
if (altId.charAt(0) !== '.') {
altId = './' + altId;
}
deps.push(altId);
}
deps.push(altId);
} else {
deps.push(id);
if (options.quickMode) {
deps.push(path.join(path.dirname(rootpath), id));
} else {
deps.push(id);
}
}
if (/\.js$/.test(iduri.appendext(id))) {
deps = grunt.util._.union(deps, relativeDependencies(id, options, fpath));
}
} else if (!moduleDeps[id]) {
var alias = iduri.parseAlias(options, id);
deps.push(alias);

if (options.quickMode) {
var filename = iduri.appendext(alias);
var mpath;
options.paths.some(function(base) {
var filepath = path.join(base, filename);
if (grunt.file.exists(filepath)) {
grunt.log.verbose.writeln('find module "' + filepath + '"');
mpath = path.join(base, alias);
return true;
}
});
if (mpath) {
deps.push(mpath);
}
} else {
deps.push(alias);
}

// don't parse no javascript dependencies
var ext = path.extname(alias);
Expand All @@ -213,8 +275,17 @@ exports.init = function(grunt) {
var mdeps = moduleDependencies(id, options);
moduleDeps[id] = mdeps;
deps = grunt.util._.union(deps, mdeps);
} else {
if (moduleDeps[id]) {
console.log(moduleDeps[id])
}
}
});

if (options.quickMode) {
GLOBAL_PARSED_FILE_LIST[fpath] = deps;
}

return deps;
}

Expand Down