|
1 | | -TODO: |
2 | | -- must import nullstack |
3 | | -- must have one component class |
4 | | -- must extend nullstack |
5 | | -- must export the class |
6 | | -- components doesent need any method to be valid |
7 | | -- will change your code with optimizations |
8 | | -- will remove server functions from client |
9 | | -- will create a registry of whitelisted function calls |
| 1 | +--- |
| 2 | +title: NJS File Extension |
| 3 | +description: Nullstack Javascript files let Webpack know which loaders to use at transpile time |
| 4 | +--- |
| 5 | + |
| 6 | +Nullstack Javascript files let Webpack know which loaders to use at transpile time. |
| 7 | + |
| 8 | +NJS files must import Nullstack because at transpile time JSX tags will be replaced with *Nullstack.element* |
| 9 | + |
| 10 | +This extension also allows Nullstack to make free transpile time optimizations like source injection and others mentioned in the documentation. |
| 11 | + |
| 12 | +On the *server* bundle static async functions are mapped into a registry for security. |
| 13 | + |
| 14 | +On the *client* bundle static async functions are removed and replaced with a flag. |
| 15 | + |
| 16 | +On both *server* and *client* bundles, a hash with the md5 of the original source code is added to the class. |
| 17 | + |
| 18 | +> 🐱💻 Bellow an example of a original .njs file. |
| 19 | +
|
| 20 | +```jsx |
| 21 | +import Nullstack from 'nullstack'; |
| 22 | + |
| 23 | +class Tasks extends Nullstack { |
| 24 | + |
| 25 | + static async getTasks({limit}) { |
| 26 | + const {readFileSync} = await import('fs'); |
| 27 | + const json = readFileSync('tasks.json', 'utf-8'); |
| 28 | + return JSON.parse(json).tasks.slice(0, limit); |
| 29 | + } |
| 30 | + |
| 31 | + prepare(context) { |
| 32 | + context.tasks = []; |
| 33 | + } |
| 34 | + |
| 35 | + async initiate(context) { |
| 36 | + context.tasks = await this.getTasks({limit: 10}); |
| 37 | + } |
| 38 | + |
| 39 | + renderTask({task}) { |
| 40 | + return ( |
| 41 | + <li> |
| 42 | + <input bind={task.description} /> |
| 43 | + </li> |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + return ( |
| 49 | + <main> |
| 50 | + <ul> |
| 51 | + {tasks.map((task) => <Task task={task} />)} |
| 52 | + </ul> |
| 53 | + </main> |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +export default Tasks; |
| 60 | +``` |
| 61 | + |
| 62 | +> 🐱💻 Bellow an example of the same transpiled .njs file. |
| 63 | +
|
| 64 | +```jsx |
| 65 | +import Nullstack from 'nullstack'; |
| 66 | + |
| 67 | +class Tasks extends Nullstack { |
| 68 | + |
| 69 | + static getTasks = true; |
| 70 | + |
| 71 | + prepare(context) { |
| 72 | + context.tasks = []; |
| 73 | + } |
| 74 | + |
| 75 | + async initiate(context) { |
| 76 | + context.tasks = await this.getTasks({limit: 10}); |
| 77 | + } |
| 78 | + |
| 79 | + renderTask({task}) { |
| 80 | + return ( |
| 81 | + <li> |
| 82 | + <input source={task} bind="description" /> |
| 83 | + </li> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + render() { |
| 88 | + const Task = this.renderTask; |
| 89 | + return ( |
| 90 | + <main> |
| 91 | + <ul> |
| 92 | + {tasks.map((task) => <Task task={task} />)} |
| 93 | + </ul> |
| 94 | + </main> |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +export default Tasks; |
| 101 | +Tasks.hash = 'd493ac09d0d57574a30f136d31da455f'; |
| 102 | +``` |
| 103 | + |
| 104 | +## Next step |
| 105 | + |
| 106 | +⚔ Learn about [server-side rendering](/server-side-rendering). |
0 commit comments