Updated js code so vendors can be added to the modules folder, so

they can be required properly and we can finally get rid of that
really annoying "mismatched anonymous" error in Require.js.

First module to make the transition: Chart.js
This commit is contained in:
Julian Lam
2016-02-26 11:29:17 -05:00
parent 5997f2f5c4
commit e1323c0295
8 changed files with 54 additions and 7 deletions

View File

@@ -57,7 +57,7 @@ module.exports = function(grunt) {
files: ['public/**/*.less', 'node_modules/nodebb-*/*.less', 'node_modules/nodebb-*/*/*.less', 'node_modules/nodebb-*/*/*/*.less', 'node_modules/nodebb-*/*/*/*/*.less']
},
clientUpdated: {
files: ['public/src/**/*.js', 'node_modules/nodebb-*/*.js', 'node_modules/nodebb-*/*/*.js', 'node_modules/nodebb-*/*/*/*.js', 'node_modules/nodebb-*/*/*/*/*.js', 'node_modules/templates.js/lib/templates.js']
files: ['public/src/**/*.js', '!public/src/modules/*.js', 'node_modules/nodebb-*/*.js', 'node_modules/nodebb-*/*/*.js', 'node_modules/nodebb-*/*/*/*.js', 'node_modules/nodebb-*/*/*/*/*.js', 'node_modules/templates.js/lib/templates.js']
},
serverUpdated: {
files: ['*.js', 'install/*.js', 'src/**/*.js']

View File

@@ -18,6 +18,7 @@
"autoprefixer": "^6.2.3",
"bcryptjs": "~2.3.0",
"body-parser": "^1.9.0",
"chart.js": "^1.0.2",
"colors": "^1.1.0",
"compression": "^1.1.0",
"connect-ensure-login": "^0.1.1",

View File

@@ -1,7 +1,7 @@
"use strict";
/*global define, ajaxify, app, socket, utils, bootbox, Chart, RELATIVE_PATH*/
/*global define, ajaxify, app, socket, utils, bootbox, RELATIVE_PATH*/
define('admin/general/dashboard', ['semver'], function(semver) {
define('admin/general/dashboard', ['semver', 'Chart'], function(semver, Chart) {
var Admin = {},
intervals = {
rooms: false,

View File

@@ -1,12 +1,13 @@
"use strict";
/*global define, app, socket, ajaxify, RELATIVE_PATH, bootbox, templates, Chart */
/*global define, app, socket, ajaxify, RELATIVE_PATH, bootbox, templates */
define('admin/manage/category', [
'uploader',
'iconSelect',
'admin/modules/colorpicker',
'autocomplete'
], function(uploader, iconSelect, colorpicker, autocomplete) {
'autocomplete',
'Chart'
], function(uploader, iconSelect, colorpicker, autocomplete, Chart) {
var Category = {};
Category.init = function() {

View File

@@ -62,6 +62,7 @@ var async = require('async'),
async.apply(plugins.reloadRoutes),
function(next) {
async.parallel([
async.apply(Meta.js.symlinkModules),
async.apply(Meta.js.minify, 'nodebb.min.js'),
async.apply(Meta.js.minify, 'acp.min.js'),
async.apply(Meta.css.minify),

View File

@@ -7,6 +7,7 @@ var winston = require('winston'),
_ = require('underscore'),
nconf = require('nconf'),
fs = require('fs'),
rimraf = require('rimraf'),
file = require('../file'),
plugins = require('../plugins'),
emitter = require('../emitter'),
@@ -43,6 +44,8 @@ module.exports = function(Meta) {
'public/src/variables.js',
'public/src/widgets.js'
],
// files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load
rjs: [
'public/src/client/footer.js',
'public/src/client/chats.js',
@@ -77,10 +80,51 @@ module.exports = function(Meta) {
'public/src/modules/helpers.js',
'public/src/modules/sounds.js',
'public/src/modules/string.js'
],
// modules listed below are symlinked to public/src/modules so they can be defined anonymously
modules: [
'./node_modules/chart.js/Chart.js'
]
}
};
Meta.js.symlinkModules = function(callback) {
// Symlink all defined modules to /public/src/modules
var modulesLoaded = 0,
targetPath;
async.series([
function(next) {
async.each(Meta.js.scripts.modules, function(localPath, next) {
targetPath = path.join(__dirname, '../../public/src/modules', path.basename(localPath));
async.waterfall([
async.apply(fs.access, localPath, fs.R_OK),
async.apply(rimraf, targetPath),
async.apply(fs.link, localPath, targetPath)
], function(err) {
if (err) {
winston.error('[meta/js] Could not symlink `' + localPath + '` to modules folder');
} else {
winston.verbose('[meta/js] Symlinked `' + localPath + '` to modules folder');
++modulesLoaded;
}
next(err);
});
}, next);
}
], function(err) {
if (err) {
winston.error('[meta/js] Encountered error while symlinking modules:' + err.message);
}
winston.verbose('[meta/js] ' + modulesLoaded + ' of ' + Meta.js.scripts.modules.length + ' modules symlinked');
callback(err);
});
};
Meta.js.minify = function(target, callback) {
if (nconf.get('isPrimary') !== 'true') {
if (typeof callback === 'function') {

View File

@@ -27,7 +27,6 @@
<![endif]-->
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.3/material.min.js"></script>
<script type="text/javascript" src="{relative_path}/vendor/chart.js/chart.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/hammer/hammer.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/jquery/sortable/Sortable.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/acp.min.js?{cache-buster}"></script>

View File

@@ -92,6 +92,7 @@ function initializeNodeBB(callback) {
function(next) {
async.parallel([
async.apply(meta.templates.compile),
async.apply(meta.js.symlinkModules),
async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'nodebb.min.js'),
async.apply(!skipJS ? meta.js.minify : meta.js.getFromFile, 'acp.min.js'),
async.apply(!skipLess ? meta.css.minify : meta.css.getFromFile),