Skip to content

Commit

Permalink
Use rollup presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Mevrael committed Oct 16, 2016
1 parent 7f977bc commit e8e95cb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
10 changes: 1 addition & 9 deletions .babelrc
@@ -1,11 +1,3 @@
{
"presets": [
[
"es2015",
{
"modules": false
}
]
]
"presets": [ "es2015-rollup" ]
}

2 changes: 1 addition & 1 deletion .eslintrc
@@ -1,3 +1,3 @@
{
"extends": "airbnb"
"extends": "airbnb-base"
}
58 changes: 48 additions & 10 deletions node_scripts/js.js
Expand Up @@ -9,6 +9,7 @@ const resolve = require('rollup-plugin-node-resolve');
const eslint = require('rollup-plugin-eslint');
const uglify = require('uglify-js');
const fs = require("fs");
const path = require("path");

const ASSETS_FOLDER_JS = Config.srcPathJs;
const PUBLIC_BUILD_FOLDER = Config.buildPathJs;
Expand All @@ -18,9 +19,9 @@ const plugins = [

babel({
babelrc: false,
//exclude: 'node_modules/**',
//exclude: 'node_modules/!**',
presets: [
["es2015", {"modules": false}],
'es2015-rollup',
'stage-0'
]
}),
Expand All @@ -31,34 +32,71 @@ const plugins = [
})
];

/*if (isProduction) {
const uglify = require("rollup-plugin-uglify");
plugins.push(uglify);
}*/
function removeLines(file) {

function buildJs(appEntryFile, file = null, destFile = null) {
return new Promise(resolve => {
const fs = require('fs');
const readline = require('readline');
const stream = require('stream');

const instream = fs.createReadStream(file);
const outstream = new stream;
const rl = readline.createInterface(instream, outstream);

const lines = [];

rl.on('line', function(line) {

if (line.indexOf('import') !== 0 && line.indexOf('export') !== 0) {
lines.push(line);
}

});

rl.on('close', function() {

const str = fs.createWriteStream(file);
lines.forEach(line => {
str.write(line + "\n");
});
str.end();
resolve();

});
})

}

function buildJs(appEntryFile, file = null, destFile = null, excludeFiles = []) {
return new Promise((res, reject) => {
if (file === null) {
file = ASSETS_FOLDER_JS + '/' + appEntryFile + '.js';
}
if (destFile === null) {
destFile = PUBLIC_BUILD_FOLDER + '/' + appEntryFile + '.js';
}

const external = excludeFiles.map(item => path.resolve(item));

console.log(colors.yellow('Bundling JS file: ' + file));
rollup.rollup({
dest: destFile,
entry: file,
format: 'iife',
//treeshake: false,
external: external,
//format: 'iife',
treeshake: false,
plugins: plugins,
sourceMap: true
}).then(function(bundle) {
// write bundle to a file and use the IIFE format so it executes immediately
return bundle.write({
format: 'cjs',
//format: 'cjs',
dest: destFile
});
}).then(function() {
// remove imports and exports
return removeLines(destFile);
}).then(() => {
if (isProduction) {
const res = uglify.minify(destFile, {
compress: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,7 +25,7 @@
"homepage": "https://github.com/Mevrael/assets-builder#readme",
"dependencies": {
"babel-eslint": "^7.0.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-es2015-rollup": "^1.2.0",
"babel-preset-stage-0": "^6.5.0",
"clean-css": "^3.4.20",
"colors": "^1.1.2",
Expand Down

0 comments on commit e8e95cb

Please sign in to comment.