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

Add keepAlias options #79

Open
wants to merge 19 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
94 changes: 77 additions & 17 deletions tasks/lib/script.js
Expand Up @@ -35,19 +35,25 @@ exports.init = function(grunt) {
depsSpecified = true;
grunt.log.verbose.writeln('dependencies exists in "' + fileObj.src + '"');
} else {
deps = parseDependencies(fileObj.src, options);
deps = parseDependencies(fileObj.src, fileObj.name, options);
grunt.log.verbose.writeln(deps.length ?
'found dependencies ' + deps : 'found no dependencies');
}

function replaceRequire(v) {
if (typeof options.keepAlias == 'string') {
return getMinAlias(v, options, fileObj.name);
} else {
// ignore when deps is specified by developer
return depsSpecified || options.keepAlias === true ? v : iduri.parseAlias(options, v);
}
}
// create .js file
astCache = ast.modify(astCache, {
id: meta.id ? meta.id : unixy(options.idleading + fileObj.name.replace(/\.js$/, '')),
id: getMinAlias(meta.id ? meta.id : unixy(options.idleading + fileObj.name.replace(/\.js$/, '')), options, fileObj.name),
dependencies: deps,
require: function(v) {
// ignore when deps is specified by developer
return depsSpecified ? v : iduri.parseAlias(options, v);
}
require: replaceRequire,
async: replaceRequire
});
data = astCache.print_to_string(options.uglify);
grunt.file.write(fileObj.dest, addOuterBoxClass(data, options));
Expand Down Expand Up @@ -95,7 +101,7 @@ exports.init = function(grunt) {
return data;
}

function moduleDependencies(id, options) {
function moduleDependencies(id, options, fromfile) {
var alias = iduri.parseAlias(options, id);

if (iduri.isAlias(options, id) && alias === id) {
Expand All @@ -118,18 +124,18 @@ exports.init = function(grunt) {
options.paths.some(function(base) {
var filepath = path.join(base, file);
if (grunt.file.exists(filepath)) {
grunt.log.verbose.writeln('find module "' + filepath + '"');
grunt.log.verbose.writeln('find module "' + filepath + '", from: '+fromfile);
fpath = filepath;
return true;
}
});

if (!fpath) {
grunt.fail.warn("can't find module " + alias);
grunt.fail.warn("can't find module " + alias + ', from: '+fromfile);
return [];
}
if (!grunt.file.exists(fpath)) {
grunt.fail.warn("can't find " + fpath);
grunt.fail.warn("can't find " + fpath + ', from: '+fromfile);
return [];
}
var data = grunt.file.read(fpath);
Expand All @@ -144,14 +150,68 @@ exports.init = function(grunt) {
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);
deps.push(getMinAlias(dep, options));
}
});
});
return deps;
}

