From 4500dc5130a6830e6fdc5ebbcf2675348f569432 Mon Sep 17 00:00:00 2001 From: psnbaotg Date: Fri, 29 Sep 2023 14:32:34 +0800 Subject: [PATCH] Use rollup's generatedCode for ES5 compatibility --- README.MD | 17 +++++++++-------- package.json | 2 +- src/index.ts | 14 ++++++++------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/README.MD b/README.MD index 7c6eae5..f2a5909 100644 --- a/README.MD +++ b/README.MD @@ -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`: @@ -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, }) ] }; diff --git a/package.json b/package.json index 56ca196..82f9684 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 11781f7..39f14cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = /\[(.*?)\]/; @@ -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', @@ -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}")})`, }; } },