{"_id":"rollup-utils","_rev":"3782601","name":"rollup-utils","description":"A set of utility functions commonly used by Rollup plugins","dist-tags":{"latest":"0.0.2"},"maintainers":[{"name":"zmjs","email":""}],"time":{"modified":"2025-05-16T07:38:16.000Z","created":"2025-01-18T14:55:30.984Z","0.0.2":"2025-01-20T11:25:14.055Z","0.0.1":"2025-01-18T14:55:30.984Z"},"users":{},"author":{"name":"Rich Harris","email":"richard.a.harris@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/zhiyuanzmj/rollup-utils.git"},"versions":{"0.0.2":{"name":"rollup-utils","version":"0.0.2","publishConfig":{"access":"public"},"description":"A set of utility functions commonly used by Rollup plugins","license":"MIT","repository":{"type":"git","url":"git+https://github.com/zhiyuanzmj/rollup-utils.git"},"author":{"name":"Rich Harris","email":"richard.a.harris@gmail.com"},"homepage":"https://github.com/zhiyuanzmj/rollup-utils#readme","bugs":{"url":"https://github.com/zhiyuanzmj/rollup-utils/issues"},"main":"./dist/cjs/index.js","module":"./dist/es/index.js","type":"commonjs","exports":{"types":"./types/index.d.ts","import":"./dist/es/index.js","default":"./dist/cjs/index.js"},"engines":{"node":">=14.0.0"},"keywords":["rollup","plugin","utils"],"peerDependencies":{"rollup":"^1.20.0||^2.0.0||^3.0.0||^4.0.0"},"peerDependenciesMeta":{"rollup":{"optional":true}},"dependencies":{"@types/estree":"^1.0.0","estree-walker":"^2.0.2","pathe":"^2.0.2","picomatch":"^4.0.2"},"devDependencies":{"@rollup/plugin-commonjs":"^23.0.0","@rollup/plugin-node-resolve":"^15.0.0","@rollup/plugin-typescript":"^9.0.1","@types/node":"^14.18.30","@types/picomatch":"^2.3.0","acorn":"^8.8.0","ava":"^6.2.0","del-cli":"^6.0.0","rollup":"^4.0.0-24","ts-node":"^10.9.2","tslib":"^2.8.1","typescript":"^4.8.3"},"types":"./types/index.d.ts","ava":{"extensions":["ts"],"require":["ts-node/register"],"workerThreads":false,"files":["!**/fixtures/**","!**/helpers/**","!**/recipes/**","!**/types.ts"]},"nyc":{"extension":[".js",".ts"]},"scripts":{"build":"rollup -c","ci:coverage":"nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov","ci:lint":"pnpm build && pnpm lint","ci:lint:commits":"commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}","ci:test":"pnpm test -- --verbose","prebuild":"del-cli dist","prerelease":"pnpm build","pretest":"pnpm build --sourcemap","test":"ava","test:ts":"tsc --noEmit"},"_id":"rollup-utils@0.0.2","_integrity":"sha512-AG1m8fwwkm20WELaXubxgs8ir1gEN7sv+1FfFyEPJA8HtPjL0JndzpLIVzbXyQ5meBfWfv8OnouZ9AC+OsNP+A==","_resolved":"/private/var/folders/25/34_3hk_j1055wx930zmg8lkc0000gn/T/06d0db41da5abce79b681a7f061f52d9/rollup-utils-0.0.2.tgz","_from":"file:rollup-utils-0.0.2.tgz","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"shasum":"72a73b9119dac7274189e5ef27f0f573e56d8b6b","size":8667,"noattachment":false,"key":"/rollup-utils/-/rollup-utils-0.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/rollup-utils/download/rollup-utils-0.0.2.tgz"},"_npmUser":{"name":"zmjs","email":"zhiyuanzmj@gmail.com"},"directories":{},"maintainers":[{"name":"zmjs","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/rollup-utils_0.0.2_1737372313877_0.0138216430418443"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-01-20T11:25:14.055Z","publish_time":1737372314055,"_source_registry_name":"default","_cnpm_publish_time":1737372314055},"0.0.1":{"name":"rollup-utils","version":"0.0.1","publishConfig":{"access":"public"},"description":"A set of utility functions commonly used by Rollup plugins","license":"MIT","repository":{"type":"git","url":"git+https://github.com/zhiyuanzmj/rollup-utils.git"},"author":{"name":"Rich Harris","email":"richard.a.harris@gmail.com"},"homepage":"https://github.com/zhiyuanzmj/rollup-utils#readme","bugs":{"url":"https://github.com/zhiyuanzmj/rollup-utils/issues"},"main":"./dist/cjs/index.js","module":"./dist/es/index.js","type":"commonjs","exports":{"types":"./types/index.d.ts","import":"./dist/es/index.js","default":"./dist/cjs/index.js"},"engines":{"node":">=14.0.0"},"keywords":["rollup","plugin","utils"],"peerDependencies":{"rollup":"^1.20.0||^2.0.0||^3.0.0||^4.0.0"},"peerDependenciesMeta":{"rollup":{"optional":true}},"dependencies":{"@types/estree":"^1.0.0","estree-walker":"^2.0.2","pathe":"^2.0.2","picomatch":"^4.0.2"},"devDependencies":{"@rollup/plugin-commonjs":"^23.0.0","@rollup/plugin-node-resolve":"^15.0.0","@rollup/plugin-typescript":"^9.0.1","@types/node":"^14.18.30","@types/picomatch":"^2.3.0","acorn":"^8.8.0","ava":"^6.2.0","del-cli":"^6.0.0","rollup":"^4.0.0-24","ts-node":"^10.9.2","tslib":"^2.8.1","typescript":"^4.8.3"},"types":"./types/index.d.ts","ava":{"extensions":["ts"],"require":["ts-node/register"],"workerThreads":false,"files":["!**/fixtures/**","!**/helpers/**","!**/recipes/**","!**/types.ts"]},"nyc":{"extension":[".js",".ts"]},"scripts":{"build":"rollup -c","ci:coverage":"nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov","ci:lint":"pnpm build && pnpm lint","ci:lint:commits":"commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}","ci:test":"pnpm test -- --verbose","prebuild":"del-cli dist","prerelease":"pnpm build","pretest":"pnpm build --sourcemap","test":"ava","test:ts":"tsc --noEmit"},"_id":"rollup-utils@0.0.1","_integrity":"sha512-+aXa+q+uUN+jv+X408kTdUAtVHoAKmocrRXkxgcFny+8+db40L7rIWkxtL9g3kljZqA0TE1TlZtK/agDJNeAEw==","_resolved":"/private/var/folders/25/34_3hk_j1055wx930zmg8lkc0000gn/T/f142995f3f47b6fd95830a757b45e4aa/rollup-utils-0.0.1.tgz","_from":"file:rollup-utils-0.0.1.tgz","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"shasum":"18277ab235276fb378f123670755e4d3c7c0120d","size":8762,"noattachment":false,"key":"/rollup-utils/-/rollup-utils-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/rollup-utils/download/rollup-utils-0.0.1.tgz"},"_npmUser":{"name":"zmjs","email":"zhiyuanzmj@gmail.com"},"directories":{},"maintainers":[{"name":"zmjs","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/rollup-utils_0.0.1_1737212130779_0.7218912538206423"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-01-18T14:55:30.984Z","publish_time":1737212130984,"_source_registry_name":"default","_cnpm_publish_time":1737212130984}},"readme":"[npm]: https://img.shields.io/npm/v/rollup-utils\n[npm-url]: https://www.npmjs.com/package/rollup-utils\n[size]: https://packagephobia.now.sh/badge?p=rollup-utils\n[size-url]: https://packagephobia.now.sh/result?p=rollup-utils\n\n[![npm][npm]][npm-url]\n[![size][size]][size-url]\n[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)\n\n# rollup-utils\n\nForked from [@rollup/pluginutils](https://github.com/rollup/plugins/tree/master/packages/pluginutils), using [pathe](https://github.com/unjs/pathe) instead of the native `path` module to support browser environments.\n\nA set of utility functions commonly used by ???? Rollup plugins.\n\n## Requirements\n\nThe plugin utils require an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.\n\n## Install\n\nUsing npm:\n\n```console\nnpm install rollup-utils --save-dev\n```\n\n## Usage\n\n```js\nimport utils from 'rollup-utils';\n//...\n```\n\n## API\n\nAvailable utility functions are listed below:\n\n_Note: Parameter names immediately followed by a `?` indicate that the parameter is optional._\n\n### addExtension\n\nAdds an extension to a module ID if one does not exist.\n\nParameters: `(filename: String, ext?: String)`<br>\nReturns: `String`\n\n```js\nimport { addExtension } from 'rollup-utils';\n\nexport default function myPlugin(options = {}) {\n  return {\n    resolveId(code, id) {\n      // only adds an extension if there isn't one already\n      id = addExtension(id); // `foo` -> `foo.js`, `foo.js` -> `foo.js`\n      id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js` -> `foo.js`\n    }\n  };\n}\n```\n\n### attachScopes\n\nAttaches `Scope` objects to the relevant nodes of an AST. Each `Scope` object has a `scope.contains(name)` method that returns `true` if a given name is defined in the current scope or a parent scope.\n\nParameters: `(ast: Node, propertyName?: String)`<br>\nReturns: `Object`\n\nSee [@rollup/plugin-inject](https://github.com/rollup/plugins/tree/master/packages/inject) or [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs) for an example of usage.\n\n```js\nimport { attachScopes } from 'rollup-utils';\nimport { walk } from 'estree-walker';\n\nexport default function myPlugin(options = {}) {\n  return {\n    transform(code) {\n      const ast = this.parse(code);\n\n      let scope = attachScopes(ast, 'scope');\n\n      walk(ast, {\n        enter(node) {\n          if (node.scope) scope = node.scope;\n\n          if (!scope.contains('foo')) {\n            // `foo` is not defined, so if we encounter it,\n            // we assume it's a global\n          }\n        },\n        leave(node) {\n          if (node.scope) scope = scope.parent;\n        }\n      });\n    }\n  };\n}\n```\n\n### createFilter\n\nConstructs a filter function which can be used to determine whether or not certain modules should be operated upon.\n\nParameters: `(include?: <picomatch>, exclude?: <picomatch>, options?: Object)`<br>\nReturns: `(id: string | unknown) => boolean`\n\n#### `include` and `exclude`\n\nType: `String | RegExp | Array[...String|RegExp]`<br>\n\nA valid [`picomatch`](https://github.com/micromatch/picomatch#globbing-features) pattern, or array of patterns. If `options.include` is omitted or has zero length, filter will return `true` by default. Otherwise, an ID must match one or more of the `picomatch` patterns, and must not match any of the `options.exclude` patterns.\n\nNote that `picomatch` patterns are very similar to [`minimatch`](https://github.com/isaacs/minimatch#readme) patterns, and in most use cases, they are interchangeable. If you have more specific pattern matching needs, you can view [this comparison table](https://github.com/micromatch/picomatch#library-comparisons) to learn more about where the libraries differ.\n\n#### `options`\n\n##### `resolve`\n\nType: `String | Boolean | null`\n\nOptionally resolves the patterns against a directory other than `process.cwd()`. If a `String` is specified, then the value will be used as the base directory. Relative paths will be resolved against `process.cwd()` first. If `false`, then the patterns will not be resolved against any directory. This can be useful if you want to create a filter for virtual module names.\n\n#### Usage\n\n```js\nimport { createFilter } from 'rollup-utils';\n\nexport default function myPlugin(options = {}) {\n  // assume that the myPlugin accepts options of `options.include` and `options.exclude`\n  var filter = createFilter(options.include, options.exclude, {\n    resolve: '/my/base/dir'\n  });\n\n  return {\n    transform(code, id) {\n      if (!filter(id)) return;\n\n      // proceed with the transformation...\n    }\n  };\n}\n```\n\n### dataToEsm\n\nTransforms objects into tree-shakable ES Module imports.\n\nParameters: `(data: Object, options: DataToEsmOptions)`<br>\nReturns: `String`\n\n#### `data`\n\nType: `Object`\n\nAn object to transform into an ES module.\n\n#### `options`\n\nType: `DataToEsmOptions`\n\n_Note: Please see the TypeScript definition for complete documentation of these options_\n\n#### Usage\n\n```js\nimport { dataToEsm } from 'rollup-utils';\n\nconst esModuleSource = dataToEsm(\n  {\n    custom: 'data',\n    to: ['treeshake']\n  },\n  {\n    compact: false,\n    indent: '\\t',\n    preferConst: true,\n    objectShorthand: true,\n    namedExports: true,\n    includeArbitraryNames: false\n  }\n);\n/*\nOutputs the string ES module source:\n  export const custom = 'data';\n  export const to = ['treeshake'];\n  export default { custom, to };\n*/\n```\n\n### extractAssignedNames\n\nExtracts the names of all assignment targets based upon specified patterns.\n\nParameters: `(param: Node)`<br>\nReturns: `Array[...String]`\n\n#### `param`\n\nType: `Node`\n\nAn `acorn` AST Node.\n\n#### Usage\n\n```js\nimport { extractAssignedNames } from 'rollup-utils';\nimport { walk } from 'estree-walker';\n\nexport default function myPlugin(options = {}) {\n  return {\n    transform(code) {\n      const ast = this.parse(code);\n\n      walk(ast, {\n        enter(node) {\n          if (node.type === 'VariableDeclarator') {\n            const declaredNames = extractAssignedNames(node.id);\n            // do something with the declared names\n            // e.g. for `const {x, y: z} = ...` => declaredNames = ['x', 'z']\n          }\n        }\n      });\n    }\n  };\n}\n```\n\n### makeLegalIdentifier\n\nConstructs a bundle-safe identifier from a `String`.\n\nParameters: `(str: String)`<br>\nReturns: `String`\n\n#### Usage\n\n```js\nimport { makeLegalIdentifier } from 'rollup-utils';\n\nmakeLegalIdentifier('foo-bar'); // 'foo_bar'\nmakeLegalIdentifier('typeof'); // '_typeof'\n```\n\n### normalizePath\n\nConverts path separators to forward slash.\n\nParameters: `(filename: String)`<br>\nReturns: `String`\n\n#### Usage\n\n```js\nimport { normalizePath } from 'rollup-utils';\n\nnormalizePath('foo\\\\bar'); // 'foo/bar'\nnormalizePath('foo/bar'); // 'foo/bar'\n```\n\n## Meta\n\n[CONTRIBUTING](/.github/CONTRIBUTING.md)\n\n[LICENSE (MIT)](/LICENSE)\n","_attachments":{},"homepage":"https://github.com/zhiyuanzmj/rollup-utils#readme","bugs":{"url":"https://github.com/zhiyuanzmj/rollup-utils/issues"},"license":"MIT"}