Skip to content

Commit

Permalink
Fix imports random behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaalalach committed Aug 2, 2020
1 parent f71c2be commit 4e8c986
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 119 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ yarn add jscomps
<br>

## Recent changes
- New command: --iife, wraps the entire output code in a IIFE, default value: false. 🆕
- Logging improved: Mention which file triggered the change. 📝
- Outputting result code strange behavior fixed.
- Performence improved.

<br>

Expand Down
6 changes: 3 additions & 3 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import "partOne";
import "partTwo";
import "partThree";
import 'partOne';
import 'partTwo';
import 'partThree';
28 changes: 28 additions & 0 deletions importer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = (function() {
const path = require('path');
const fs = require('fs');

return function(importFile) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(importFile)) {
reject('Not a valid file!');
} else {
const data = fs.readFileSync(importFile);
let imports = [];

for (const line of data.toString().trim().split('\n')) {
if (!/^import\s["']([a-zA-Z_][a-zA-Z0-9_-]+)["'];?/.test(line)) {
reject('Imports format error!');
} else {
imports.push(RegExp.$1);
}
}
let result = '';
for (const elem of imports) {
result += fs.readFileSync(path.resolve(path.dirname(importFile) + '/' + elem) + '.js') + '\n';
}
resolve(result);
}
});
};
})();
190 changes: 99 additions & 91 deletions jscomps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,62 @@ const fs = require('fs'),
uglifyjs = require('uglify-js'),
path = require('path'),
chalk = require('chalk'),
load = require('./importer'),
argv = require('yargs')
.usage('Usage: $0 [options]')
.options({
'f': {
describe: 'Component folder.',
type: 'string',
demandeOption: true,
nargs: 1
},
'w': {
describe: 'Watch a directory for changes.',
default: true,
boolean: true,
demandeOption: false,
nargs: 1
},
'o': {
describe: 'Custom output file path.',
type: 'string',
demandeOption: false,
nargs: 1
},
'i': {
describe: 'Custom input file path.',
type: 'string',
demandeOption: false,
nargs: 1
},
'm': {
describe: 'Minify and compress output file.',
default: true,
boolean: true,
demandeOption: false,
nargs: 1
},
'iife': {
describe: 'Wrap the result in an IIFE.',
default: false,
boolean: true,
demandeOption: false,
nargs: 1
}
})
.demandOption(['f'], "Please provide an folder file.")
.example('$0 -f example')
.example('$0 -f example -m false -w false -o exampleoutput.js')
.alias('h', 'help')
.alias('v', 'version')
.help()
.argv;
.usage('Usage: $0 [options]')
.options({
f: {
describe: 'Component folder.',
type: 'string',
demandeOption: true,
nargs: 1
},
w: {
describe: 'Watch a directory for changes.',
default: true,
boolean: true,
demandeOption: false,
nargs: 1
},
o: {
describe: 'Custom output file path.',
type: 'string',
demandeOption: false,
nargs: 1
},
i: {
describe: 'Custom input file path.',
type: 'string',
demandeOption: false,
nargs: 1
},
m: {
describe: 'Minify and compress output file.',
default: true,
boolean: true,
demandeOption: false,
nargs: 1
},
iife: {
describe: 'Wrap the result in an IIFE.',
default: false,
boolean: true,
demandeOption: false,
nargs: 1
}
})
.demandOption([ 'f' ], 'Please provide an folder file.')
.example('$0 -f example')
.example('$0 -f example -m false -w false -o exampleoutput.js')
.alias('h', 'help')
.alias('v', 'version')
.help().argv;

let fsWait = false,
inputPath,
outputPath,
folderName;
outputPath;

const setup = (folderName) => {
const setup = folderName => {
return new Promise(function(resolve, reject) {
fs.stat(argv.f, (err, stats) => {
if (err || !stats.isDirectory()) {
Expand All @@ -84,7 +83,7 @@ const setup = (folderName) => {
}
});
});
}
};

const startAndWatch = folderPath => {
fs.watch(folderPath, function(event, filename) {
Expand All @@ -94,45 +93,54 @@ const startAndWatch = folderPath => {
fsWait = false;
}, 1000);
}
start(filename);
setTimeout(() => {
start(filename);
}, 500);
});
}
};

const start = (filename) => {
let result;
if (argv.m) {
minifyOutput = uglifyjs.minify((require('import')(inputPath)), {compress: true});
result = minifyOutput.code;
if (minifyOutput.error) {
console.log(minifyOutput.error);
console.log(chalk.red("Minification error detected, waiting for changes..."));
return;
}
} else {
result = require('import')(inputPath);
}
if (argv.iife) {
let iifeStart = '(function() {';
if (!argv.m) {
iifeStart += '\n';
}
result = iifeStart + result + '})();';
}
fs.writeFile(outputPath, result, function(err) {
if (err) throw err;
filename ? console.log(filename + ' changed: ' + chalk.green("Files imported."))
: console.log("Files imported.");
});
}
const start = filename => {
load(inputPath)
.then(importedCode => {
let result;
if (argv.m) {
minifyOutput = uglifyjs.minify(importedCode, { compress: true });
result = minifyOutput.code;
if (minifyOutput.error) {
console.log(minifyOutput.error);
console.log(chalk.red('Minification error detected, waiting for changes...'));
return;
}
} else {
result = importedCode;
}
if (argv.iife) {
let iifeStart = '(function() {';
if (!argv.m) {
iifeStart += '\n';
}
result = iifeStart + result + '})();';
}
fs.writeFile(outputPath, result, function(err) {
if (err) throw err;
filename
? console.log(filename + ' changed: ' + chalk.green('Files imported.'))
: console.log('Files imported.');
});
})
.catch(error => {
console.log(chalk.red(error));
});
};

setup()
.then(() => {
if (argv.w) {
console.log(chalk.green('Watching folder: ' + chalk.white(argv.f)));
console.log(chalk.green('Waiting for changes...'));
startAndWatch(argv.f);
} else {
start();
}
})
.catch(() => console.log(chalk.red("Not a valid watch folder.")));
.then(() => {
if (argv.w) {
console.log(chalk.green('Watching folder: ' + chalk.white(argv.f)));
console.log(chalk.green('Waiting for changes...'));
startAndWatch(argv.f);
} else {
start();
}
})
.catch(() => console.log(chalk.red('Not a valid watch folder.')));
23 changes: 1 addition & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"homepage": "https://github.com/hamzaalalach/jscomps#readme",
"dependencies": {
"chalk": "^3.0.0",
"import": "0.0.6",
"uglify-js": "^3.7.0",
"yargs": "^15.0.2"
},
Expand Down

0 comments on commit 4e8c986

Please sign in to comment.