@airiot/cli 1.0.6 → 1.0.7
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.
- package/package.json +1 -1
- package/vite-plugin/buildinfo.js +42 -1
- package/vite-plugin/plugin.js +93 -82
package/package.json
CHANGED
package/vite-plugin/buildinfo.js
CHANGED
|
@@ -9,7 +9,6 @@ const isCI = process.env.CI &&
|
|
|
9
9
|
|
|
10
10
|
const jsonData = await fsPromises.readFile(paths.appPackageJson, 'utf-8');
|
|
11
11
|
const appPackage = JSON.parse(jsonData);
|
|
12
|
-
appPackage.dependencies = {}
|
|
13
12
|
|
|
14
13
|
const gitInfo = (...args) => {
|
|
15
14
|
return execFileSync('git', args, {
|
|
@@ -52,12 +51,54 @@ const buildinfoPlugin = () => {
|
|
|
52
51
|
if (config.command === 'serve') {
|
|
53
52
|
return
|
|
54
53
|
}
|
|
54
|
+
// check package.json exports and files
|
|
55
|
+
try {
|
|
56
|
+
appPackage.exports = appPackage.exports || {}
|
|
57
|
+
|
|
58
|
+
if (!appPackage.exports.airiot) {
|
|
59
|
+
const inputs = config.build && config.build.rollupOptions && config.build.rollupOptions.input
|
|
60
|
+
const outputTemplate = config.build && config.build.rollupOptions && config.build.rollupOptions.output && config.build.rollupOptions.output.entryFileNames
|
|
61
|
+
? config.build.rollupOptions.output.entryFileNames
|
|
62
|
+
: '[name].js'
|
|
63
|
+
const outDirName = (config.build && config.build.outDir) ? config.build.outDir : 'dist'
|
|
64
|
+
const makeFileName = (name) => outputTemplate.replace(/\[name\]/g, name)
|
|
65
|
+
|
|
66
|
+
const exportsObj = {}
|
|
67
|
+
|
|
68
|
+
if (inputs && typeof inputs === 'object') {
|
|
69
|
+
for (const name of Object.keys(inputs)) {
|
|
70
|
+
const fileName = makeFileName(name)
|
|
71
|
+
const exportKey = name
|
|
72
|
+
const relPath = './' + join(outDirName, fileName).replace(/\\/g, '/') + '?type=module'
|
|
73
|
+
exportsObj[exportKey] = relPath
|
|
74
|
+
}
|
|
75
|
+
} else if (typeof inputs === 'string') {
|
|
76
|
+
const base = 'index'
|
|
77
|
+
const fileName = makeFileName(base)
|
|
78
|
+
exportsObj[base] = './' + join(outDirName, fileName).replace(/\\/g, '/') + '?type=module'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
appPackage.exports.airiot = exportsObj
|
|
82
|
+
|
|
83
|
+
// ensure files includes outDir so published package contains built files
|
|
84
|
+
appPackage.files = Array.isArray(appPackage.files) ? appPackage.files : []
|
|
85
|
+
if (!appPackage.files.includes(outDirName)) {
|
|
86
|
+
appPackage.files.push(outDirName)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
writeFileSync(paths.appPackageJson, JSON.stringify(appPackage, 0, 2), { flag: 'w' })
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {
|
|
92
|
+
// silently ignore errors here
|
|
93
|
+
}
|
|
55
94
|
|
|
56
95
|
if (!isCI) {
|
|
57
96
|
try {
|
|
58
97
|
gitInfo('log')
|
|
59
98
|
const branch = gitInfo('rev-parse', '--abbrev-ref', 'HEAD')
|
|
60
99
|
const longHead = gitInfo('rev-parse', 'HEAD')
|
|
100
|
+
|
|
101
|
+
appPackage.dependencies = {}
|
|
61
102
|
appPackage.gitHead = longHead
|
|
62
103
|
|
|
63
104
|
if (branch != 'master' && branch != 'HEAD') {
|
package/vite-plugin/plugin.js
CHANGED
|
@@ -12,6 +12,7 @@ const findModule = (scripts, packageName) => {
|
|
|
12
12
|
return scripts.find(s => s.src && s.src.indexOf(packageName) >= 0)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
const loadOnline = true
|
|
15
16
|
const host = getHost()
|
|
16
17
|
const basePath = getBaseUrl() + '/node_modules'
|
|
17
18
|
|
|
@@ -19,11 +20,7 @@ const defaultModules = [
|
|
|
19
20
|
'@airiot/i18n', '@airiot/theme', '@airiot/core'
|
|
20
21
|
]
|
|
21
22
|
|
|
22
|
-
let scripts
|
|
23
|
-
&& developConfig.scripts(basePath, 'admin') || []
|
|
24
|
-
|
|
25
|
-
let frontScripts = (developConfig && typeof developConfig.scripts == 'function')
|
|
26
|
-
&& developConfig.scripts(basePath, 'front') || []
|
|
23
|
+
let scripts, frontScripts;
|
|
27
24
|
|
|
28
25
|
let dllScript = basePath + '/@airiot/dll/dist/iot.min.js',
|
|
29
26
|
dllDevScript = basePath + '/@airiot/dll/dist/iot.js';
|
|
@@ -37,90 +34,104 @@ const filterDll = s => {
|
|
|
37
34
|
return true
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
frontScripts = frontScripts.filter(filterDll).map(s => typeof s === 'string' ? { src: s } : s);
|
|
37
|
+
const loadPackageScripts = async () => {
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
scripts = (developConfig && typeof developConfig.scripts == 'function')
|
|
40
|
+
&& await developConfig.scripts(basePath, 'admin') || []
|
|
44
41
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const packagePath = basePath + '/' + packageName
|
|
48
|
-
try {
|
|
49
|
-
let pkgUrl = packagePath.replace(/\/+$/,'') + '/package.json'
|
|
50
|
-
pkgUrl = (pkgUrl.startsWith("http://") || pkgUrl.startsWith("https://")) ? pkgUrl : (host + pkgUrl)
|
|
51
|
-
const res = await fetch(pkgUrl)
|
|
52
|
-
if (!res.ok) {
|
|
53
|
-
return [ packagePath + '/dist/index.js', packagePath + '/dist/front.js' ]
|
|
54
|
-
}
|
|
55
|
-
const pkg = await res.json()
|
|
56
|
-
|
|
57
|
-
const unwrap = entry => {
|
|
58
|
-
if (!entry) return null
|
|
59
|
-
if (typeof entry === 'string') return entry
|
|
60
|
-
if (typeof entry === 'object') {
|
|
61
|
-
// Prefer standard fields or first available value
|
|
62
|
-
return unwrap(entry.import || entry.module || entry.default || Object.values(entry)[0])
|
|
63
|
-
}
|
|
64
|
-
return null
|
|
65
|
-
}
|
|
42
|
+
frontScripts = (developConfig && typeof developConfig.scripts == 'function')
|
|
43
|
+
&& await developConfig.scripts(basePath, 'front') || []
|
|
66
44
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
for (const [k, v] of new URLSearchParams(qs)) {
|
|
70
|
-
if (obj[k] === undefined) obj[k] = v
|
|
71
|
-
else if (Array.isArray(obj[k])) obj[k].push(v)
|
|
72
|
-
else obj[k] = [obj[k], v]
|
|
73
|
-
}
|
|
74
|
-
return obj
|
|
75
|
-
}
|
|
45
|
+
scripts = scripts.filter(filterDll).map(s => typeof s === 'string' ? { src: s } : s);
|
|
46
|
+
frontScripts = frontScripts.filter(filterDll).map(s => typeof s === 'string' ? { src: s } : s);
|
|
76
47
|
|
|
77
|
-
|
|
78
|
-
if (!rel) return null
|
|
79
|
-
// allow "./foo.js" or "foo.js" or "/foo.js"
|
|
80
|
-
let url = base.replace(/\/+$/,'') + '/' + rel.replace(/^\.?\/+/,'')
|
|
81
|
-
const qIndex = url.indexOf('?')
|
|
82
|
-
if (qIndex >= 0) {
|
|
83
|
-
const hashIndex = url.indexOf('#', qIndex)
|
|
84
|
-
const qs = url.slice(qIndex + 1, hashIndex >= 0 ? hashIndex : undefined)
|
|
85
|
-
const cleanUrl = hashIndex >= 0 ? url.slice(0, hashIndex) : url.slice(0, qIndex)
|
|
86
|
-
return { src: cleanUrl, ...parseQuery(qs) }
|
|
87
|
-
}
|
|
88
|
-
return { src: url }
|
|
89
|
-
}
|
|
48
|
+
const packageNames = [ ...defaultModules, ...(appPackage.iotDependencies||[]) ];
|
|
90
49
|
|
|
91
|
-
|
|
50
|
+
const packageScripts = await Promise.all(packageNames.map(async packageName => {
|
|
51
|
+
if(packageName != appPackage.name && !findModule(scripts, packageName)) {
|
|
52
|
+
const packagePath = basePath + '/' + packageName
|
|
92
53
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
54
|
+
if (loadOnline) {
|
|
55
|
+
try {
|
|
56
|
+
let pkgUrl = packagePath.replace(/\/+$/,'') + '/package.json'
|
|
57
|
+
pkgUrl = (pkgUrl.startsWith("http://") || pkgUrl.startsWith("https://")) ? pkgUrl : (host + pkgUrl)
|
|
58
|
+
const res = await fetch(pkgUrl)
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
return [ packagePath + '/dist/index.js', packagePath + '/dist/front.js' ]
|
|
61
|
+
}
|
|
62
|
+
const pkg = await res.json()
|
|
63
|
+
|
|
64
|
+
const unwrap = entry => {
|
|
65
|
+
if (!entry) return null
|
|
66
|
+
if (typeof entry === 'string') return entry
|
|
67
|
+
if (typeof entry === 'object') {
|
|
68
|
+
// Prefer standard fields or first available value
|
|
69
|
+
return unwrap(entry.import || entry.module || entry.default || Object.values(entry)[0])
|
|
70
|
+
}
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
98
73
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
74
|
+
const parseQuery = qs => {
|
|
75
|
+
const obj = {}
|
|
76
|
+
for (const [k, v] of new URLSearchParams(qs)) {
|
|
77
|
+
if (obj[k] === undefined) obj[k] = v
|
|
78
|
+
else if (Array.isArray(obj[k])) obj[k].push(v)
|
|
79
|
+
else obj[k] = [obj[k], v]
|
|
80
|
+
}
|
|
81
|
+
return obj
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const join = (base, rel) => {
|
|
85
|
+
if (!rel) return null
|
|
86
|
+
// allow "./foo.js" or "foo.js" or "/foo.js"
|
|
87
|
+
let url = base.replace(/\/+$/,'') + '/' + rel.replace(/^\.?\/+/,'')
|
|
88
|
+
const qIndex = url.indexOf('?')
|
|
89
|
+
if (qIndex >= 0) {
|
|
90
|
+
const hashIndex = url.indexOf('#', qIndex)
|
|
91
|
+
const qs = url.slice(qIndex + 1, hashIndex >= 0 ? hashIndex : undefined)
|
|
92
|
+
const cleanUrl = hashIndex >= 0 ? url.slice(0, hashIndex) : url.slice(0, qIndex)
|
|
93
|
+
return { src: cleanUrl, ...parseQuery(qs) }
|
|
94
|
+
}
|
|
95
|
+
return { src: url }
|
|
96
|
+
}
|
|
103
97
|
|
|
104
|
-
|
|
105
|
-
const frontScript = frontRel ? join(packagePath, frontRel) : null
|
|
98
|
+
let adminRel = null, frontRel = null
|
|
106
99
|
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
if (pkg.exports && pkg.exports.airiot) {
|
|
101
|
+
const a = pkg.exports.airiot
|
|
102
|
+
adminRel = unwrap(a.admin || a.index )
|
|
103
|
+
frontRel = unwrap(a.front)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!adminRel && !frontRel) {
|
|
107
|
+
adminRel = pkg.main || null
|
|
108
|
+
frontRel = pkg.iotFront || null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const adminScript = adminRel ? join(packagePath, adminRel) : null
|
|
112
|
+
const frontScript = frontRel ? join(packagePath, frontRel) : null
|
|
113
|
+
|
|
114
|
+
return [ adminScript, frontScript ]
|
|
115
|
+
} catch (e) { }
|
|
116
|
+
}
|
|
109
117
|
return [ packagePath + '/dist/index.js', packagePath + '/dist/front.js' ]
|
|
110
118
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
}
|
|
119
|
+
return null
|
|
120
|
+
}));
|
|
121
|
+
|
|
122
|
+
packageScripts.forEach(scriptsArr => {
|
|
123
|
+
if(scriptsArr) {
|
|
124
|
+
const [adminScript, frontScript] = scriptsArr
|
|
125
|
+
adminScript && scripts.push(typeof adminScript === "string" ? { src: adminScript } : adminScript)
|
|
126
|
+
frontScript && frontScripts.push(typeof frontScript === "string" ? { src: frontScript } : frontScript)
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
122
130
|
|
|
123
|
-
const getHtmlScripts = (html, isAdmin, mode) => {
|
|
131
|
+
const getHtmlScripts = async (html, isAdmin, mode) => {
|
|
132
|
+
if(!scripts || !frontScripts) {
|
|
133
|
+
await loadPackageScripts()
|
|
134
|
+
}
|
|
124
135
|
const appendScripts = isAdmin ? scripts : frontScripts
|
|
125
136
|
return [
|
|
126
137
|
{ tag: 'script', attrs: { }, children: 'window._r = s => s; window._t1 = s => s', injectTo: 'body-prepend' },
|
|
@@ -134,16 +145,16 @@ const airiotPlugin = (mode) => {
|
|
|
134
145
|
return {
|
|
135
146
|
name: 'airiot',
|
|
136
147
|
enforce: 'pre',
|
|
137
|
-
transformIndexHtml(html, ctx) {
|
|
138
|
-
return getHtmlScripts(html, ctx.path == '/admin.html', mode)
|
|
148
|
+
transformIndexHtml: async (html, ctx) => {
|
|
149
|
+
return await getHtmlScripts(html, ctx.path == '/admin.html', mode)
|
|
139
150
|
},
|
|
140
|
-
configurePreviewServer(server) {
|
|
151
|
+
configurePreviewServer: (server) => {
|
|
141
152
|
// 返回一个钩子,会在其他中间件安装完成后调用
|
|
142
153
|
return () => {
|
|
143
|
-
server.middlewares.use((req, res, next) => {
|
|
154
|
+
server.middlewares.use(async (req, res, next) => {
|
|
144
155
|
// 自定义处理请求 ...
|
|
145
156
|
if(req.url == '/index.html') {
|
|
146
|
-
let scripts = getHtmlScripts(htmlTpl, true, mode)
|
|
157
|
+
let scripts = await getHtmlScripts(htmlTpl, true, mode)
|
|
147
158
|
scripts = [ ...scripts.slice(0, scripts.length - 1), {
|
|
148
159
|
tag: 'script', attrs: { src: 'index.js', type: 'module' }, injectTo: 'body'
|
|
149
160
|
}, scripts[scripts.length - 1] ]
|