Create JS library paths relative to HTML #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
This PR modifies the
attach_library
function to produce JS library paths that are relative to the generated component HTML documents, rather than to the root of the site.(Long-winded) Background
We’re using Emulsify and have CI automatically building a static version of Pattern Lab so developers and stakeholders can review the patterns on the live environment. This saves us having to keep a separate Node process running to serve from the theme directory. We access the pattern library via a URL that looks like this:
To accomplish this, our deployment script simply navigates to the theme directory, installs dependencies, and invokes a script that builds static files, like so:
Where the
build
script in our theme’spackage.json
is as follows:Note that
gulp build
simply invokes this task inemulsify-gulp
. The result is a navigable set of static Pattern Lab files that point to../../../../dist/style.css
for CSS andscript
tags that point to/dist/path_to_component/component_name.js
, i.e. while CSS paths are relative to the HTML document, JS paths are relative to the root of the site.To illustrate the problem with this in our scenario, here’s that sample URL again with some notes:
Note that using paths relative to the root of the site works correctly when serving Pattern Lab from the theme directory (i.e. when using
npm start
) becausedist/
happens to be at the root level in that context. However, this is brittle, because it requires Pattern Lab to be served from the theme directory in order for JS paths to work.This PR addresses this problem by producing JS library paths that, much like the
style.css
path, are relative to the component’s HTML file, and not to the root of the site, which enables us to access Pattern Lab in the way described above while continuing to support the server run by Emulsify’snpm start
.