kirbyup 0.22.1 → 0.23.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Johann Schopplich
3
+ Copyright (c) 2021-2022 Johann Schopplich
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,293 @@
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 picocolors = require('picocolors');
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
+ consola__default.error(error.message);
111
+ process.exitCode = 1;
112
+ }
113
+
114
+ const compress = util.promisify(zlib.gzip);
115
+ async function getCompressedSize(code) {
116
+ const size = (await compress(typeof code === "string" ? code : Buffer.from(code))).length / 1024;
117
+ return ` / gzip: ${size.toFixed(2)} KiB`;
118
+ }
119
+ async function printFileInfo(root, outDir, filePath, type, content) {
120
+ content ?? (content = await promises.readFile(pathe.resolve(outDir, filePath), "utf8"));
121
+ const prettyOutDir = pathe.normalize(pathe.relative(root, pathe.resolve(root, outDir))) + "/";
122
+ const kibs = content.length / 1024;
123
+ const compressedSize = await getCompressedSize(content);
124
+ const writeColor = type === "chunk" ? picocolors.cyan : picocolors.magenta;
125
+ consola__default.log(picocolors.white(picocolors.dim(prettyOutDir)) + writeColor(filePath) + " " + picocolors.dim(`${kibs.toFixed(2)} KiB${compressedSize}`));
126
+ }
127
+ function debouncePromise(fn, delay, onError) {
128
+ let timeout;
129
+ let promiseInFly;
130
+ let callbackPending;
131
+ return function debounced(...args) {
132
+ if (promiseInFly) {
133
+ callbackPending = () => {
134
+ debounced(...args);
135
+ callbackPending = void 0;
136
+ };
137
+ } else {
138
+ if (timeout)
139
+ clearTimeout(timeout);
140
+ timeout = setTimeout(() => {
141
+ timeout = void 0;
142
+ promiseInFly = fn(...args).catch(onError).finally(() => {
143
+ promiseInFly = void 0;
144
+ if (callbackPending)
145
+ callbackPending();
146
+ });
147
+ }, delay);
148
+ }
149
+ };
150
+ }
151
+
152
+ function toArray(array) {
153
+ array = array || [];
154
+ if (Array.isArray(array))
155
+ return array;
156
+ return [array];
157
+ }
158
+
159
+ const name = "kirbyup";
160
+ const version = "0.23.0";
161
+
162
+ let resolvedKirbyupConfig;
163
+ let resolvedPostCssConfig;
164
+ async function viteBuild(options) {
165
+ let result;
166
+ const mode = options.watch ? "development" : "production";
167
+ const root = process.cwd();
168
+ const outDir = options.outDir ?? root;
169
+ const aliasDir = pathe.resolve(root, pathe.dirname(options.entry));
170
+ const { alias = {}, extendViteConfig = {} } = resolvedKirbyupConfig;
171
+ const defaultConfig = {
172
+ mode,
173
+ plugins: [vitePluginVue2.createVuePlugin(), kirbyupAutoImportPlugin()],
174
+ build: {
175
+ lib: {
176
+ entry: pathe.resolve(root, options.entry),
177
+ formats: ["iife"],
178
+ name: "kirbyupExport",
179
+ fileName: () => "index.js"
180
+ },
181
+ minify: mode === "production",
182
+ outDir,
183
+ emptyOutDir: false,
184
+ rollupOptions: {
185
+ external: ["vue"],
186
+ output: {
187
+ assetFileNames: "index.[ext]",
188
+ globals: {
189
+ vue: "Vue"
190
+ }
191
+ }
192
+ }
193
+ },
194
+ resolve: {
195
+ alias: {
196
+ "~/": `${aliasDir}/`,
197
+ "@/": `${aliasDir}/`,
198
+ ...alias
199
+ }
200
+ },
201
+ css: {
202
+ postcss: resolvedPostCssConfig
203
+ },
204
+ envPrefix: ["VITE_", "KIRBYUP_"],
205
+ logLevel: "warn"
206
+ };
207
+ try {
208
+ result = await vite.build(vite.mergeConfig(defaultConfig, extendViteConfig));
209
+ } catch (error) {
210
+ consola__default.error("Build failed");
211
+ if (mode === "production") {
212
+ throw error;
213
+ }
214
+ }
215
+ if (result && !options.watch) {
216
+ const { output } = toArray(result)[0];
217
+ for (const { fileName, type, code } of output) {
218
+ printFileInfo(root, outDir, fileName, type, code);
219
+ }
220
+ }
221
+ return result;
222
+ }
223
+ async function resolveOptions(options) {
224
+ if (!options.entry) {
225
+ throw new PrettyError(`No input file, try ${picocolors.cyan(`${name} <path/to/file.js>`)}`);
226
+ }
227
+ if (!fs.existsSync(options.entry)) {
228
+ throw new PrettyError(`Cannot find ${options.entry}`);
229
+ }
230
+ return options;
231
+ }
232
+ async function build(_options) {
233
+ const options = await resolveOptions(_options);
234
+ const loadConfig = createConfigLoader();
235
+ const { config, sources: configSources } = await loadConfig();
236
+ resolvedKirbyupConfig = config;
237
+ try {
238
+ resolvedPostCssConfig = await postcssrc__default({});
239
+ } catch (err) {
240
+ if (!/No PostCSS Config found/.test(err.message)) {
241
+ throw err;
242
+ }
243
+ resolvedPostCssConfig = {
244
+ plugins: [postcssLogical__default(), postcssDirPseudoClass__default()]
245
+ };
246
+ }
247
+ consola__default.log(picocolors.green(`${name} v${version}`));
248
+ consola__default.start(`Building ${picocolors.cyan(options.entry)}`);
249
+ if (options.watch) {
250
+ consola__default.info("Running in watch mode");
251
+ }
252
+ const debouncedBuild = debouncePromise(async () => {
253
+ viteBuild(options);
254
+ }, 100, handleError);
255
+ const startWatcher = async () => {
256
+ if (!options.watch)
257
+ return;
258
+ const { watch } = await import('chokidar');
259
+ const ignored = [
260
+ "**/{.git,node_modules}/**",
261
+ "index.{css,js}"
262
+ ];
263
+ const watchPaths = typeof options.watch === "boolean" ? pathe.dirname(options.entry) : Array.isArray(options.watch) ? options.watch.filter((path) => typeof path === "string") : options.watch;
264
+ consola__default.info("Watching for changes in " + toArray(watchPaths).map((i) => picocolors.cyan(i)).join(", "));
265
+ const watcher = watch(watchPaths, {
266
+ ignoreInitial: true,
267
+ ignorePermissionErrors: true,
268
+ ignored
269
+ });
270
+ if (configSources.length) {
271
+ watcher.add(configSources);
272
+ }
273
+ watcher.on("all", async (type, file) => {
274
+ if (configSources.includes(file)) {
275
+ resolvedKirbyupConfig = (await loadConfig()).config;
276
+ consola__default.info(`${picocolors.cyan(pathe.basename(file))} changed, setting new config`);
277
+ } else {
278
+ consola__default.log(picocolors.green(type) + " " + picocolors.white(picocolors.dim(file)));
279
+ }
280
+ debouncedBuild();
281
+ });
282
+ };
283
+ await viteBuild(options);
284
+ consola__default.success("Build successful");
285
+ startWatcher();
286
+ }
287
+
288
+ exports.build = build;
289
+ exports.defineConfig = defineConfig;
290
+ exports.handleError = handleError;
291
+ exports.name = name;
292
+ exports.resolveOptions = resolveOptions;
293
+ exports.version = version;
@@ -8,7 +8,7 @@ import postcssrc from 'postcss-load-config';
8
8
  import postcssLogical from 'postcss-logical';
