seitime-frappe/rollup.config.js
Faris Ansari 92e8856588
New Build System: Rollup (#5010)
* JS build working

* Css build working!

* Uglify JS in production

* fix codacy

* Add frappe.commands.popen

* FIx ESLint errors

* Add socket.io to package.json

* ignore subprocess warnings

* Add babel-runtime

* sleep 20 after bench start

* remove set -e

* [FIX] non-shell subprocess call

* [FIX] use shell = False

* split commands
2018-02-20 13:47:48 +05:30

172 lines
No EOL
3.9 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const {
get_build_json_path,
get_app_path,
apps_list,
assets_path,
get_public_path,
bench_path
} = require('./rollup.utils');
const less = require('rollup-plugin-less');
const multi_entry = require('rollup-plugin-multi-entry');
const commonjs = require('rollup-plugin-commonjs');
const node_resolve = require('rollup-plugin-node-resolve');
const buble = require('rollup-plugin-buble');
const uglify = require('rollup-plugin-uglify');
const frappe_html = require('./frappe-html-plugin');
const production = process.env.FRAPPE_ENV === 'production';
make_js_css_dirs();
build_libs();
function get_app_config(app) {
const build_map = get_build_json(app);
if (!build_map) return [];
const js_config = Object.keys(build_map)
.filter(output_file =>
output_file.endsWith('.js') &&
// libs is built separately (to be deprecated)
!output_file.endsWith('libs.min.js')
)
.map(output_file => {
const input_files = build_map[output_file].map(
// make paths absolute
input_path => path.resolve(get_app_path(app), input_path)
);
return get_js_config(output_file, input_files);
});
const less_config = Object.keys(build_map)
.filter(output_file =>
output_file.endsWith('.css')
)
.map(output_file => {
const input_files = build_map[output_file].map(
input_path => path.resolve(get_app_path(app), input_path)
);
return get_css_config(output_file, input_files);
});
return [].concat(js_config, less_config);
}
function get_js_config(output_file, input_files) {
const plugins = [
// enables array of inputs
multi_entry(),
// .html -> .js
frappe_html(),
// ES6 -> ES5
buble({
objectAssign: 'Object.assign',
transforms: {
dangerousForOf: true
}
}),
commonjs(),
node_resolve(),
production && uglify()
];
return {
input: input_files,
plugins: plugins,
output: {
file: path.resolve(assets_path, output_file),
format: 'iife',
name: 'Rollup',
globals: {
'sortablejs': 'window.Sortable',
'clusterize.js': 'window.Clusterize'
}
},
context: 'window',
onwarn: (e) => {
if (e.code === 'EVAL') return;
},
external: ['jquery']
};
}
function get_css_config(output_file, input_files) {
const plugins = [
// enables array of inputs
multi_entry(),
// less -> css
less({
output: path.resolve(assets_path, output_file),
option: {
// so that other .less files can import variables.less from frappe directly
paths: [path.resolve(get_public_path('frappe'), 'less')],
compress: production
},
include: [path.resolve(bench_path, '**/*.less'), path.resolve(bench_path, '**/*.css')]
})
];
return {
input: input_files,
plugins: plugins,
output: {
// this file is always empty, remove it later?
file: path.resolve(assets_path, `css/rollup.manifest.css`),
format: 'cjs',
}
};
}
function make_js_css_dirs() {
const paths = [
path.resolve(assets_path, 'js'),
path.resolve(assets_path, 'css')
];
paths.forEach(path => {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
});
}
function build_libs() {
const libs_path = 'js/libs.min.js';
const input_files = get_build_json('frappe')[libs_path];
const libs_content = input_files.map(file_name => {
const full_path = path.resolve(get_app_path('frappe'), file_name);
return `/* ${file_name} */\n` + fs.readFileSync(full_path);
}).join('\n\n');
const target_path = path.resolve(assets_path, libs_path);
fs.writeFileSync(target_path, libs_content);
console.log('✨ Built libs.min.js'); // eslint-disable-line
}
function get_all_apps_config() {
let configs = [];
apps_list.forEach(app => {
configs = configs.concat(get_app_config(app));
});
return configs;
}
function get_build_json(app) {
try {
return require(get_build_json_path(app));
} catch (e) {
// build.json does not exist
return null;
}
}
module.exports = get_all_apps_config();