Skip to content

Commit

Permalink
feat: load jquery-form before using
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 11, 2020
1 parent ea83087 commit 3b23136
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
14 changes: 13 additions & 1 deletion public/src/app.js
Expand Up @@ -88,7 +88,19 @@ app.cacheBuster = null;
app.showCookieWarning();
registerServiceWorker();

require(['taskbar', 'helpers', 'forum/pagination'], function (taskbar, helpers, pagination) {
require([
'taskbar',
'helpers',
'forum/pagination',
'translator',
'forum/unread',
'forum/header/notifications',
'forum/header/chat',
'timeago/jquery.timeago',
], function (taskbar, helpers, pagination, translator, unread, notifications, chat) {
notifications.prepareDOM();
chat.prepareDOM();
translator.prepareDOM();
taskbar.init();

helpers.register();
Expand Down
2 changes: 1 addition & 1 deletion public/src/client/login.js
@@ -1,7 +1,7 @@
'use strict';


define('forum/login', [], function () {
define('forum/login', ['jquery-form'], function () {
var Login = {};

Login.init = function () {
Expand Down
2 changes: 1 addition & 1 deletion public/src/client/register.js
@@ -1,7 +1,7 @@
'use strict';


define('forum/register', ['translator', 'zxcvbn'], function (translator, zxcvbn) {
define('forum/register', ['translator', 'zxcvbn', 'jquery-form'], function (translator, zxcvbn) {
var Register = {};
var validationError = false;
var successIcon = '';
Expand Down
2 changes: 1 addition & 1 deletion public/src/modules/uploader.js
@@ -1,7 +1,7 @@
'use strict';


define('uploader', ['translator', 'benchpress'], function (translator, Benchpress) {
define('uploader', ['translator', 'benchpress', 'jquery-form'], function (translator, Benchpress) {
var module = {};

module.show = function (data, callback) {
Expand Down
48 changes: 47 additions & 1 deletion src/meta/js.js
Expand Up @@ -102,10 +102,11 @@ JS.scripts = {
'zxcvbn.js': 'node_modules/zxcvbn/dist/zxcvbn.js',
ace: 'node_modules/ace-builds/src-min',
'clipboard.js': 'node_modules/clipboard/dist/clipboard.min.js',
'nprogress.js': 'node_modules/nprogress/nprogress.js',
'tinycon.js': 'node_modules/tinycon/tinycon.js',
'slideout.js': 'node_modules/slideout/dist/slideout.min.js',
'compare-versions.js': 'node_modules/compare-versions/index.js',
'timeago/locales': 'node_modules/timeago/locales',
'jquery-form.js': 'node_modules/jquery-form/dist/jquery.form.min.js',
},
};

Expand Down Expand Up @@ -272,6 +273,44 @@ JS.buildModules = function (fork, callback) {
], callback);
};

function requirejsOptimize(target, callback) {
const requirejs = require('requirejs');
let scriptText = '';
const sharedCfg = {
paths: {
jquery: 'empty:',
},
optimize: 'none',
out: function (text) {
scriptText += text;
},
};
const bundledModules = [
{
baseUrl: './node_modules',
name: 'timeago/jquery.timeago',
},
{
baseUrl: './node_modules/nprogress',
name: 'nprogress',
},
];

async.eachSeries(bundledModules, function (moduleCfg, next) {
requirejs.optimize({ ...sharedCfg, ...moduleCfg }, function () {
next();
}, function (err) {
next(err);
});
}, function (err) {
if (err) {
return callback(err);
}
const filePath = path.join(__dirname, '../../build/public/rjs-bundle-' + target + '.js');
fs.writeFile(filePath, scriptText, callback);
});
}

JS.linkStatics = function (callback) {
rimraf(path.join(__dirname, '../../build/public/plugins'), function (err) {
if (err) {
Expand Down Expand Up @@ -348,6 +387,9 @@ JS.buildBundle = function (target, fork, callback) {
};

async.waterfall([
function (next) {
requirejsOptimize(target, next);
},
function (next) {
getBundleScriptList(target, next);
},
Expand All @@ -357,6 +399,10 @@ JS.buildBundle = function (target, fork, callback) {
});
},
function (files, next) {
files.push({
srcPath: path.join(__dirname, '../../build/public/rjs-bundle-' + target + '.js'),
});

var minify = process.env.NODE_ENV !== 'development';
var filePath = path.join(__dirname, '../../build/public', fileNames[target]);

Expand Down
6 changes: 3 additions & 3 deletions test/controllers.js
Expand Up @@ -1839,7 +1839,7 @@ describe('Controllers', function () {

describe('timeago locales', function () {
it('should load timeago locale', function (done) {
request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.af.js', function (err, res, body) {
request(nconf.get('url') + '/assets/src/modules/timeago/locales/jquery.timeago.af.js', function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body.includes('Afrikaans'));
Expand All @@ -1848,15 +1848,15 @@ describe('Controllers', function () {
});

it('should return not found if NodeBB language exists but timeago locale does not exist', function (done) {
request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.ms.js', function (err, res, body) {
request(nconf.get('url') + '/assets/src/modules/timeago/locales/jquery.timeago.ms.js', function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
});
});

it('should return not found if NodeBB language does not exist', function (done) {
request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.muggle.js', function (err, res, body) {
request(nconf.get('url') + '/assets/src/modules/timeago/locales/jquery.timeago.muggle.js', function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 404);
done();
Expand Down

0 comments on commit 3b23136

Please sign in to comment.