@roots/bud-build 0.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 (306) hide show
  1. package/LICENSE.md +19 -0
  2. package/README.md +120 -0
  3. package/docs/exports.md +13 -0
  4. package/docs/rules.md +32 -0
  5. package/lib/config/bail.d.ts +3 -0
  6. package/lib/config/bail.d.ts.map +1 -0
  7. package/lib/config/bail.js +2 -0
  8. package/lib/config/bail.js.map +1 -0
  9. package/lib/config/cache.d.ts +3 -0
  10. package/lib/config/cache.d.ts.map +1 -0
  11. package/lib/config/cache.js +2 -0
  12. package/lib/config/cache.js.map +1 -0
  13. package/lib/config/context.d.ts +3 -0
  14. package/lib/config/context.d.ts.map +1 -0
  15. package/lib/config/context.js +2 -0
  16. package/lib/config/context.js.map +1 -0
  17. package/lib/config/dependencies.d.ts +3 -0
  18. package/lib/config/dependencies.d.ts.map +1 -0
  19. package/lib/config/dependencies.js +5 -0
  20. package/lib/config/dependencies.js.map +1 -0
  21. package/lib/config/devtool.d.ts +3 -0
  22. package/lib/config/devtool.d.ts.map +1 -0
  23. package/lib/config/devtool.js +2 -0
  24. package/lib/config/devtool.js.map +1 -0
  25. package/lib/config/entry.d.ts +3 -0
  26. package/lib/config/entry.d.ts.map +1 -0
  27. package/lib/config/entry.js +2 -0
  28. package/lib/config/entry.js.map +1 -0
  29. package/lib/config/experiments.d.ts +3 -0
  30. package/lib/config/experiments.d.ts.map +1 -0
  31. package/lib/config/experiments.js +2 -0
  32. package/lib/config/experiments.js.map +1 -0
  33. package/lib/config/externals.d.ts +3 -0
  34. package/lib/config/externals.d.ts.map +1 -0
  35. package/lib/config/externals.js +2 -0
  36. package/lib/config/externals.js.map +1 -0
  37. package/lib/config/externalsType.d.ts +3 -0
  38. package/lib/config/externalsType.d.ts.map +1 -0
  39. package/lib/config/externalsType.js +2 -0
  40. package/lib/config/externalsType.js.map +1 -0
  41. package/lib/config/index.d.ts +35 -0
  42. package/lib/config/index.d.ts.map +1 -0
  43. package/lib/config/index.js +27 -0
  44. package/lib/config/index.js.map +1 -0
  45. package/lib/config/infrastructureLogging.d.ts +3 -0
  46. package/lib/config/infrastructureLogging.d.ts.map +1 -0
  47. package/lib/config/infrastructureLogging.js +14 -0
  48. package/lib/config/infrastructureLogging.js.map +1 -0
  49. package/lib/config/loader.d.ts +3 -0
  50. package/lib/config/loader.d.ts.map +1 -0
  51. package/lib/config/loader.js +2 -0
  52. package/lib/config/loader.js.map +1 -0
  53. package/lib/config/mode.d.ts +3 -0
  54. package/lib/config/mode.d.ts.map +1 -0
  55. package/lib/config/mode.js +2 -0
  56. package/lib/config/mode.js.map +1 -0
  57. package/lib/config/module.d.ts +3 -0
  58. package/lib/config/module.d.ts.map +1 -0
  59. package/lib/config/module.js +23 -0
  60. package/lib/config/module.js.map +1 -0
  61. package/lib/config/name.d.ts +3 -0
  62. package/lib/config/name.d.ts.map +1 -0
  63. package/lib/config/name.js +2 -0
  64. package/lib/config/name.js.map +1 -0
  65. package/lib/config/node.d.ts +3 -0
  66. package/lib/config/node.d.ts.map +1 -0
  67. package/lib/config/node.js +2 -0
  68. package/lib/config/node.js.map +1 -0
  69. package/lib/config/optimization.d.ts +3 -0
  70. package/lib/config/optimization.d.ts.map +1 -0
  71. package/lib/config/optimization.js +11 -0
  72. package/lib/config/optimization.js.map +1 -0
  73. package/lib/config/output/assetModuleFilename.d.ts +8 -0
  74. package/lib/config/output/assetModuleFilename.d.ts.map +1 -0
  75. package/lib/config/output/assetModuleFilename.js +2 -0
  76. package/lib/config/output/assetModuleFilename.js.map +1 -0
  77. package/lib/config/output/chunkFilename.d.ts +8 -0
  78. package/lib/config/output/chunkFilename.d.ts.map +1 -0
  79. package/lib/config/output/chunkFilename.js +4 -0
  80. package/lib/config/output/chunkFilename.js.map +1 -0
  81. package/lib/config/output/filename.d.ts +8 -0
  82. package/lib/config/output/filename.d.ts.map +1 -0
  83. package/lib/config/output/filename.js +3 -0
  84. package/lib/config/output/filename.js.map +1 -0
  85. package/lib/config/output/index.d.ts +3 -0
  86. package/lib/config/output/index.d.ts.map +1 -0
  87. package/lib/config/output/index.js +18 -0
  88. package/lib/config/output/index.js.map +1 -0
  89. package/lib/config/parallelism.d.ts +3 -0
  90. package/lib/config/parallelism.d.ts.map +1 -0
  91. package/lib/config/parallelism.js +3 -0
  92. package/lib/config/parallelism.js.map +1 -0
  93. package/lib/config/performance.d.ts +3 -0
  94. package/lib/config/performance.d.ts.map +1 -0
  95. package/lib/config/performance.js +2 -0
  96. package/lib/config/performance.js.map +1 -0
  97. package/lib/config/plugins.d.ts +3 -0
  98. package/lib/config/plugins.d.ts.map +1 -0
  99. package/lib/config/plugins.js +2 -0
  100. package/lib/config/plugins.js.map +1 -0
  101. package/lib/config/profile.d.ts +3 -0
  102. package/lib/config/profile.d.ts.map +1 -0
  103. package/lib/config/profile.js +2 -0
  104. package/lib/config/profile.js.map +1 -0
  105. package/lib/config/recordsPath.d.ts +3 -0
  106. package/lib/config/recordsPath.d.ts.map +1 -0
  107. package/lib/config/recordsPath.js +2 -0
  108. package/lib/config/recordsPath.js.map +1 -0
  109. package/lib/config/resolve.d.ts +3 -0
  110. package/lib/config/resolve.d.ts.map +1 -0
  111. package/lib/config/resolve.js +30 -0
  112. package/lib/config/resolve.js.map +1 -0
  113. package/lib/config/stats.d.ts +3 -0
  114. package/lib/config/stats.d.ts.map +1 -0
  115. package/lib/config/stats.js +17 -0
  116. package/lib/config/stats.js.map +1 -0
  117. package/lib/config/target.d.ts +3 -0
  118. package/lib/config/target.d.ts.map +1 -0
  119. package/lib/config/target.js +4 -0
  120. package/lib/config/target.js.map +1 -0
  121. package/lib/handlers/index.d.ts +19 -0
  122. package/lib/handlers/index.d.ts.map +1 -0
  123. package/lib/handlers/index.js +6 -0
  124. package/lib/handlers/index.js.map +1 -0
  125. package/lib/handlers/items/items.d.ts +47 -0
  126. package/lib/handlers/items/items.d.ts.map +1 -0
  127. package/lib/handlers/items/items.js +69 -0
  128. package/lib/handlers/items/items.js.map +1 -0
  129. package/lib/handlers/loaders/loaders.d.ts +12 -0
  130. package/lib/handlers/loaders/loaders.d.ts.map +1 -0
  131. package/lib/handlers/loaders/loaders.js +11 -0
  132. package/lib/handlers/loaders/loaders.js.map +1 -0
  133. package/lib/handlers/register.d.ts +14 -0
  134. package/lib/handlers/register.d.ts.map +1 -0
  135. package/lib/handlers/register.js +26 -0
  136. package/lib/handlers/register.js.map +1 -0
  137. package/lib/handlers/rules/css.d.ts +3 -0
  138. package/lib/handlers/rules/css.d.ts.map +1 -0
  139. package/lib/handlers/rules/css.js +5 -0
  140. package/lib/handlers/rules/css.js.map +1 -0
  141. package/lib/handlers/rules/css.module.d.ts +3 -0
  142. package/lib/handlers/rules/css.module.d.ts.map +1 -0
  143. package/lib/handlers/rules/css.module.js +5 -0
  144. package/lib/handlers/rules/css.module.js.map +1 -0
  145. package/lib/handlers/rules/csv.d.ts +3 -0
  146. package/lib/handlers/rules/csv.d.ts.map +1 -0
  147. package/lib/handlers/rules/csv.js +5 -0
  148. package/lib/handlers/rules/csv.js.map +1 -0
  149. package/lib/handlers/rules/font.d.ts +3 -0
  150. package/lib/handlers/rules/font.d.ts.map +1 -0
  151. package/lib/handlers/rules/font.inline.d.ts +3 -0
  152. package/lib/handlers/rules/font.inline.d.ts.map +1 -0
  153. package/lib/handlers/rules/font.inline.js +6 -0
  154. package/lib/handlers/rules/font.inline.js.map +1 -0
  155. package/lib/handlers/rules/font.js +5 -0
  156. package/lib/handlers/rules/font.js.map +1 -0
  157. package/lib/handlers/rules/html.d.ts +3 -0
  158. package/lib/handlers/rules/html.d.ts.map +1 -0
  159. package/lib/handlers/rules/html.js +5 -0
  160. package/lib/handlers/rules/html.js.map +1 -0
  161. package/lib/handlers/rules/image.d.ts +3 -0
  162. package/lib/handlers/rules/image.d.ts.map +1 -0
  163. package/lib/handlers/rules/image.inline.d.ts +3 -0
  164. package/lib/handlers/rules/image.inline.d.ts.map +1 -0
  165. package/lib/handlers/rules/image.inline.js +6 -0
  166. package/lib/handlers/rules/image.inline.js.map +1 -0
  167. package/lib/handlers/rules/image.js +5 -0
  168. package/lib/handlers/rules/image.js.map +1 -0
  169. package/lib/handlers/rules/index.d.ts +19 -0
  170. package/lib/handlers/rules/index.d.ts.map +1 -0
  171. package/lib/handlers/rules/index.js +35 -0
  172. package/lib/handlers/rules/index.js.map +1 -0
  173. package/lib/handlers/rules/js.d.ts +3 -0
  174. package/lib/handlers/rules/js.d.ts.map +1 -0
  175. package/lib/handlers/rules/js.js +5 -0
  176. package/lib/handlers/rules/js.js.map +1 -0
  177. package/lib/handlers/rules/json.d.ts +3 -0
  178. package/lib/handlers/rules/json.d.ts.map +1 -0
  179. package/lib/handlers/rules/json.js +7 -0
  180. package/lib/handlers/rules/json.js.map +1 -0
  181. package/lib/handlers/rules/svg.d.ts +3 -0
  182. package/lib/handlers/rules/svg.d.ts.map +1 -0
  183. package/lib/handlers/rules/svg.inline.d.ts +5 -0
  184. package/lib/handlers/rules/svg.inline.d.ts.map +1 -0
  185. package/lib/handlers/rules/svg.inline.js +9 -0
  186. package/lib/handlers/rules/svg.inline.js.map +1 -0
  187. package/lib/handlers/rules/svg.js +5 -0
  188. package/lib/handlers/rules/svg.js.map +1 -0
  189. package/lib/handlers/rules/toml.d.ts +3 -0
  190. package/lib/handlers/rules/toml.d.ts.map +1 -0
  191. package/lib/handlers/rules/toml.js +7 -0
  192. package/lib/handlers/rules/toml.js.map +1 -0
  193. package/lib/handlers/rules/webp.d.ts +3 -0
  194. package/lib/handlers/rules/webp.d.ts.map +1 -0
  195. package/lib/handlers/rules/webp.js +5 -0
  196. package/lib/handlers/rules/webp.js.map +1 -0
  197. package/lib/handlers/rules/xml.d.ts +3 -0
  198. package/lib/handlers/rules/xml.d.ts.map +1 -0
  199. package/lib/handlers/rules/xml.js +5 -0
  200. package/lib/handlers/rules/xml.js.map +1 -0
  201. package/lib/handlers/rules/yml.d.ts +3 -0
  202. package/lib/handlers/rules/yml.d.ts.map +1 -0
  203. package/lib/handlers/rules/yml.js +5 -0
  204. package/lib/handlers/rules/yml.js.map +1 -0
  205. package/lib/helpers/isMjs.d.ts +3 -0
  206. package/lib/helpers/isMjs.d.ts.map +1 -0
  207. package/lib/helpers/isMjs.js +2 -0
  208. package/lib/helpers/isMjs.js.map +1 -0
  209. package/lib/helpers/scriptExtension.d.ts +3 -0
  210. package/lib/helpers/scriptExtension.d.ts.map +1 -0
  211. package/lib/helpers/scriptExtension.js +5 -0
  212. package/lib/helpers/scriptExtension.js.map +1 -0
  213. package/lib/index.d.ts +12 -0
  214. package/lib/index.d.ts.map +1 -0
  215. package/lib/index.js +14 -0
  216. package/lib/index.js.map +1 -0
  217. package/lib/item/index.d.ts +84 -0
  218. package/lib/item/index.d.ts.map +1 -0
  219. package/lib/item/index.js +165 -0
  220. package/lib/item/index.js.map +1 -0
  221. package/lib/loader/index.d.ts +26 -0
  222. package/lib/loader/index.d.ts.map +1 -0
  223. package/lib/loader/index.js +28 -0
  224. package/lib/loader/index.js.map +1 -0
  225. package/lib/rule/index.d.ts +118 -0
  226. package/lib/rule/index.d.ts.map +1 -0
  227. package/lib/rule/index.js +261 -0
  228. package/lib/rule/index.js.map +1 -0
  229. package/lib/service.d.ts +84 -0
  230. package/lib/service.d.ts.map +1 -0
  231. package/lib/service.js +212 -0
  232. package/lib/service.js.map +1 -0
  233. package/lib/shared/base.d.ts +20 -0
  234. package/lib/shared/base.d.ts.map +1 -0
  235. package/lib/shared/base.js +43 -0
  236. package/lib/shared/base.js.map +1 -0
  237. package/lib/types.d.ts +52 -0
  238. package/lib/types.d.ts.map +1 -0
  239. package/lib/types.js +4 -0
  240. package/lib/types.js.map +1 -0
  241. package/package.json +97 -0
  242. package/src/config/bail.ts +4 -0
  243. package/src/config/cache.ts +4 -0
  244. package/src/config/context.ts +3 -0
  245. package/src/config/dependencies.ts +11 -0
  246. package/src/config/devtool.ts +4 -0
  247. package/src/config/entry.ts +4 -0
  248. package/src/config/experiments.ts +4 -0
  249. package/src/config/externals.ts +4 -0
  250. package/src/config/externalsType.ts +3 -0
  251. package/src/config/index.test.ts +129 -0
  252. package/src/config/index.ts +67 -0
  253. package/src/config/infrastructureLogging.ts +18 -0
  254. package/src/config/loader.ts +4 -0
  255. package/src/config/mode.ts +4 -0
  256. package/src/config/module.ts +45 -0
  257. package/src/config/name.ts +4 -0
  258. package/src/config/node.ts +4 -0
  259. package/src/config/optimization.ts +20 -0
  260. package/src/config/output/assetModuleFilename.ts +9 -0
  261. package/src/config/output/chunkFilename.ts +18 -0
  262. package/src/config/output/filename.ts +14 -0
  263. package/src/config/output/index.ts +28 -0
  264. package/src/config/parallelism.ts +6 -0
  265. package/src/config/performance.ts +4 -0
  266. package/src/config/plugins.ts +4 -0
  267. package/src/config/profile.ts +4 -0
  268. package/src/config/recordsPath.ts +7 -0
  269. package/src/config/resolve.ts +45 -0
  270. package/src/config/stats.ts +22 -0
  271. package/src/config/target.ts +9 -0
  272. package/src/handlers/index.ts +22 -0
  273. package/src/handlers/items/items.ts +93 -0
  274. package/src/handlers/loaders/loaders.ts +31 -0
  275. package/src/handlers/register.ts +47 -0
  276. package/src/handlers/rules/css.module.ts +7 -0
  277. package/src/handlers/rules/css.ts +7 -0
  278. package/src/handlers/rules/csv.test.ts +28 -0
  279. package/src/handlers/rules/csv.ts +7 -0
  280. package/src/handlers/rules/font.inline.ts +8 -0
  281. package/src/handlers/rules/font.ts +7 -0
  282. package/src/handlers/rules/html.ts +7 -0
  283. package/src/handlers/rules/image.inline.ts +8 -0
  284. package/src/handlers/rules/image.ts +7 -0
  285. package/src/handlers/rules/index.ts +35 -0
  286. package/src/handlers/rules/js.ts +7 -0
  287. package/src/handlers/rules/json.ts +10 -0
  288. package/src/handlers/rules/svg.inline.test.ts +30 -0
  289. package/src/handlers/rules/svg.inline.ts +13 -0
  290. package/src/handlers/rules/svg.ts +7 -0
  291. package/src/handlers/rules/toml.test.ts +22 -0
  292. package/src/handlers/rules/toml.ts +10 -0
  293. package/src/handlers/rules/webp.ts +7 -0
  294. package/src/handlers/rules/xml.test.ts +27 -0
  295. package/src/handlers/rules/xml.ts +7 -0
  296. package/src/handlers/rules/yml.ts +7 -0
  297. package/src/helpers/isMjs.ts +4 -0
  298. package/src/helpers/scriptExtension.ts +11 -0
  299. package/src/index.ts +17 -0
  300. package/src/item/index.ts +179 -0
  301. package/src/loader/index.ts +38 -0
  302. package/src/rule/index.test.ts +88 -0
  303. package/src/rule/index.ts +257 -0
  304. package/src/service.ts +204 -0
  305. package/src/shared/base.ts +37 -0
  306. package/src/types.ts +57 -0
