@airiot/cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ import paths from '../config/paths.js'
2
+ import { promises as fsPromises } from 'fs';
3
+ import { getBaseUrl, getHost, developConfig } from '../config/envs.js';
4
+
5
+ const jsonData = await fsPromises.readFile(paths.appPackageJson, 'utf-8');
6
+ const htmlTpl = await fsPromises.readFile(paths.appHtml, 'utf-8');
7
+
8
+ const appPackage = JSON.parse(jsonData);
9
+
10
+ const findModule = (scripts, packageName) => {
11
+ return scripts.find(s => s.indexOf(packageName) >= 0)
12
+ }
13
+
14
+ const host = getHost()
15
+ const basePath = getBaseUrl() + '/node_modules'
16
+
17
+ const defaultModules = [
18
+ '@airiot/i18n', '@airiot/theme', '@airiot/core'
19
+ ]
20
+
21
+ let scripts = (developConfig && typeof developConfig.scripts == 'function')
22
+ && developConfig.scripts(basePath, 'admin') || []
23
+
24
+ let frontScripts = (developConfig && typeof developConfig.scripts == 'function')
25
+ && developConfig.scripts(basePath, 'front') || []
26
+
27
+ let scriptsAppend = appPackage.name == '@airiot/core'
28
+
29
+ let dllScript = basePath + ('/@airiot/dll/dist/iot.js')
30
+ const filterDll = s => {
31
+ if(s.indexOf("@airiot/dll") >= 0) {
32
+ dllScript = s
33
+ return false
34
+ }
35
+ return true
36
+ }
37
+
38
+ scripts = scripts.filter(filterDll);
39
+ frontScripts = frontScripts.filter(filterDll);
40
+
41
+ [ ...defaultModules, ...(appPackage.iotDependencies||[]) ].forEach(packageName => {
42
+ if(packageName != appPackage.name && !findModule(scripts, packageName)) {
43
+ scripts.push(basePath + '/' + packageName + '/dist/index.js')
44
+ frontScripts.push(basePath + '/' + packageName + '/dist/front.js')
45
+ }
46
+ })
47
+
48
+ const addPublicPath = (path, publicPath) => {
49
+ if((path.startsWith('http://') || path.startsWith('https://') || path.startsWith('//'))) {
50
+ return path
51
+ } else if(path.startsWith('/')) {
52
+ return path
53
+ }
54
+ return publicPath + path
55
+ }
56
+
57
+ const getHtmlScripts = (html, isAdmin) => {
58
+ const appendScripts = isAdmin ? scripts : frontScripts
59
+ return [
60
+ { tag: 'script', attrs: { }, children: 'window._r = s => s; window._t1 = s => s', injectTo: 'body-prepend' },
61
+ { tag: 'script', attrs: { src: dllScript }, injectTo: 'body' },
62
+ ...appendScripts.map(script => ({ tag: 'script', attrs: { src: addPublicPath(script, ''), type: 'module' }, injectTo: 'body' })),
63
+ { tag: 'script', attrs: { type: 'module' }, children: '__app__.start()', injectTo: 'body' },
64
+ ]
65
+ }
66
+
67
+ const airiotPlugin = () => {
68
+ return {
69
+ name: 'airiot',
70
+ enforce: 'pre',
71
+ transformIndexHtml(html, ctx) {
72
+ return getHtmlScripts(html, ctx.path == '/admin.html')
73
+ },
74
+ configurePreviewServer(server) {
75
+ // 返回一个钩子,会在其他中间件安装完成后调用
76
+ return () => {
77
+ server.middlewares.use((req, res, next) => {
78
+ // 自定义处理请求 ...
79
+ if(req.url == '/index.html') {
80
+ let scripts = getHtmlScripts(htmlTpl, true)
81
+ scripts = [ ...scripts.slice(0, scripts.length - 1), {
82
+ tag: 'script', attrs: { src: 'index.js', type: 'module' }, injectTo: 'body'
83
+ }, scripts[scripts.length - 1] ]
84
+ const makeScriptTag = script => `<${script.tag} ${Object.keys(script.attrs).map(k => `${k}="${script.attrs[k]}"`).join(' ')}>${script.children||''}</${script.tag}>`
85
+ const html = scripts.reduce((html, script) => {
86
+ if(script.injectTo == 'body-prepend') {
87
+ return html.replace('<body>', '<body>' + makeScriptTag(script))
88
+ } else if(script.injectTo == 'body') {
89
+ return html.replace('</body>', makeScriptTag(script) + '</body>')
90
+ }
91
+ return html
92
+ }, htmlTpl)
93
+ return res.end(html)
94
+ }
95
+ next()
96
+ })
97
+ }
98
+ },
99
+ }
100
+ }
101
+
102
+ export default airiotPlugin
@@ -0,0 +1,29 @@
1
+ import { promises as fsPromises } from 'fs';
2
+ import svgLoader from "svg-inline-loader";
3
+
4
+ const svgInlinePlugin = (option) => {
5
+ return {
6
+ name: 'svg-inline',
7
+ enforce: 'pre',
8
+ async load (id) {
9
+ if (!id.match(/\.svg(\?(raw))?$/)) {
10
+ return
11
+ }
12
+ const [path, query] = id.split('?', 2)
13
+ if(query) {
14
+ return
15
+ }
16
+ let svg
17
+ try {
18
+ svg = await fsPromises.readFile(path, 'utf-8')
19
+ } catch (ex) {
20
+ debug('\n', `${id} couldn't be loaded by vite-svg-loader, fallback to default loader`)
21
+ return
22
+ }
23
+ svg = svgLoader.getExtractedSVG(svg, option);
24
+ return `export default ${JSON.stringify(svg)}`
25
+ }
26
+ }
27
+ }
28
+
29
+ export default svgInlinePlugin