9
9
  import postcssDirPseudoClass from 'postcss-dir-pseudo-class';
10
10
  import consola from 'consola';
11
- import { white, dim, cyan, magenta, green } from 'colorette';
11
+ import { white, dim, cyan, magenta, green } from 'picocolors';
12
12
  import { readFile } from 'fs/promises';
13
13
  import { gzip } from 'zlib';
14
14
  import { promisify } from 'util';
@@ -97,9 +97,7 @@ class PrettyError extends Error {
97
97
  }
98
98
  }
99
99
  function handleError(error) {
100
- if (error instanceof PrettyError) {
101
- consola.error(error.message);
102
- }
100
+ consola.error(error.message);
103
101
  process.exitCode = 1;
104
102
  }
105
103
 
@@ -141,9 +139,6 @@ function debouncePromise(fn, delay, onError) {
141
139
  };
142
140
  }
143
141
 
144
- // src/math.ts
145
-
146
- // src/array.ts
147
142
  function toArray(array) {
148
143
  array = array || [];
149
144
  if (Array.isArray(array))
@@ -152,7 +147,7 @@ function toArray(array) {
152
147
  }
153
148
 
154
149
  const name = "kirbyup";
155
- const version = "0.22.1";
150
+ const version = "0.23.0";
156
151
 
157
152
  let resolvedKirbyupConfig;
158
153
  let resolvedPostCssConfig;
@@ -217,7 +212,7 @@ async function viteBuild(options) {
217
212
  }
218
213
  async function resolveOptions(options) {
219
214
  if (!options.entry) {
220
- throw new PrettyError("No input file, try " + cyan(`${name} <path/to/file.js>`));
215
+ throw new PrettyError(`No input file, try ${cyan(`${name} <path/to/file.js>`)}`);
221
216
  }
222
217
  if (!existsSync(options.entry)) {
223
218
  throw new PrettyError(`Cannot find ${options.entry}`);
@@ -240,7 +235,7 @@ async function build(_options) {
240
235
  };
241
236
  }
242
237
  consola.log(green(`${name} v${version}`));
243
- consola.start("Building " + cyan(options.entry));
238
+ consola.start(`Building ${cyan(options.entry)}`);
244
239
  if (options.watch) {
245
240
  consola.info("Running in watch mode");
246
241
  }
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('picocolors');
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/cli.mjs CHANGED
@@ -10,7 +10,7 @@ import 'postcss-load-config';
10
10
  import 'postcss-logical';
11
11
  import 'postcss-dir-pseudo-class';
12
12
  import 'consola';
13
- import 'colorette';
13
+ import 'picocolors';
14
14
  import 'fs/promises';
15
15
  import 'zlib';
16
16
  import 'util';
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('picocolors');
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;
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import 'postcss-load-config';
7
7
  import 'postcss-logical';
8
8
  import 'postcss-dir-pseudo-class';
9
9
  import 'consola';
10
- import 'colorette';
10
+ import 'picocolors';
11
11
  import 'magic-string';
12
12
  import 'unconfig';
13
13
  import 'fs/promises';
@@ -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/dist/plugin.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- /** Inherits Vite's `import.meta.globEager` result type */
2
- declare type GlobEagerResult = Record<string, {
1
+ declare type Module = {
3
2
  [key: string]: any;
4
- }>;
3
+ };
5
4
  declare const kirbyup: Readonly<{
6
5
  /**
7
6
  * Auto-import Kirby Panel components, will be transformed by
@@ -10,7 +9,7 @@ declare const kirbyup: Readonly<{
10
9
  * @example
11
10
  * kirbyup.import('./components/blocks/*.vue')
12
11
  */
13
- import(modules: GlobEagerResult): Record<string, any>;
12
+ import(modules: Record<string, Module>): Record<string, any>;
14
13
  }>;
15
14
 
16
15
  export { kirbyup };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kirbyup",
3
- "version": "0.22.1",
3
+ "version": "0.23.0",
4
4
  "description": "Zero-config bundler for Kirby Panel plugins",
5
5
  "keywords": [
6
6
  "kirby-cms",
@@ -49,46 +49,50 @@
49
49
  "scripts": {
50
50
  "build": "unbuild",
51
51
  "stub": "unbuild --stub",
52
- "test": "vitest",
53
- "test:update": "vitest -u",
52
+ "test": "vitest --watch",
53
+ "test:update": "vitest --update",
54
54
  "format": "prettier --write \"src/**/*.ts\"",
55
55
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
56
- "release": "node scripts/release.js",
57
- "prepare": "husky install"
56
+ "release": "vitest --run && esno scripts/release.ts",
57
+ "prepare": "pnpm exec simple-git-hooks"
58
+ },
59
+ "simple-git-hooks": {
60
+ "commit-msg": "pnpm exec esno scripts/verifyCommit.ts $1"
58
61
  },
59
62
  "dependencies": {
60
63
  "cac": "^6.7.12",
61
- "chokidar": "^3.5.2",
62
- "colorette": "^2.0.16",
64
+ "chokidar": "^3.5.3",
63
65
  "consola": "^2.15.3",
66
+ "execa": "5.1.1",
64
67
  "pathe": "^0.2.0",
65
- "postcss": "^8.4.5",
66
- "postcss-dir-pseudo-class": "^6.0.2",
67
- "postcss-load-config": "^3.1.1",
68
- "postcss-logical": "^5.0.2",
69
- "sass": "^1.46.0",
70
- "unconfig": "^0.2.2",
71
- "vite": "^2.7.10",
72
- "vite-plugin-vue2": "^1.9.2",
68
+ "picocolors": "^1.0.0",
69
+ "postcss": "^8.4.12",
70
+ "postcss-dir-pseudo-class": "^6.0.4",
71
+ "postcss-load-config": "^3.1.3",
72
+ "postcss-logical": "^5.0.4",
73
+ "sass": "^1.49.9",
74
+ "unconfig": "^0.3.2",
75
+ "vite": "^2.8.6",
76
+ "vite-plugin-vue2": "^1.9.3",
73
77
  "vue": "^2.6.14",
74
78
  "vue-template-compiler": "^2.6.14"
75
79
  },
76
80
  "devDependencies": {
77
- "@antfu/utils": "^0.4.0",
78
- "@types/cross-spawn": "^6.0.2",
81
+ "@antfu/utils": "^0.5.0",
79
82
  "@types/fs-extra": "^9.0.13",
80
- "@types/node": "^17.0.8",
83
+ "@types/node": "^17.0.21",
84
+ "@types/prompts": "^2.4.0",
85
+ "@types/semver": "^7.3.9",
81
86
  "conventional-changelog-cli": "^2.2.2",
82
- "esno": "^0.13.0",
83
- "execa": "5.1.1",
84
- "fast-glob": "^3.2.7",
85
- "fs-extra": "^10.0.0",
86
- "husky": "^7.0.4",
87
- "prettier": "^2.5.1",
87
+ "esno": "^0.14.1",
88
+ "fast-glob": "^3.2.11",
89
+ "fs-extra": "^10.0.1",
90
+ "minimist": "^1.2.5",
91
+ "prettier": "^2.6.0",
88
92
  "prompts": "^2.4.2",
89
- "ts-essentials": "^9.1.2",
90
- "typescript": "^4.5.4",
91
- "unbuild": "^0.6.7",
92
- "vitest": "^0.0.134"
93
+ "simple-git-hooks": "^2.7.0",
94
+ "typescript": "^4.6.2",
95
+ "unbuild": "^0.7.0",
96
+ "vitest": "^0.7.4"
93
97
  }
94
98
  }