Permalink
Browse files

slowparse errors are now stored as raw i18n json bundles in optimized…

… builds.

previously, their raw HTML was being parsed into i18n bundle format
at runtime.
  • Loading branch information...
1 parent 45079a9 commit aa6ffad74258d927f29568c3c89c740185d2dcce @toolness committed Jan 15, 2013
View
@@ -8,11 +8,13 @@ var NLS_PATHS = [
];
var fs = require('fs');
+var buildRequire = require('./build-require');
var requirejs = require('requirejs');
-var requireConfig = require('./js/require-config');
var bundles = {};
+var config = buildRequire.generateConfig();
-requirejs.config(requireConfig);
+config.isBuild = true;
+requirejs.config(config);
NLS_PATHS.forEach(function(path) {
fs.readdirSync(__dirname + '/js/' + path).forEach(function(filename) {
View
@@ -6,6 +6,7 @@
* 3. command `node build-require.js` or `npm start`
*/
var requirejs = require('requirejs'),
+ jsdom = require('jsdom').jsdom,
resolve = require('path').resolve,
requireConfig = require('./js/require-config'),
baseUrl = resolve(__dirname, 'js'),
@@ -19,7 +20,7 @@ function optimize(done) {
});
}
-function generateConfig() {
+var generateConfig = exports.generateConfig = function() {
var config = {
name: name,
out: out,
@@ -31,6 +32,16 @@ function generateConfig() {
// beautify: true,
mangle: true
},
+ makeDocument: function() {
+ return jsdom('<html></html>', null, {
+ features: {
+ FetchExternalResources: false,
+ ProcessExternalResources: false,
+ MutationEvents: false,
+ QuerySelector: true
+ }
+ });
+ },
// TODO above config setting is temporary, it shuould use mainConfigFile
// https://github.com/toolness/friendlycode/pull/112#issuecomment-6625412
// mainConfigFile: "./js/main.js",
@@ -1,34 +0,0 @@
-define(function() {
- function getDocument() {
- if (typeof(document) == "undefined") {
- // We're being run in node, so make a document using jsdom.
- // Rename require to syncRequire so the r.js optimizer doesn't
- // think the Web version of this file needs jsdom.
- var syncRequire = require;
- var jsdom = syncRequire("jsdom").jsdom;
- var doc = jsdom('<html></html>', null, {
- features: {
- FetchExternalResources: false,
- ProcessExternalResources: false,
- MutationEvents: false,
- QuerySelector: true
- }
- });
- return doc;
- }
- return document;
- }
-
- return function htmlToI18nBundle(html) {
- var result = {};
- var div = getDocument().createElement('div');
-
- div.innerHTML = html;
- [].slice.call(div.querySelectorAll('.error-msg')).forEach(function(el) {
- var name = el.className.split(' ').slice(-1)[0];
- result[name] = el.innerHTML;
- });
-
- return result;
- };
-});
@@ -0,0 +1,37 @@
+define(["text"], function(text) {
+ var buildMap = {};
+
+ function htmlToI18nBundle(document, html) {
+ var result = {};
+ var div = document.createElement('div');
+
+ div.innerHTML = html;
+ [].slice.call(div.querySelectorAll('.error-msg')).forEach(function(el) {
+ var name = el.className.split(' ').slice(-1)[0];
+ result[name] = el.innerHTML;
+ });
+
+ return result;
+ };
+
+ return {
+ load: function(name, req, onLoad, config) {
+ var url = req.toUrl(name).replace(".js", ".html");
+
+ text.get(url, function(html) {
+ var template;
+ if (config.isBuild) {
+ buildMap[name] = htmlToI18nBundle(config.makeDocument(), html);
+ onLoad();
+ } else {
+ onLoad(htmlToI18nBundle(document, html));
+ }
+ });
+ },
+ write: function(pluginName, moduleName, write) {
+ var content = JSON.stringify(buildMap[moduleName]);
+ write.asModule(pluginName + "!" + moduleName,
+ "define(function() { return " + content + "; });\n");
+ }
+ };
+});
@@ -1,9 +1,8 @@
define([
- '../html-to-i18n-bundle',
- 'text!slowparse/spec/errors.base.html'
-], function(htmlToI18nBundle, base) {
+ '../html-to-i18n!slowparse/spec/errors.base.html',
+], function(root) {
return {
description: '<a href="http://mozilla.github.com/slowparse/spec/">Slowparse errors</a> shown when the user makes basic mistakes like forgetting to close a tag.',
- root: htmlToI18nBundle(base)
+ root: root
};
});
@@ -1,9 +1,8 @@
define([
- '../html-to-i18n-bundle',
- 'text!slowparse/spec/errors.forbidjs.html'
-], function(htmlToI18nBundle, forbidjs) {
+ '../html-to-i18n!slowparse/spec/errors.forbidjs.html',
+], function(root) {
return {
description: '<a href="http://mozilla.github.com/slowparse/spec/">Slowparse errors</a> shown when the user tries to use JavaScript but it\'s not allowed.',
- root: htmlToI18nBundle(forbidjs)
+ root: root
};
});

0 comments on commit aa6ffad

Please sign in to comment.