|
1 | | -# Template: SolidJS Library |
| 1 | +<p> |
| 2 | + <img width="100%" src="https://assets.solidjs.com/banner?type=Ecosystem&background=tiles&project=solid-create-script" alt="solid-create-script"> |
| 3 | +</p> |
2 | 4 |
|
3 | | -Template for [SolidJS](https://www.solidjs.com/) library package. Bundling of the library is managed by [tsup](https://tsup.egoist.dev/). |
| 5 | +# solid-create-script |
4 | 6 |
|
5 | | -Other things configured include: |
| 7 | +Solid utility hook to dynamically load an external script. |
6 | 8 |
|
7 | | -- Bun (for dependency management and running scripts) |
8 | | -- TypeScript |
9 | | -- ESLint / Prettier |
10 | | -- Solid Testing Library + Vitest (for testing) |
11 | | -- Playground app using library |
12 | | -- GitHub Actions (for all CI/CD) |
| 9 | +### Installation |
13 | 10 |
|
14 | | -## Getting Started |
| 11 | +```bash |
| 12 | +npm install solid-js solid-create-script |
| 13 | +pnpm add solid-js solid-create-script |
| 14 | +yarn add solid-js solid-create-script |
| 15 | +bun add solid-js solid-create-script |
| 16 | +``` |
15 | 17 |
|
16 | | -Some pre-requisites before install dependencies: |
| 18 | +### Usage |
17 | 19 |
|
18 | | -- Install Node Version Manager (NVM) |
19 | | - ```bash |
20 | | - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash |
21 | | - ``` |
22 | | -- Install Bun |
23 | | - ```bash |
24 | | - curl -fsSL https://bun.sh/install | bash |
25 | | - ``` |
| 20 | +```tsx |
| 21 | +const script = createScript("https://some-library.js"); |
| 22 | +const script = createScript("https://some-library.js", { async: true, ...scriptAttributes }); |
| 23 | +const script = createScript("https://some-library.js", { defer: true, ...scriptAttributes }); |
| 24 | +``` |
26 | 25 |
|
27 | | -### Installing Dependencies |
| 26 | +Under the hood `createScript` makes use of the `createResource` Solid API when loading the desired script at the specified `src`. As such, the result of `createScript` is a `Resource<Event>` object that allows you to inspect when the script has finished loading or returned an error via `script.loading` and `script.error` respectively. |
28 | 27 |
|
29 | | -```bash |
30 | | -nvm use |
31 | | -bun install |
32 | | -``` |
| 28 | +When using `createScript`, here are some points to be aware of: |
33 | 29 |
|
34 | | -### Local Development Build |
| 30 | +- The created `<script>` tag will be appeneded to `<head>`. |
| 31 | +- The created `<script>` tag will not be removed from the DOM when a component that uses this hook unmounts. (i.e. we do not make use of `onCleanup` to remove the `<script>` from `<head>`). |
| 32 | +- The hook will ensure no duplicate `<script>` tags referencing the same `src` will be created. Moreover, when multiple components reference the same `src`, they will all point to the same shared resource ensuring consistency within the reactive graph. |
35 | 33 |
|
36 | | -```bash |
37 | | -bun start |
38 | | -``` |
| 34 | +### Full Example |
39 | 35 |
|
40 | | -### Linting & Formatting |
| 36 | +```tsx |
| 37 | +import { Switch, Match } from "solid-js"; |
| 38 | +import { createScript } from "solid-create-script"; |
41 | 39 |
|
42 | | -```bash |
43 | | -bun run lint # checks source for lint violations |
44 | | -bun run format # checks source for format violations |
| 40 | +function App() { |
| 41 | + const script = createScript("https://some-library.js"); |
45 | 42 |
|
46 | | -bun run lint:fix # fixes lint violations |
47 | | -bun run format:fix # fixes format violations |
| 43 | + return ( |
| 44 | + <Switch fallback={<ScriptProvider>...</ScriptProvider>}> |
| 45 | + <Match when={script.loading}>Loading Script...</Match> |
| 46 | + <Match when={script.error}>Failed to load script: {script.error.message}</Match> |
| 47 | + </Switch> |
| 48 | + ); |
| 49 | +} |
48 | 50 | ``` |
49 | 51 |
|
50 | | -### Contributing |
51 | | - |
52 | | -The only requirements when contributing are: |
| 52 | +### Feedback |
53 | 53 |
|
54 | | -- You keep a clean git history in your branch |
55 | | - - rebasing `main` instead of making merge commits. |
56 | | -- Using proper commit message formats that adhere to [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) |
57 | | - - Additionally, squashing (via rebase) commits that are not [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) |
58 | | -- CI checks pass before merging into `main` |
| 54 | +Feel free to post any issues or suggestions to help improve this utility hook. |
0 commit comments