{"id":273,"date":"2023-11-19T20:10:34","date_gmt":"2023-11-19T20:10:34","guid":{"rendered":"https:\/\/miklcct.com\/wordpress\/?p=273"},"modified":"2023-11-19T20:13:52","modified_gmt":"2023-11-19T20:13:52","slug":"using-civicrm-composer-compile-plugin-to-compile-javascript-in-a-php-library","status":"publish","type":"post","link":"https:\/\/miklcct.com\/wordpress\/2023\/11\/19\/using-civicrm-composer-compile-plugin-to-compile-javascript-in-a-php-library\/","title":{"rendered":"Using civicrm\/composer-compile-plugin to compile JavaScript in a PHP library"},"content":{"rendered":"\n<p>I have a PHP component, miklcct\/journey_recorder, which can be used either as a standalone website or as part of another site. It started initially as a pure PHP component which does all work server-side and outputs HTML to the browser, but over time JavaScript has been added as a form of progressive enhancement, while retaining the usability without it.<\/p>\n\n\n\n<p>At first, it was just a single script file which used jQuery but nothing else, so I used the old fashioned way to include jQuery and my script file in script tags of the HTML, but over time I started to add more libraries including form serialisation and date picker, and introduced a service worker to provide offline functionality as well, so the old fashioned way of loading libraries from a CDN is no longer maintainable.<\/p>\n\n\n\n<p>I then introduced a dependency manager (yarn) and a build system (esbuild) into the project and got it working when the PHP component was used as a standalone application, but how can I build the JavaScript automatically even when the component is a dependency of another application? Composer does not offer a built in way to run scripts in dependencies, and it is understandable because it is a security risk to allow third party libraries to run arbitrary programs as part of regular updates.<\/p>\n\n\n\n<p>Fortunately this problem is common enough that a Composer plugin has already been written, which is called civicrm\/composer-compile-plugin. By adding it as a dependency of the library, it allows library developers to include a build step, and if an application uses the library, the plugin prompts the user to run the build script (or automatically if the library is trusted).<\/p>\n\n\n\n<p>The usage can be as simple as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br \/>  \"require\": {\"civicrm\/composer-compile-plugin\": \"~0.14\"},<br \/>  \"extra\": {<br \/>    \"compile\": [<br \/>      {\"run\": \"@sh cd css; cat one.css two.css three.css &gt; all.css\"}<br \/>    ]<br \/>  }<\/pre>\n\n\n\n<p>So I put in two build steps into it, one to install the JavaScript dependencies using yarn, and the other to build the JavaScript bundle for browser consumption.<\/p>\n\n\n\n<p>The job is now done. When the application includes this library, after <code>composer install<\/code>, the JavaScript bundle is now ready to be used by the client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a PHP component, miklcct\/journey_recorder, which can be used either as a standalone website or as part of another site. It started initially as a pure PHP component which does all work server-side and outputs HTML to the browser, but over&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[30,31],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript","tag-php"],"_links":{"self":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":3,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/273\/revisions\/277"}],"wp:attachment":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}