diff --git a/Makefile b/Makefile index e8b3e4879..939965830 100644 --- a/Makefile +++ b/Makefile @@ -149,16 +149,19 @@ $(foreach p, \ tmp/platform_%.coffee : tmp/platform.jst $(call imports,platform) $(template_deps) $(template) $< $@ type=$* +to_compile := $(filter-out globals css platform,$(parts)) platform_crx platform_userscript + define compile tmp/$1.js : tmp/$1.coffee $(coffee_deps) tools/globalize.js - $(coffee) $$< - node tools/globalize.js $1 + $(RM) $$@ endef -$(foreach p, \ - $(filter-out globals css platform,$(parts)) platform_crx platform_userscript, \ - $(eval $(call compile,$(p))) \ -) +$(foreach p, $(to_compile), $(eval $(call compile,$(p)))) + +.events/compile : $(foreach p, $(to_compile), tmp/$(p).js) | .events + $(coffee) $(subst .js,.coffee,$?) + node tools/globalize.js $(subst tmp/,,$(subst .js,,$?)) + echo -> $@ tmp/eventPage.js : src/meta/eventPage.coffee $(coffee_deps) | tmp $(coffee) -o tmp src/meta/eventPage.coffee @@ -168,7 +171,7 @@ define rules_channel testbuilds/crx$1 : $$(MKDIR) -testbuilds/crx$1/script.js : $(call intermediate,crx) $(cat_deps) | testbuilds/crx$1 +testbuilds/crx$1/script.js : $(call intermediate,crx) $(cat_deps) | testbuilds/crx$1 .events/compile $(cat) $(call intermediate,crx) $$@ testbuilds/crx$1/eventPage.js : tmp/eventPage.js | testbuilds/crx$1 @@ -194,7 +197,7 @@ testbuilds/$(name)$1.crx : testbuilds/$(name)$1.crx.zip package.json tools/sign. testbuilds/$(name)$1.meta.js : src/meta/metadata.js src/meta/icon48.png version.json $(template_deps) | testbuilds $(template) $$< $$@ type=userscript channel=$1 -testbuilds/$(name)$1.user.js : testbuilds/$(name)$1.meta.js $(call intermediate,userscript) $(cat_deps) +testbuilds/$(name)$1.user.js : testbuilds/$(name)$1.meta.js $(call intermediate,userscript) $(cat_deps) | .events/compile $(cat) testbuilds/$(name)$1.meta.js $(call intermediate,userscript) $$@ endef @@ -218,7 +221,7 @@ index.html : test.html tmp/.jshintrc : src/meta/jshint.json tmp/declaration.js tmp/globals.js $(template_deps) | tmp $(template) $< $@ -.events/jshint.% : tmp/%.js tmp/.jshintrc node_modules/jshint/package.json | .events +.events/jshint.% : tmp/%.js tmp/.jshintrc node_modules/jshint/package.json | .events/compile $(BIN)jshint $< echo -> $@ diff --git a/tools/globalize.js b/tools/globalize.js index 566835576..ec8b6107d 100644 --- a/tools/globalize.js +++ b/tools/globalize.js @@ -1,38 +1,40 @@ var fs = require('fs'); -var filename = `tmp/${process.argv[2]}.js`; -var basename = process.argv[2].split('_')[0]; // e.g. template_crx -> template -var sources = fs.readdirSync(`src/${basename}`); +for (var arg of process.argv.slice(2)) { + var filename = `tmp/${arg}.js`; + var basename = arg.split('_')[0]; // e.g. template_crx -> template + var sources = fs.readdirSync(`src/${basename}`); -// Extract variables to be made global from source file list -// e.g. ImageExpand from src/Images/ImageExpand.coffee -// but not QR.post or eventPage -var names = []; -for (var f of sources) { - var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); - if (m) names.push(m[1]); -} - -var script = fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n'); - -var replaced = 0; - -script = script.replace( - - // matches declaration at the start of the function, not including helper function assignments - /^( *var )(.*)(,\n *|;\n)/m, - - function(declaration, v, n, e) { - replaced++; - var n0 = names.sort().join(', '); - if (n0 !== n) throw new Error(`${filename}: expected variables (${n0}) found (${n})`); - return (e[0] === ',') ? v : ''; + // Extract variables to be made global from source file list + // e.g. ImageExpand from src/Images/ImageExpand.coffee + // but not QR.post or eventPage + var names = []; + for (var f of sources) { + var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); + if (m) names.push(m[1]); } -); + var script = fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n'); -if (replaced !== 1) { - throw new Error(`${filename}: no declaration found`); + var replaced = 0; + + script = script.replace( + + // matches declaration at the start of the function, not including helper function assignments + /^( *var )(.*)(,\n *|;\n)/m, + + function(declaration, v, n, e) { + replaced++; + var n0 = names.sort().join(', '); + if (n0 !== n) throw new Error(`${filename}: expected variables (${n0}) found (${n})`); + return (e[0] === ',') ? v : ''; + } + + ); + + if (replaced !== 1) { + throw new Error(`${filename}: no declaration found`); + } + + fs.writeFileSync(filename, script); } - -fs.writeFileSync(filename, script);