Skip to content

Commit

Permalink
upgrade acorn and added eslint (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajaydev authored May 14, 2020
1 parent fb228b0 commit 209c8c2
Show file tree
Hide file tree
Showing 10 changed files with 1,971 additions and 67 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": [
"eslint-config-ajay"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
}
}
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# lasso-unpack
[![npm version](https://badge.fury.io/js/lasso-unpack.svg)](https://www.npmjs.com/package/eslint-config-ajay)
[![Build Status](https://travis-ci.org/pajaydev/lasso-unpack.svg?branch=master)](https://www.npmjs.com/package/lasso-unpack)

plugin to create manifest from the bundle created by Lasso.

Usage
Expand Down
7 changes: 4 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ if (input.length > 0) {
input.map((fileName) => {
lassoUnpack(fileName);
});
} else {
console.log("No input provided\nUsage: lasso-unpack < bundle.js >");
return process.exit(1);
}
else {
console.log('No input provided\nUsage: lasso-unpack < bundle.js >');
process.exit(1);
}
20 changes: 0 additions & 20 deletions lib/build.js

This file was deleted.

33 changes: 16 additions & 17 deletions lib/lasso-unpack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const acorn = require('acorn');
const fs = require('fs');
const path = require('path');
const walk = require('acorn/dist/walk');
const walk = require('acorn-walk');
const gzip = require('gzip-size');
const brotli = require('brotli-size');
const Stats = require('./stats');
Expand All @@ -26,7 +27,7 @@ function parseLassoBundle(fileName) {
});
let initial = new Stats();
const walkState = [];
if (ast.body.length === 0) return "Empty File";
if (ast.body.length === 0) return 'Empty File';
// get total bundle size.
isProgram(ast, walkState, initial, fileContent);
// iterate the AST tree.
Expand All @@ -38,55 +39,53 @@ function parseLassoBundle(fileName) {
const stats = new Stats();
if (node.start && node.end) {
stats.setSize(node.end - node.start);
} else {
}
else {
stats.setSize(0);
}
if (isFunctionExpression(node)) {
stats.setPackageName("module.js");
stats.setFileName("module.js");
stats.setPath("/module.js");
stats.setPackageName('module.js');
stats.setFileName('module.js');
stats.setPath('/module.js');
}
if (isMemberExpression(node)) {
let memberNode = node.callee;
if (memberNode.property && memberNode.property.type === "Identifier") {
if (memberNode.property && memberNode.property.type === 'Identifier') {
stats.setType(memberNode.property.name);
}
}
const args = getArguments(node);
if (args.length > 0) {
extractLiterals(stats, args);
}
if (stats.getType() === "def") {
if (stats.getType() === 'def') {
extractContent(fileContent, stats, args);
}
walkState.push(stats);
},
}
});
const manifestPath = process.cwd();
fs.writeFileSync(manifestPath + '/lasso-stats.json', JSON.stringify(walkState, null, 2));
return { walkState };
};
let options = {
input: 'lib'
}

// extract literal from AST tree.
function extractLiterals(stats, args) {
if (stats.getType() != null && (stats.getType() === "installed" || stats.getType() === "builtin")) {
if (stats.getType() !== null && (stats.getType() === 'installed' || stats.getType() === 'builtin')) {
extractLiteralFromInstalled(stats, args);
}

if (stats.getType() != null && stats.getType() === "def") {
if (stats.getType() !== null && stats.getType() === 'def') {
extractLiteralFromDef(stats, args[0]);
}

if (stats.getType() != null && (stats.getType() === "main" || stats.getType() === "remap")) {
if (stats.getType() !== null && (stats.getType() === 'main' || stats.getType() === 'remap')) {
extractLiteralFromMain(stats, args[0]);
}
}

function isProgram(ast, walkState, initial, fileContent) {
if (ast.type === "Program") {
if (ast.type === 'Program') {
initial.setPackageName('BundleSize');
initial.setFileName('program');
initial.setSize(ast.end - ast.start);
Expand All @@ -111,4 +110,4 @@ function extractContent(fileContent, stats, node) {
}
}

module.exports = parseLassoBundle;
module.exports = parseLassoBundle;
2 changes: 1 addition & 1 deletion lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ class Stats {
}
}

module.exports = Stats;
module.exports = Stats;
30 changes: 11 additions & 19 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ function getArguments(node) {
// Extract packageName, version and fileName from given literal if type = "installed".
function extractLiteralFromInstalled(stats, element) {
if (element.length === 0) return stats;
let value = "";
let value = '';
if (validLiteral(element[0])) {
value = element[0].value;
// let array = value.split('$');
// if (array.length == 0) {
// stats.setPackageName(value);
// } else {
// stats.setPackageName(array[0]);
// stats.setPackageVersion(array[1]);
// }
stats.setPackageName(value);
}
if (validLiteral(element[1])) {
Expand All @@ -42,7 +35,7 @@ function extractLiteralFromInstalled(stats, element) {

// check Literal is valid or not.
function validLiteral(node) {
if (node && node.type === "Literal" && typeof node.value === 'string') {
if (node && node.type === 'Literal' && typeof node.value === 'string') {
return true;
}
return false;
Expand All @@ -51,7 +44,7 @@ function validLiteral(node) {
function validIndex(array) {

for (let i = 0; i < array.length; i++) {
if (array[i].includes("$")) {
if (array[i].includes('$')) {
return i;
break;
}
Expand All @@ -67,24 +60,24 @@ function extractLiteralFromDef(stats, element) {
let length = arrayLiteral.length;
if (length > 0) {
const index = validIndex(arrayLiteral);
stats.setPackageName(arrayLiteral[index].split("$")[0]);
stats.setVersion(arrayLiteral[index].split("$")[1] || '');
stats.setPackageName(arrayLiteral[index].split('$')[0]);
stats.setVersion(arrayLiteral[index].split('$')[1] || '');
stats.setFileName(arrayLiteral.splice(index + 1, length).join('/'));
}
}
return stats;
}

function extractLiteralFromMain(stats, element) {
if (element.value && element.value != '') {
if (element.value && element.value !== '') {
stats.setPath(element.value);
let arrayOfValues = element.value.split('/');
if (arrayOfValues.length > 0) {
let packageName = arrayOfValues[0] == '' ? arrayOfValues[1] : arrayOfValues[0];
let packageName = arrayOfValues[0] === '' ? arrayOfValues[1] : arrayOfValues[0];
// stats.setPackageName(packageName);
let fileName = element.value.replace(packageName + '/', '');
stats.setPackageName(packageName.split("$")[0]);
stats.setVersion(packageName.split("$")[1] || '');
stats.setPackageName(packageName.split('$')[0]);
stats.setVersion(packageName.split('$')[1] || '');
stats.setFileName(fileName);
}
}
Expand All @@ -93,15 +86,14 @@ function extractLiteralFromMain(stats, element) {
}

function extractLiteralFromBuiltin(stats, packageName, fileName) {

stats.setPackageName(packageName);
stats.setFileName(fileName);
return stats;
}

// check function expression contains body or not.
function isValidFunctionExpression(node) {
if (node && node.type === "FunctionExpression" && node.body) {
if (node && node.type === 'FunctionExpression' && node.body) {
return true;
}
return false;
Expand All @@ -125,4 +117,4 @@ module.exports = {
extractLiteralFromBuiltin,
isValidFunctionExpression,
isFunctionExpression
};
};
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
{
"name": "lasso-unpack",
"version": "1.2.0",
"version": "1.3.2",
"description": "unpack bundle - Generate stats json from lasso bundle",
"main": "lib/lasso-unpack.js",
"bin": {
"lasso-unpack": "bin/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "npm run lint",
"lint": "eslint bin/index.js lib/*"
},
"keywords": [
"lasso",
"bundle",
"manifest"
"manifest",
"unpack",
"parse"
],
"author": "Ajaykumar Prathap",
"license": "MIT",
"homepage": "https://github.com/ajay2507/lasso-unpack",
"dependencies": {
"acorn": "^5.5.3",
"minimist": "1.2.0",
"acorn": "6.4.0",
"acorn-walk": "^7.1.1",
"brotli-size": "0.0.3",
"gzip-size": "5.0.0"
"gzip-size": "5.0.0",
"minimist": "1.2.0"
},
"devDependencies": {
"lasso": "^3.1.4"
"lasso": "^3.1.4",
"eslint-config-ajay": "^1.0.7"
}
}
Loading

0 comments on commit 209c8c2

Please sign in to comment.