Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhance] build only changed files, get others from cache #4380

Merged
merged 1 commit into from
Oct 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 40 additions & 16 deletions frappe/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const apps = apps_contents.split('\n');
const app_paths = apps.map(app => path_join(apps_path, app, app)) // base_path of each app
const assets_path = path_join(sites_path, 'assets');
let build_map = make_build_map();
let compiled_js_cache = {}; // cache each js file after it is compiled
const file_watcher_port = get_conf().file_watcher_port;

// command line args
Expand Down Expand Up @@ -78,9 +79,7 @@ function watch() {
});
}

function pack(output_path, inputs, minify) {
const output_type = output_path.split('.').pop();

function pack(output_path, inputs, minify, file_changed) {
let output_txt = '';
for (const file of inputs) {

Expand All @@ -89,25 +88,18 @@ function pack(output_path, inputs, minify) {
continue;
}

let file_content = fs.readFileSync(file, 'utf-8');

if (file.endsWith('.html') && output_type === 'js') {
file_content = html_to_js_template(file, file_content);
}

if(file.endsWith('class.js')) {
file_content = minify_js(file_content, file);
let force_compile = false;
if (file_changed) {
// if file_changed is passed and is equal to file, force_compile it
force_compile = file_changed === file;
}

if (file.endsWith('.js') && !file.includes('/lib/') && output_type === 'js' && !file.endsWith('class.js')) {
file_content = babelify(file_content, file, minify);
}
let file_content = get_compiled_file(file, output_path, minify, force_compile);

if(!minify) {
output_txt += `\n/*\n *\t${file}\n */\n`
}
output_txt += file_content;

output_txt = output_txt.replace(/['"]use strict['"];/, '');
}

Expand All @@ -123,6 +115,38 @@ function pack(output_path, inputs, minify) {
}
}

function get_compiled_file(file, output_path, minify, force_compile) {
const output_type = output_path.split('.').pop();

let file_content;

if (force_compile === false) {
// force compile is false
// attempt to get from cache
file_content = compiled_js_cache[file];
if (file_content) {
return file_content;
}
}

file_content = fs.readFileSync(file, 'utf-8');

if (file.endsWith('.html') && output_type === 'js') {
file_content = html_to_js_template(file, file_content);
}

if(file.endsWith('class.js')) {
file_content = minify_js(file_content, file);
}

if (file.endsWith('.js') && !file.includes('/lib/') && output_type === 'js' && !file.endsWith('class.js')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_content = babelify(file_content, file, minify);
}

compiled_js_cache[file] = file_content;
return file_content;
}

function babelify(content, path, minify) {
let presets = ['env'];
// Minification doesn't work when loading Frappe Desk
Expand Down Expand Up @@ -263,7 +287,7 @@ function watch_js(ondirty) {
for (const target in build_map) {
const sources = build_map[target];
if (sources.includes(filename)) {
pack(target, sources);
pack(target, sources, null, filename);
ondirty && ondirty(target);
// break;
}
Expand Down