diff --git a/README.md b/README.md
index e520a8a..9f878c6 100644
--- a/README.md
+++ b/README.md
@@ -27,8 +27,8 @@ yarn add jscomps
## 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.
diff --git a/example/example.js b/example/example.js
index 6410092..ce2c85b 100644
--- a/example/example.js
+++ b/example/example.js
@@ -1,3 +1,3 @@
-import "partOne";
-import "partTwo";
-import "partThree";
\ No newline at end of file
+import 'partOne';
+import 'partTwo';
+import 'partThree';
diff --git a/importer.js b/importer.js
new file mode 100644
index 0000000..a335c85
--- /dev/null
+++ b/importer.js
@@ -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);
+ }
+ });
+ };
+})();
diff --git a/jscomps.js b/jscomps.js
index 6d1271a..82188e8 100755
--- a/jscomps.js
+++ b/jscomps.js
@@ -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()) {
@@ -84,7 +83,7 @@ const setup = (folderName) => {
}
});
});
-}
+};
const startAndWatch = folderPath => {
fs.watch(folderPath, function(event, filename) {
@@ -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.")));
\ No newline at end of file
+ .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.')));
diff --git a/package-lock.json b/package-lock.json
index 6facbbb..0aa9662 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "jscomps",
- "version": "1.0.6",
+ "version": "1.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -2633,14 +2633,6 @@
"safer-buffer": ">= 2.1.2 < 3"
}
},
- "import": {
- "version": "0.0.6",
- "resolved": "https://registry.npmjs.org/import/-/import-0.0.6.tgz",
- "integrity": "sha1-0Ot534aqJnfG22FXilISswMeYEI=",
- "requires": {
- "optimist": "0.3.x"
- }
- },
"import-local": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/import-local/-/import-local-2.0.0.tgz",
@@ -4780,14 +4772,6 @@
"wrappy": "1"
}
},
- "optimist": {
- "version": "0.3.7",
- "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz",
- "integrity": "sha1-yQlBrVnkJzMokjB00s8ufLxuwNk=",
- "requires": {
- "wordwrap": "~0.0.2"
- }
- },
"optionator": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.3.tgz",
@@ -6048,11 +6032,6 @@
"integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==",
"dev": true
},
- "wordwrap": {
- "version": "0.0.3",
- "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
- "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
- },
"wrap-ansi": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz",
diff --git a/package.json b/package.json
index 754b8fa..1c579cd 100644
--- a/package.json
+++ b/package.json
@@ -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"
},