Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build as a single file for use in a <script>? #20

Open
karx1 opened this issue Aug 17, 2022 · 6 comments
Open

Build as a single file for use in a <script>? #20

karx1 opened this issue Aug 17, 2022 · 6 comments

Comments

@karx1
Copy link

karx1 commented Aug 17, 2022

Hi, I'm working on 2m2d whose webserver is written in Rust, so I can't use npm for the frontend (It has to be static sites only). Is there a way to build this library as a single file so I can load it in a <script> tag?

@will-abule
Copy link

will-abule commented Aug 17, 2022

@karx1 I'm pretty sure Rust has a way to render static files so you can try the following

  1. create a new folder and name it something like npm-packages
  2. create an index.js file in the folder
  3. Initialize npm using npm init
  4. install the module with npm i flamethrower-router (NOTE this applies to any other npm library/packages/module)
  5. install webpack
  6. in the index.js file import the entire library/packages/module and save it as a window object
  7. build the index.js with webpack to a new js file eg dist/bundle.js.

That's it you can now import the build file as a script tag in your HTML and use window.(whatever you named it) to access the library/packages/module features

Example, to be clearer

npm-packages/index.js

import flamethrower from 'flamethrower-router';

window.router = flamethrower();

bundle index.js with webpack

HTML

<script src="dist/bundle.js"></script>

Usage

<script>

    const router = window.router
    router.go('/somewhere');

</script>

just remember to export the path to the bundled file and import it accordingly in your HTML script tag.

I usually do this when I'm working with Go using Gin Framework.

@codediodeio
Copy link
Member

codediodeio commented Aug 17, 2022

Clone it and run npm run build to generate the main.js bundle, then use it in a module script:

<script type="module">
  import flamethrower from '/path/to/main.js';
  const router = flamethrower();
</script>

@bennykay
Copy link

You can also clone my webpack wrapper for flamethrower github.com/bennykay/flamethrower-webpack and run npm run build to get a single flamethrower.js bundle in your /dist folder.

If you want to change options, you can do it before building in src/index.js.

@shreyassanthu77
Copy link

Or just visit this link and download the output

@micziz
Copy link

micziz commented Aug 27, 2022

Another option is to use the ESM CDN as shown here #31

@DarrenSem
Copy link

Hi, I'm working on 2m2d whose webserver is written in Rust, so I can't use npm for the frontend (It has to be static sites only). Is there a way to build this library as a single file so I can load it in a <script> tag?

@karx1
Most examples seem to use import AKA they presume ES6 modules.

You could also include from unpkg, since Jeff published this to npm:

<script href="https://unpkg.com/[email protected]/dist/main.umd.cjs">

URL found via the ending-slash trick:
https://unpkg.com/flamethrower-router/

NOTE: I haven't tried this yet, but it seems to use the standard Universal Module Definition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants