{"id":13782578,"url":"https://github.com/FlorianRappl/parcel-plugin-codegen","last_synced_at":"2025-05-11T15:32:37.227Z","repository":{"id":147576237,"uuid":"199157296","full_name":"FlorianRappl/parcel-plugin-codegen","owner":"FlorianRappl","description":"Parcel plugin for bundle-time code generation. Simple, powerful, and flexible. :package:","archived":true,"fork":false,"pushed_at":"2020-03-20T16:56:22.000Z","size":14,"stargazers_count":10,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-17T17:43:27.090Z","etag":null,"topics":["asset","code","generation","parcel","parcel-bundler","parcel-plugin"],"latest_commit_sha":null,"homepage":"https://piral.io","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/FlorianRappl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["FlorianRappl"],"custom":["https://www.paypal.me/FlorianRappl"]}},"created_at":"2019-07-27T11:39:56.000Z","updated_at":"2023-01-28T03:25:05.000Z","dependencies_parsed_at":"2024-01-18T14:40:02.992Z","dependency_job_id":"635c6c6f-982a-40af-8575-066665da4765","html_url":"https://github.com/FlorianRappl/parcel-plugin-codegen","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianRappl%2Fparcel-plugin-codegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianRappl%2Fparcel-plugin-codegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianRappl%2Fparcel-plugin-codegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianRappl%2Fparcel-plugin-codegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FlorianRappl","download_url":"https://codeload.github.com/FlorianRappl/parcel-plugin-codegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253588772,"owners_count":21932323,"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":["asset","code","generation","parcel","parcel-bundler","parcel-plugin"],"created_at":"2024-08-03T18:01:39.476Z","updated_at":"2025-05-11T15:32:36.908Z","avatar_url":"https://github.com/FlorianRappl.png","language":"JavaScript","funding_links":["https://github.com/sponsors/FlorianRappl","https://www.paypal.me/FlorianRappl"],"categories":["Plugins"],"sub_categories":["Other"],"readme":"# parcel-plugin-codegen\r\n\r\n[![Build Status](https://florianrappl.visualstudio.com/parcel-plugin-codegen/_apis/build/status/FlorianRappl.parcel-plugin-codegen?branchName=master)](https://florianrappl.visualstudio.com/parcel-plugin-codegen/_build/latest?definitionId=12\u0026branchName=master)\r\n[![npm](https://img.shields.io/npm/v/parcel-plugin-codegen.svg)](https://www.npmjs.com/package/parcel-plugin-codegen)\r\n[![GitHub tag](https://img.shields.io/github/tag/FlorianRappl/parcel-plugin-codegen.svg)](https://github.com/FlorianRappl/parcel-plugin-codegen/releases)\r\n[![GitHub issues](https://img.shields.io/github/issues/FlorianRappl/parcel-plugin-codegen.svg)](https://github.com/FlorianRappl/parcel-plugin-codegen/issues)\r\n\r\nA plugin for Parcel to allow bundle-time asset generation. This can be useful to work efficiently with established conventions and reduce duplication and boilerplate code.\r\n\r\n## Usage\r\n\r\nJust install the plugin. In any file reference a `.codegen` file, e.g., in a TypeScript asset\r\n\r\n```js\r\nconst generatedModule = require('./my.codegen');\r\n```\r\n\r\nCreate a `.codegen` file with the structure:\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return `module.exports = {}`;\r\n};\r\n```\r\n\r\n### Async Generation\r\n\r\nYou can also use promises in your code generation. As an example, if your `.codegen` file looks similar to this:\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return callSomeApi().then(result =\u003e `module.exports = ${JSON.stringify(result)}`);\r\n};\r\n```\r\n\r\nThe new asset will be created asynchronously. Furthermore, you can obviously use `require` or `import` directly in your generated code. Since the asset will be run through Parcel like any other asset, you can use this mechanism to include files from a directory without referencing them explicitly:\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return `\r\n    const { lazy } = require('react');\r\n    module.exports = lazy(() =\u003e import(${JSON.stringify(filePath)}));\r\n  `;\r\n};\r\n```\r\n\r\n### Asset Type Declaration\r\n\r\nBy default, the type of the generated asset will be a JS module. However, you could also generate, e.g., an HTML file:\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return `\u003c!doctype html\u003e\u003ch1\u003eHi Mum!\u003c/h1\u003e`;\r\n};\r\nmodule.exports.type = 'html';\r\n```\r\n\r\nMake sure that the type you return is understood by Parcel. It will be further processed (as such you could also generate, e.g., `ts` assets).\r\n\r\nAlternatively, you can return an object using the following structure:\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return {\r\n    value: `\u003c!doctype html\u003e\u003ch1\u003eHi Mum!\u003c/h1\u003e`,\r\n    type: 'html',\r\n  };\r\n};\r\n```\r\n\r\nThe latter is especially handy when the `type` is not fixed or will be determined via an async operation.\r\n\r\n### Multiple Assets\r\n\r\nLike the explicit asset type declaration we can return multiple assets.\r\n\r\n```js\r\nmodule.exports = function() {\r\n  return [\r\n    {\r\n      value: `\u003c!doctype html\u003e\u003ch1\u003eHi Mum!\u003c/h1\u003e`,\r\n      type: 'html',\r\n    },\r\n    {\r\n      value: `module.exports = () =\u003e 'Hello World!';`,\r\n      type: 'js',\r\n    },\r\n  ];\r\n};\r\n```\r\n\r\nKeep in mind that the bundler refers to them all as content declared from \"inside\" our `.codegen` file even though only JavaScript would be part of the main bundle.\r\n\r\n## Changelog\r\n\r\nThis project adheres to [semantic versioning](https://semver.org).\r\n\r\nYou can find the changelog in the [CHANGELOG.md](CHANGELOG.md) file.\r\n\r\n## License\r\n\r\nThis plugin is released using the MIT license. For more information see the [LICENSE file](LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFlorianRappl%2Fparcel-plugin-codegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFlorianRappl%2Fparcel-plugin-codegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFlorianRappl%2Fparcel-plugin-codegen/lists"}