Skip to content

Commit

Permalink
Use rollup's generatedCode for ES5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
diskdance committed Sep 29, 2023
1 parent d0bd66d commit 4500dc5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
17 changes: 9 additions & 8 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ This plugin provides support for integrating Rollup with external packages from


## Usage
`gadget-def.txt`:
`.gadgetdefinition`:
```
* codextest [ResourceLoader | package | dependencies=user.options ] | codextest.js | codextest-main.js | codextest-ChangeNameDialog.js
* my-gadget [ ResourceLoader | package | dependencies=user.options ] | my-gadget.js | my-gadget-main.js | my-gadget-AppComponent.js
```

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

export default {
output:{
format: 'cjs', // Or 'iife' for a single bundle
// Or 'iife' for a single bundle
format: 'cjs',

// By default the generated code uses arrow functions for smaller code size
// Set this if you need ECMAScript 5 compatibility
generatedCode: 'es5',
},
plugins: [
mwGadget({
// Note that the gadget must be ResourceLoader compatible
gadgetDef: './gadget-def.txt',
gadgetDef: '.gadgetdefinition',

// Additional dependencies to load conditionally using import()
// It will be transpiled to mw.loader.using calls on the fly
softDependencies: ['vue', '@wikimedia/codex'],

// By default the generated code uses arrow functions for smaller code size
// Set this to `true` if you need ECMAScript 5 compatibility
legacy: false,
})
]
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-mediawiki-gadget",
"version": "1.0.7",
"version": "1.1.0",
"description": "Rollup MediaWiki gadgets with modern goodies",
"source": "src/index.ts",
"main": "dist/index.cjs",
Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { readFileSync } from 'fs';
import type { Plugin } from 'rollup';
import type { NormalizedOutputOptions, Plugin } from 'rollup';

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

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

return {
name: 'mediawiki-gadget',
Expand All @@ -50,13 +49,16 @@ function mwGadget(config: MwGadgetConfig): Plugin {
return false;
}
},
async renderStart(receivedOutputOptions) {
outputOptions = receivedOutputOptions;
},
renderDynamicImport({ targetModuleId }) {
if (targetModuleId && softDependencies.includes(targetModuleId)) {
return {
left: 'mw.loader.using(',
right: config.legacy
? `).then(function(require){return require("${targetModuleId}")})`
: `).then(require=>require("${targetModuleId}"))`,
right: outputOptions.generatedCode.arrowFunctions
? `).then(require=>require("${targetModuleId}"))`
: `).then(function(require){return require("${targetModuleId}")})`,
};
}
},
Expand Down

0 comments on commit 4500dc5

Please sign in to comment.