kirbyup 0.22.1 → 0.22.2

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,298 @@
1
+ 'use strict';
2
+
3
+ const pathe = require('pathe');
4
+ const fs = require('fs');
5
+ const vite = require('vite');
6
+ const vitePluginVue2 = require('vite-plugin-vue2');
7
+ const MagicString = require('magic-string');
8
+ const unconfig = require('unconfig');
9
+ const postcssrc = require('postcss-load-config');
10
+ const postcssLogical = require('postcss-logical');
11
+ const postcssDirPseudoClass = require('postcss-dir-pseudo-class');
12
+ const consola = require('consola');
13
+ const colorette = require('colorette');
14
+ const promises = require('fs/promises');
15
+ const zlib = require('zlib');
16
+ const util = require('util');
17
+
18
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
19
+
20
+ const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
21
+ const postcssrc__default = /*#__PURE__*/_interopDefaultLegacy(postcssrc);
22
+ const postcssLogical__default = /*#__PURE__*/_interopDefaultLegacy(postcssLogical);
23
+ const postcssDirPseudoClass__default = /*#__PURE__*/_interopDefaultLegacy(postcssDirPseudoClass);
24
+ const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
25
+
26
+ const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm;
27
+ const singlelineCommentsRE = /\/\/.*/g;
28
+
29
+ function kirbyupAutoImportPlugin() {
30
+ let config;
31
+ return {
32
+ name: "kirbyup:auto-import",
33
+ configResolved(resolvedConfig) {
34
+ config = resolvedConfig;
35
+ },
36
+ async transform(code, id) {
37
+ if (code.includes("kirbyup.import")) {
38
+ const kirbyupImportRE = /\bkirbyup\.import\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*\)/g;
39
+ const noCommentsCode = code.replace(multilineCommentsRE, (m) => " ".repeat(m.length)).replace(singlelineCommentsRE, (m) => " ".repeat(m.length));
40
+ let s = null;
41
+ let match;
42
+ while (match = kirbyupImportRE.exec(noCommentsCode)) {
43
+ const { 0: exp, 1: rawPath, index } = match;
44
+ if (!s)
45
+ s = new MagicString__default(code);
46
+ s.overwrite(index, index + exp.length, `kirbyup.import(import.meta.globEager(${rawPath}))`);
47
+ }
48
+ if (s) {
49
+ return {
50
+ code: s.toString(),
51
+ map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
52
+ };
53
+ }
54
+ }
55
+ return null;
56
+ }
57
+ };
58
+ }
59
+
60
+ function defineConfig(config) {
61
+ return config;
62
+ }
63
+ function createConfigLoader(configOrPath = process.cwd(), extraConfigSources = []) {
64
+ let inlineConfig = {};
65
+ if (typeof configOrPath !== "string") {
66
+ inlineConfig = configOrPath;
67
+ configOrPath = process.cwd();
68
+ }
69
+ const resolved = pathe.resolve(configOrPath);
70
+ let cwd = resolved;
71
+ let isFile = false;
72
+ if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
73
+ isFile = true;
74
+ cwd = pathe.dirname(resolved);
75
+ }
76
+ const loader = unconfig.createConfigLoader({
77
+ sources: isFile ? [
78
+ {
79
+ files: resolved,
80
+ extensions: []
81
+ }
82
+ ] : [
83
+ {
84
+ files: ["kirbyup.config"]
85
+ },
86
+ ...extraConfigSources
87
+ ],
88
+ cwd,
89
+ defaults: inlineConfig
90
+ });
91
+ return async () => {
92
+ const result = await loader.load();
93
+ result.config = result.config || inlineConfig;
94
+ return result;
95
+ };
96
+ }
97
+
98
+ class PrettyError extends Error {
99
+ constructor(message) {
100
+ super(message);
101
+ this.name = this.constructor.name;
102
+ if (typeof Error.captureStackTrace === "function") {
103
+ Error.captureStackTrace(this, this.constructor);
104
+ } else {
105
+ this.stack = new Error(message).stack;
106
+ }
107
+ }
108
+ }
109
+ function handleError(error) {
110
+ if (error instanceof PrettyError) {
111
+ consola__default.error(error.message);
112
+ }
113
+ process.exitCode = 1;
114
+ }
115
+
116
+ const compress = util.promisify(zlib.gzip);
117
+ async function getCompressedSize(code) {
118
+ const size = (await compress(typeof code === "string" ? code : Buffer.from(code))).length / 1024;
119
+ return ` / gzip: ${size.toFixed(2)} KiB`;
120
+ }
121
+ async function printFileInfo(root, outDir, filePath, type, content) {
122
+ content ?? (content = await promises.readFile(pathe.resolve(outDir, filePath), "utf8"));
123
+ const prettyOutDir = pathe.normalize(pathe.relative(root, pathe.resolve(root, outDir))) + "/";
124
+ const kibs = content.length / 1024;
125
+ const compressedSize = await getCompressedSize(content);
126
+ const writeColor = type === "chunk" ? colorette.cyan : colorette.magenta;
127
+ consola__default.log(colorette.white(colorette.dim(prettyOutDir)) + writeColor(filePath) + " " + colorette.dim(`${kibs.toFixed(2)} KiB${compressedSize}`));
128
+ }
129
+ function debouncePromise(fn, delay, onError) {
130
+ let timeout;
131
+ let promiseInFly;
132
+ let callbackPending;
133
+ return function debounced(...args) {
134
+ if (promiseInFly) {
135
+ callbackPending = () => {
136
+ debounced(...args);
137
+ callbackPending = void 0;
138
+ };
139
+ } else {
140
+ if (timeout)
141
+ clearTimeout(timeout);
142
+ timeout = setTimeout(() => {
143
+ timeout = void 0;
144
+ promiseInFly = fn(...args).catch(onError).finally(() => {
145
+ promiseInFly = void 0;
146
+ if (callbackPending)
147
+ callbackPending();
148
+ });
149
+ }, delay);
150
+ }
151
+ };
152
+ }
153
+
154
+ // src/math.ts
155
+
156
+ // src/array.ts
157
+ function toArray(array) {
158
+ array = array || [];
159
+ if (Array.isArray(array))
160
+ return array;
161
+ return [array];
162
+ }
163
+
164
+ const name = "kirbyup";
165
+ const version = "0.22.2";
166
+
167
+ let resolvedKirbyupConfig;
168
+ let resolvedPostCssConfig;
169
+ async function viteBuild(options) {
170
+ let result;
171
+ const mode = options.watch ? "development" : "production";
172
+ const root = process.cwd();
173
+ const outDir = options.outDir ?? root;
174
+ const aliasDir = pathe.resolve(root, pathe.dirname(options.entry));
175
+ const { alias = {}, extendViteConfig = {} } = resolvedKirbyupConfig;
176
+ const defaultConfig = {
177
+ mode,
178
+ plugins: [vitePluginVue2.createVuePlugin(), kirbyupAutoImportPlugin()],
179
+ build: {
180
+ lib: {
181
+ entry: pathe.resolve(root, options.entry),
182
+ formats: ["iife"],
183
+ name: "kirbyupExport",
184
+ fileName: () => "index.js"
185
+ },
186
+ minify: mode === "production",
187
+ outDir,
188
+ emptyOutDir: false,
189
+ rollupOptions: {
190
+ external: ["vue"],
191
+ output: {
192
+ assetFileNames: "index.[ext]",
193
+ globals: {
194
+ vue: "Vue"
195
+ }
196
+ }
197
+ }
198
+ },
199
+ resolve: {
200
+ alias: {
201
+ "~/": `${aliasDir}/`,
202
+ "@/": `${aliasDir}/`,
203
+ ...alias
204
+ }
205
+ },
206
+ css: {
207
+ postcss: resolvedPostCssConfig
208
+ },
209
+ envPrefix: ["VITE_", "KIRBYUP_"],
210
+ logLevel: "warn"
211
+ };
212
+ try {
213
+ result = await vite.build(vite.mergeConfig(defaultConfig, extendViteConfig));
214
+ } catch (error) {
215
+ consola__default.error("Build failed");
216
+ if (mode === "production") {
217
+ throw error;
218
+ }
219
+ }
220
+ if (result && !options.watch) {
221
+ const { output } = toArray(result)[0];
222
+ for (const { fileName, type, code } of output) {
223
+ printFileInfo(root, outDir, fileName, type, code);
224
+ }
225
+ }
226
+ return result;
227
+ }
228
+ async function resolveOptions(options) {
229
+ if (!options.entry) {
230
+ throw new PrettyError("No input file, try " + colorette.cyan(`${name} <path/to/file.js>`));
231
+ }
232
+ if (!fs.existsSync(options.entry)) {
233
+ throw new PrettyError(`Cannot find ${options.entry}`);
234
+ }
235
+ return options;
236
+ }
237
+ async function build(_options) {
238
+ const options = await resolveOptions(_options);
239
+ const loadConfig = createConfigLoader();
240
+ const { config, sources: configSources } = await loadConfig();
241
+ resolvedKirbyupConfig = config;
242
+ try {
243
+ resolvedPostCssConfig = await postcssrc__default({});
244
+ } catch (err) {
245
+ if (!/No PostCSS Config found/.test(err.message)) {
246
+ throw err;
247
+ }
248
+ resolvedPostCssConfig = {
249
+ plugins: [postcssLogical__default(), postcssDirPseudoClass__default()]
250
+ };
251
+ }
252
+ consola__default.log(colorette.green(`${name} v${version}`));
253
+ consola__default.start("Building " + colorette.cyan(options.entry));
254
+ if (options.watch) {
255
+ consola__default.info("Running in watch mode");
256
+ }
257
+ const debouncedBuild = debouncePromise(async () => {
258
+ viteBuild(options);
259
+ }, 100, handleError);
260
+ const startWatcher = async () => {
261
+ if (!options.watch)
262
+ return;
263
+ const { watch } = await import('chokidar');
264
+ const ignored = [
265
+ "**/{.git,node_modules}/**",
266
+ "index.{css,js}"
267
+ ];
268
+ const watchPaths = typeof options.watch === "boolean" ? pathe.dirname(options.entry) : Array.isArray(options.watch) ? options.watch.filter((path) => typeof path === "string") : options.watch;
269
+ consola__default.info("Watching for changes in " + toArray(watchPaths).map((i) => colorette.cyan(i)).join(", "));
270
+ const watcher = watch(watchPaths, {
271
+ ignoreInitial: true,
272
+ ignorePermissionErrors: true,
273
+ ignored
274
+ });
275
+ if (configSources.length) {
276
+ watcher.add(configSources);
277
+ }
278
+ watcher.on("all", async (type, file) => {
279
+ if (configSources.includes(file)) {
280
+ resolvedKirbyupConfig = (await loadConfig()).config;
281
+ consola__default.info(`${colorette.cyan(pathe.basename(file))} changed, setting new config`);
282
+ } else {
283
+ consola__default.log(colorette.green(type) + " " + colorette.white(colorette.dim(file)));
284
+ }
285
+ debouncedBuild();
286
+ });
287
+ };
288
+ await viteBuild(options);
289
+ consola__default.success("Build successful");
290
+ startWatcher();
291
+ }
292
+
293
+ exports.build = build;
294
+ exports.defineConfig = defineConfig;
295
+ exports.handleError = handleError;
296
+ exports.name = name;
297
+ exports.resolveOptions = resolveOptions;
298
+ exports.version = version;
@@ -152,7 +152,7 @@ function toArray(array) {
152
152
  }
