kenchris / urlpattern-polyfill

URLPattern polyfill
https://www.npmjs.com/package/urlpattern-polyfill
MIT License
268 stars 31 forks source link

Denoland deploy #52

Closed fabiancook closed 2 years ago

fabiancook commented 2 years ago

Would it be possible to have this added to the denoland registry?

This would involve adding a webhook to this module, which would then publish the typescript source.

Details are listed here: https://deno.land/x

fabiancook commented 2 years ago

Using an import map, this polyfill can be utilised directly through GitHub:

{
  "imports": {
    "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern.ts",
    "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-utils": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-utils.ts",
    "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern.interfaces": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern.interfaces.ts",
    "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/path-to-regex-modified": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/path-to-regex-modified.ts",
    "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern-parser": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/url-pattern-parser.ts",
    "urlpattern-polyfill": "https://raw.githubusercontent.com/kenchris/urlpattern-polyfill/main/src/index.ts",
  }
}

This would be similar if it was deployed to deno.land unless these typescript files were process further to include extensions for deployment.

fabiancook commented 2 years ago

Ignoring the later failing jobs in these actions, we can see this code running across deno, node, webkit, chrome, and firefox:

https://github.com/virtualstate/app-history/blob/7171f6aea0e26847525ff65796b96447a52e4169/src/tests/examples/url-pattern.ts#L12-L21

appHistory.addEventListener("navigate", ({ destination }) => {
        const identifiedTest = new URLPattern({
            pathname: "/test/:id"
        });
        if (identifiedTest.test({ pathname: destination.url })) {
            body.innerHTML = destination.getState<{ innerHTML: string }>().innerHTML;
        } else {
            throw new Error(unexpectedPage);
        }
    });

Running the tests we can see the matcher is working:

START  urlPatternExample
Updated window pathname to /test/1
Updated window pathname to /test/2
PASS   urlPatternExample
kenchris commented 2 years ago

I don't know much about deno land or how to publish it there. Could you share instructions?

fabiancook commented 2 years ago

Now that your module that is published to npm with exports, it is also directly compatible with skypack (maybe it was before too?)

I found that I can now just use the import map:

{
  "imports": {
    "urlpattern-polyfill": "https://cdn.skypack.dev/urlpattern-polyfill"
  }
}

However some might still want to access from deno.land, here are steps to publish to the deno.land registry:

  1. Go to https://deno.land/x
  2. Click "Publish Module"

image

  1. Add a module name

image

  1. Add a subdirectory, src/ here is usable because your source is typescript, deno can directly utilise it

image

  1. Follow these steps to add the webhook to this repository:

image

I think the problem here would be though... the import map still will be a bit messy because of deno's requirement for extensions to be present for exports, while this isn't common practice when writing typescript

Once deployed the import map would probably look something like:

{
  "imports": {
    "https://deno.land/x/urlpattern_polyfill/url-pattern": "https://deno.land/x/urlpattern_polyfill/url-pattern.ts",
    "https://deno.land/x/urlpattern_polyfill/url-utils": "https://deno.land/x/urlpattern_polyfill/url-utils.ts",
    "https://deno.land/x/urlpattern_polyfill/url-pattern.interfaces": "https://deno.land/x/urlpattern_polyfill/url-pattern.interfaces.ts",
    "https://deno.land/x/urlpattern_polyfill/path-to-regex-modified": "https://deno.land/x/urlpattern_polyfill/path-to-regex-modified.ts",
    "https://deno.land/x/urlpattern_polyfill/url-pattern-parser": "https://deno.land/x/urlpattern_polyfill/url-pattern-parser.ts",
    "urlpattern-polyfill": "https://deno.land/x/urlpattern_polyfill"
  }
}

Even then, I'm not sure how deno's resolution exactly works, and it might rely on the redirected to url for this individual mapping, meaning possibly this is the required import map with version pinning included:

{
  "imports": {
    "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern.ts",
    "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-utils": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-utils.ts",
    "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern.interfaces": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern.interfaces.ts",
    "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/path-to-regex-modified": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/path-to-regex-modified.ts",
    "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern-parser": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2/url-pattern-parser.ts",
    "urlpattern-polyfill": "https://deno.land/x/urlpattern_polyfill@v1.0.0-rc2"
  }
}

TLDR seems like skypack is a better goto here, no additional steps required there.

kenchris commented 2 years ago

Why do you need it in Deno? Deno supports URLPattern natively now

fabiancook commented 2 years ago

Ha! Well, I guess I could work a local polyfill file to detect globalThis.URLPattern exists!

I'm writing code that is compatible across all platforms, so using the polyfill is a must for others like node or the browser. I would have expected this polyfill to eventually provide the global where it is present.

kenchris commented 2 years ago

I see. That is tracked in https://github.com/kenchris/urlpattern-polyfill/issues/47 so closing this one