Utilize __non_webpack_require__ so we can source virtual files

This removes the need for a separate init module. Using
`__non_webpack_require__` will force Webpack to compile as `require`
instead of `__webpack_require__`, allowing ResourceLoader to inject the
virtual file.

Change-Id: I00203f4665b49cb92ee9db356445fdc2ab17fc5f
This commit is contained in:
MusikAnimal 2024-02-13 20:01:08 -05:00
parent 46b458cc0c
commit 15215cb81f
9 changed files with 19 additions and 17 deletions

View file

@ -162,15 +162,6 @@
"codemirror-toggle-label"
]
},
"ext.CodeMirror.v6.WikiEditor.init": {
"packageFiles": [
"ext.CodeMirror.v6.WikiEditor.init.js",
{
"name": "ext.CodeMirror.data.js",
"callback": "MediaWiki\\Extension\\CodeMirror\\DataScript::makeScript"
}
]
},
"ext.CodeMirror.v6.WikiEditor": {
"dependencies": [
"web2017-polyfills",
@ -180,7 +171,11 @@
"ext.CodeMirror.v6.messages"
],
"packageFiles": [
"dist/main.js"
"dist/main.js",
{
"name": "ext.CodeMirror.data.js",
"callback": "MediaWiki\\Extension\\CodeMirror\\DataScript::makeScript"
}
],
"styles": [
"ext.CodeMirror.v6.WikiEditor.less",

View file

@ -103,7 +103,7 @@ class Hooks implements
}
if ( $this->shouldUseV6( $out ) ) {
$out->addModules( 'ext.CodeMirror.v6.WikiEditor.init' );
$out->addModules( 'ext.CodeMirror.v6.WikiEditor' );
} else {
$out->addModules( 'ext.CodeMirror.WikiEditor' );

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,3 +0,0 @@
require( './ext.CodeMirror.data.js' );
mw.loader.load( 'ext.CodeMirror.v6.WikiEditor' );

View file

@ -13,6 +13,9 @@
"browser": true,
"commonjs": true
},
"globals": {
"__non_webpack_require__": "readonly"
},
"rules": {
"es-x/no-array-prototype-includes": 0,
"es-x/no-class-fields": 0,

View file

@ -5,6 +5,11 @@ import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
import { searchKeymap } from '@codemirror/search';
import { bracketMatching } from '@codemirror/language';
// Necessary so that `require` doesn't get mangled into `__webpack_require__`,
// which ResourceLoader won't recognize and thus be unable to load the virtual file.
// See https://webpack-v3.jsx.app/api/module-variables/#__non_webpack_require__-webpack-specific-
__non_webpack_require__( '../ext.CodeMirror.data.js' );
/**
* @class CodeMirrorWikiEditor
*/

View file

@ -13,5 +13,7 @@ mw.config.get = jest.fn().mockReturnValue( '1000+ edits' );
mw.track = jest.fn();
mw.Api.prototype.saveOption = jest.fn();
// eslint-disable-next-line no-underscore-dangle, camelcase
global.__non_webpack_require__ = jest.fn();
global.$ = require( 'jquery' );
$.fn.textSelection = () => {};

View file

@ -63,7 +63,7 @@ class HookTest extends MediaWikiIntegrationTestCase {
public function provideOnBeforePageDisplay(): array {
return [
[ false, 2, 'ext.CodeMirror.WikiEditor' ],
[ true, 1, 'ext.CodeMirror.v6.WikiEditor.init' ]
[ true, 1, 'ext.CodeMirror.v6.WikiEditor' ]
];
}