Skip to content

Commit 4500dc5

Browse files
committed
Use rollup's generatedCode for ES5 compatibility
1 parent d0bd66d commit 4500dc5

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

README.MD

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ This plugin provides support for integrating Rollup with external packages from
1010

1111

1212
## Usage
13-
`gadget-def.txt`:
13+
`.gadgetdefinition`:
1414
```
15-
* codextest [ResourceLoader | package | dependencies=user.options ] | codextest.js | codextest-main.js | codextest-ChangeNameDialog.js
15+
* my-gadget [ ResourceLoader | package | dependencies=user.options ] | my-gadget.js | my-gadget-main.js | my-gadget-AppComponent.js
1616
```
1717

1818
`rollup.config.js`:
@@ -21,20 +21,21 @@ import mwGadget from 'rollup-plugin-mediawiki-gadgets';
2121

2222
export default {
2323
output:{
24-
format: 'cjs', // Or 'iife' for a single bundle
24+
// Or 'iife' for a single bundle
25+
format: 'cjs',
26+
27+
// By default the generated code uses arrow functions for smaller code size
28+
// Set this if you need ECMAScript 5 compatibility
29+
generatedCode: 'es5',
2530
},
2631
plugins: [
2732
mwGadget({
2833
// Note that the gadget must be ResourceLoader compatible
29-
gadgetDef: './gadget-def.txt',
34+
gadgetDef: '.gadgetdefinition',
3035

3136
// Additional dependencies to load conditionally using import()
3237
// It will be transpiled to mw.loader.using calls on the fly
3338
softDependencies: ['vue', '@wikimedia/codex'],
34-
35-
// By default the generated code uses arrow functions for smaller code size
36-
// Set this to `true` if you need ECMAScript 5 compatibility
37-
legacy: false,
3839
})
3940
]
4041
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-mediawiki-gadget",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"description": "Rollup MediaWiki gadgets with modern goodies",
55
"source": "src/index.ts",
66
"main": "dist/index.cjs",

src/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { readFileSync } from 'fs';
2-
import type { Plugin } from 'rollup';
2+
import type { NormalizedOutputOptions, Plugin } from 'rollup';
33

44
interface MwGadgetConfig {
55
/** Path of the gadget definition file. */
66
gadgetDef: string,
77
/** Additional lazy-load dependencies. */
88
softDependencies?: string[],
9-
/** Set to `true` if you need ECMAScript 5 compatibility. */
10-
legacy?: boolean,
119
}
1210

1311
const OPTION_REGEX = /\[(.*?)\]/;
@@ -42,6 +40,7 @@ function getGadgetDependencies(gadgetDefPath: string): string[] {
4240
function mwGadget(config: MwGadgetConfig): Plugin {
4341
const dependencies = getGadgetDependencies(config.gadgetDef);
4442
const softDependencies = config.softDependencies ?? [];
43+
let outputOptions: NormalizedOutputOptions;
4544

4645
return {
4746
name: 'mediawiki-gadget',
@@ -50,13 +49,16 @@ function mwGadget(config: MwGadgetConfig): Plugin {
5049
return false;
5150
}
5251
},
52+
async renderStart(receivedOutputOptions) {
53+
outputOptions = receivedOutputOptions;
54+
},
5355
renderDynamicImport({ targetModuleId }) {
5456
if (targetModuleId && softDependencies.includes(targetModuleId)) {
5557
return {
5658
left: 'mw.loader.using(',
57-
right: config.legacy
58-
? `).then(function(require){return require("${targetModuleId}")})`
59-
: `).then(require=>require("${targetModuleId}"))`,
59+
right: outputOptions.generatedCode.arrowFunctions
60+
? `).then(require=>require("${targetModuleId}"))`
61+
: `).then(function(require){return require("${targetModuleId}")})`,
6062
};
6163
}
6264
},

0 commit comments

Comments
 (0)