Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 3.77 KB

how-to-use-exports-and-require-in-mediawiki.md

File metadata and controls

105 lines (74 loc) · 3.77 KB

如何使用 MediaWiki 中的module.exportsrequire()
How to use module.exports and require() in MediaWiki?

使用module.exports
Using module.exports

在本仓库中使用module.exports,你需要
To use module.exports in this repository, you need to

  1. 确保目标 MediaWiki 的版本至少为 1.38
    Ensure that the target MediaWiki version is at least 1.38

  2. 如果需要让仓库中的其他小工具使用导出的变量和方法,则应该在小工具的*.d.ts中声明相关类型,如:
    Then, if you need other gadgets in this repository to use the exported variables and methods, you should declare the relevant types in the gadget's *.d.ts, for example:

declare module 'ext.gadget.any_package' {
	export function func(): void;
}
  1. 在入口文件中使用export导出变量和方法
    Finally, use export to export variables and methods in the entry file:
const func = () => {};

export {func};

使用require()
Using require()

在本仓库中使用require(),你需要
To use require() in this repository, you need to

  1. 确保目标 MediaWiki 的版本至少为 1.38
    Ensure that the target MediaWiki version is at least 1.38
  2. 在小工具对应的definition.json中,将需要导入的包添加为依赖项(dependencies
    In the corresponding definition.json of the gadget, add the required package as a dependency
{
	"dependencies": ["@wikimedia/codex", "ext.gadget.any_package"],
	// Other properties...
}

然后,运行以下命令
Then, run the following command

pnpm add <package-name>

如:
For example:

pnpm add @wikimedia/codex

如果需要导入的不是 npm 包或包名与 MiediaWiki 内置模块不同,则应该在导入前于小工具的*.d.ts中声明相关类型,如:
If the imported package is not a npm package or the package name is different from MediaWiki built-in modules, you should declare the types in the *.d.ts of the gadget before importing it. For example:

declare module 'ext.gadget.any_package' {
	export function func(): void;
}
  1. 使用import来导入依赖项,如:
    Finally, use import to import the dependency. For example:
import {CdxButton} from '@wikimedia/codex';
import {func} from 'ext.gadget.any_package';

console.log({
	CdxButton,
	func,
});

使用 MediaWiki 内置的包
Using MediaWiki built-in packages

对于 MediaWiki 内置的一部分包,可以通过以下的方式使用:
For some MediaWiki built-in packages, you can use them as follows:

  • jquery.uimediawiki.utiloojs-ui-core等“第一方”包
    First-party packages such as jquery.ui, mediawiki.util, and oojs-ui-core
    • 在小工具对应的definition.json中,将对应的包添加为依赖项(dependencies
      In the corresponding definition.json of the gadget, just add the used package as a dependency
{
	"dependencies": ["jquery.ui", "mediawiki.util", "oojs-ui-core"],
	// Other properties...
}
  • moment

    • 目标 MediaWiki 的版本不低于 1.38
      The target MediaWiki version is at least 1.38
      • 在小工具对应的definition.json中,将moment添加为依赖项(dependencies
        In the corresponding definition.json of the gadget, add the moment package as a dependency
      • 使用import来导入,如:
        Use import. For example:
{
	"dependencies": ["moment"],
	// Other properties...
}
import moment from 'moment';

console.log(moment.utc());
  • @wikimedia/codexvuepinia等 Vue 相关的包
    Vue-related packages such as @wikimedia/codex, vue and pinia