1
0
mirror of https://github.com/flarum/core.git synced 2025-08-16 05:14:20 +02:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Alexander Skvortsov
3ef541a152 Webpack 5, use v1 of flarum webpack config 2021-05-09 14:02:34 -04:00
Alexander Skvortsov
81894d7cc2 Implement registry, move patchMithril call to Application boot. 2021-05-09 14:01:10 -04:00
Alexander Skvortsov
97aa569bfa Don't export objects directly 2021-05-09 13:59:34 -04:00
Alexander Skvortsov
7d80b88d5c Add semicolons in flarum.extension assignments
Without these, content generated by webpack 5 breaks
2021-05-09 01:30:13 -04:00
10 changed files with 87 additions and 16 deletions

View File

@@ -8,7 +8,7 @@
"clsx": "^1.1.1", "clsx": "^1.1.1",
"color-thief-browser": "^2.0.2", "color-thief-browser": "^2.0.2",
"dayjs": "^1.10.4", "dayjs": "^1.10.4",
"expose-loader": "^1.0.3", "expose-loader": "^2.0.0",
"jquery": "^3.6.0", "jquery": "^3.6.0",
"jquery.hotkeys": "^0.1.0", "jquery.hotkeys": "^0.1.0",
"mithril": "^2.0.4", "mithril": "^2.0.4",
@@ -25,13 +25,13 @@
"@types/textarea-caret": "^3.0.0", "@types/textarea-caret": "^3.0.0",
"bundlewatch": "^0.3.2", "bundlewatch": "^0.3.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"flarum-webpack-config": "0.1.0-beta.10", "flarum-webpack-config": "^1.0.0",
"husky": "^4.3.8", "husky": "^4.3.8",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"webpack": "^4.46.0", "webpack": "^5.0.0",
"webpack-bundle-analyzer": "^4.4.0", "webpack-bundle-analyzer": "^4.4.1",
"webpack-cli": "^3.3.12", "webpack-cli": "^4.0.0",
"webpack-merge": "^4.2.2" "webpack-merge": "^4.0.0"
}, },
"scripts": { "scripts": {
"dev": "webpack --mode development --watch", "dev": "webpack --mode development --watch",

View File

@@ -12,6 +12,7 @@ import mapRoutes from './utils/mapRoutes';
import RequestError from './utils/RequestError'; import RequestError from './utils/RequestError';
import ScrollListener from './utils/ScrollListener'; import ScrollListener from './utils/ScrollListener';
import liveHumanTimes from './utils/liveHumanTimes'; import liveHumanTimes from './utils/liveHumanTimes';
import patchMithril from './utils/patchMithril';
import { extend } from './extend'; import { extend } from './extend';
import Forum from './models/Forum'; import Forum from './models/Forum';
@@ -166,6 +167,8 @@ export default class Application {
} }
boot() { boot() {
patchMithril(window);
this.initializers.toArray().forEach((initializer) => initializer(this)); this.initializers.toArray().forEach((initializer) => initializer(this));
this.store.pushPayload({ data: this.data.resources }); this.store.pushPayload({ data: this.data.resources });

View File

@@ -0,0 +1,60 @@
interface ExportRegistry {
moduleExports: object;
onLoads: object;
/**
* Add an instance to the registry.
* This serves as the equivalent of `flarum.core.compat[id] = object`
*/
add(namespace: string, id: string, object: any): void;
/**
* Add a function to run when object of id "id" is added (or overriden).
* If such an object is already registered, the handler will be applied immediately.
*/
onLoad(namespace: string, id: string, handler: Function): void;
/**
* Retrieve an object of type `id` from the registry.
*/
get(namespace: string, id: string): any;
}
export default class FlarumRegistry implements ExportRegistry {
moduleExports = new Map<string, any>();
onLoads = new Map<string, Function[]>();
protected genKey(namespace: string, id: string): string {
return `${namespace};${id}`;
}
add(namespace: string, id: string, object: any) {
const key = this.genKey(namespace, id);
const onLoads = this.onLoads.get(key);
if (onLoads) {
onLoads.reduce((acc, handler) => handler(acc), object);
}
this.moduleExports.set(key, object);
}
onLoad(namespace: string, id: string, handler: Function) {
const key = this.genKey(namespace, id);
const loadedObject = this.moduleExports.get(key);
if (loadedObject) {
this.moduleExports[id] = handler(loadedObject);
} else {
const currOnLoads = this.onLoads.get(key);
this.onLoads.set(key, [...(currOnLoads || []), handler]);
}
}
get(namespace: string, id: string): any {
const key = this.genKey(namespace, id);
return this.moduleExports.get(key);
}
}

View File

@@ -1,5 +1,5 @@
// Expose jQuery, mithril and dayjs to the window browser object // Expose jQuery, mithril and dayjs to the window browser object
import 'expose-loader?exposes[]=$&exposes[]=jQuery!jquery'; import 'expose-loader?exposes=$,jQuery!jquery';
import 'expose-loader?exposes=m!mithril'; import 'expose-loader?exposes=m!mithril';
import 'expose-loader?exposes=dayjs!dayjs'; import 'expose-loader?exposes=dayjs!dayjs';
@@ -16,9 +16,9 @@ import localizedFormat from 'dayjs/plugin/localizedFormat';
dayjs.extend(relativeTime); dayjs.extend(relativeTime);
dayjs.extend(localizedFormat); dayjs.extend(localizedFormat);
import patchMithril from './utils/patchMithril'; import FlarumRegistry from './FlarumRegistry';
patchMithril(window); window.flreg = new FlarumRegistry();
import * as Extend from './extend/index'; import * as Extend from './extend/index';

View File

@@ -2,7 +2,7 @@
* The `evented` mixin provides methods allowing an object to trigger events, * The `evented` mixin provides methods allowing an object to trigger events,
* running externally registered event handlers. * running externally registered event handlers.
*/ */
export default { const evented = {
/** /**
* Arrays of registered event handlers, grouped by the event name. * Arrays of registered event handlers, grouped by the event name.
* *
@@ -79,3 +79,5 @@ export default {
} }
}, },
}; };
export default evented;

View File

@@ -11,7 +11,7 @@ import extractText from '../../common/utils/extractText';
* The `DiscussionControls` utility constructs a list of buttons for a * The `DiscussionControls` utility constructs a list of buttons for a
* discussion which perform actions on it. * discussion which perform actions on it.
*/ */
export default { const DiscussionControls = {
/** /**
* Get a list of controls for a discussion. * Get a list of controls for a discussion.
* *
@@ -259,3 +259,5 @@ export default {
}); });
}, },
}; };
export default DiscussionControls;

View File

@@ -8,7 +8,7 @@ import extractText from '../../common/utils/extractText';
* The `PostControls` utility constructs a list of buttons for a post which * The `PostControls` utility constructs a list of buttons for a post which
* perform actions on it. * perform actions on it.
*/ */
export default { const PostControls = {
/** /**
* Get a list of controls for a post. * Get a list of controls for a post.
* *
@@ -199,3 +199,5 @@ export default {
}); });
}, },
}; };
export default PostControls;

View File

@@ -8,7 +8,7 @@ import ItemList from '../../common/utils/ItemList';
* The `UserControls` utility constructs a list of buttons for a user which * The `UserControls` utility constructs a list of buttons for a user which
* perform actions on it. * perform actions on it.
*/ */
export default { const UserControls = {
/** /**
* Get a list of controls for a user. * Get a list of controls for a user.
* *
@@ -141,3 +141,5 @@ export default {
app.modal.show(EditUserModal, { user }); app.modal.show(EditUserModal, { user });
}, },
}; };
export default UserControls;

View File

@@ -24,4 +24,4 @@ module.exports = merge(config(), {
}); });
module.exports['module'].rules[0].test = /\.(tsx?|js)$/; module.exports['module'].rules[0].test = /\.(tsx?|js)$/;
module.exports['module'].rules[0].use.options.presets.push('@babel/preset-typescript'); module.exports['module'].rules[0].use[1].options.presets.push('@babel/preset-typescript');

View File

@@ -97,11 +97,11 @@ class Frontend implements ExtenderInterface
if ($this->js) { if ($this->js) {
$assets->js(function (SourceCollector $sources) use ($moduleName) { $assets->js(function (SourceCollector $sources) use ($moduleName) {
$sources->addString(function () { $sources->addString(function () {
return 'var module={}'; return 'var module={};';
}); });
$sources->addFile($this->js); $sources->addFile($this->js);
$sources->addString(function () use ($moduleName) { $sources->addString(function () use ($moduleName) {
return "flarum.extensions['$moduleName']=module.exports"; return "flarum.extensions['$moduleName']=module.exports;";
}); });
}); });
} }