npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-script

v0.1.1

Published

A simple script management component for Astro

Downloads

8

Readme

Welcome to Astro Script 👋

Astro Script is a simple component to make it easier to manage script tags in your Astro components and pages.

Installing the plugin

Once you have setup your Astro project simply run the following command:

# yarn
yarn add astro-script

# npm
npm i astro-script

You can then import the component like this:

---
import { AstroScript } from "astro-script";
// or import Spa from 'astro-spa/Spa.astro'
---

<AstroScript src="/script.js" />

And that's it, you're now ready to go!

How to add scripts

There are two ways to add scripts to AstroScript. You can either add them to the src prop or put them inside the <AstroScript></AstroScript> tags. The following example shows both ways:

---
import { AstroScript } from "astro-script";
---

// Add script to src prop
<AstroScript src="/script.js" />
<AstroScript src={Astro.resolve("../relative/path/to/script.js")} />

// Add script to <AstroScript />
<AstroScript>
  <script> console.log("Hello World"); </script>
</AstroScript>

The src prop also supports an array of paths. The following example shows how to add multiple scripts:

---
import { AstroScript } from "astro-script";
---

// Add multiple scripts to src prop
<AstroScript src={["/script1.js", "/script2.js"]} />

You can also pass multiple script tags inside the <AstroScript></AstroScript> tag. The following example shows how to add multiple scripts:

---
import { AstroScript } from "astro-script";
---

// Add multiple scripts to <AstroScript />
<AstroScript>
  <script> console.log("Hello World"); </script>
  <script> console.log("Hello World"); </script>
</AstroScript>

AstroScript also works with external scripts. The following example shows how to add an external script:

---
import { AstroScript } from "astro-script";
---

// Add external script to src prop
<AstroScript src="https://example.com/script.js" />
<AstroScript src={["https://example.com/script1.js", "https://example.com/script2.js"]} />

How AstroScript works

If the src prop is present, AstroScript will first fetch the external scripts and read the internal scripts from the filesystem referenced in the src prop. It'll then merge the scripts and minify & inline them if enabled. If inline is set to false, it'll create a .js file at ${publicPath}/astro-script. The name of the file will a 8 digit truncated hash of the scripts.

If you have passed the scripts inside the <AstroScript></AstroScript> tag, AstroScript will read the scripts from the <script> tags and merge them & minify them if enabled. Any elements other than <script> tags will be ignored.

By default, AstroScript won't run in development mode. If the src prop is present, it'll loop through them and create a script tag for each script. And if you have passed the scripts inside the <AstroScript></AstroScript> tag, the scripts will be left as is.

<script hoist> vs <AstroScript>

Starting with v0.20.2, Astro provides the ability to hoist your script tags to the top of the page and bundle them. On the other hand, AstroScript will not hoist the scripts to the top of the page. Instead it will allow you to bundle them at the exact point you want them to be bundled. It also allows you to bundle external scripts.

Supported Component Props

src

Type: string or string[]

Default: undefined

The source(s) of the script(s). You can resolve the sources using the Astro.resolve API or you can pass a path relative to the public folder. If the path is relative to the public folder, it must start with /.

inline

Type: boolean

Default: true

Whether or not the scripts should be inline.

minify

Type: boolean

Default: true

Whether or not the scripts should be minified. If true the scripts will be minified using terser.

minifyOptions

Type: MinifyOptions (check the link below)

Default: {}

The minify configuration options supported by terser. For more info check the minify options supported by terser.

publicPath

Type: string

Default: public

The public path of the project.

dev

Type: boolean

Default: import.meta.env !== "development"

Whether or not the optimizations will be performed in development mode.

Warnings

  • If the src prop is present, any <script> tags inside the <AstroScript></AstroScript> tag will be ignored.
  • Files resolved with Astro.resolve can't have extensions other than .js. Because AstroScript doesn't have ability to perform transformation of .jsx, .ts, .tsx, .mjs, .cjs files.
  • You may encounter issues with HMR if you have set dev to true.
  • If you have set dev to true you should add the ${publicPath}/astro-script directory to your .gitignore file. Otherwise, you may end up pushing lots of redundant files to your Github repo and CI.