Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const requirejs = require('requirejs');
const amdclean = require('./build/amdclean');
const fs = require("fs");
const Jasmine = require('jasmine');

module.exports = function (grunt) {
function getHeaderText() {
let packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')),
licenseText = '\n\n/*' + fs.readFileSync('./LICENSE.txt', 'utf8') + '\n*/\n\n',
currentDate = (function () {
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth() + 1,
yyyy = today.getFullYear();

if (dd < 10) {
dd = '0' + dd
}

if (mm < 10) {
mm = '0' + mm
}

today = yyyy + '-' + mm + '-' + dd;
return today;
}()),
currentYear = (function () {
var today = new Date(),
yyyy = today.getFullYear();

return yyyy;
}());
return '/*! amdclean - v' + packageJson.version + ' - ' + currentDate +
'\n* https://github.com/gfranko/amdclean' +
'\n* Copyright (c) ' + currentYear + ' Greg Franko */\n' + licenseText;
}
const header = getHeaderText();
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: header,
},
build: {
src: 'src/amdclean.js',
dest: 'build/amdclean.min.js'
}
},
jshint: {
amdclean: {
options: {
"loopfunc": true,
"evil": true,
},
src: ['src/amdclean.js'],
},
},
requirejs: {
"./build/amdclean.optimized.js": {
'findNestedDependencies': false,
'baseUrl': './src/modules/',
'optimize': 'none',
'paths': {
'amdclean': 'index'
},
'include': ['amdclean'],
}
},
amdclean: {
"./build/amdclean.optimized.cleaned.js": {
'filePath': "./build/amdclean.optimized.js",
'transformAMDChecks': false,
'aggressiveOptimizations': true,
'ignoreModules': ['esprima', 'estraverse', 'escodegen', 'lodash', 'fs', 'sourcemap_to_ast'], // wtf? parsed name here?
'removeUseStricts': false,
'wrap': {
// All of the third party dependencies are hoisted here
// It's a hack, but it's not too painful
'start': ';(function(esprima, estraverse, escodegen, _, sourcemapToAst) {\n',
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null, typeof sourcemapToAst !== "undefined" ? sourcemapToAst : null));'
},
'createAnonymousAMDModule': true
}
},
prepend: {
"./src/amdclean.js": {
header: getHeaderText,
src: "./build/amdclean.optimized.cleaned.js",
}
}
});

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
// Default task(s).
grunt.registerTask('build', ['requirejs', 'amdclean', 'prepend:./src/amdclean.js']);
grunt.registerTask('default', ['build', 'jshint:amdclean', 'test']);

grunt.registerTask('lint', ['build', 'jshint:amdclean']);
grunt.registerTask('minify', ['build', 'jshint:amdclean', 'test', 'uglify']);


grunt.registerTask('test', 'Runs Jasmine on the Spec File', function() {
const cb = this.async();
const jasmine = new Jasmine();
jasmine.loadConfig({
spec_dir: 'test/specs',
spec_files: [
'convert.js'
]
});
jasmine.exitOnCompletion = false;
jasmine.execute().then((data)=>{
cb(data.overallStatus === "passed");
});
});

grunt.registerMultiTask('requirejs', 'Uses RequireJS to optimize a file', function () {
const target = this.target;
const data = this.data;
const cb = this.async();
requirejs.optimize({
...data,
out: target,
'onModuleBundleComplete': function (data) {
cb();
}
});
});

grunt.registerMultiTask('amdclean', "Uses AMDClean on a file", function () {
const target = this.target;
const data = this.data;
const code = amdclean.clean({
...data,
});
fs.writeFileSync(target, code);
});
grunt.registerMultiTask('prepend', 'Prepends some text to a file', function() {
const target = this.target;
const data = this.data;
let header = data.header;
if(typeof header === "function") {
header = header();
}
const file = header + fs.readFileSync(data.src);
fs.writeFileSync(target, file);
})
};
42 changes: 21 additions & 21 deletions build/amdclean.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,14 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
});
callbackFunc.body.body = body;
// Returns an array of all return statements
returnStatements = _.where(body, { 'type': 'ReturnStatement' });
exportsExpressions = _.where(body, {
returnStatements = _.filter(body, { 'type': 'ReturnStatement' });
exportsExpressions = _.filter(body, {
'left': {
'type': 'Identifier',
'name': 'exports'
}
});
moduleExportsExpressions = _.where(body, {
moduleExportsExpressions = _.filter(body, {
'left': {
'type': 'MemberExpression',
'object': {
Expand Down Expand Up @@ -647,18 +647,18 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
}(), hasReturnStatement = function () {
var returns = [];
if (callbackFunc && callbackFunc.body && _.isArray(callbackFunc.body.body)) {
returns = _.where(callbackFunc.body.body, { 'type': 'ReturnStatement' });
returns = _.filter(callbackFunc.body.body, { 'type': 'ReturnStatement' });
if (returns.length) {
return true;
}
}
return false;
}(), originalCallbackFuncParams, hasExportsParam = function () {
var cbParams = callbackFunc.params || [];
return _.where(cbParams, { 'name': 'exports' }).length;
return _.filter(cbParams, { 'name': 'exports' }).length;
}(), hasModuleParam = function () {
var cbParams = callbackFunc.params || [];
return _.where(cbParams, { 'name': 'module' }).length;
return _.filter(cbParams, { 'name': 'module' }).length;
}(), normalizeDependencyNames = {}, dependencyNames = function () {
var deps = [], currentName;
_.each(dependencies, function (currentDependency) {
Expand Down Expand Up @@ -709,7 +709,7 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
if (node.id && node.id.name && node.init && node.init['arguments'] && node.init['arguments'][0] && node.init['arguments'][0].value) {
variableName = node.id.name;
expressionName = normalizeModuleName.call(amdclean, utils.normalizeDependencyName(moduleId, node.init['arguments'][0].value, moduleId));
if (!_.contains(ignoreModules, expressionName) && variableName === expressionName) {
if (!_.includes(ignoreModules, expressionName) && variableName === expressionName) {
matchingNames.push({
'originalName': expressionName,
'newName': findNewParamName(expressionName),
Expand Down Expand Up @@ -792,7 +792,7 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
'count': 1
}];
} else {
mappedParameter = _.where(amdclean.callbackParameterMap[dependencyNames[iterator].name], { 'name': currentName });
mappedParameter = _.filter(amdclean.callbackParameterMap[dependencyNames[iterator].name], { 'name': currentName });
if (mappedParameter.length) {
mappedParameter = mappedParameter[0];
mappedParameter.count += 1;
Expand Down Expand Up @@ -852,11 +852,11 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
if (utils.isRequireExpression(node)) {
if (node['arguments'] && node['arguments'][0] && node['arguments'][0].value) {
normalizedModuleName = normalizeModuleName.call(amdclean, utils.normalizeDependencyName(moduleId, node['arguments'][0].value, moduleId));
if (_.contains(ignoreModules, normalizedModuleName)) {
if (_.includes(ignoreModules, normalizedModuleName)) {
return node;
}
if (_.where(matchingRequireExpressionNames, { 'originalName': normalizedModuleName }).length) {
newName = _.where(matchingRequireExpressionNames, { 'originalName': normalizedModuleName })[0].newName;
if (_.filter(matchingRequireExpressionNames, { 'originalName': normalizedModuleName }).length) {
newName = _.filter(matchingRequireExpressionNames, { 'originalName': normalizedModuleName })[0].newName;
}
return {
'type': 'Identifier',
Expand Down Expand Up @@ -898,13 +898,13 @@ convertToObjectDeclaration = function (obj, type) {
modReturnValue = obj.moduleReturnValue;
callee = modReturnValue.callee;
params = callee.params;
if (params && params.length && _.isArray(params) && _.where(params, { 'name': 'global' })) {
if (params && params.length && _.isArray(params) && _.filter(params, { 'name': 'global' })) {
if (_.isObject(callee.body) && _.isArray(callee.body.body)) {
returnStatement = _.where(callee.body.body, { 'type': 'ReturnStatement' })[0];
returnStatement = _.filter(callee.body.body, { 'type': 'ReturnStatement' })[0];
if (_.isObject(returnStatement) && _.isObject(returnStatement.argument) && returnStatement.argument.type === 'FunctionExpression') {
internalFunctionExpression = returnStatement.argument;
if (_.isObject(internalFunctionExpression.body) && _.isArray(internalFunctionExpression.body.body)) {
nestedReturnStatement = _.where(internalFunctionExpression.body.body, { 'type': 'ReturnStatement' })[0];
nestedReturnStatement = _.filter(internalFunctionExpression.body.body, { 'type': 'ReturnStatement' })[0];
if (_.isObject(nestedReturnStatement.argument) && _.isObject(nestedReturnStatement.argument.right) && _.isObject(nestedReturnStatement.argument.right.property)) {
if (nestedReturnStatement.argument.right.property.name) {
modReturnValue = {
Expand Down Expand Up @@ -1032,7 +1032,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
} else {
deps = [];
}
hasExportsParam = _.where(deps, { 'value': 'exports' }).length;
hasExportsParam = _.filter(deps, { 'value': 'exports' }).length;
if (_.isArray(deps) && deps.length) {
_.each(deps, function (currentDependency) {
if (dependencyBlacklist[currentDependency.value] && !shouldOptimize) {
Expand Down Expand Up @@ -1072,7 +1072,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
amdclean.options.ignoreModules.push(moduleName);
return node;
}
if (_.contains(options.removeModules, moduleName)) {
if (_.includes(options.removeModules, moduleName)) {
// Remove the current module from the source
return { 'type': 'EmptyStatement' };
}
Expand All @@ -1089,7 +1089,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
} else if (params.moduleReturnValue && params.moduleReturnValue.type === 'Identifier') {
type = 'functionExpression';
}
if (_.contains(options.ignoreModules, moduleName)) {
if (_.includes(options.ignoreModules, moduleName)) {
return node;
} else if (utils.isFunctionExpression(moduleReturnValue) || type === 'functionExpression') {
return convertToFunctionExpression.call(amdclean, params);
Expand All @@ -1116,16 +1116,16 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
}
} else {
// If the node is a function expression that has an exports parameter and does not return anything, return exports
if (node.type === 'FunctionExpression' && _.isArray(node.params) && _.where(node.params, {
if (node.type === 'FunctionExpression' && _.isArray(node.params) && _.filter(node.params, {
'type': 'Identifier',
'name': 'exports'
}).length && _.isObject(node.body) && _.isArray(node.body.body) && !_.where(node.body.body, { 'type': 'ReturnStatement' }).length) {
}).length && _.isObject(node.body) && _.isArray(node.body.body) && !_.filter(node.body.body, { 'type': 'ReturnStatement' }).length) {
parentHasFunctionExpressionArgument = function () {
if (!parent || !parent.arguments) {
return false;
}
if (parent && parent.arguments && parent.arguments.length) {
return _.where(parent.arguments, { 'type': 'FunctionExpression' }).length;
return _.filter(parent.arguments, { 'type': 'FunctionExpression' }).length;
}
return false;
}();
Expand Down Expand Up @@ -1432,7 +1432,7 @@ clean = function clean() {
}, {})), hoistedCallbackParameters);
// Creates variable declarations for each AMD module/callback parameter that needs to be hoisted
_.each(hoistedVariables, function (moduleValue, moduleName) {
if (!_.contains(options.ignoreModules, moduleName)) {
if (!_.includes(options.ignoreModules, moduleName)) {
var _initValue = amdclean.exportsModules[moduleName] !== true ? null : {
type: 'ObjectExpression',
properties: []
Expand Down
10 changes: 5 additions & 5 deletions build/amdclean.min.js

Large diffs are not rendered by default.

Loading