瀏覽代碼

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

version-14
Faris Ansari 7 年之前
committed by GitHub
父節點
當前提交
b51d9a04b1
共有 1 個檔案被更改,包括 40 行新增16 行删除
  1. +40
    -16
      frappe/build.js

+ 40
- 16
frappe/build.js 查看文件

@@ -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 app_paths = apps.map(app => path_join(apps_path, app, app)) // base_path of each app
const assets_path = path_join(sites_path, 'assets'); const assets_path = path_join(sites_path, 'assets');
let build_map = make_build_map(); 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; const file_watcher_port = get_conf().file_watcher_port;


// command line args // command line args
@@ -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 = ''; let output_txt = '';
for (const file of inputs) { for (const file of inputs) {


@@ -89,25 +88,18 @@ function pack(output_path, inputs, minify) {
continue; 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) { if(!minify) {
output_txt += `\n/*\n *\t${file}\n */\n` output_txt += `\n/*\n *\t${file}\n */\n`
} }
output_txt += file_content; output_txt += file_content;

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


@@ -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')) {
file_content = babelify(file_content, file, minify);
}

compiled_js_cache[file] = file_content;
return file_content;
}

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


Loading…
取消
儲存