From e2aaefa7df5fbfce019092187b3f57e0eed9cca3 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 12 Apr 2016 20:14:06 -0700 Subject: [PATCH] Get rid of recursive file imports. --- Makefile | 18 +++++++++++++++--- src/General/Main.coffee | 2 +- tools/template.js | 38 +++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index af73acab6..d72748a58 100644 --- a/Makefile +++ b/Makefile @@ -66,8 +66,8 @@ sources := \ src/General/Main.coffee imports := \ - node_modules/font-awesome/package.json \ - $(wildcard src/Linkification/icons/*.png) \ + tmp/font-awesome.css \ + tmp/style.css \ src/Archive/archives.json \ src/meta/icon48.png \ $(wildcard src/Monitoring/Favicon/*/*.png) \ @@ -80,6 +80,12 @@ imports := \ $(wildcard src/css/*.css) \ .tests_enabled +imports_font_awesome := \ + node_modules/font-awesome/css/font-awesome.css \ + node_modules/font-awesome/fonts/fontawesome-webfont.woff +imports_style := \ + $(wildcard src/Linkification/icons/*.png) + bds := \ $(foreach f, \ $(foreach c,. -beta.,$(name)$(c)crx updates$(c)xml $(name)$(c)user.js $(name)$(c)meta.js) \ @@ -103,12 +109,18 @@ all : jshint bds install npm install echo -> $@ -node_modules/%/package.json : .events/npm +node_modules/% : .events/npm .tests_enabled : echo false> .tests_enabled +tmp/font-awesome.css : src/css/font-awesome.css $(imports_font_awesome) $(template_deps) | tmp + $(template) $< $@ + +tmp/style.css : src/css/style.css $(imports_style) $(template_deps) | tmp + $(template) $< $@ + tmp/script.coffee : $(sources) $(cat_deps) | tmp $(cat) $(sources) $@ diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 44aef4c32..efb69f4e2 100644 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -391,7 +391,7 @@ Main = $.ready -> cb() if Main.isThisPageLegit() - css: `<%= importCSS('font-awesome', 'style', 'yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon', 'supports') %>` + css: `<%= importCSS('../../tmp/font-awesome', '../../tmp/style', 'yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon', 'supports') %>` cssWWW: `<%= importCSS('www') %>` diff --git a/tools/template.js b/tools/template.js index e5bffaf8a..fdd439c28 100644 --- a/tools/template.js +++ b/tools/template.js @@ -4,29 +4,26 @@ var _ = require('lodash'); // disable ES6 delimiters _.templateSettings.interpolate = /<%=([\s\S]+?)%>/g; -var pkg = {}; +var obj = {}; -var read = pkg.read = filename => fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n'); -var readJSON = pkg.readJSON = filename => JSON.parse(read(filename)); -pkg.readBase64 = filename => fs.readFileSync(filename).toString('base64'); -pkg.ls = pathname => fs.readdirSync(pathname); - -_.assign(pkg, readJSON('package.json')); -_.assign(pkg.meta, readJSON('version.json')); +var read = obj.read = filename => fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n'); +var readJSON = obj.readJSON = filename => JSON.parse(read(filename)); +obj.readBase64 = filename => fs.readFileSync(filename).toString('base64'); +obj.ls = pathname => fs.readdirSync(pathname); // Convert JSON object to Coffeescript expression (via embedded JS). var constExpression = data => '`' + JSON.stringify(data).replace(/`/g, '\\`') + '`'; -pkg.importCSS = function() { +obj.importCSS = function() { var text = Array.prototype.slice.call(arguments).map(name => read(`src/css/${name}.css`)).join(''); - text = _.template(text)(pkg); + text = _.template(text)(pkg); // variables only; no recursive imports return text.trim().replace(/\n+/g, '\n').split(/^/m).map(JSON.stringify).join(' +\n').replace(/`/g, '\\`'); }; -pkg.importHTML = function(filename) { +obj.importHTML = function(filename) { var text = read(`src/${filename}.html`).replace(/^ +/gm, '').replace(/\r?\n/g, ''); - text = _.template(text)(pkg); - return pkg.html(text); + text = _.template(text)(pkg); // variables only; no recursive imports + return obj.html(text); }; function TextStream(text) { @@ -155,7 +152,7 @@ Placeholder.prototype.build = function() { // HTML template generator with placeholders of forms ${}, &{}, @{}, and ?{}{}{} (see Placeholder.prototype.build) // that checks safety of generated expressions at compile time. -pkg.html = function(template) { +obj.html = function(template) { var stream = new TextStream(template); var output = parseHTMLTemplate(stream); if (stream.text) { @@ -164,16 +161,23 @@ pkg.html = function(template) { return `(innerHTML: ${output})`; }; -pkg.assert = function(statement) { - if (!pkg.tests_enabled) return ''; +obj.assert = function(statement) { + if (!obj.tests_enabled) return ''; return `throw new Error 'Assertion failed: ' + ${constExpression(statement)} unless ${statement}`; }; +// Import variables from package.json and version.json. +var pkg = readJSON('package.json'); +_.assign(pkg.meta, readJSON('version.json')); + +// Take variables from command line. for (var i = 4; i < process.argv.length; i++) { var m = process.argv[i].match(/(.*?)=(.*)/); pkg[m[1]] = m[2]; } +_.assign(obj, pkg); + var text = read(process.argv[2]); -text = _.template(text)(pkg); +text = _.template(text)(obj); fs.writeFileSync(process.argv[3], text);