UNPKG

10.5 kBMarkdownView Raw
1# APEX Nitro code patterns
2
3One of the most beneficial aspect of APEX Nitro that it helps creating a clean and logical file structure for JavaScript, CSS and others.
4
5This document explains many patterns you can use to structure your files. For simplification purposes, this document assumes the APEX Nitro files are uploaded to `Shared Components > Application Static Files`, so we will use `#APP_IMAGES#` to reference the files root path in APEX.
6
7Your folder structure will be different based on the APEX Nitro mode you picked when running `apex-nitro init`.
8
9## File structure in APEX Nitro Basic mode
10
11If you have picked the basic mode for your project, here are some file structure examples you can follow.
12
13### Basic structure 1 (simplest)
14
15This is the simplest way we can structure files for APEX Nitro:
16
17```bash
18|-/myprojectpath/
19 |-apexnitro.config.json
20 |-/src/
21 |-myproject.css
22 |-myproject.js
23```
24
25Notes:
26
27- This simple structure contains two files: `myproject.css` and `myproject.js`
28- Both files are located in the `/myprojectpath/src/` directory
29
30In APEX, we would reference those files as:
31
32```bash
33#APP_IMAGES#myproject#MIN#.css
34#APP_IMAGES#myproject#MIN#.js
35```
36
37### Basic structure 2 (subfolders)
38
39As you develop your application, you may require to include more and more files in your directory. In that case it becomes useful to split those files into separate subfolders. In the long run, this structure makes it easier to maintain your files and dependencies.
40
41```bash
42|-/myprojectpath/
43 |-apexnitro.config.json
44 |-/src/
45 |-css
46 |-myproject.css
47 |-js
48 |-myproject.js
49 |-lib
50 |-bootstrap.css
51 |-bootstrap.js
52 |-img
53 |-logo.png
54```
55
56In APEX, we would reference those files as:
57
58```bash
59#APP_IMAGES#css/myproject.css
60#APP_IMAGES#js/myproject.js
61#APP_IMAGES#lib/bootstrap.css
62#APP_IMAGES#lib/bootstrap.js
63#APP_IMAGES#img/logo.png
64```
65
66## File structure in APEX Nitro Pro mode
67
68APEX Nitro Pro mode offers the best experience for developers. It improves the performance of your files, allows next generation JavaScripts, allows CSS pre-processors and much more. This section reviews how APEX Nitro Pro works and provides file structure patterns.
69
70### Building the source files
71
72When using APEX Nitro Pro, the content of your `/src/` directory will no longer be synchronized to APEX. Instead, APEX Nitro compiles your `/src/` directory into a new directory called `/build/`. By compiling your code, APEX Nitro is able to tweak it so that it performs faster, resolves next generation JavaScript, and more.
73
74There are two ways of compiling your code:
75
761. `apex-nitro build`: compiles `/src/` to `/build/`
771. `apex-nitro launch`: compiles `/src/` to `/build/` and opens your app, allowing real time edits.
78
79Once your code is compiled, only the `/build/` directory is exposed to APEX. APEX only sees your compiled code. From a directory perspective, building the source files looks like this:
80
81```bash
82|-/myprojectpath/
83 |-apexnitro.config.json
84 |-/src/
85 |-main.css
86 |-main.js
87```
88
89Compiles to:
90
91```bash
92|-/myprojectpath/
93 |-apexnitro.config.json
94 |-/build/
95 |-myproject.css
96 |-myproject.js
97```
98
99### ./src/main.js
100
101When initializing a new APEX Nitro Pro project, a simple file structure will be generated for you in your `/src/` directory:
102
103```bash
104|-/myprojectpath/
105 |-apexnitro.config.json
106 |-/src/
107 |-main.css
108 |-main.js
109```
110
111`main.js` and `main.css` represent the main entry point for your front-end code. Keep them.
112
113We will use `main.js` to import the JavaScript, CSS and other code, and generate a single output file which will be much easier to manage in your APEX app. Let's expand our structure and add `p1.js`, `p2.js` and `p3.js`:
114
115```bash
116|-/myprojectpath/
117 |-apexnitro.config.json
118 |-/src/
119 |-/js/
120 |-p1.js
121 |-p2.js
122 |-p3.js
123 |-main.css
124 |-main.js
125```
126
127Now let's have `main.js` import `p1.js`, `p2.js` and `p3.js`:
128
129```javascript
130// main.js
131
132import './main.css';
133
134import p1 from './js/p1.js';
135import p2 from './js/p2.js';
136import p3 from './js/p3.js';
137
138export {
139 p1,
140 p2,
141 p3
142};
143```
144
145Even if we have added 3 new files, the `/build/` directory will still generate one single output file, using your project `libraryName` from `apexnitro.config.json`:
146
147```bash
148|-/myprojectpath/
149 |-apexnitro.config.json
150 |-/build/
151 |-myproject.css
152 |-myproject.js
153 |-/src/
154 |-main.css
155 |-main.js
156```
157
158### Minify files
159
160By using APEX Nitro Pro, you automatically benefit from file minification. That means your code from the `/src/` directory will be compiled to a minified version in the `/build/` directory. For example:
161
162```bash
163|-/myprojectpath/
164 |-apexnitro.config.json
165 |-/src/
166 |-main.css
167 |-main.js
168```
169
170Compiles to:
171
172```bash
173|-/myprojectpath/
174 |-apexnitro.config.json
175 |-/build/
176 |-myproject.css
177 |-myproject.js
178 |-myproject.min.css
179 |-myproject.min.js
180```
181
182In APEX, we would reference those files as:
183
184```bash
185#APP_IMAGES#myproject#MIN#.css
186#APP_IMAGES#myproject#MIN#.js
187```
188
189### Global variables and external libraries
190
191As your codebase grow, you might be interested in using external libraries instead of writing your own code to solve common problems. When you use external libraries, those often come with global variables you can use to easily reference the objects of the library.
192
193For example, `Moment.js` is a popular library to parse, validate, manipulate, and display dates and times in JavaScript. You can use `Moment.js` by simply adding their script in your app and start using their global variable, like this:
194
195```javascript
196<script src="moment.js"></script>
197<script>
198 moment().format();
199</script>
200```
201
202With this code, `moment` is then a globally accessible variable. However, if we try to access the `moment` object in APEX Nitro, we will receive an error.
203
204That is because APEX Nitro can't possibly know that you have included Moment.js externally in your app. To fix that, open your project `apexnitro.config.json` file, look for the `external` array and add the `moment` object:
205
206```json
207{
208 "mode": "pro",
209 "template": "apex-nitro-template-default",
210 "libraryName": "myproject",
211 "main": "./src/main.js",
212 "srcFolder": "./src",
213 "distFolder": "./build",
214 "external": [
215 "apex",
216 "moment"
217 ],
218 ...
219}
220```
221
222That is basically a way of saying to APEX Nitro "Don't worry, I know what I'm doing with the moment object".
223
224### Pro structure 1 (default)
225
226We encourage to create small granular files based on your application logic. A common structure looks like:
227
228```bash
229|-/myprojectpath/
230 |-apexnitro.config.json
231 |-/src/
232 |-css
233 |-header.css
234 |-main.css
235 |-footer.css
236 |-js
237 |-p1.js
238 |-p2.js
239 |-util.js
240 |-main.js
241```
242
243Compiles to:
244
245```bash
246|-/myprojectpath/
247 |-apexnitro.config.json
248 |-/build/
249 |-myproject.css
250 |-myproject.js
251 |-myproject.min.css
252 |-myproject.min.js
253```
254
255In APEX, we would reference those files as:
256
257```bash
258#APP_IMAGES#myproject#MIN#.css
259#APP_IMAGES#myproject#MIN#.js
260```
261
262### Pro structure 2 (with static files)
263
264Often times in JavaScript or CSS, you need access to other types of files, like images, icons or external libraries. In APEX Nitro, you can put those files in a special directory name `/src/static/` and those files won't be compiled, minified or anything but you will still be able to access them.
265
266```bash
267|-/myprojectpath/
268 |-apexnitro.config.json
269 |-/src/
270 |-static
271 |-image1.png
272 |-image2.png
273 |-main.css
274 |-main.js
275```
276
277Compiles to:
278
279```bash
280|-/myprojectpath/
281 |-apexnitro.config.json
282 |-/build/
283 |-static
284 |-image1.png
285 |-image2.png
286 |-myproject.css
287 |-myproject.js
288 |-myproject.min.css
289 |-myproject.min.js
290```
291
292In APEX, we would reference those files as:
293
294```bash
295#APP_IMAGES#myproject#MIN#.css
296#APP_IMAGES#myproject#MIN#.js
297#APP_IMAGES#static/image1.png
298#APP_IMAGES#static/image2.png
299```
300
301## Using APEX Nitro on version control systems
302
303You may have noticed that when using `apex-nitro launch`, you APEX app opens, but the URL shows `localhost`. That is because APEX Nitro uses a proxy to display your application while accessing localhost ressources (your files). Other team members obviously don't have access to your `localhost`, so how do you work as a team using APEX Nitro?
304
305Let's assume that you are using a version control system, like Git. Your repository should look somehow similar to this:
306
307```bash
308|-/myprojectpath/
309 |-/apex/
310 |-/f200.sql
311 |-/packages/
312 |-/mypkg.pks
313 |-/mypkg.pkb
314 |-/views/
315 |-/myview.sql
316 |-/www/
317 |-/src/
318 |-css/
319 |-header.css
320 |-footer.css
321 |-js/
322 |-p10.js
323 |-p20.js
324 |-img/
325 |-background.png
326 |-logo.png
327 |-/build/
328 |-css/
329 |-myproject.css
330 |-myproject.min.css
331 |-js/
332 |-myproject.js
333 |-myproject.min.js
334 |-img/
335 |-background.png
336 |-logo.png
337```
338
339From a structural perspective
340
341- `/myprojectpath/www/src/` is where you do the coding.
342- `/myprojectpath/www/build/` is what's being exposed to your APEX app.
343
344To work as a team, here's the typical workflow:
345
3461. Every team member installs APEX Nitro
3471. Every team member use `apex-nitro launch` when they are working on the project front-end files (`/myprojectpath/www/src/`)
3481. Have your version control system ignore the entire `/myprojectpath/www/build/` folder by adding `/myprojectpath/www/build/` to the repository `.gitignore` file
349 The `/myprojectpath/www/build/` folder should not be committed
350 Only `/myprojectpath/www/src/` should be committed
3511. During your own development cycle, you make changes to any files you want within `/myprojectpath/www/src/` and you will be the only one seeing those changes in your APEX app through your localhost proxy, as long as you don't commit `/myprojectpath/www/src/`
3521. When you your changes are stable, commit `/myprojectpath/www/src/`
3531. Other team members can refresh their repository
3541. Other team members APEX Nitro will automatically pick up the updated files and it will push it automatically to their app
355
356By using this workflow, you are ensuring that your JavaScript and CSS development doesn't affect other team members until you think it's stable.
357
358In addition to commiting your `/myprojectpath/www/src/` folder, use `apex-nitro upload` to upload your files directly in APEX Shared Components, so anyone using your app can benefit from your changes without installing APEX Nitro.