flecks/website/docs/creating-a-fleck.mdx

156 lines
4.4 KiB
Plaintext
Raw Normal View History

2023-12-31 16:23:28 -06:00
---
title: Creating a fleck
description: A fleck is a module but also so much more.
---
If you are following along from the previous getting started
2024-01-04 18:39:21 -06:00
[configuration page](./configuration), you have an application with 2 flecks:
2023-12-31 16:23:28 -06:00
- `@flecks/core`
- `@flecks/server`
<details>
2024-01-04 18:39:21 -06:00
<summary>About that "2 flecks" thing...</summary>
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
Actually, your server application has **4 flecks** at this point:
2023-12-31 16:23:28 -06:00
- `@flecks/core`
- `@flecks/core/server`
- `@flecks/server`
- `@flecks/server/server`
flecks will load the `[...]/server` fleck under any fleck that is loaded on the server. This is
also the case when using `@flecks/web` which will automatically load `[...]/client` flecks
which are only loaded in the browser. We'll be exploring this specifically in the next section.
If you're intersted in diving deeper, see [the platforms concept page](#todo).
2024-01-04 18:39:21 -06:00
Some frameworks make it a little more opaque to work with isomorphic code, but the flecks
2023-12-31 16:23:28 -06:00
philosophy is that visibility is believability: you'll have to be explicit about which code
runs where.
</details>
## Your first fleck
Let's make your website greet the user with a good ol' **hello world**.
2024-01-03 16:14:01 -06:00
To do this, you'll be creating your own little fleck. flecks also provides a built-in utility to
create a fleck.
2023-12-31 16:23:28 -06:00
2024-01-06 20:31:12 -06:00
:::note[Your application is a monorepo by default]
2023-12-31 16:23:28 -06:00
2024-01-03 16:14:01 -06:00
You may have noticed that your little starter application has a `packages` directory. By default,
flecks structures your application as a monorepo.
This isn't any hard requirement, it's only a suggestion.
:::
2023-12-31 16:23:28 -06:00
2024-01-04 03:23:04 -06:00
### Create the fleck
2024-01-03 16:14:01 -06:00
Let's create our little fleck:
2023-12-31 16:23:28 -06:00
2024-01-04 15:51:19 -06:00
```bash
npx create-fleck say-hello
```
2024-01-03 16:14:01 -06:00
2024-01-04 03:23:04 -06:00
After some output, you'll find your new fleck at `packages/say-hello`. Let's inspect our
`build/flecks.yml`:
2023-12-31 16:23:28 -06:00
2024-01-04 03:23:04 -06:00
```yml title="build/flecks.yml"
2023-12-31 16:23:28 -06:00
'@flecks/core':
id: 'hello_world'
'@flecks/server': {}
// highlight-next-line
2024-01-03 16:14:01 -06:00
'@hello-world/say-hello:./packages/say-hello/src': {}
2023-12-31 16:23:28 -06:00
```
### Aliasing for the win
Notice there's a colon separating the path for this one. This is because this is an
[aliased fleck](#todo). The part before the colon is the alias and the part after is the
path to the package.
2024-01-03 16:14:01 -06:00
By the way, your other application code can import using the alias (e.g.
`require('@hello-world/say-hello');`)
2023-12-31 16:23:28 -06:00
[as if it were a package](https://webpack.js.org/configuration/resolve/#resolvealias).
<details>
2024-01-04 03:23:04 -06:00
<summary>Wait, my modules don't have to be in <code>node_modules</code>?</summary>
2023-12-31 16:23:28 -06:00
2024-01-03 16:14:01 -06:00
Nope! When you're developing applications, it can be real nice to
2023-12-31 16:23:28 -06:00
just pull in local source "packages". If you're wondering how this works, see
[the alias concept page](#todo).
2024-01-04 03:23:04 -06:00
That being said, sharing your packages on npm is a cool thing to do, so be rad and share your
awesome flecks with the rest of us!
:::warning
2023-12-31 16:23:28 -06:00
You probably shouldn't do things like name an alias the same thing as a package that actually
2024-01-03 16:14:01 -06:00
exists in your `node_modules` directory. This is mitigated if you use the default monorepo
structure (unless your application name is identical to a monorepo organization that already
exists on `npm`: don't do that).
If you'd like to help define what happens in these edge cases you could
always [submit a pull request](https://github.com/cha0s/flecks/compare). :smile:
2023-12-31 16:23:28 -06:00
2024-01-04 03:23:04 -06:00
:::
2023-12-31 16:23:28 -06:00
</details>
2024-01-04 03:23:04 -06:00
### Your first hook
There is a source file at `packages/say-hello/src/index.js` but for now it's empty. Let's fill it
out a bit:
```javascript title="packages/say-hello/src/index.js"
2024-01-04 18:39:21 -06:00
export const hooks = {
'@flecks/server.up': async () => {
process.stdout.write(' hello server\n');
2024-01-04 03:23:04 -06:00
},
};
```
2024-01-04 18:39:21 -06:00
Now, restart your application:
2023-12-31 16:23:28 -06:00
2024-01-04 03:23:04 -06:00
```bash
2024-01-04 18:39:21 -06:00
npm run start
2024-01-04 03:23:04 -06:00
```
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
You will be greeted by a line in the output:
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
```terminal
hello server
2023-12-31 16:23:28 -06:00
```
2024-01-04 18:39:21 -06:00
### flecks injection
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
Hook implementations may receive arguments. After any arguments, the `flecks` instance is always
passed.
2024-01-03 16:14:01 -06:00
2024-01-04 18:39:21 -06:00
`@flecks/server.up` doesn't pass any arguments, so the `flecks` instance is the first argument.
Let's see how to use the instance to read some configuration:
2024-01-04 03:23:04 -06:00
2024-01-04 18:39:21 -06:00
```javascript title="packages/say-hello/src/index.js"
export const hooks = {
'@flecks/server.up': async (flecks) => {
const {id} = flecks.get('@flecks/core');
process.stdout.write(` hello server: ID ${id}\n`);
},
};
```
2024-01-04 03:23:04 -06:00
2024-01-04 18:39:21 -06:00
This time, you will see:
2024-01-04 03:23:04 -06:00
2024-01-04 18:39:21 -06:00
```terminal
hello server: ID hello-world
2024-01-04 03:23:04 -06:00
```
2024-01-03 16:14:01 -06:00
2024-01-06 20:31:12 -06:00
:::note[Still with us?]
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
...or whatever your application's ID is. We're assuming you're following along from
[the configuration page](./configuration).
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
:::
2023-12-31 16:23:28 -06:00
2024-01-04 18:39:21 -06:00
Next, we'll add and interact with some of the flecks shipped by default.