153
153
 
154
154
  const name = "kirbyup";
155
- const version = "0.22.1";
155
+ const version = "0.22.2";
156
156
 
157
157
  let resolvedKirbyupConfig;
158
158
  let resolvedPostCssConfig;
package/dist/cli.cjs ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const cac = require('cac');
4
+ const index = require('./chunks/index.cjs');
5
+ require('pathe');
6
+ require('fs');
7
+ require('vite');
8
+ require('vite-plugin-vue2');
9
+ require('magic-string');
10
+ require('unconfig');
11
+ require('postcss-load-config');
12
+ require('postcss-logical');
13
+ require('postcss-dir-pseudo-class');
14
+ require('consola');
15
+ require('colorette');
16
+ require('fs/promises');
17
+ require('zlib');
18
+ require('util');
19
+
20
+ async function main(options = {}) {
21
+ const cli = cac.cac(index.name);
22
+ cli.command("[file]", "Panel input file", {
23
+ ignoreOptionDefaultValue: true
24
+ }).option("-d, --out-dir <dir>", "Output directory", {
25
+ default: process.cwd()
26
+ }).option("--watch [path]", 'Watch mode, if path is not specified, it watches the folder of the input file. Repeat "--watch" for multiple paths').action(async (file, flags) => {
27
+ Object.assign(options, {
28
+ ...flags
29
+ });
30
+ if (file) {
31
+ options.entry = file;
32
+ }
33
+ await index.build(options);
34
+ });
35
+ cli.help();
36
+ cli.version(index.version);
37
+ cli.parse(process.argv, { run: false });
38
+ await cli.runMatchedCommand();
39
+ }
40
+ main().catch(index.handleError);
package/dist/index.cjs ADDED
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('pathe');
6
+ require('fs');
7
+ require('vite');
8
+ require('vite-plugin-vue2');
9
+ const index = require('./chunks/index.cjs');
10
+ require('postcss-load-config');
11
+ require('postcss-logical');
12
+ require('postcss-dir-pseudo-class');
13
+ require('consola');
14
+ require('colorette');
15
+ require('magic-string');
16
+ require('unconfig');
17
+ require('fs/promises');
18
+ require('zlib');
19
+ require('util');
20
+
21
+
22
+
23
+ exports.build = index.build;
24
+ exports.defineConfig = index.defineConfig;
25
+ exports.resolveOptions = index.resolveOptions;
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const getComponentName = (path) => path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")).toLowerCase();
6
+ const kirbyup = Object.freeze({
7
+ import(modules) {
8
+ return Object.entries(modules).reduce((accumulator, [path, component]) => {
9
+ accumulator[getComponentName(path)] = component.default;
10
+ return accumulator;
11
+ }, {});
12
+ }
13
+ });
14
+
15
+ exports.kirbyup = kirbyup;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kirbyup",
3
- "version": "0.22.1",
3
+ "version": "0.22.2",
4
4
  "description": "Zero-config bundler for Kirby Panel plugins",
5
5
  "keywords": [
6
6
  "kirby-cms",