-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
136 lines (120 loc) · 3.38 KB
/
Copy pathbuild.mjs
File metadata and controls
136 lines (120 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import fs from 'node:fs';
import zlib from 'node:zlib';
import {build} from '@nfps.dev/sdk';
import * as dotenv from 'dotenv';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
// load environment variables
dotenv.config();
const h_env = process.env;
const {
NFP_SELF_CHAIN: si_chain,
NFP_SELF_CONTRACT: sa_contract,
NFP_SELF_TOKEN: si_token,
NODE_ENV: SI_NODE_ENV,
} = h_env;
const B_DEV = 'development' === SI_NODE_ENV;
const h_argv = yargs(hideBin(process.argv))
.scriptName('build').usage('$0 [flags]')
.option('a', {
alias: 'autoboot',
describe: 'omits the actual bootloader and loads the local app directly (development MUST be enabled)',
default: false,
type: 'boolean',
})
.option('o', {
alias: 'output',
describe: 'output file',
default: './dist/nfp.svg',
})
.option('l', {
alias: 'link',
describe: 'link to stylesheets and scripts instead of inlining during development',
default: false,
type: 'boolean',
})
.alias('v', 'version').alias('h', 'help').help().argv;
// make sure autoboot is only used in development mode
if(h_argv.autoboot && !B_DEV) {
throw new Error(`Option '-a' can only be used when NODE_ENV=development`);
}
const sx_out = await build({
source: fs.readFileSync('./media/template.svg'),
metadata: {
web: {
lcds: h_env['NFP_WEB_LCDS']?.split(','),
comcs: h_env['NFP_WEB_COMCS']?.split(','),
},
self: {
chain: si_chain,
contract: sa_contract,
token: si_token,
},
},
macros: {
// inline global stylesheet
global_style: ({create}) => [
// depending on environment
B_DEV && h_argv.link
// development: link to css file
? create.svg('style', {}, [
`@import url(../media/global.css);`,
])
// testing/production: inline css
: create.svg('style', {}, [
fs.readFileSync('./media/global.css'),
]),
],
// creates a link to a hosted sandbox container
launch_sandbox: ({create, body}) => [
create.html('a', {
href: `https://nfps.dev/sandbox?${new URLSearchParams({
chain: si_chain,
contract: sa_contract,
token: si_token,
})}`,
}, [
create.html('button', {}, body ?? ['Launch in browser']),
]),
],
},
closeDocument({document, create}) {
const dm_root = document.documentElement;
// inject content
dm_root.append(...h_argv.autoboot
// autoboot mode: omit bootloader and use autoboot script instead
? [
// autoboot (should inject app once it's completed any async bootup tasks)
create.svg('script', {
href: './_autoboot.dev.js'
}),
]
// use bootloader
: [
// bootloader; depending on environment
B_DEV && h_argv.link
// development: link to bootloader for better debugging experience in browser
? create.svg('script', {
href: './bootloader.dev.js'
})
// testing/production: inline script
: create.svg('script', {}, [
fs.readFileSync(`./dist/bootloader${B_DEV? '.dev': ''}.js`, 'utf8'),
]),
// fetch latest app from chain
create.nfp('script', {
src: 'app.js?tag=latest',
}),
]
);
},
});
fs.writeFileSync(h_argv.output, sx_out);
fs.copyFileSync('./media/preview.html', './dist/preview.html');
// compress
if(!B_DEV) {
const atu8_compressed = zlib.gzipSync(sx_out);
fs.writeFileSync(h_argv.output+'.gz', atu8_compressed);
console.log(`Optimized SVG size: ${sx_out.length} bytes`)
console.log(`Compressed file size: ${atu8_compressed.byteLength} bytes`);
}