@ rollup/ pluginutils
由QQ Rollup插件常用的一套公用功能.
所需资源
插件 utils 需要一个 长期 节点版本(v14.0.0+)和"滚动"v1.2.0.0+.
安装
使用 npm :
npm install @rollup/pluginutils --save-dev
使用量
import utils from '@rollup/pluginutils';
//...
API 密码
现有公用事业职能如下:
注:参数名称紧接其后 ? 表示参数是可选的.
扩展
如果模块 ID 不存在,则在模块 ID 中添加扩展名.
参数: (filename: String, ext?: String)
回返情况: String
import { addExtension } from '@rollup/pluginutils';
export default function myPlugin(options = {}) {
return {
resolveId(code, id) {
// only adds an extension if there isn't one already
id = addExtension(id); // `foo` -> `foo.js`, `foo.js` -> `foo.js`
id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js` -> `foo.js`
}
};
}
附 录
附 录 Scope 对象到 AST 的相关节点。 每个 Scope 对象有一个 scope.contains(name) 返回的方法 true 如果在当前范围或母范围中定义了某个名称.
参数: (ast: Node, propertyName?: String)
回返情况: Object
见 @ rollup/ plugin- 注射 或者说 @ rollup/ plugin- commonjs (英语) 作为使用的例子.
import { attachScopes } from '@rollup/pluginutils';
import { walk } from 'estree-walker';
export default function myPlugin(options = {}) {
return {
transform(code) {
const ast = this.parse(code);
let scope = attachScopes(ast, 'scope');
walk(ast, {
enter(node) {
if (node.scope) scope = node.scope;
if (!scope.contains('foo')) {
// `foo` is not defined, so if we encounter it,
// we assume it's a global
}
},
leave(node) {
if (node.scope) scope = scope.parent;
}
});
}
};
}
创建筛选
构造一个过滤函数,用于确定是否应该运行某些模块.
参数: (include?: <picomatch>, exclude?: <picomatch>, options?: Object)
回返情况: (id: string | unknown) => boolean
include 财务报告和审定财务报表 exclude
类型 : String | RegExp | Array[...String|RegExp]
一个有效的 picomatch 模式,或一系列模式。 若为 options.include 被省略或有零长度,过滤器将返回 true 默认。 否则,身份证必须匹配其中一个或多个 picomatch 模式,并且不得与 options.exclude 模式.
请注意: picomatch 模式与 minimatch 在多数情况下,它们可以互换。 如果您有更具体的模式匹配需求, 您可以查看 本比较表 了解图书馆的不同之处.
options
resolve
类型 : String | Boolean | null
可选解决模式相对于目录以外的 process.cwd()如为 String ,然后该值将用作基准目录。 相对路径将针对 process.cwd() 先说 若为 false,那么这些模式将不会以任何目录来解决。 如果您想要为虚拟模块名称创建过滤器,此选项可能有用 .
使用量
import { createFilter } from '@rollup/pluginutils';
export default function myPlugin(options = {}) {
// assume that the myPlugin accepts options of `options.include` and `options.exclude`
var filter = createFilter(options.include, options.exclude, {
resolve: '/my/base/dir'
});
return {
transform(code, id) {
if (!filter(id)) return;
// proceed with the transformation...
}
};
}
数据toEsm
将对象转换为可树摇动的ES模块导入.
参数: (data: Object, options: DataToEsmOptions)
回返情况: String
data
类型 : Object
一个可以转换成ES模块的对象.
options
类型 : DataToEsmOptions
注意: 这些选项的完整文档请参见TypeScript定义
使用量
import { dataToEsm } from '@rollup/pluginutils';
const esModuleSource = dataToEsm(
{
custom: 'data',
to: ['treeshake']
},
{
compact: false,
indent: '\t',
preferConst: true,
objectShorthand: true,
namedExports: true,
includeArbitraryNames: false
}
);
/*
Outputs the string ES module source:
export const custom = 'data';
export const to = ['treeshake'];
export default { custom, to };
*/
已签名的名称
根据指定的模式提取所有任务目标的名称.
参数: (param: Node)
回返情况: Array[...String]
param
类型 : Node
一个 acorn AST节点.
使用量
import { extractAssignedNames } from '@rollup/pluginutils';
import { walk } from 'estree-walker';
export default function myPlugin(options = {}) {
return {
transform(code) {
const ast = this.parse(code);
walk(ast, {
enter(node) {
if (node.type === 'VariableDeclarator') {
const declaredNames = extractAssignedNames(node.id);
// do something with the declared names
// e.g. for `const {x, y: z} = ...` => declaredNames = ['x', 'z']
}
}
});
}
};
}
准确的Regex
构造一个匹配指定字符串的 RegExp。 这对插件钩过滤器有用 .
参数: (str: String | Array[...String], flags?: String)
回返情况: RegExp
使用量
import { exactRegex } from '@rollup/pluginutils';
exactRegex('foobar'); // /^foobar$/
exactRegex(['foo', 'bar']); // /^(?:foo|bar)$/
exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
创建法律标识符
构造一个捆绑安全标识符 String。 。 。 .
参数: (str: String)
回返情况: String
使用量
import { makeLegalIdentifier } from '@rollup/pluginutils';
makeLegalIdentifier('foo-bar'); // 'foo_bar'
makeLegalIdentifier('typeof'); // '_typeof'
常态Path
将路径分隔符转换为前切.
参数: (filename: String)
回返情况: String
使用量
import { normalizePath } from '@rollup/pluginutils';
normalizePath('foo\\bar'); // 'foo/bar'
normalizePath('foo/bar'); // 'foo/bar'
前缀Regex
构造匹配有指定前缀的值的 RegExp。 这对插件钩过滤器有用 .
参数: (str: String | Array[...String], flags?: String)
回返情况: RegExp
使用量
import { prefixRegex } from '@rollup/pluginutils';
prefixRegex('foobar'); // /^foobar/
prefixRegex(['foo', 'bar']); // /^(?:foo|bar)/
prefixRegex('foo(bar)', 'i'); // /^foo\(bar\)/i
后缀
构造一个匹配带有指定后缀的值的RegExp。 这对插件钩过滤器有用 .
参数: (str: String | Array[...String], flags?: String)
回返情况: RegExp
使用量
import { suffixRegex } from '@rollup/pluginutils';
suffixRegex('foobar'); // /foobar$/
suffixRegex(['foo', 'bar']); // /(?:foo|bar)$/
suffixRegex('foo(bar)', 'i'); // /foo\(bar\)$/i