@@ -0,0 +1,47 @@
1
+ import type {Bud} from '@roots/bud-framework'
2
+
3
+ import type {Factory} from './index.js'
4
+ import * as items from './items/items.js'
5
+ import * as loaders from './loaders/loaders.js'
6
+ import {rules} from './rules/index.js'
7
+
8
+ /**
9
+ * Registry factory curry function
10
+ */
11
+ export interface makeRegister {
12
+ <T, F extends CallableFunction>(props: Partial<Bud>, setRule: F): <
13
+ K extends keyof T & string,
14
+ >([key, factory]: [K, Factory<T[K]>]) => void
15
+ }
16
+
17
+ /**
18
+ * Register built-in {@link loaders}, {@link items} and {@link rules}
19
+ */
20
+ export async function register(bud: Bud) {
21
+ Object.entries(loaders).map(makeRegister(bud, bud.build.setLoader))
22
+ Object.entries(items).map(makeRegister(bud, bud.build.setItem))
23
+
24
+ // this is a bit of a hack
25
+ // that sets the base stylesheet loader
26
+ // to minicss in production and style-loader in development
27
+ bud.build.items.precss = bud.isProduction
28
+ ? bud.build.items.minicss
29
+ : bud.build.items.style
30
+
31
+ Object.entries(rules).map(makeRegister(bud, bud.build.setRule))
32
+ }
33
+
34
+ export const makeRegister: makeRegister =
35
+ ({build, hooks, isProduction, path}, setRule) =>
36
+ ([key, factory]) =>
37
+ setRule(
38
+ key,
39
+ factory({
40
+ filter: hooks.filter,
41
+ makeItem: build.makeItem,
42
+ makeLoader: build.makeLoader,
43
+ makeRule: build.makeRule,
44
+ isProduction,
45
+ path,
46
+ }),
47
+ )
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const cssModule: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.cssModule`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setUse([`precss`, `cssModule`])
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const css: Factory = ({filter, makeRule}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.css`))
6
+ .setInclude([({path}) => path(`@src`)])
7
+ .setUse([`precss`, `css`])
@@ -0,0 +1,28 @@
1
+ import {factory} from '@repo/test-kit/bud'
2
+ import {describe, expect, it} from 'vitest'
3
+
4
+ import {csv} from './csv.js'
5
+
6
+ describe(`csv loader`, () => {
7
+ it(`should return a rule`, async () => {
8
+ const bud = await factory()
9
+
10
+ const result = csv({
11
+ filter: bud.hooks.filter,
12
+ makeItem: bud.build.makeItem,
13
+ makeLoader: bud.build.makeLoader,
14
+ makeRule: bud.build.makeRule,
15
+ isProduction: bud.isProduction,
16
+ path: bud.path,
17
+ })
18
+
19
+ const webpackOutput = result.toWebpack()
20
+
21
+ expect(webpackOutput.use?.[0]).toEqual(
22
+ expect.objectContaining({
23
+ ident: `csv`,
24
+ loader: `@roots/bud-support/csv-loader`,
25
+ }),
26
+ )
27
+ })
28
+ })
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const csv: Factory = ({makeRule, path, filter}) =>
4
+ makeRule()
5
+ .setInclude([path()])
6
+ .setTest(() => filter(`pattern.csv`))
7
+ .setUse([`csv`])
@@ -0,0 +1,8 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const inlineFont: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.font`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setResourceQuery(/inline/)
8
+ .setType(`asset/inline`)
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const font: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setType(`asset`)
6
+ .setTest(filter(`pattern.font`))
7
+ .setInclude([() => path(`@src`)])
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const html: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setInclude([() => path()])
6
+ .setTest(filter(`pattern.html`))
7
+ .setUse([`html`])
@@ -0,0 +1,8 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const inlineImage: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.image`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setType(`asset/inline`)
8
+ .setResourceQuery(/inline/)
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const image: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.image`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setType(`asset/resource`)
@@ -0,0 +1,35 @@
1
+ import {css} from './css.js'
2
+ import {cssModule} from './css.module.js'
3
+ import {csv} from './csv.js'
4
+ import {inlineFont} from './font.inline.js'
5
+ import {font} from './font.js'
6
+ import {html} from './html.js'
7
+ import {inlineImage} from './image.inline.js'
8
+ import {image} from './image.js'
9
+ import {js} from './js.js'
10
+ import {json} from './json.js'
11
+ import {inlineSvg} from './svg.inline.js'
12
+ import {svg} from './svg.js'
13
+ import {toml} from './toml.js'
14
+ import {webp} from './webp.js'
15
+ import {xml} from './xml.js'
16
+ import {yml} from './yml.js'
17
+
18
+ export const rules = {
19
+ toml,
20
+ xml,
21
+ csv,
22
+ yml,
23
+ html,
24
+ json,
25
+ font,
26
+ svg,
27
+ webp,
28
+ image,
29
+ inlineFont,
30
+ inlineSvg,
31
+ inlineImage,
32
+ cssModule,
33
+ css,
34
+ js,
35
+ }
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const js: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.js`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setUse([])
@@ -0,0 +1,10 @@
1
+ import json5 from '@roots/bud-support/json5'
2
+
3
+ import type {Factory} from '../index.js'
4
+
5
+ export const json: Factory = ({filter, makeRule, path}) =>
6
+ makeRule()
7
+ .setType(`json`)
8
+ .setInclude([() => path()])
9
+ .setTest(filter(`pattern.json`))
10
+ .setParser({parse: json5.parse})
@@ -0,0 +1,30 @@
1
+ import {describe, expect, it} from 'vitest'
2
+
3
+ import {inlineSvg, dataUrl} from './svg.inline.js'
4
+
5
+ describe(`svg-inline`, () => {
6
+ it(`should return a rule`, async () => {
7
+ const bud = await import(`@repo/test-kit/bud`).then(
8
+ async ({factory}) => await factory(),
9
+ )
10
+ const result = inlineSvg({
11
+ filter: bud.hooks.filter,
12
+ makeItem: bud.build.makeItem,
13
+ makeLoader: bud.build.makeLoader,
14
+ makeRule: bud.build.makeRule,
15
+ isProduction: bud.isProduction,
16
+ path: bud.path,
17
+ })
18
+
19
+ const webpackOutput = result.toWebpack()
20
+ expect(webpackOutput.test).toEqual(bud.hooks.filter(`pattern.svg`))
21
+ expect(webpackOutput.include).toEqual([bud.path(`@src`)])
22
+ expect(webpackOutput.resourceQuery).toEqual(/inline/)
23
+ expect(webpackOutput.type).toEqual(`asset/inline`)
24
+ expect(webpackOutput.generator).toEqual(
25
+ expect.objectContaining({
26
+ dataUrl,
27
+ }),
28
+ )
29
+ })
30
+ })
@@ -0,0 +1,13 @@
1
+ import dataUri from '@roots/bud-support/mini-svg-data-uri'
2
+
3
+ import type {Factory} from '../index.js'
4
+
5
+ export const inlineSvg: Factory = ({filter, makeRule, path}) =>
6
+ makeRule()
7
+ .setTest(filter(`pattern.svg`))
8
+ .setInclude([() => path(`@src`)])
9
+ .setResourceQuery(/inline/)
10
+ .setGenerator({dataUrl})
11
+ .setType(`asset/inline`)
12
+
13
+ export const dataUrl = (data: Buffer) => dataUri(data.toString())
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const svg: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.svg`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setType(`asset/resource`)
@@ -0,0 +1,22 @@
1
+ import {factory} from '@repo/test-kit/bud'
2
+ import {describe, expect, it} from 'vitest'
3
+
4
+ import {toml} from './toml'
5
+
6
+ describe(`toml loader`, () => {
7
+ let bud
8
+
9
+ it(`should return a rule`, async () => {
10
+ bud = await factory()
11
+ const result = toml({
12
+ filter: bud.hooks.filter,
13
+ makeItem: bud.build.makeItem,
14
+ makeLoader: bud.build.makeLoader,
15
+ makeRule: bud.build.makeRule,
16
+ isProduction: bud.isProduction,
17
+ path: bud.path,
18
+ })
19
+ const webpackOutput = result.toWebpack()
20
+ expect(webpackOutput.type).toEqual(`json`)
21
+ })
22
+ })
@@ -0,0 +1,10 @@
1
+ import tomlParser from '@roots/bud-support/toml'
2
+
3
+ import type {Factory} from '../index.js'
4
+
5
+ export const toml: Factory = ({filter, makeRule, path}) =>
6
+ makeRule()
7
+ .setType(`json`)
8
+ .setInclude([() => path()])
9
+ .setTest(() => filter(`pattern.toml`))
10
+ .setParser({parse: tomlParser.parse})
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const webp: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setTest(filter(`pattern.webp`))
6
+ .setInclude([() => path(`@src`)])
7
+ .setType(`asset/resource`)
@@ -0,0 +1,27 @@
1
+ import {describe, expect, it} from 'vitest'
2
+
3
+ import {xml} from './xml'
4
+
5
+ describe(`xml loader`, () => {
6
+ it(`should return a rule`, async () => {
7
+ const bud = await import(`@repo/test-kit/bud`).then(
8
+ async ({factory}) => await factory(),
9
+ )
10
+ const result = xml({
11
+ filter: bud.hooks.filter,
12
+ makeItem: bud.build.makeItem,
13
+ makeLoader: bud.build.makeLoader,
14
+ makeRule: bud.build.makeRule,
15
+ isProduction: bud.isProduction,
16
+ path: bud.path,
17
+ })
18
+
19
+ const webpackOutput = result.toWebpack()
20
+ expect(webpackOutput.use?.[0]).toEqual(
21
+ expect.objectContaining({
22
+ ident: `xml`,
23
+ loader: `@roots/bud-support/xml-loader`,
24
+ }),
25
+ )
26
+ })
27
+ })
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const xml: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setInclude([() => path()])
6
+ .setTest(() => filter(`pattern.xml`))
7
+ .setUse([`xml`])
@@ -0,0 +1,7 @@
1
+ import type {Factory} from '../index.js'
2
+
3
+ export const yml: Factory = ({filter, makeRule, path}) =>
4
+ makeRule()
5
+ .setInclude([() => path()])
6
+ .setTest(filter(`pattern.yml`))
7
+ .setUse([`yml`])
@@ -0,0 +1,4 @@
1
+ import type {Bud} from '@roots/bud-framework'
2
+
3
+ export const isMjs = (filter: Bud[`hooks`][`filter`]) =>
4
+ filter(`build.experiments`)?.outputModule === true
@@ -0,0 +1,11 @@
1
+ import type {Bud} from '@roots/bud-framework'
2
+
3
+ import {isMjs} from './isMjs.js'
4
+
5
+ export const scriptExtension = (
6
+ filter: Bud[`hooks`][`filter`],
7
+ mjs: `.${string}` = `.mjs`,
8
+ js: `.${string}` = `.js`,
9
+ ): `.${string}` => {
10
+ return isMjs(filter) ? mjs : js
11
+ }
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ // Copyright © Roots Software Foundation LLC
2
+ // Licensed under the MIT license.
3
+
4
+ /**
5
+ * Configuration builder
6
+ *
7
+ * @see https://bud.js.org
8
+ * @see https://github.com/roots/bud
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+
13
+ import './types.js'
14
+
15
+ import {Build} from './service.js'
16
+
17
+ export default Build
@@ -0,0 +1,179 @@
1
+ import type {Bud, Loaders} from '@roots/bud-framework'
2
+ import type * as Build from '@roots/bud-framework/services/build'
3
+ import {bind} from '@roots/bud-support/decorators'
4
+ import isString from '@roots/bud-support/lodash/isString'
5
+ import {basename} from 'path'
6
+
7
+ import {Loader} from '../loader/index.js'
8
+ import Base from '../shared/base.js'
9
+
10
+ export type ConstructorOptions = Build.Item.ConstructorOptions
11
+
12
+ /**
13
+ * Item class
14
+ */
15
+ class Item extends Base implements Build.Item {
16
+ /**
17
+ * Identifier
18
+ * @public
19
+ */
20
+ public ident: string
21
+
22
+ /**
23
+ * Loader
24
+ *
25
+ * @public
26
+ */
27
+ public loader: Loader | `${keyof Loaders & string}`
28
+
29
+ /**
30
+ * Loader options
31
+ *
32
+ * @public
33
+ */
34
+ public options: Build.Item['options']
35
+
36
+ /**
37
+ * Class constructor
38
+ *
39
+ * @public
40
+ */
41
+ public constructor(
42
+ public override _app: () => Bud,
43
+ constructorParams?: {
44
+ ident?: string
45
+ loader?: Loader | `${keyof Loaders & string}`
46
+ options?: Item['options']
47
+ },
48
+ ) {
49
+ super(_app)
50
+
51
+ constructorParams?.ident && this.setIdent(constructorParams.ident)
52
+ constructorParams?.loader && this.setLoader(constructorParams.loader)
53
+
54
+ !constructorParams?.ident &&
55
+ constructorParams?.loader &&
56
+ this.setIdent(
57
+ isString(constructorParams.loader)
58
+ ? constructorParams.loader
59
+ : basename(constructorParams.loader.getSrc()),
60
+ )
61
+
62
+ constructorParams?.options &&
63
+ this.setOptions(constructorParams.options)
64
+ }
65
+
66
+ @bind
67
+ public getIdent(): Build.Item['ident'] {
68
+ return this.ident
69
+ }
70
+
71
+ @bind
72
+ public setIdent(ident: Build.Item['ident']): this {
73
+ this.ident = ident
74
+ return this
75
+ }
76
+
77
+ /**
78
+ * Get rule set item loader
79
+ *
80
+ * @public
81
+ * @decorator `@bind`
82
+ */
83
+ @bind
84
+ public getLoader(): Loader {
85
+ return this.loader instanceof Loader
86
+ ? this.loader
87
+ : this.app.build.loaders[this.loader]
88
+ }
89
+
90
+ /**
91
+ * Set rule set item loader
92
+ *
93
+ * @public
94
+ * @decorator `@bind`
95
+ */
96
+ @bind
97
+ public setLoader(loader: Loader | `${keyof Loaders & string}`): this {
98
+ this.loader = loader
99
+
100
+ if (!this.ident)
101
+ this.setIdent(basename(isString(loader) ? loader : loader.getSrc()))
102
+
103
+ return this
104
+ }
105
+
106
+ /**
107
+ * Get rule set item options
108
+ *
109
+ * @public
110
+ * @decorator `@bind`
111
+ */
112
+ @bind
113
+ public getOptions(): Item['options'] {
114
+ return this.unwrap(this.options)
115
+ }
116
+
117
+ /**
118
+ * Set rule set item options
119
+ *
120
+ * @public
121
+ * @decorator `@bind`
122
+ */
123
+ @bind
124
+ public setOptions(options: Item['options']) {
125
+ this.options = options
126
+ return this
127
+ }
128
+
129
+ /**
130
+ * Merge rule set item options
131
+ *
132
+ * @public
133
+ * @decorator `@bind`
134
+ */
135
+ @bind
136
+ public mergeOptions(options: Build.Item.Options): this {
137
+ this.setOptions({
138
+ ...(this.getOptions() ?? {}),
139
+ ...options,
140
+ })
141
+
142
+ return this
143
+ }
144
+
145
+ /**
146
+ * Produce rule set item object for Webpack
147
+ *
148
+ * @public
149
+ * @decorator `@bind`
150
+ */
151
+ @bind
152
+ public toWebpack(): Build.Item.Output {
153
+ const output: Build.Item.Output = {
154
+ loader: this.getLoader()?.getSrc(),
155
+ }
156
+
157
+ if (this.options) {
158
+ output.options = this.getOptions()
159
+ }
160
+
161
+ if (this.ident) {
162
+ output.ident = this.getIdent()
163
+ }
164
+
165
+ if (!output.loader) {
166
+ this.app.error(`error in ${this.ident}`, `no loader registered`)
167
+ }
168
+
169
+ return Object.entries(output).reduce(
170
+ (output, [key, value]) => ({
171
+ ...(output ?? {}),
172
+ ...(value ? {[key]: value} : {}),
173
+ }),
174
+ {},
175
+ )
176
+ }
177
+ }
178
+
179
+ export {Item}
@@ -0,0 +1,38 @@
1
+ import type {Bud} from '@roots/bud-framework'
2
+
3
+ import Base from '../shared/base.js'
4
+
5
+ /**
6
+ * Bud Loader
7
+ */
8
+ class Loader extends Base {
9
+ /**
10
+ * Factory returning the loader path
11
+ *
12
+ * @public
13
+ */
14
+ public src: string
15
+ /**
16
+ * Class constructor
17
+ *
18
+ * @param src - Either a factory returning a string or a literal string
19
+ *
20
+ * @public
21
+ */
22
+ public constructor(public override _app: () => Bud, src: string) {
23
+ super(_app)
24
+ this._app = _app
25
+ this.src = src
26
+ }
27
+
28
+ public getSrc(): string {
29
+ return this.src
30
+ }
31
+
32
+ public setSrc(src: string) {
33
+ this.src = src
34
+ return this
35
+ }
36
+ }
37
+
38
+ export {Loader}