@cdlab996/lowcode-material-builder 0.0.0-stage → 2.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.
- package/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +414 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.mjs +1 -0
- package/dist/options-o2AorB1K.mjs +14 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, cdLab996
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cdlab996/lowcode-material-builder
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Builds [LowCode Engine](https://github.com/alibaba/lowcode-engine) component
|
|
4
|
+
materials with Vite, for both the Vue 2.7 and the Vue 3 line.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add -D @cdlab996/lowcode-material-builder vite
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`vite.config.ts`:
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { lowcodeMaterial } from '@cdlab996/lowcode-material-builder'
|
|
16
|
+
import { defineConfig } from 'vite'
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [
|
|
20
|
+
lowcodeMaterial({
|
|
21
|
+
// entry: 'src/index.ts',
|
|
22
|
+
// metaDir: 'src',
|
|
23
|
+
// sourceMap: true,
|
|
24
|
+
// assetsConfig: { baseUrl: 'https://unpkg.com/{name}@{version}' },
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Add other Vite plugins (for example a Vue SFC plugin) and CSS options to the
|
|
31
|
+
same config; the builder uses it for every output.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
lowcode-material build [--clean] [--mode <mode>]
|
|
35
|
+
lowcode-material dev [--host 127.0.0.1] [--port 9000] [--mode <mode>]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Output
|
|
39
|
+
|
|
40
|
+
`build` writes to `build.outDir` (default `dist`):
|
|
41
|
+
|
|
42
|
+
| File | Content |
|
|
43
|
+
| ----------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
44
|
+
| `index.js` / `index.min.js` (+ css) | Component library, assigned to `window[library]`. |
|
|
45
|
+
| `meta.js` / `meta.min.js` | `{ components }` from every `meta.*` / `*.meta.*` file, assigned to `window[library + 'Meta']`. |
|
|
46
|
+
| `assets.json` / `assets.min.json` | Assets description for the engine, pointing at `assetsConfig.baseUrl`. |
|
|
47
|
+
|
|
48
|
+
`library` defaults to a PascalCase form of the package name
|
|
49
|
+
(`@cdlab996/lowcode-materials-vant2` -> `Cdlab996LowcodeMaterialsVant2`).
|
|
50
|
+
A meta file may export one meta or an array of metas; metas without `npm` get
|
|
51
|
+
`exportName` / `subName` from `componentName` (`Select.Option`).
|
|
52
|
+
|
|
53
|
+
When the material entry re-exports components from packages
|
|
54
|
+
(`export { Button as VanButton } from 'vant'`), the package entry of
|
|
55
|
+
`assets.json` also gets a `codegen` manifest for code generation: the export
|
|
56
|
+
of each component, the bare style imports of the entry and the versions of
|
|
57
|
+
those packages.
|
|
58
|
+
|
|
59
|
+
A material can bring directives for `v-<name>` props: export them from the
|
|
60
|
+
entry as `directives`, and the renderer registers them when it loads the
|
|
61
|
+
library (on Vue 3 nothing else would: a library script cannot register a
|
|
62
|
+
directive globally there).
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { vLoading } from 'element-plus'
|
|
66
|
+
|
|
67
|
+
export const directives = { loading: vLoading } // v-loading
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Snippet screenshots can ship with the material: files under `screenshots/`
|
|
71
|
+
are copied into the output, and a snippet `screenshot` starting with `./`
|
|
72
|
+
(`./screenshots/button.webp`) is resolved against the URL of `meta.js` when
|
|
73
|
+
the meta script runs.
|
|
74
|
+
|
|
75
|
+
`dev` watches the sources, writes `assets.json` with local URLs and serves the
|
|
76
|
+
output directory with CORS enabled.
|
|
77
|
+
|
|
78
|
+
`vue`, `@cdlab996/lowcode-vue2` / `@cdlab996/lowcode-vue3`, `react` and
|
|
79
|
+
`react-dom` are read from the page globals (`Vue`, `LCVueRenderer`, `React`,
|
|
80
|
+
`ReactDOM`); add more with `externals` (module id -> global name).
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
[MIT](https://github.com/wuchendi/lowcode-engine-vue/blob/main/LICENSE)
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "./options-o2AorB1K.mjs";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { cp, glob, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
7
|
+
import { build, loadConfigFromFile, parseAst, preview } from "vite";
|
|
8
|
+
// src/assets.ts
|
|
9
|
+
function joinUrl(base, ...paths) {
|
|
10
|
+
if (base.startsWith("http")) {
|
|
11
|
+
const { origin, pathname } = new URL(base);
|
|
12
|
+
return origin + join(pathname, ...paths);
|
|
13
|
+
}
|
|
14
|
+
return join(base, ...paths);
|
|
15
|
+
}
|
|
16
|
+
function getOrigin(url) {
|
|
17
|
+
return url.match(/(https?:)?\/\/([^/]+)/)?.[0] ?? "";
|
|
18
|
+
}
|
|
19
|
+
/** Builds the `assets.json` content consumed by the LowCode Engine. */
|
|
20
|
+
function createAssets(input) {
|
|
21
|
+
const { assetsConfig, npmInfo, mode, library } = input;
|
|
22
|
+
const { baseUrl = "", groups = [], categories = [], builtinAssets = {} } = assetsConfig;
|
|
23
|
+
const currentBaseUrl = typeof baseUrl === "string" ? baseUrl : baseUrl[mode] ?? "";
|
|
24
|
+
const origin = getOrigin(currentBaseUrl);
|
|
25
|
+
const fill = (text) => text.replace(/{name}/g, npmInfo.package ?? "").replace(/{origin}/g, origin).replace(/{version}/g, npmInfo.version ?? "");
|
|
26
|
+
const resolveUrl = (file) => joinUrl(fill(input.localBaseUrl || currentBaseUrl), input.localBaseUrl ? "" : input.relativePath, file);
|
|
27
|
+
const assets = {
|
|
28
|
+
version: npmInfo.version ?? "",
|
|
29
|
+
packages: [...builtinAssets.packages ?? [], {
|
|
30
|
+
package: npmInfo.package,
|
|
31
|
+
version: npmInfo.version,
|
|
32
|
+
library,
|
|
33
|
+
urls: input.files.map(resolveUrl),
|
|
34
|
+
...input.codegen ? { codegen: input.codegen } : {}
|
|
35
|
+
}],
|
|
36
|
+
components: [...builtinAssets.components ?? [], {
|
|
37
|
+
exportName: `${library}Meta`,
|
|
38
|
+
url: resolveUrl(input.metaFile),
|
|
39
|
+
package: { npm: npmInfo.package ?? "" }
|
|
40
|
+
}],
|
|
41
|
+
sort: {
|
|
42
|
+
groupList: groups,
|
|
43
|
+
categoryList: categories
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return fill(JSON.stringify(assets, void 0, input.minify ? void 0 : 2));
|
|
47
|
+
}
|
|
48
|
+
// src/codegen.ts
|
|
49
|
+
const isBare = (source) => !source.startsWith(".") && !source.startsWith("/");
|
|
50
|
+
/** `@scope/ui/dist/a.css` -> `@scope/ui`, `vant/lib/index.css` -> `vant`. */
|
|
51
|
+
function packageName(source) {
|
|
52
|
+
const parts = source.split("/");
|
|
53
|
+
return source.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
54
|
+
}
|
|
55
|
+
const STYLE = /\.(css|less|s[ac]ss|styl)$/;
|
|
56
|
+
/**
|
|
57
|
+
* Reads the material entry: components re-exported from a library
|
|
58
|
+
* (`export { Button as VanButton } from 'vant'`) and side-effect style
|
|
59
|
+
* imports, with the library versions of the material's package.json.
|
|
60
|
+
*/
|
|
61
|
+
function readCodegenManifest(code, pkg) {
|
|
62
|
+
const manifest = {
|
|
63
|
+
exports: {},
|
|
64
|
+
styles: [],
|
|
65
|
+
dependencies: {}
|
|
66
|
+
};
|
|
67
|
+
const used = /* @__PURE__ */ new Set();
|
|
68
|
+
const name = (id) => id.type === "Identifier" ? String(id.name) : String(id.value);
|
|
69
|
+
/** Local name -> named import of a library. */
|
|
70
|
+
const imported = /* @__PURE__ */ new Map();
|
|
71
|
+
for (const node of parseAst(code, { lang: "ts" }).body) if (node.type === "ImportDeclaration") {
|
|
72
|
+
const source = String(node.source.value);
|
|
73
|
+
if (isBare(source)) {
|
|
74
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportSpecifier") imported.set(specifier.local.name, {
|
|
75
|
+
package: source,
|
|
76
|
+
exportName: name(specifier.imported)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (node.specifiers.length === 0 && isBare(source) && STYLE.test(source)) {
|
|
80
|
+
manifest.styles.push(source);
|
|
81
|
+
used.add(packageName(source));
|
|
82
|
+
}
|
|
83
|
+
} else if (node.type === "ExportNamedDeclaration" && node.source) {
|
|
84
|
+
const source = String(node.source.value);
|
|
85
|
+
if (!isBare(source)) continue;
|
|
86
|
+
for (const specifier of node.specifiers) manifest.exports[name(specifier.exported)] = {
|
|
87
|
+
package: source,
|
|
88
|
+
exportName: name(specifier.local)
|
|
89
|
+
};
|
|
90
|
+
used.add(packageName(source));
|
|
91
|
+
} else if (node.type === "ExportNamedDeclaration" && node.declaration?.type === "VariableDeclaration") for (const declarator of node.declaration.declarations) {
|
|
92
|
+
const path = [];
|
|
93
|
+
let init = declarator.init;
|
|
94
|
+
while (init?.type === "MemberExpression" && !init.computed) {
|
|
95
|
+
path.unshift(name(init.property));
|
|
96
|
+
init = init.object;
|
|
97
|
+
}
|
|
98
|
+
const base = init?.type === "Identifier" && imported.get(init.name);
|
|
99
|
+
if (!base || !path.length || declarator.id.type !== "Identifier") continue;
|
|
100
|
+
manifest.exports[declarator.id.name] = {
|
|
101
|
+
...base,
|
|
102
|
+
subName: path.join(".")
|
|
103
|
+
};
|
|
104
|
+
used.add(packageName(base.package));
|
|
105
|
+
}
|
|
106
|
+
const versions = {
|
|
107
|
+
...pkg.peerDependencies,
|
|
108
|
+
...pkg.dependencies
|
|
109
|
+
};
|
|
110
|
+
for (const name of used) if (versions[name]) manifest.dependencies[name] = versions[name];
|
|
111
|
+
return manifest;
|
|
112
|
+
}
|
|
113
|
+
// src/entries.ts
|
|
114
|
+
const slash = (path) => path.replace(/\\/g, "/");
|
|
115
|
+
/**
|
|
116
|
+
* Writes the meta entry: imports every `meta.*` / `*.meta.*` under `metaDir`
|
|
117
|
+
* (a file may export one meta or an array of metas), fills in the npm info,
|
|
118
|
+
* resolves relative snippet screenshots against the URL of the meta script
|
|
119
|
+
* and exports `components`.
|
|
120
|
+
*/
|
|
121
|
+
async function writeMetaEntry(tempDir, metaDir, npmInfo) {
|
|
122
|
+
const files = [];
|
|
123
|
+
for await (const file of glob(["**/meta.{js,jsx,ts,tsx}", "**/*.meta.{js,jsx,ts,tsx}"], {
|
|
124
|
+
cwd: metaDir,
|
|
125
|
+
exclude: (name) => name === "node_modules"
|
|
126
|
+
})) files.push(slash(join(metaDir, file)));
|
|
127
|
+
files.sort();
|
|
128
|
+
const code = `${files.map((file, i) => `import meta${i} from ${JSON.stringify(file)}`).join("\n")}
|
|
129
|
+
|
|
130
|
+
const npmInfo = ${JSON.stringify(npmInfo)}
|
|
131
|
+
const components = []
|
|
132
|
+
// Relative snippet screenshots (\`./screenshots/x.webp\`) are files next to meta.js
|
|
133
|
+
const base = typeof document !== 'undefined' && document.currentScript && document.currentScript.src
|
|
134
|
+
|
|
135
|
+
function pushComponentMeta(item) {
|
|
136
|
+
if (!item.npm) {
|
|
137
|
+
const { componentName } = item
|
|
138
|
+
const names = componentName.split('.')
|
|
139
|
+
item.npm = {
|
|
140
|
+
exportName: names[0],
|
|
141
|
+
main: '',
|
|
142
|
+
destructuring: true,
|
|
143
|
+
subName: names.length > 1 ? componentName.slice(componentName.indexOf('.') + 1) : names[1],
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
item.npm = { ...npmInfo, ...item.npm }
|
|
147
|
+
for (const snippet of item.snippets || []) {
|
|
148
|
+
if (base && typeof snippet.screenshot === 'string' && snippet.screenshot.startsWith('./')) {
|
|
149
|
+
snippet.screenshot = new URL(snippet.screenshot, base).href
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
components.push(item)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (const item of [${files.map((_, i) => `meta${i}`).join(", ")}]) {
|
|
156
|
+
if (Array.isArray(item)) item.forEach(pushComponentMeta)
|
|
157
|
+
else pushComponentMeta(item)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { components }
|
|
161
|
+
`;
|
|
162
|
+
await mkdir(tempDir, { recursive: true });
|
|
163
|
+
const path = join(tempDir, "meta-entry.js");
|
|
164
|
+
await writeFile(path, code);
|
|
165
|
+
return path;
|
|
166
|
+
}
|
|
167
|
+
// src/build.ts
|
|
168
|
+
/** Globals of the canvas and preview pages: Vue, the renderer API and React. */
|
|
169
|
+
const DEFAULT_EXTERNALS = {
|
|
170
|
+
vue: "Vue",
|
|
171
|
+
// @vitejs/plugin-vue2 aliases `vue` to the runtime build
|
|
172
|
+
"vue/dist/vue.runtime.esm.js": "Vue",
|
|
173
|
+
react: "React",
|
|
174
|
+
"react-dom": "ReactDOM",
|
|
175
|
+
"@cdlab996/lowcode-vue2": "LCVueRenderer",
|
|
176
|
+
"@cdlab996/lowcode-vue3": "LCVueRenderer"
|
|
177
|
+
};
|
|
178
|
+
async function flattenPlugins(plugins = []) {
|
|
179
|
+
const result = [];
|
|
180
|
+
for (const plugin of plugins) {
|
|
181
|
+
const resolved = await plugin;
|
|
182
|
+
if (Array.isArray(resolved)) result.push(...await flattenPlugins(resolved));
|
|
183
|
+
else if (resolved) result.push(resolved);
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
/** `@scope/my-pkg2.7` -> `ScopeMyPkg2.7`, the naming the materials always used. */
|
|
188
|
+
function toLibraryName(packageName) {
|
|
189
|
+
return packageName.replace(/[/@-](\w)/g, (_, char) => char.toUpperCase()).replace(/^\w/, (char) => char.toUpperCase());
|
|
190
|
+
}
|
|
191
|
+
async function createContext({ root, mode }, command) {
|
|
192
|
+
const resolvedMode = mode ?? (command === "build" ? "production" : "development");
|
|
193
|
+
const loaded = await loadConfigFromFile({
|
|
194
|
+
command,
|
|
195
|
+
mode: resolvedMode
|
|
196
|
+
}, void 0, root);
|
|
197
|
+
const options = (await flattenPlugins(loaded?.config.plugins)).find((p) => p.name === "lowcode-material")?.api.options ?? {};
|
|
198
|
+
const pkg = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
|
199
|
+
const entry = resolve(root, options.entry ?? "src/index.ts");
|
|
200
|
+
const manifest = readCodegenManifest(await readFile(entry, "utf8"), pkg);
|
|
201
|
+
return {
|
|
202
|
+
root,
|
|
203
|
+
mode: resolvedMode,
|
|
204
|
+
configFile: loaded?.path ?? false,
|
|
205
|
+
outDir: resolve(root, loaded?.config.build?.outDir ?? "dist"),
|
|
206
|
+
entry,
|
|
207
|
+
metaDir: options.metaDir ? resolve(root, options.metaDir) : dirname(entry),
|
|
208
|
+
library: options.library ?? toLibraryName(pkg.name ?? "Library"),
|
|
209
|
+
sourceMap: options.sourceMap ?? true,
|
|
210
|
+
npmInfo: {
|
|
211
|
+
package: pkg.name ?? "",
|
|
212
|
+
version: pkg.version ?? "",
|
|
213
|
+
destructuring: true,
|
|
214
|
+
...options.npmInfo
|
|
215
|
+
},
|
|
216
|
+
globals: {
|
|
217
|
+
...DEFAULT_EXTERNALS,
|
|
218
|
+
...options.externals
|
|
219
|
+
},
|
|
220
|
+
assetsConfig: {
|
|
221
|
+
baseUrl: "https://unpkg.com/{name}@{version}",
|
|
222
|
+
groups: [],
|
|
223
|
+
categories: [],
|
|
224
|
+
builtinAssets: {},
|
|
225
|
+
...options.assetsConfig
|
|
226
|
+
},
|
|
227
|
+
codegen: Object.keys(manifest.exports).length > 0 ? manifest : void 0
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const GLOBAL_PREFIX = "\0lowcode-global:";
|
|
231
|
+
/**
|
|
232
|
+
* Resolves each external to a CommonJS module that reads the global variable,
|
|
233
|
+
* so both `import` and the `require()` calls inside CommonJS dependencies
|
|
234
|
+
* (Element UI, Ant Design Vue 1, ...) end up on the page globals.
|
|
235
|
+
*/
|
|
236
|
+
function globalsPlugin(globals) {
|
|
237
|
+
return {
|
|
238
|
+
name: "lowcode-material:globals",
|
|
239
|
+
enforce: "pre",
|
|
240
|
+
resolveId(id) {
|
|
241
|
+
return Object.prototype.hasOwnProperty.call(globals, id) ? GLOBAL_PREFIX + id : null;
|
|
242
|
+
},
|
|
243
|
+
load(id) {
|
|
244
|
+
if (!id.startsWith(GLOBAL_PREFIX)) return null;
|
|
245
|
+
return `module.exports = (typeof globalThis !== 'undefined' ? globalThis : self)${globals[id.slice(16)].split(".").map((key) => `[${JSON.stringify(key)}]`).join("")}`;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Vite config of one output: an IIFE that assigns the entry's exports to
|
|
251
|
+
* `window[globalName]`.
|
|
252
|
+
*/
|
|
253
|
+
function createConfig(ctx, entry, fileBase, globalName, minify, watch) {
|
|
254
|
+
const variable = `__lowcode_${globalName.replace(/[^\w$]/g, "_")}`;
|
|
255
|
+
const output = {
|
|
256
|
+
esModule: true,
|
|
257
|
+
footer: `;(typeof globalThis !== 'undefined' ? globalThis : self)[${JSON.stringify(globalName)}] = ${variable};`
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
root: ctx.root,
|
|
261
|
+
mode: ctx.mode,
|
|
262
|
+
configFile: ctx.configFile,
|
|
263
|
+
publicDir: false,
|
|
264
|
+
logLevel: "warn",
|
|
265
|
+
plugins: [globalsPlugin(ctx.globals)],
|
|
266
|
+
resolve: { alias: [{
|
|
267
|
+
find: /^~(?=[@\w])/,
|
|
268
|
+
replacement: ""
|
|
269
|
+
}] },
|
|
270
|
+
define: { "process.env.NODE_ENV": JSON.stringify(ctx.mode === "production" ? "production" : "development") },
|
|
271
|
+
build: {
|
|
272
|
+
outDir: ctx.outDir,
|
|
273
|
+
emptyOutDir: false,
|
|
274
|
+
copyPublicDir: false,
|
|
275
|
+
sourcemap: !minify && ctx.sourceMap,
|
|
276
|
+
minify: minify || !ctx.sourceMap,
|
|
277
|
+
cssMinify: minify || !ctx.sourceMap,
|
|
278
|
+
reportCompressedSize: false,
|
|
279
|
+
watch: watch ? {} : null,
|
|
280
|
+
lib: {
|
|
281
|
+
entry,
|
|
282
|
+
formats: ["iife"],
|
|
283
|
+
name: variable,
|
|
284
|
+
fileName: () => `${fileBase}.js`,
|
|
285
|
+
cssFileName: fileBase
|
|
286
|
+
},
|
|
287
|
+
rolldownOptions: { output }
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function libraryFiles(ctx, fileBase) {
|
|
292
|
+
const css = `${fileBase}.css`;
|
|
293
|
+
return [...existsSync(join(ctx.outDir, css)) ? [css] : [], `${fileBase}.js`];
|
|
294
|
+
}
|
|
295
|
+
async function buildMaterial(args) {
|
|
296
|
+
const ctx = await createContext(args, "build");
|
|
297
|
+
const metaEntry = await writeMetaEntry(join(ctx.root, "node_modules/.lowcode-builder"), ctx.metaDir, ctx.npmInfo);
|
|
298
|
+
if (args.clean) await rm(ctx.outDir, {
|
|
299
|
+
recursive: true,
|
|
300
|
+
force: true
|
|
301
|
+
});
|
|
302
|
+
await mkdir(ctx.outDir, { recursive: true });
|
|
303
|
+
await copyScreenshots(ctx);
|
|
304
|
+
const metaLibrary = `${ctx.library}Meta`;
|
|
305
|
+
for (const minify of [false, true]) {
|
|
306
|
+
const suffix = minify ? ".min" : "";
|
|
307
|
+
if (minify && !ctx.sourceMap) for (const file of [...libraryFiles(ctx, "index"), ...libraryFiles(ctx, "meta")]) await cp(join(ctx.outDir, file), join(ctx.outDir, file.replace(/(\.\w+)$/, ".min$1")));
|
|
308
|
+
else {
|
|
309
|
+
await build(createConfig(ctx, ctx.entry, `index${suffix}`, ctx.library, minify, false));
|
|
310
|
+
await build(createConfig(ctx, metaEntry, `meta${suffix}`, metaLibrary, minify, false));
|
|
311
|
+
}
|
|
312
|
+
await writeFile(join(ctx.outDir, `assets${suffix}.json`), createAssets({
|
|
313
|
+
assetsConfig: ctx.assetsConfig,
|
|
314
|
+
npmInfo: ctx.npmInfo,
|
|
315
|
+
mode: ctx.mode,
|
|
316
|
+
library: ctx.library,
|
|
317
|
+
files: libraryFiles(ctx, `index${suffix}`),
|
|
318
|
+
metaFile: `meta${suffix}.js`,
|
|
319
|
+
relativePath: relative(ctx.root, ctx.outDir),
|
|
320
|
+
minify,
|
|
321
|
+
codegen: ctx.codegen
|
|
322
|
+
}));
|
|
323
|
+
}
|
|
324
|
+
console.log(`Built ${ctx.npmInfo.package} into ${relative(process.cwd(), ctx.outDir) || "."}`);
|
|
325
|
+
}
|
|
326
|
+
/** Copies the material's `screenshots/` (snippet images) into the output. */
|
|
327
|
+
async function copyScreenshots(ctx) {
|
|
328
|
+
const source = join(ctx.root, "screenshots");
|
|
329
|
+
if (existsSync(source)) await cp(source, join(ctx.outDir, "screenshots"), { recursive: true });
|
|
330
|
+
}
|
|
331
|
+
function waitForFirstBuild(watcher) {
|
|
332
|
+
return new Promise((done, fail) => {
|
|
333
|
+
watcher.on("event", (event) => {
|
|
334
|
+
if (event.code === "END") done();
|
|
335
|
+
else if (event.code === "ERROR") fail(event.error);
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
async function devMaterial(args) {
|
|
340
|
+
const ctx = await createContext(args, "serve");
|
|
341
|
+
const host = args.host ?? "127.0.0.1";
|
|
342
|
+
const port = args.port ?? 9e3;
|
|
343
|
+
const metaEntry = await writeMetaEntry(join(ctx.root, "node_modules/.lowcode-builder"), ctx.metaDir, ctx.npmInfo);
|
|
344
|
+
await mkdir(ctx.outDir, { recursive: true });
|
|
345
|
+
const watchers = await Promise.all([build(createConfig(ctx, ctx.entry, "index", ctx.library, false, true)), build(createConfig(ctx, metaEntry, "meta", `${ctx.library}Meta`, false, true))]);
|
|
346
|
+
await Promise.all(watchers.map(waitForFirstBuild));
|
|
347
|
+
await copyScreenshots(ctx);
|
|
348
|
+
await writeFile(join(ctx.outDir, "assets.json"), createAssets({
|
|
349
|
+
assetsConfig: ctx.assetsConfig,
|
|
350
|
+
npmInfo: ctx.npmInfo,
|
|
351
|
+
mode: ctx.mode,
|
|
352
|
+
library: ctx.library,
|
|
353
|
+
files: libraryFiles(ctx, "index"),
|
|
354
|
+
metaFile: "meta.js",
|
|
355
|
+
relativePath: "",
|
|
356
|
+
localBaseUrl: `http://${host}:${port}`,
|
|
357
|
+
minify: false,
|
|
358
|
+
codegen: ctx.codegen
|
|
359
|
+
}));
|
|
360
|
+
const server = await preview({
|
|
361
|
+
root: ctx.root,
|
|
362
|
+
configFile: false,
|
|
363
|
+
logLevel: "warn",
|
|
364
|
+
build: { outDir: ctx.outDir },
|
|
365
|
+
preview: {
|
|
366
|
+
host,
|
|
367
|
+
port,
|
|
368
|
+
strictPort: true,
|
|
369
|
+
cors: true
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
console.log(`Serving ${ctx.npmInfo.package}: http://${host}:${port}/assets.json`);
|
|
373
|
+
return server;
|
|
374
|
+
}
|
|
375
|
+
// src/cli.ts
|
|
376
|
+
const usage = `Usage:
|
|
377
|
+
lowcode-material build [--clean] [--mode <mode>]
|
|
378
|
+
lowcode-material dev [--host <host>] [--port <port>] [--mode <mode>]`;
|
|
379
|
+
const { positionals, values } = parseArgs({
|
|
380
|
+
allowPositionals: true,
|
|
381
|
+
options: {
|
|
382
|
+
clean: { type: "boolean" },
|
|
383
|
+
mode: { type: "string" },
|
|
384
|
+
host: { type: "string" },
|
|
385
|
+
port: { type: "string" },
|
|
386
|
+
help: {
|
|
387
|
+
type: "boolean",
|
|
388
|
+
short: "h"
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
const command = positionals[0];
|
|
393
|
+
const root = process.cwd();
|
|
394
|
+
if (values.help || command !== "build" && command !== "dev") {
|
|
395
|
+
console.log(usage);
|
|
396
|
+
process.exit(values.help ? 0 : 1);
|
|
397
|
+
}
|
|
398
|
+
try {
|
|
399
|
+
if (command === "build") await buildMaterial({
|
|
400
|
+
root,
|
|
401
|
+
mode: values.mode,
|
|
402
|
+
clean: values.clean
|
|
403
|
+
});
|
|
404
|
+
else await devMaterial({
|
|
405
|
+
root,
|
|
406
|
+
mode: values.mode,
|
|
407
|
+
host: values.host,
|
|
408
|
+
port: values.port ? Number(values.port) : void 0
|
|
409
|
+
});
|
|
410
|
+
} catch (error) {
|
|
411
|
+
console.error(error);
|
|
412
|
+
process.exit(1);
|
|
413
|
+
}
|
|
414
|
+
export {};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
interface LowcodeNpmInfo {
|
|
3
|
+
/** Package name. Defaults to `name` in package.json. */
|
|
4
|
+
package?: string;
|
|
5
|
+
/** Package version. Defaults to `version` in package.json. */
|
|
6
|
+
version?: string;
|
|
7
|
+
/** Whether components are read from the library by destructuring. */
|
|
8
|
+
destructuring?: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface LowcodeBuiltinAssets {
|
|
11
|
+
packages?: unknown[];
|
|
12
|
+
components?: unknown[];
|
|
13
|
+
}
|
|
14
|
+
interface LowcodeAssetsConfig {
|
|
15
|
+
/**
|
|
16
|
+
* Base URL of the published files, or one URL per mode. `{name}`,
|
|
17
|
+
* `{version}` and `{origin}` are replaced.
|
|
18
|
+
* Defaults to `https://unpkg.com/{name}@{version}`.
|
|
19
|
+
*/
|
|
20
|
+
baseUrl?: string | Record<string, string>;
|
|
21
|
+
groups?: string[];
|
|
22
|
+
categories?: string[];
|
|
23
|
+
builtinAssets?: LowcodeBuiltinAssets;
|
|
24
|
+
}
|
|
25
|
+
interface LowcodeMaterialOptions {
|
|
26
|
+
/** Component entry. Defaults to `src/index.ts`. */
|
|
27
|
+
entry?: string;
|
|
28
|
+
/** Directory searched for `meta.*` and `*.meta.*`. Defaults to the entry directory. */
|
|
29
|
+
metaDir?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Global name of the component library; the meta library is
|
|
32
|
+
* `${library}Meta`. Defaults to a PascalCase form of the package name.
|
|
33
|
+
*/
|
|
34
|
+
library?: string;
|
|
35
|
+
/** Emit source maps for the non-minified files. Defaults to `true`. */
|
|
36
|
+
sourceMap?: boolean;
|
|
37
|
+
npmInfo?: LowcodeNpmInfo;
|
|
38
|
+
/** Extra externals: module id -> global variable name. */
|
|
39
|
+
externals?: Record<string, string>;
|
|
40
|
+
assetsConfig?: LowcodeAssetsConfig;
|
|
41
|
+
}
|
|
42
|
+
interface LowcodeMaterialPlugin extends Plugin {
|
|
43
|
+
api: {
|
|
44
|
+
options: LowcodeMaterialOptions;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Declares the material options in `vite.config.ts`. The plugin does not
|
|
49
|
+
* change the Vite build; `lowcode-material build` and `lowcode-material dev`
|
|
50
|
+
* read the options from it.
|
|
51
|
+
*/
|
|
52
|
+
export declare function lowcodeMaterial(options?: LowcodeMaterialOptions): LowcodeMaterialPlugin;
|
|
53
|
+
export type { LowcodeAssetsConfig, LowcodeBuiltinAssets, LowcodeMaterialOptions, LowcodeMaterialPlugin, LowcodeNpmInfo };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { n as lowcodeMaterial } from "./options-o2AorB1K.mjs";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/options.ts
|
|
2
|
+
const PLUGIN_NAME = "lowcode-material";
|
|
3
|
+
/**
|
|
4
|
+
* Declares the material options in `vite.config.ts`. The plugin does not
|
|
5
|
+
* change the Vite build; `lowcode-material build` and `lowcode-material dev`
|
|
6
|
+
* read the options from it.
|
|
7
|
+
*/
|
|
8
|
+
function lowcodeMaterial(options = {}) {
|
|
9
|
+
return {
|
|
10
|
+
name: PLUGIN_NAME,
|
|
11
|
+
api: { options }
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export { lowcodeMaterial as n, PLUGIN_NAME as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cdlab996/lowcode-material-builder",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"description": "Vite-based build and dev server for LowCode Engine component materials.",
|
|
6
|
+
"author": "wudi <wuchendi96@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/wuchendi/lowcode-engine-vue/tree/main/packages/tools/material-builder",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/wuchendi/lowcode-engine-vue.git",
|
|
12
|
+
"directory": "packages/tools/material-builder"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/wuchendi/lowcode-engine-vue/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"lowcode",
|
|
19
|
+
"lowcode-engine",
|
|
20
|
+
"lowcode-material",
|
|
21
|
+
"vite"
|
|
22
|
+
],
|
|
23
|
+
"bin": {
|
|
24
|
+
"lowcode-material": "./dist/cli.mjs"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.mjs",
|
|
33
|
+
"module": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=24.15"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"vite": "^8.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@cdlab996/lowcode-tsconfig": "1.0.0",
|
|
47
|
+
"@types/node": "^26.6.6",
|
|
48
|
+
"tsdown": "^0.23.7",
|
|
49
|
+
"vite": "^8.3.8",
|
|
50
|
+
"vue": "2.7.16"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public",
|
|
54
|
+
"registry": "https://registry.npmjs.org/"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsdown",
|
|
58
|
+
"lint:type": "tsc -p ./tsconfig.json --noEmit"
|
|
59
|
+
}
|
|
6
60
|
}
|