dxfjs / writer

A JavaScript dxf generator written in TypeScript.
https://dxf.vercel.app
MIT License
79 stars 17 forks source link

Possible to import via CDN? #127

Closed Tom1827 closed 11 months ago

Tom1827 commented 11 months ago

Hi,

I'd really like to use this for a project; any chance to make it importable via a CDN?

Thanks.

tarikjabiri commented 11 months ago

Hi,

Of course you can using jsDelivr it creates urls for es modules and common js. You can use it for any package in npm registry.

regards

Tom1827 commented 11 months ago

Thanks for the very quick response!

Apologies for what must be very basic questions, but this approach doesn't seem to work.

1) Adding <script src="https://cdn.jsdelivr.net/npm/@tarikjabiri/dxf@2.8.9/lib/index.cjs.min.js"></script> (with or without type="module" into my main html works (as in, the browser imports the file) but then import { DxfWriter } from "@tarikjabiri/dxf"; throws an error: Uncaught TypeError: The specifier “@tarikjabiri/dxf” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”.

2) Trying import { DxfWriter } from "https://cdn.jsdelivr.net/npm/@tarikjabiri/dxf@2.8.9/lib/index.cjs.min.js"; gets a step closer, but the error then becomes Uncaught SyntaxError: ambiguous indirect export: DxfWriter.

3) I also tried adding the module to an existing importmap: "dxf": "https://cdn.jsdelivr.net/npm/@tarikjabiri/dxf@2.8.9/lib/index.cjs.min.js" - then import { DxfWriter } from "dxf" also gives the error Uncaught SyntaxError: ambiguous indirect export: DxfWriter.


It's also worth noting that here the import is import { DxfWriter, point3d } from "@tarikjabiri/dxf"; whereas on Github, it's import { XWriter, point } from "@tarikjabiri/dxf";. In my case, neither helps.

Thanks in advance for any help...

tarikjabiri commented 11 months ago

Common JS is not supported in the browser only ES Modules.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script type="module">
    import { DxfWriter } from 'https://cdn.jsdelivr.net/npm/@tarikjabiri/dxf@2.8.9/+esm'

    const dxf = new DxfWriter();
    // your code here
    console.log(dxf.stringify()) // get the dxf content.
    </script>
</body>
</html>
dxf
Tom1827 commented 11 months ago

Thanks!