fix: fix taskbar warning

add app.importScript
copy public/src/modules to build folder
This commit is contained in:
Barış Soner Uşaklı
2022-02-15 10:43:22 -05:00
parent 36f172c1ac
commit fd97f9ffcc
7 changed files with 57 additions and 50 deletions

View File

@@ -334,9 +334,13 @@ ajaxify.widgets = { render: render };
}; };
ajaxify.loadScript = function (tpl_url, callback) { ajaxify.loadScript = function (tpl_url, callback) {
let location = !app.inAdmin ? 'forum/' : '';
if (tpl_url.startsWith('admin')) {
location = '';
}
const data = { const data = {
tpl_url: tpl_url, tpl_url: tpl_url,
scripts: [tpl_url], scripts: [location + tpl_url],
}; };
// Hint: useful if you want to load a module on a specific page (append module name to `scripts`) // Hint: useful if you want to load a module on a specific page (append module name to `scripts`)
@@ -354,7 +358,7 @@ ajaxify.widgets = { render: render };
} }
if (typeof script === 'string') { if (typeof script === 'string') {
return async function (next) { return async function (next) {
const module = await importScript(script); const module = await app.importScript(script);
// Hint: useful if you want to override a loaded library (e.g. replace core client-side logic), // Hint: useful if you want to override a loaded library (e.g. replace core client-side logic),
// or call a method other than .init() // or call a method other than .init()
hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => { hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => {
@@ -383,25 +387,6 @@ ajaxify.widgets = { render: render };
}); });
}; };
async function importScript(scriptName) {
let pageScript;
try {
if (scriptName.startsWith('admin/plugins')) {
pageScript = await import(/* webpackChunkName: "admin/plugins/[request]" */ 'admin/plugins/' + scriptName.replace(/^admin\/plugins\//, ''));
} else if (scriptName.startsWith('admin')) {
pageScript = await import(/* webpackChunkName: "admin/[request]" */ 'admin/' + scriptName.replace(/^admin\//, ''));
} else if (scriptName.startsWith('forum/plugins')) {
pageScript = await import(/* webpackChunkName: "forum/plugins/[request]" */ 'forum/plugins/' + scriptName.replace(/^forum\/plugins\//, ''));
} else {
pageScript = await import(/* webpackChunkName: "forum/[request]" */ 'forum/' + scriptName);
}
} catch (err) {
console.warn('error loading script' + err.stack);
}
return pageScript;
}
ajaxify.loadData = function (url, callback) { ajaxify.loadData = function (url, callback) {
url = ajaxify.removeRelativePath(url); url = ajaxify.removeRelativePath(url);

View File

@@ -49,6 +49,26 @@ app.flags = {};
} }
}; };
app.importScript = async function (scriptName) {
let pageScript;
try {
if (scriptName.startsWith('admin/plugins')) {
pageScript = await import(/* webpackChunkName: "admin/plugins/[request]" */ 'admin/plugins/' + scriptName.replace(/^admin\/plugins\//, ''));
} else if (scriptName.startsWith('admin')) {
pageScript = await import(/* webpackChunkName: "admin/[request]" */ 'admin/' + scriptName.replace(/^admin\//, ''));
} else if (scriptName.startsWith('forum/plugins')) {
pageScript = await import(/* webpackChunkName: "forum/plugins/[request]" */ 'forum/plugins/' + scriptName.replace(/^forum\/plugins\//, ''));
} else if (scriptName.startsWith('forum')) {
pageScript = await import(/* webpackChunkName: "forum/[request]" */ 'forum/' + scriptName.replace(/^forum\//, ''));
} else {
pageScript = await import(/* webpackChunkName: "modules/[request]" */ '../../build/public/src/modules/' + scriptName);
}
} catch (err) {
console.warn('error loading script' + err.stack);
}
return pageScript && pageScript.default;
}
app.handleEarlyClicks = function () { app.handleEarlyClicks = function () {
/** /**
* Occasionally, a button or anchor (not meant to be ajaxified) is clicked before * Occasionally, a button or anchor (not meant to be ajaxified) is clicked before

View File

@@ -7,9 +7,9 @@ import * as autocomplete from 'autocomplete';
import 'jquery-deserialize'; import 'jquery-deserialize';
import * as api from 'api'; import * as api from 'api';
import * as alerts from 'alerts'; import * as alerts from 'alerts';
export function init() { export function init() {
console.log('should be true semver.gt("1.1.1", "1.0.0")', semver.gt('1.1.1', '1.0.0')); console.log('should be true semver.gt("1.1.1", "1.0.0")', semver.gt('1.1.1', '1.0.0'));
$('#change-skin').val(config.bootswatchSkin); $('#change-skin').val(config.bootswatchSkin);
$('#inputTags').tagsinput({ $('#inputTags').tagsinput({
@@ -99,3 +99,6 @@ export function init() {
document.head.appendChild(linkEl); document.head.appendChild(linkEl);
} }
} }
const testPage = { init };
export default testPage;

View File

@@ -12,14 +12,12 @@ define('taskbar', ['benchpress', 'translator', 'hooks'], function (Benchpress, t
self.tasklist = self.taskbar.find('ul'); self.tasklist = self.taskbar.find('ul');
$(document.body).append(self.taskbar); $(document.body).append(self.taskbar);
self.taskbar.on('click', 'li', function () { self.taskbar.on('click', 'li', async function () {
const $btn = $(this); const $btn = $(this);
const moduleName = $btn.attr('data-module'); const moduleName = $btn.attr('data-module');
const uuid = $btn.attr('data-uuid'); const uuid = $btn.attr('data-uuid');
// TODO: throws warning in webpack const module = await app.importScript(moduleName);
// https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import
require([moduleName], function (module) {
if (!$btn.hasClass('active')) { if (!$btn.hasClass('active')) {
minimizeAll(); minimizeAll();
module.load(uuid); module.load(uuid);
@@ -30,8 +28,6 @@ define('taskbar', ['benchpress', 'translator', 'hooks'], function (Benchpress, t
} else { } else {
module.minimize(uuid); module.minimize(uuid);
} }
});
return false; return false;
}); });
}); });
@@ -41,22 +37,21 @@ define('taskbar', ['benchpress', 'translator', 'hooks'], function (Benchpress, t
}); });
}; };
taskbar.close = function (module, uuid) { taskbar.close = async function (moduleName, uuid) {
// Sends signal to the appropriate module's .close() fn (if present) // Sends signal to the appropriate module's .close() fn (if present)
const btnEl = taskbar.tasklist.find('[data-module="' + module + '"][data-uuid="' + uuid + '"]'); const btnEl = taskbar.tasklist.find('[data-module="' + module + '"][data-uuid="' + uuid + '"]');
let fnName = 'close'; let fnName = 'close';
// TODO: Refactor chat module to not take uuid in close instead of by jQuery element // TODO: Refactor chat module to not take uuid in close instead of by jQuery element
if (module === 'chat') { if (moduleName === 'chat') {
fnName = 'closeByUUID'; fnName = 'closeByUUID';
} }
if (btnEl.length) { if (btnEl.length) {
require([module], function (module) { const module = await app.importScript(moduleName);
if (typeof module[fnName] === 'function') { if (module && typeof module[fnName] === 'function') {
module[fnName](uuid); module[fnName](uuid);
} }
});
} }
}; };

View File

@@ -134,6 +134,13 @@ async function clearModules() {
JS.buildModules = async function () { JS.buildModules = async function () {
await clearModules(); await clearModules();
const fse = require('fs-extra');
await fse.copy(
path.join(__dirname, `../../public/src/modules`),
path.join(__dirname, `../../build/public/src/modules`)
);
await linkModules(); await linkModules();
}; };

View File

@@ -28,21 +28,18 @@ module.exports = {
}, },
output: { output: {
filename: '[name].bundle.js', filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'), path: path.resolve(__dirname, 'dist'),
publicPath: `${relativePath}/dist/`, publicPath: `${relativePath}/dist/`,
}, },
watchOptions: { watchOptions: {
poll: 500, poll: 500,
aggregateTimeout: 500, aggregateTimeout: 250,
}, },
resolve: { resolve: {
symlinks: false, symlinks: false,
modules: [ modules: [
'build/public/src/modules', 'build/public/src/modules',
'public/src', 'public/src',
'public/src/modules',
'public/src/client',
'node_modules', 'node_modules',
...activePlugins.map(p => `node_modules/${p}/node_modules`), ...activePlugins.map(p => `node_modules/${p}/node_modules`),
], ],