function parseDependencies(fpath, options) {
function getMinAlias(alias, options, fromfile) {
options.__minAliasIndex__ || (options.__minAliasIndex__ = 14);
var minAlias = options.__minAlias__ || (options.__minAlias__ = {});
var newAlias = options.__newAlias__ || (options.__newAlias__ = {});
var usedAlias = options.__usedAlias__ || (options.__usedAlias__ = {});

if (typeof options.keepAlias == 'string') {
alias = iduri.parseAlias(options, alias);

if (alias.charAt(0) == '.') {
alias = path.join(path.dirname(fromfile), alias);
}

// ignore protocol & //example.com/xxxx
if (alias.indexOf(':') == -1 || alias.indexOf('//') === 0) alias = path.normalize(alias).replace(/\\/g, '/');

if (minAlias[alias]) {
usedAlias[minAlias[alias]] = alias;
return minAlias[alias];
}

var uin;
do {
uin = cutInt(options.__minAliasIndex__++);
} while(options.alias[uin]);


options.alias[uin] = alias;
minAlias[alias] = uin;
newAlias[uin] = alias;
usedAlias[uin] = alias;

return uin;
}

return alias;
}
var cutInt = (function(chars) {
var radix = chars.length;

return function (num) {
var out = [];
var mod;

do {
mod = num % radix;
num = (num - mod) / radix;
out.push(chars[mod]);
} while (num);

return out.join('');
};
})('-0123456789=+$&%ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_');

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

function relativeDependencies(fpath, options, basefile) {
Expand All @@ -165,7 +225,7 @@ exports.init = function(grunt) {

if (!grunt.file.exists(fpath)) {
if (!/\{\w+\}/.test(fpath)) {
grunt.log.warn("can't find " + fpath);
grunt.log.warn("can't find " + fpath + ', basefile: '+basefile);
}
return [];
}
Expand Down Expand Up @@ -195,22 +255,22 @@ exports.init = function(grunt) {
if (altId.charAt(0) !== '.') {
altId = './' + altId;
}
deps.push(altId);
deps.push(getMinAlias(altId, options, fname));
} else {
deps.push(id);
deps.push(getMinAlias(id, options, fname));
}
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);
deps.push(getMinAlias(typeof options.keepAlias === true ? id : alias, options, fname));

// don't parse no javascript dependencies
var ext = path.extname(alias);
if (ext && ext !== '.js') return;

var mdeps = moduleDependencies(id, options);
var mdeps = moduleDependencies(id, options, fpath);
moduleDeps[id] = mdeps;
deps = grunt.util._.union(deps, mdeps);
}
Expand Down
48 changes: 48 additions & 0 deletions tasks/transport.js
Expand Up @@ -23,6 +23,7 @@ module.exports = function(grunt) {

idleading: '',
alias: {},
keepAlias: false,

// create a debug file or not
debug: true,
Expand Down Expand Up @@ -57,10 +58,42 @@ module.exports = function(grunt) {
styleBox: false
});

var customAliasKeys = Object.keys(options.alias);
if (typeof options.keepAlias == 'string') {
options.__usedAlias__ = {};
options.__newAlias__ = {};
if (grunt.file.exists(options.keepAlias)) {
var tmpAllMinAlias = grunt.file.readJSON(options.keepAlias).minAll;
var tmpAllMinAliasF = options.__minAlias__ = {};
grunt.util._.keys(tmpAllMinAlias).forEach(function(key) {
tmpAllMinAliasF[tmpAllMinAlias[key]] = key;
});
options.alias = grunt.util._.extend({}, tmpAllMinAlias, options.alias);
}
}

if (options.process === true) {
options.process = {};
}

// parse alias for normal id
for(var i in options.alias)
{
options.paths.some(function(base) {
var filepath = path.join(base, options.alias[i]);
var extname = path.extname(filepath);
if (!extname) {
filepath += '.js';
}
if (grunt.file.exists(filepath)) {
filepath = path.relative(base, filepath).replace(/\\/g, '/');
grunt.log.verbose.writeln('alias find module "' + filepath + '"');
options.alias[i] = extname ? filepath : filepath.replace(/\.js$/, '');
return true;
}
});
}

var count = 0;
this.files.forEach(function(fileObj) {
// cwd shouldn't exist after normalize path
Expand Down Expand Up @@ -94,5 +127,20 @@ module.exports = function(grunt) {
count++;
});
grunt.log.writeln('transport ' + count.toString().cyan + ' files');

if (typeof options.keepAlias == 'string') {
var tmpAlias = grunt.util._.extend({}, options.alias);
// It's safly to delete custom alias
customAliasKeys.forEach(function(alias) {
delete tmpAlias[alias];
});
grunt.file.write(options.keepAlias, JSON.stringify({
all: options.alias,
minAll: tmpAlias,
last: options.__usedAlias__,
'new': options.__newAlias__
}, null, '\t'));
grunt.log.writeln('transport write minAlias file: ' + options.keepAlias);
}
});
};