{"id":19281238,"url":"https://github.com/zouloux/parcel-namer-functional","last_synced_at":"2025-04-22T01:30:56.746Z","repository":{"id":44688195,"uuid":"408557217","full_name":"zouloux/parcel-namer-functional","owner":"zouloux","description":"A Parcel namer plugin with functional way to give name to generated files.","archived":false,"fork":false,"pushed_at":"2022-03-10T15:28:54.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T17:53:18.781Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zouloux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-20T18:28:24.000Z","updated_at":"2022-11-19T01:57:33.000Z","dependencies_parsed_at":"2022-09-12T15:14:10.264Z","dependency_job_id":null,"html_url":"https://github.com/zouloux/parcel-namer-functional","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouloux%2Fparcel-namer-functional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouloux%2Fparcel-namer-functional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouloux%2Fparcel-namer-functional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zouloux%2Fparcel-namer-functional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zouloux","download_url":"https://codeload.github.com/zouloux/parcel-namer-functional/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249565234,"owners_count":21292427,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T21:22:05.372Z","updated_at":"2025-04-22T01:30:56.435Z","avatar_url":"https://github.com/zouloux.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Parcel Namer Functional\nA Parcel namer plugin with a functional way to give name to generated files.\nNo crazy regex to type into a `package.json`, name things with full JS power.\n\n### Why ?\n\nSometimes renaming files with regex only is not handy.\nAlso some projects may need very specific naming functions and having to push a package to npm only for a specific project is cumbersome.\n\n### Installation\n\n`npm i --save-dev parcel-namer-functional`\n\n### Usage\n\nAdd renamer to `.parcelrc`. You can optionally add `\"...\"` after renamer to allow Parcel's default namer to name files if functional namer to rename.\n\n```json5\n{\n    \"extends\": \"@parcel/config-default\",\n    \"namers\": [ \"parcel-namer-functional\", \"...\" ]\n}\n```\n\n\n### Functional renamer API\n\nHere is the API of a functional renamer.\n\n```javascript\nfunction rename ( filePath, fileName, bundle, bundleGraph, config, options ) {\n    // filePath is output bundle default filepath\n    //      ex : /root/.../dist/my-file.js\n    // fileName is default fileName, or previous function transformed fileName\n    //      ex : my-file.5c13ab.css\n    // bundle -\u003e @see Parcel's Bundle object\n    // bundleGraph -\u003e @see Parcel's BundleGraph object\n    // config -\u003e @see Parcel's config object\n    // options -\u003e @see Parcel's options object\n    \n    // Return a new name\n    if ( bundle.type == 'jpg' )\n        return path.join('images', fileName) // -\u003e 'images/my-image.b536c2.jpg\n    else if ( bundle.type == 'mp3' )\n        return path.join('audio', path.basename(filePath)) // -\u003e audio/interview.mp3\n    else\n        return null // -\u003e Will use next parcel namer from .parcelrc\n}\n```\n\nYou can find help about how to rename here :\n- [Parcel's DefaultNamer](https://github.com/parcel-bundler/parcel/blob/v2/packages/namers/default/src/DefaultNamer.js)\n- [Parcel's Namer documentation](https://v2.parceljs.org/plugin-system/namer/)\n\n\n### Rename with functions from modules\n\n`package.json` :\n```json5\n{\n  \"name\" : \"your-parcel-app\",\n  //[...]\n  \"parcel-namer-functional\" : [\n    {\n      \"type\" : \"require\",\n      \"file\" : \"renamer.js\", // relative to package.json\n      \"function\" : \"pleaseRename\"\n    }\n  ]\n}\n```\n\n`renamer.js` :\n```javascript\nconst path = require('path')\nmodule.exports = {\n\tpleaseRename : function ( filePath, ... ) { ... }\n}\n```\n\n\u003e If `\"function\"` parameter is omitted, `module.exports` will be used as renamer function.\n\n\u003e If `\"function\"` targets to an array, every functions will be called in chain.\n\n\n### Rename with a function in global scope\n\nIf you use Parcel bundler API rather than CLI, you can use a global rename function.\nTo register a global renamer, in your code before calling `new Parcel` :\n\n```javascript\n// Register rename as global\nglobal.__globalRename = function ( filePath, ... ) { ... }\n```\n\nAnd in `package.json` :\n```json5\n{\n  \"name\" : \"your-parcel-app\",\n  //[...]\n  \"parcel-namer-functional\" : [\n    {\n      \"type\" : \"global\",\n      \"function\" : \"__globalRename\"\n    }\n  ]\n}\n```\n\n\u003e In this example `__globalRename` can be an array of renamer functions.\n\n### Fail\n\nTo fail silently, add this property to `\"require\"` or `\"global\"` in `package.json`\n\n```json5\n{\n  \"name\" : \"your-parcel-app\",\n  //[...]\n  \"parcel-namer-functional\" : [\n    {\n      \"type\" : \"global\",\n      \"function\" : \"thisFunctionDoesNotExists\",\n      \"fail\" : false\n    },\n    {\n      \"type\" : \"require\",\n      \"file\" : \"thisFileDoesNotExists.js\",\n      \"function\" : \"thisFunctionDoesNotExists\",\n      \"fail\" : false\n    }\n  ]\n}\n```\n\n# Examples\n\nHere are some examples of functional renamers :\n\n`parcel-rename.js` :\n```javascript\nconst assetExtensions = {\n  // https://developer.mozilla.org/fr/docs/orphaned/Web/HTML/Preloading_content\n  audio: ['wav', 'mp3', 'ogg', 'aac', 'flac'],\n  document: ['html', 'htm'],\n  font: ['woff', 'woff2', 'ttf', 'otf', 'eot'],\n  image: ['jpg', 'jpeg', 'gif', 'png', 'apng', 'svg', 'avif', 'webp', 'ico', 'bmp'],\n  script: ['js'],\n  style: ['css'],\n  video: ['mp4', 'webm', 'flv', 'avi', 'mov', 'mkv', 'ogv'],\n}\n\n/**\n * All assets, but html documents, will be in put inside directory parameter.\n */\nfunction createAllAssetsInDirectoryRenamer ( directory = 'assets' ) {\n  return function allAssetsInDirectoryRenamer ( filePath, fileName, bundle, bundleGraph, appOptions ) {\n    let name = ''\n    Object.keys( SolidNamerPlugin.assetExtensions ).map( assetType =\u003e {\n      if ( assetType == 'document' ) return;\n      if ( SolidNamerPlugin.assetExtensions[assetType].includes( bundle.type ) )\n        name = directory\n    })\n    return path.join(name, fileName);\n  }\n}\n\n/**\n * All assets are put inside their type directory.\n * Ex : audio/track.mp3, video/help.mp4, images/flower.jpg ...\n */\nfunction createAssetsByTypeRenamer () {\n  return function assetsInDirectoryByTypeRenamer ( filePath, fileName, bundle, bundleGraph, appOptions ) {\n    let name = ''\n    Object.keys( SolidNamerPlugin.assetExtensions ).map( assetType =\u003e {\n      if ( assetType == 'document' ) return;\n      if ( SolidNamerPlugin.assetExtensions[assetType].includes( bundle.type ) )\n        name += assetType + 's'\n    })\n    return path.join(name, fileName);\n  }\n}\n\nmodule.exports = {\n\tallAssetsInStaticDirectory: createAllAssetsInDirectoryRenamer('static'),\n    assetsByType: createAssetsByTypeRenamer(),\n}\n```\n\n`package.json` :\n```json5\n{\n  \"parcel-namer-functional\": [\n    {\n      \"type\": \"require\",\n      \"file\": \"parcel-rename.js\",\n      \"function\": \"allAssetsInStaticDirectory\"\n    }\n  ]\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzouloux%2Fparcel-namer-functional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzouloux%2Fparcel-namer-functional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzouloux%2Fparcel-namer-functional/lists"}