minista 3.0.0-alpha.9 → 3.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.
Files changed (140) hide show
  1. package/README.md +70 -0
  2. package/dist/cli/build.js +204 -151
  3. package/dist/cli/develop.js +125 -9
  4. package/dist/cli/logger.js +40 -0
  5. package/dist/config/entry.js +30 -7
  6. package/dist/config/image.js +23 -0
  7. package/dist/config/index.js +1 -2
  8. package/dist/config/main.js +67 -37
  9. package/dist/config/mdx.js +13 -30
  10. package/dist/config/sub.js +3 -0
  11. package/dist/config/system.js +9 -0
  12. package/dist/config/vite.js +7 -27
  13. package/dist/generate/archive.js +54 -0
  14. package/dist/generate/asset.js +46 -0
  15. package/dist/generate/image.js +82 -0
  16. package/dist/generate/page.js +67 -0
  17. package/dist/generate/public.js +14 -0
  18. package/dist/generate/remote.js +29 -0
  19. package/dist/generate/search.js +23 -0
  20. package/dist/generate/sprite.js +68 -0
  21. package/dist/plugins/bundle.js +1 -1
  22. package/dist/plugins/fetch.js +1 -1
  23. package/dist/plugins/hydrate.js +1 -1
  24. package/dist/plugins/icon.js +14 -0
  25. package/dist/plugins/image.js +14 -0
  26. package/dist/plugins/mdx.js +7 -0
  27. package/dist/plugins/partial.js +9 -56
  28. package/dist/plugins/react.js +7 -0
  29. package/dist/plugins/search.js +55 -0
  30. package/dist/plugins/ssg.js +1 -1
  31. package/dist/plugins/svgr.js +3 -1
  32. package/dist/server/global.js +8 -2
  33. package/dist/server/hydrate.js +3 -0
  34. package/dist/server/{pages.js → page.js} +17 -6
  35. package/dist/server/{sources.js → source.js} +3 -3
  36. package/dist/server/sprite.js +20 -0
  37. package/dist/server/ssg.js +17 -0
  38. package/dist/shared/comment.js +3 -6
  39. package/dist/shared/delivery.js +379 -0
  40. package/dist/shared/head.js +3 -6
  41. package/dist/shared/icon.js +19 -14
  42. package/dist/shared/image.js +230 -0
  43. package/dist/shared/index.js +4 -1
  44. package/dist/shared/markdown.js +56 -8
  45. package/dist/shared/search.js +260 -0
  46. package/dist/transform/archive.js +43 -0
  47. package/dist/transform/comment.js +13 -12
  48. package/dist/transform/delivery.js +134 -0
  49. package/dist/transform/encode.js +27 -0
  50. package/dist/transform/entry.js +84 -0
  51. package/dist/transform/hydrate.js +38 -0
  52. package/dist/transform/icon.js +74 -0
  53. package/dist/transform/image.js +308 -0
  54. package/dist/transform/page.js +110 -0
  55. package/dist/transform/partial.js +18 -0
  56. package/dist/transform/relative.js +73 -0
  57. package/dist/transform/remote.js +110 -0
  58. package/dist/transform/search.js +136 -0
  59. package/dist/transform/sprite.js +21 -0
  60. package/dist/transform/ssg.js +72 -0
  61. package/dist/transform/tag.js +163 -0
  62. package/dist/types/cli/build.d.ts +7 -2
  63. package/dist/types/cli/logger.d.ts +9 -0
  64. package/dist/types/config/alias.d.ts +5 -5
  65. package/dist/types/config/entry.d.ts +13 -6
  66. package/dist/types/config/image.d.ts +29 -0
  67. package/dist/types/config/index.d.ts +2 -2
  68. package/dist/types/config/main.d.ts +54 -41
  69. package/dist/types/config/mdx.d.ts +1 -2
  70. package/dist/types/config/sub.d.ts +2 -1
  71. package/dist/types/config/system.d.ts +6 -0
  72. package/dist/types/config/user.d.ts +54 -41
  73. package/dist/types/config/vite.d.ts +2 -8
  74. package/dist/types/generate/archive.d.ts +5 -0
  75. package/dist/types/generate/asset.d.ts +10 -0
  76. package/dist/types/generate/image.d.ts +35 -0
  77. package/dist/types/generate/page.d.ts +11 -0
  78. package/dist/types/generate/public.d.ts +4 -0
  79. package/dist/types/generate/remote.d.ts +11 -0
  80. package/dist/types/generate/search.d.ts +5 -0
  81. package/dist/types/generate/sprite.d.ts +14 -0
  82. package/dist/types/plugins/{sprite.d.ts → icon.d.ts} +1 -1
  83. package/dist/types/plugins/{serve.d.ts → image.d.ts} +1 -1
  84. package/dist/types/plugins/mdx.d.ts +3 -0
  85. package/dist/types/plugins/react.d.ts +1 -0
  86. package/dist/types/plugins/search.d.ts +3 -0
  87. package/dist/types/server/global.d.ts +9 -10
  88. package/dist/types/server/page.d.ts +18 -0
  89. package/dist/types/server/source.d.ts +13 -0
  90. package/dist/types/server/sprite.d.ts +8 -0
  91. package/dist/types/server/ssg.d.ts +6 -0
  92. package/dist/types/shared/comment.d.ts +4 -4
  93. package/dist/types/shared/delivery.d.ts +56 -0
  94. package/dist/types/shared/head.d.ts +2 -2
  95. package/dist/types/shared/icon.d.ts +3 -2
  96. package/dist/types/shared/image.d.ts +32 -0
  97. package/dist/types/shared/index.d.ts +41 -1
  98. package/dist/types/shared/markdown.d.ts +21 -8
  99. package/dist/types/shared/search.d.ts +37 -0
  100. package/dist/types/transform/archive.d.ts +7 -0
  101. package/dist/types/transform/comment.d.ts +2 -1
  102. package/dist/types/transform/delivery.d.ts +33 -0
  103. package/dist/types/transform/encode.d.ts +2 -0
  104. package/dist/types/transform/entry.d.ts +9 -0
  105. package/dist/types/transform/hydrate.d.ts +8 -0
  106. package/dist/types/transform/icon.d.ts +15 -0
  107. package/dist/types/transform/image.d.ts +22 -0
  108. package/dist/types/transform/page.d.ts +10 -0
  109. package/dist/types/transform/partial.d.ts +6 -0
  110. package/dist/types/transform/relative.d.ts +12 -0
  111. package/dist/types/transform/remote.d.ts +23 -0
  112. package/dist/types/transform/search.d.ts +16 -0
  113. package/dist/types/transform/sprite.d.ts +5 -0
  114. package/dist/types/transform/ssg.d.ts +17 -0
  115. package/dist/types/transform/tag.d.ts +24 -0
  116. package/dist/types/utility/base.d.ts +1 -0
  117. package/dist/types/utility/element.d.ts +4 -0
  118. package/dist/types/utility/path.d.ts +2 -5
  119. package/dist/types/utility/space.d.ts +5 -0
  120. package/dist/utility/base.js +14 -0
  121. package/dist/utility/element.js +22 -0
  122. package/dist/utility/path.js +20 -14
  123. package/dist/utility/space.js +12 -0
  124. package/package.json +29 -16
  125. package/dist/plugins/serve.js +0 -56
  126. package/dist/plugins/sprite.js +0 -99
  127. package/dist/scripts/hydrate.js +0 -3
  128. package/dist/scripts/ssg.js +0 -59
  129. package/dist/server/app.js +0 -107
  130. package/dist/transform/markdown.js +0 -38
  131. package/dist/transform/tags.js +0 -160
  132. package/dist/types/scripts/ssg.d.ts +0 -10
  133. package/dist/types/server/app.d.ts +0 -14
  134. package/dist/types/server/pages.d.ts +0 -25
  135. package/dist/types/server/sources.d.ts +0 -9
  136. package/dist/types/transform/markdown.d.ts +0 -2
  137. package/dist/types/transform/tags.d.ts +0 -23
  138. /package/dist/{scripts → server}/bundle.js +0 -0
  139. /package/dist/types/{scripts → server}/bundle.d.ts +0 -0
  140. /package/dist/types/{scripts → server}/hydrate.d.ts +0 -0
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # minista
2
+
3
+ <p>
4
+ <a aria-label="Made by QRANOKO" href="https://qranoko.jp">
5
+ <img src="https://img.shields.io/badge/MADE%20BY%20QRANOKO-212121.svg?style=for-the-badge&labelColor=212121">
6
+ </a>
7
+ <a aria-label="NPM version" href="https://www.npmjs.com/package/minista">
8
+ <img alt="" src="https://img.shields.io/npm/v/minista.svg?style=for-the-badge&labelColor=212121">
9
+ </a>
10
+ <a aria-label="License" href="https://github.com/qrac/minista/blob/master/LICENSE">
11
+ <img alt="" src="https://img.shields.io/npm/l/minista.svg?style=for-the-badge&labelColor=212121">
12
+ </a>
13
+ </p>
14
+
15
+ ## Site & Documentation
16
+
17
+ https://minista.qranoko.jp
18
+
19
+ ## About
20
+
21
+ minista(ミニスタ)は React の JSX から綺麗な HTML を作る日本製のスタティックサイトジェネレーターです。
22
+
23
+ ## How To Use
24
+
25
+ ### Automatic Setup
26
+
27
+ ```sh
28
+ $ npm create minista@latest
29
+ ```
30
+
31
+ ### Manual Setup
32
+
33
+ ```sh
34
+ $ npm install --save-dev minista react react-dom
35
+ $ touch ./src/pages/index.jsx
36
+ ```
37
+
38
+ ```js
39
+ // ./src/pages/index.jsx
40
+ export default function () {
41
+ return <h1>Hello!</h1>
42
+ }
43
+ ```
44
+
45
+ `package.json` を開き、以下のスクリプトを追加します。
46
+
47
+ ```json
48
+ "scripts": {
49
+ "dev": "minista",
50
+ "build": "minista build",
51
+ "preview": "minista preview",
52
+ }
53
+ ```
54
+
55
+ ## Commands
56
+
57
+ | コマンド | 内容 |
58
+ | ----------------- | ------------------------------- |
59
+ | `minista` | 開発モード(`Ctrl + C` で停止) |
60
+ | `minista build` | 静的書き出し |
61
+ | `minista preview` | 静的書き出し後の動作確認 |
62
+
63
+ ## License
64
+
65
+ - MIT
66
+
67
+ ## Credit
68
+
69
+ - Author: [Qrac](https://qrac.jp)
70
+ - Organization: [QRANOKO](https://qranoko.jp)
package/dist/cli/build.js CHANGED
@@ -1,35 +1,79 @@
1
1
  import path from "node:path";
2
2
  import fs from "fs-extra";
3
- import pc from "picocolors";
4
3
  import {
5
4
  defineConfig as defineViteConfig,
6
5
  mergeConfig as mergeViteConfig,
7
6
  build as viteBuild,
8
7
  createLogger
9
8
  } from "vite";
10
- import { default as pluginReact } from "@vitejs/plugin-react";
11
- import { default as pluginMdx } from "@mdx-js/rollup";
12
- import beautify from "js-beautify";
9
+ import { parse as parseHtml } from "node-html-parser";
13
10
  import { resolveConfig } from "../config/index.js";
11
+ import { resolveViteEntry } from "../config/entry.js";
12
+ import { pluginReact } from "../plugins/react.js";
13
+ import { pluginPreact } from "../plugins/preact.js";
14
+ import { pluginMdx } from "../plugins/mdx.js";
15
+ import { pluginImage } from "../plugins/image.js";
14
16
  import { pluginSvgr } from "../plugins/svgr.js";
15
- import { pluginSprite } from "../plugins/sprite.js";
17
+ import { pluginIcon } from "../plugins/icon.js";
16
18
  import { pluginFetch } from "../plugins/fetch.js";
17
19
  import { pluginSsg } from "../plugins/ssg.js";
18
20
  import { pluginPartial } from "../plugins/partial.js";
19
21
  import { pluginHydrate } from "../plugins/hydrate.js";
20
- import { pluginPreact } from "../plugins/preact.js";
21
22
  import { pluginBundle } from "../plugins/bundle.js";
23
+ import { pluginSearch } from "../plugins/search.js";
24
+ import { transformDeliveries } from "../transform/delivery.js";
25
+ import { transformArchives } from "../transform/archive.js";
26
+ import { transformRemotes } from "../transform/remote.js";
27
+ import { transformEntries } from "../transform/entry.js";
28
+ import { transformImages } from "../transform/image.js";
29
+ import { transformIcons } from "../transform/icon.js";
30
+ import { transformRelative } from "../transform/relative.js";
31
+ import { transformSearch } from "../transform/search.js";
32
+ import { generatePublics } from "../generate/public.js";
33
+ import { generatePages } from "../generate/page.js";
34
+ import { generateAssets } from "../generate/asset.js";
35
+ import { generateImages } from "../generate/image.js";
36
+ import { generateSprites } from "../generate/sprite.js";
37
+ import { generateArchives } from "../generate/archive.js";
38
+ import { hasElement } from "../utility/element.js";
22
39
  async function build(inlineConfig = {}) {
23
40
  const config = await resolveConfig(inlineConfig);
41
+ const { resolvedRoot, resolvedEntry, tempDir } = config.sub;
42
+ const { assets, search, delivery } = config.main;
43
+ const { partial } = assets;
44
+ const resolvedOut = path.join(resolvedRoot, config.main.out);
45
+ const bundleCssName = path.join(assets.outDir, assets.bundle.outName + ".css");
46
+ const bugBundleCssName = path.join(assets.outDir, "bundle.css");
47
+ const hydrateJsName = path.join(assets.outDir, assets.partial.outName + ".js");
48
+ let ssgResult;
49
+ let assetsResult;
50
+ let hydrateResult;
51
+ let ssgItems;
52
+ let assetItems;
53
+ let hydrateItems;
54
+ let ssgPages = [];
55
+ let parsedPages = [];
56
+ let dynamicEntries = {};
57
+ let assetEntries = {};
58
+ let createPages = [];
59
+ let createAssets = [];
60
+ let createImages = {};
61
+ let createSprites = {};
62
+ let cssNameBugFix = {};
63
+ let hasBundleCss = false;
64
+ let hasHydrate = false;
65
+ let hasSearch = false;
24
66
  const ssgConfig = mergeViteConfig(
25
67
  config.vite,
26
68
  defineViteConfig({
27
69
  build: { write: false, ssr: true, minify: false },
70
+ ssr: { noExternal: "minista" },
28
71
  plugins: [
29
72
  pluginReact(),
30
- pluginMdx(config.mdx),
73
+ pluginMdx(config),
74
+ pluginImage(config),
31
75
  pluginSvgr(config),
32
- pluginSprite(config, true),
76
+ pluginIcon(config),
33
77
  pluginFetch(config),
34
78
  pluginSsg(),
35
79
  pluginPartial(config)
@@ -37,178 +81,187 @@ async function build(inlineConfig = {}) {
37
81
  customLogger: createLogger("warn", { prefix: "[minista]" })
38
82
  })
39
83
  );
84
+ ssgResult = await viteBuild(ssgConfig);
85
+ ssgItems = ssgResult.output.filter((item) => {
86
+ return item.fileName.match(/__minista_plugin_ssg\.js$/);
87
+ });
88
+ if (ssgItems.length > 0) {
89
+ const ssgPath = path.join(tempDir, "ssg.mjs");
90
+ const ssgData = ssgItems[0].source || ssgItems[0].code || "";
91
+ await fs.outputFile(ssgPath, ssgData);
92
+ const { runSsg } = await import(ssgPath);
93
+ ssgPages = await runSsg(config);
94
+ }
95
+ if (ssgPages.length > 0) {
96
+ parsedPages = ssgPages.map((page) => {
97
+ const parsedHtml = parseHtml(page.html, { comment: true });
98
+ return {
99
+ fileName: page.fileName,
100
+ path: page.path,
101
+ group: page.group,
102
+ title: page.path,
103
+ draft: page.draft,
104
+ parsedHtml
105
+ };
106
+ });
107
+ let parsedData = parsedPages.map((item) => item.parsedHtml);
108
+ hasSearch = hasElement(parsedData, `[data-full-text-search]`);
109
+ hasHydrate = hasElement(parsedData, `[data-${partial.rootAttrSuffix}]`);
110
+ transformDeliveries({ parsedData, ssgPages, config });
111
+ transformArchives({ parsedData, config });
112
+ await transformRemotes({
113
+ command: "build",
114
+ parsedData,
115
+ config
116
+ });
117
+ await transformImages({
118
+ command: "build",
119
+ parsedData,
120
+ config,
121
+ createImages
122
+ });
123
+ await transformIcons({
124
+ command: "build",
125
+ parsedData,
126
+ config,
127
+ createSprites
128
+ });
129
+ await transformEntries({
130
+ parsedData,
131
+ config,
132
+ dynamicEntries
133
+ });
134
+ createPages = await Promise.all(
135
+ parsedPages.map(async (page) => {
136
+ const pathname = page.path;
137
+ let parsedHtml = page.parsedHtml;
138
+ if (config.main.base === "" || config.main.base === "./") {
139
+ transformRelative({
140
+ parsedHtml,
141
+ pathname,
142
+ config
143
+ });
144
+ }
145
+ return {
146
+ fileName: page.fileName,
147
+ data: parsedHtml.toString()
148
+ };
149
+ })
150
+ );
151
+ }
152
+ assetEntries = resolveViteEntry(resolvedRoot, resolvedEntry);
153
+ assetEntries = { ...assetEntries, ...dynamicEntries };
40
154
  const assetsConfig = mergeViteConfig(
41
155
  config.vite,
42
156
  defineViteConfig({
43
- build: { write: false },
157
+ build: {
158
+ rollupOptions: {
159
+ input: assetEntries
160
+ },
161
+ write: false
162
+ },
44
163
  plugins: [
45
164
  pluginReact(),
46
- pluginMdx(config.mdx),
165
+ pluginMdx(config),
47
166
  pluginSvgr(config),
48
- pluginSprite(config),
49
167
  pluginBundle()
50
168
  ],
51
169
  customLogger: createLogger("warn", { prefix: "[minista]" })
52
170
  })
53
171
  );
54
- const partialConfig = mergeViteConfig(
172
+ assetsResult = await viteBuild(assetsConfig);
173
+ const hydrateConfig = mergeViteConfig(
55
174
  config.vite,
56
175
  defineViteConfig({
57
176
  build: { write: false },
58
177
  plugins: [
59
178
  pluginReact(),
60
179
  pluginPreact(config),
61
- pluginMdx(config.mdx),
180
+ pluginMdx(config),
62
181
  pluginSvgr(config),
63
- pluginSprite(config),
64
- pluginHydrate()
182
+ pluginHydrate(),
183
+ pluginSearch(config)
65
184
  ],
66
185
  customLogger: createLogger("warn", { prefix: "[minista]" })
67
186
  })
68
187
  );
69
- let ssgResult;
70
- let assetsResult;
71
- let partialResult;
72
- await Promise.all([
73
- ssgResult = await viteBuild(ssgConfig),
74
- assetsResult = await viteBuild(assetsConfig)
75
- ]);
76
- const hasPartial = fs.existsSync(path.join(config.sub.tempDir, "partials"));
77
- if (hasPartial) {
78
- partialResult = await viteBuild(partialConfig);
188
+ if (hasHydrate) {
189
+ hydrateResult = await viteBuild(hydrateConfig);
79
190
  } else {
80
- partialResult = { output: [] };
191
+ hydrateResult = { output: [] };
81
192
  }
82
- const resolvedOut = path.join(config.sub.resolvedRoot, config.main.out);
83
- const resolvedPublic = path.join(config.sub.resolvedRoot, config.main.public);
84
- const hasPublic = fs.existsSync(resolvedPublic);
85
- await fs.emptyDir(resolvedOut);
86
- hasPublic && await fs.copy(resolvedPublic, resolvedOut);
87
- const bundleCssName = path.join(
88
- config.main.assets.outDir,
89
- config.main.assets.bundle.outName + ".css"
90
- );
91
- const bugBundleCssName = path.join(config.main.assets.outDir, "bundle.css");
92
- const hasBundleCss = assetsResult.output.some(
93
- (item) => item.fileName === bundleCssName || item.fileName === bugBundleCssName
94
- );
95
- const partialJsName = path.join(
96
- config.main.assets.outDir,
97
- config.main.assets.partial.outName + ".js"
98
- );
99
- const ssgItems = ssgResult.output.filter(
100
- (item) => item.fileName.match(/__minista_plugin_ssg\.js$/)
101
- );
102
- const assetItems = assetsResult.output.filter(
103
- (item) => !item.fileName.match(/__minista_plugin_bundle\.js$/)
104
- );
105
- const partialItems = partialResult.output.filter(
106
- (item) => item.fileName.match(/__minista_plugin_hydrate\.js$/)
193
+ assetItems = assetsResult.output.filter((item) => {
194
+ return !item.fileName.match(/__minista_plugin_bundle\.js$/);
195
+ });
196
+ hydrateItems = hydrateResult.output.filter((item) => {
197
+ return item.fileName.match(/__minista_plugin_hydrate\.js$/);
198
+ });
199
+ hasBundleCss = assetsResult.output.some((item) => {
200
+ return item.fileName === bundleCssName || item.fileName === bugBundleCssName;
201
+ });
202
+ cssNameBugFix = Object.fromEntries(
203
+ Object.entries(assetEntries).map((item) => {
204
+ return [
205
+ path.join(assets.outDir, path.parse(item[1]).name + ".css"),
206
+ path.join(assets.outDir, item[0] + ".css")
207
+ ];
208
+ })
107
209
  );
108
- let ssgPages = [];
109
- async function getHtmlItems(ssgItem) {
110
- const ssgPath = path.join(config.sub.tempDir, "ssg.mjs");
111
- const ssgData = ssgItem.source || ssgItem.code || "";
112
- if (!ssgData) {
113
- return [];
114
- }
115
- await fs.outputFile(ssgPath, ssgData);
116
- const { runSsg } = await import(ssgPath);
117
- ssgPages = await runSsg(config);
118
- if (ssgPages.length === 0) {
119
- return [];
210
+ cssNameBugFix = { ...cssNameBugFix, ...{ [bugBundleCssName]: bundleCssName } };
211
+ createAssets = [...assetItems, ...hydrateItems].map((item) => {
212
+ const isCss = item.fileName.match(/.*\.css$/);
213
+ const isBundleCss = item.fileName.match(/__minista_plugin_bundle\.css$/);
214
+ const isHydrateJs = item.fileName.match(/__minista_plugin_hydrate\.js$/);
215
+ let fileName = item.fileName;
216
+ isBundleCss && (fileName = bundleCssName);
217
+ isHydrateJs && (fileName = hydrateJsName);
218
+ if (isCss && Object.hasOwn(cssNameBugFix, fileName)) {
219
+ fileName = cssNameBugFix[fileName];
120
220
  }
121
- return ssgPages.map((item) => {
122
- return {
123
- fileName: item.fileName,
124
- data: item.html
125
- };
221
+ fileName = fileName.replace(/-ministaDuplicateName\d*/, "");
222
+ let data = "";
223
+ item.source && (data = item.source);
224
+ item.code && (data = item.code);
225
+ data === "\n" && (data = "");
226
+ return {
227
+ fileName,
228
+ data
229
+ };
230
+ });
231
+ createAssets = createAssets.filter((item) => item.data);
232
+ if (hasSearch && ssgPages.length) {
233
+ const fileName = path.join(search.outDir, search.outName + ".json");
234
+ const searchObj = await transformSearch({
235
+ command: "build",
236
+ ssgPages,
237
+ config
126
238
  });
239
+ const data = JSON.stringify(searchObj);
240
+ createAssets.push({ fileName, data });
127
241
  }
128
- function optimizeItems(items) {
129
- return items.map((item) => {
130
- const isBundleCss = item.fileName.match(/__minista_plugin_bundle\.css$/);
131
- const isBugBundleCss = item.fileName === bugBundleCssName;
132
- const isPartialJs = item.fileName.match(/__minista_plugin_hydrate\.js$/);
133
- let fileName = item.fileName;
134
- isBundleCss && (fileName = bundleCssName);
135
- isBugBundleCss && (fileName = bundleCssName);
136
- isPartialJs && (fileName = partialJsName);
137
- let data = "";
138
- item.source && (data = item.source);
139
- item.code && (data = item.code);
140
- if (data === "\n") {
141
- data = "";
142
- }
143
- return {
144
- fileName,
145
- data
146
- };
147
- }).filter((item) => item.data);
148
- }
149
- const htmlItems = ssgItems[0] ? await getHtmlItems(ssgItems[0]) : [];
150
- const optimizedAssetItems = optimizeItems([...assetItems, ...partialItems]);
151
- const mergedItems = [...htmlItems, ...optimizedAssetItems];
152
- const nameLengths = mergedItems.map((api) => api.fileName.length);
242
+ const pageNames = createPages.map((item) => item.fileName);
243
+ const assetNames = createAssets.map((item) => item.fileName);
244
+ const imageNames = Object.keys(createImages).map((item) => item);
245
+ const iconNames = Object.keys(createSprites).map((item) => item);
246
+ const archiveNames = delivery.archives.map((item) => {
247
+ return path.join(item.outDir, item.outName + "." + item.format);
248
+ });
249
+ const mergedItemNames = [
250
+ ...pageNames,
251
+ ...assetNames,
252
+ ...imageNames,
253
+ ...iconNames,
254
+ ...archiveNames
255
+ ];
256
+ const nameLengths = mergedItemNames.map((item) => item.length);
153
257
  const maxNameLength = nameLengths.reduce((a, b) => a > b ? a : b, 0);
154
- await Promise.all(
155
- mergedItems.map(async (item) => {
156
- const isHtml = item.fileName.match(/.*\.html$/);
157
- const isCss = item.fileName.match(/.*\.css$/);
158
- const isJs = item.fileName.match(/.*\.js$/);
159
- let hasPartialJs = false;
160
- let fileName = item.fileName;
161
- let data = item.data;
162
- if (isHtml) {
163
- hasPartialJs = data.includes(
164
- `data-${config.main.assets.partial.rootAttrSuffix}`
165
- );
166
- if (hasPartialJs) {
167
- data = data.replace(/data-minista-build-partial-src=/g, "src=");
168
- } else {
169
- data = data.replace(
170
- /<script.*data-minista-build-partial-src=.*?><\/script>/g,
171
- "\n\n"
172
- );
173
- }
174
- if (hasBundleCss) {
175
- data = data.replace(/data-minista-build-bundle-href=/g, "href=");
176
- } else {
177
- data = data.replace(
178
- /<link.*data-minista-build-bundle-href=.*?>/g,
179
- "\n\n"
180
- );
181
- }
182
- if (config.main.beautify.useHtml) {
183
- data = beautify.html(data, config.main.beautify.htmlOptions);
184
- }
185
- }
186
- if (isCss && config.main.beautify.useAssets) {
187
- data = beautify.css(data, config.main.beautify.cssOptions);
188
- }
189
- if (isJs && config.main.beautify.useAssets) {
190
- data = beautify.js(data, config.main.beautify.jsOptions);
191
- }
192
- const nameLength = fileName.length;
193
- const spaceCount = maxNameLength - nameLength + 1;
194
- const space = " ".repeat(spaceCount);
195
- const routePath = path.join(
196
- config.sub.resolvedRoot,
197
- config.main.out,
198
- fileName
199
- );
200
- const relativePath = path.relative(process.cwd(), routePath);
201
- const dataSize = (data.length / 1024).toFixed(2);
202
- return await fs.outputFile(routePath, data).then(() => {
203
- console.log(
204
- `${pc.bold(pc.green("BUILD"))} ${pc.bold(relativePath)}` + space + pc.gray(`${dataSize} KiB`)
205
- );
206
- }).catch((err) => {
207
- console.error(err);
208
- });
209
- })
210
- );
211
- return;
258
+ await fs.emptyDir(resolvedOut);
259
+ await generatePublics({ config });
260
+ await generatePages({ createPages, config, hasBundleCss, maxNameLength });
261
+ await generateAssets({ createAssets, config, maxNameLength });
262
+ await generateImages({ createImages, config, maxNameLength });
263
+ await generateSprites({ createSprites, config, maxNameLength });
264
+ await generateArchives({ config, maxNameLength });
212
265
  }
213
266
  export {
214
267
  build
@@ -1,31 +1,52 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
1
3
  import {
2
4
  createServer as createViteServer,
3
5
  defineConfig as defineViteConfig,
4
6
  mergeConfig as mergeViteConfig
5
7
  } from "vite";
6
- import { default as pluginReact } from "@vitejs/plugin-react";
7
- import { default as pluginMdx } from "@mdx-js/rollup";
8
+ import { parse as parseHtml } from "node-html-parser";
8
9
  import { resolveConfig } from "../config/index.js";
10
+ import { pluginReact } from "../plugins/react.js";
11
+ import { pluginPreact } from "../plugins/preact.js";
12
+ import { pluginMdx } from "../plugins/mdx.js";
13
+ import { pluginImage } from "../plugins/image.js";
9
14
  import { pluginSvgr } from "../plugins/svgr.js";
10
- import { pluginSprite } from "../plugins/sprite.js";
15
+ import { pluginIcon } from "../plugins/icon.js";
11
16
  import { pluginFetch } from "../plugins/fetch.js";
12
- import { pluginServe } from "../plugins/serve.js";
13
17
  import { pluginPartial } from "../plugins/partial.js";
14
- import { pluginPreact } from "../plugins/preact.js";
18
+ import { pluginSearch } from "../plugins/search.js";
19
+ import { transformPage } from "../transform/page.js";
20
+ import { transformSsg } from "../transform/ssg.js";
21
+ import { transformTags } from "../transform/tag.js";
22
+ import { transformComments } from "../transform/comment.js";
23
+ import { transformDeliveries } from "../transform/delivery.js";
24
+ import { transformArchives } from "../transform/archive.js";
25
+ import { transformRemotes } from "../transform/remote.js";
26
+ import { transformImages } from "../transform/image.js";
27
+ import { transformIcons } from "../transform/icon.js";
28
+ import { transformEncode } from "../transform/encode.js";
29
+ import { transformSearch } from "../transform/search.js";
30
+ import { generateTempSearch } from "../generate/search.js";
31
+ import { hasElement } from "../utility/element.js";
32
+ const __filename = fileURLToPath(import.meta.url);
33
+ const __dirname = path.dirname(__filename);
15
34
  async function develop(inlineConfig = {}) {
16
35
  const config = await resolveConfig(inlineConfig);
17
36
  const mergedViteConfig = mergeViteConfig(
18
37
  config.vite,
19
38
  defineViteConfig({
20
39
  plugins: [
40
+ pluginDevelop(config),
21
41
  pluginReact(),
22
42
  pluginPreact(config),
23
- pluginMdx(config.mdx),
43
+ pluginMdx(config),
44
+ pluginImage(config),
24
45
  pluginSvgr(config),
25
- pluginSprite(config, true),
46
+ pluginIcon(config),
26
47
  pluginFetch(config),
27
- pluginServe(config),
28
- pluginPartial(config)
48
+ pluginPartial(config),
49
+ pluginSearch(config)
29
50
  ]
30
51
  })
31
52
  );
@@ -33,6 +54,101 @@ async function develop(inlineConfig = {}) {
33
54
  await viteServer.listen();
34
55
  viteServer.printUrls();
35
56
  }
57
+ function pluginDevelop(config) {
58
+ let server;
59
+ let ssgPages = [];
60
+ return {
61
+ name: "minista-vite-plugin:develop",
62
+ configureServer(_server) {
63
+ server = _server;
64
+ return () => {
65
+ server.middlewares.use(async (req, res, next) => {
66
+ try {
67
+ const { resolvedBase } = config.sub;
68
+ let originalUrl = req.originalUrl || "";
69
+ let url = originalUrl;
70
+ if (resolvedBase.match(/^\/.*\/$/)) {
71
+ const reg = new RegExp(`^${resolvedBase}`);
72
+ url = url.replace(reg, "/");
73
+ }
74
+ const { getSources } = await server.ssrLoadModule(
75
+ __dirname + "/../server/source.js"
76
+ );
77
+ const { resolvedGlobal, resolvedPages } = await getSources();
78
+ const { headTags, startTags, endTags } = transformTags({
79
+ command: "serve",
80
+ pathname: url,
81
+ config
82
+ });
83
+ let html = transformPage({
84
+ url,
85
+ resolvedGlobal,
86
+ resolvedPages,
87
+ headTags,
88
+ startTags,
89
+ endTags
90
+ });
91
+ let parsedHtml = parseHtml(html, { comment: true });
92
+ const charsetEl = parsedHtml.querySelector(`meta[charset]`);
93
+ const charset = charsetEl?.getAttribute("charset") || "UTF-8";
94
+ const searchAttr = `[data-full-text-search]`;
95
+ const deliveryAttr = `[data-minista-transform-target="delivery"]`;
96
+ const hasSearch = hasElement(parsedHtml, searchAttr);
97
+ const hasDelivery = hasElement(parsedHtml, deliveryAttr);
98
+ if (hasSearch || hasDelivery) {
99
+ ssgPages = await transformSsg({
100
+ command: "serve",
101
+ resolvedGlobal,
102
+ resolvedPages,
103
+ config
104
+ });
105
+ }
106
+ if (hasSearch) {
107
+ const searchObj = await transformSearch({
108
+ command: "serve",
109
+ ssgPages,
110
+ config
111
+ });
112
+ const data = JSON.stringify(searchObj);
113
+ await generateTempSearch({ data, config });
114
+ }
115
+ transformComments(parsedHtml);
116
+ transformDeliveries({ parsedData: parsedHtml, ssgPages, config });
117
+ transformArchives({ parsedData: parsedHtml, config });
118
+ await transformRemotes({
119
+ command: "serve",
120
+ parsedData: parsedHtml,
121
+ config
122
+ });
123
+ await transformImages({
124
+ command: "serve",
125
+ parsedData: parsedHtml,
126
+ config
127
+ });
128
+ await transformIcons({
129
+ command: "serve",
130
+ parsedData: parsedHtml,
131
+ config,
132
+ server
133
+ });
134
+ html = parsedHtml.toString();
135
+ html = await server.transformIndexHtml(originalUrl, html);
136
+ if (charset.match(/^utf[\s-_]*8$/i)) {
137
+ res.statusCode = 200;
138
+ res.end(html);
139
+ } else {
140
+ res.statusCode = 200;
141
+ res.end(transformEncode(html, charset));
142
+ }
143
+ } catch (e) {
144
+ server.ssrFixStacktrace(e);
145
+ next(e);
146
+ }
147
+ });
148
+ };
149
+ }
150
+ };
151
+ }
36
152
  export {
37
153
  develop
38
154
  };