bun_bun_bundle 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74f92b0b7b6bf9ae9cba639bcd548cf447e89ac55d14a121b55926677a75658b
4
- data.tar.gz: d50ffe4d0477df3390702c9ee1c7797a63e4d3ab32cd4e885db340f12516973f
3
+ metadata.gz: 11c05e743bc0de032fa96e01847eab10641fecb9cf59151b62f14fdfb4b8384f
4
+ data.tar.gz: 3d6f0044f6c3afcbb38dcc847ac2250eec8616c5a09bf51c7022468fff86e766
5
5
  SHA512:
6
- metadata.gz: 1b34424077d7ffb800d89c0a1790278524a5c1ea2640c38eefd5f9c6a4c2f7c7fa6e42a06ee08a4082889cb9f524b0ae7594e0da5914d9e29d7d19085c79e194
7
- data.tar.gz: c7d5c0f4b24c2025c12ad3f1c33a7933f8806bc13a3a0f7e1f6d3b2c3b31fd048237ad5dbb1b07b3686f2d1e196808ddbb99fc08ae9e7552f01826bb211c7eaf
6
+ metadata.gz: 9063a677cbb30db62e3162e7c0b753dd1f34b3b36ecc1c076efeba45ad3d3d09b7dac00ca575c6159cd0ea9ea13066963a053a7d2928501b9ebc879e4eecb52e
7
+ data.tar.gz: 8e9b0d27cb3f11dadfd88ac64deb1b52c54f20b63bb738d55dbb196d40b83049a1cb1285eb68ee379cc60cffbcb9f516d15864c74b9c2a05497c4fa1713f044c
data/README.md CHANGED
@@ -32,7 +32,7 @@ but nothing is holding you back from enabling them in development as well.
32
32
 
33
33
  ### Extensible plugin system
34
34
 
35
- BunBunBundle comes with built-in plugins for CSS glob imports, root aliases,
35
+ BunBunBundle comes with built-in plugins for root aliases, CSS glob imports,
36
36
  and JS glob imports. Plugins are simple, plain JS files, so you can create your
37
37
  own JS/CSS transformers, and raw Bun plugins are supported as well.
38
38
 
@@ -207,8 +207,8 @@ Place a `config/bun.json` in your project root:
207
207
  "secure": false
208
208
  },
209
209
  "plugins": {
210
- "css": ["cssAliases", "cssGlobs"],
211
- "js": ["jsGlobs"]
210
+ "css": ["aliases", "cssGlobs"],
211
+ "js": ["aliases", "jsGlobs"]
212
212
  }
213
213
  }
214
214
  ```
@@ -218,35 +218,133 @@ override.
218
218
 
219
219
  ## Plugins
220
220
 
221
- Three plugins are included out of the box:
221
+ Three plugins are included out of the box.
222
222
 
223
- | Plugin | Description |
224
- | ------------ | ---------------------------------------------------------------- |
225
- | `cssAliases` | Resolves `$/` root aliases in CSS `url()` references |
226
- | `cssGlobs` | Expands glob patterns in `@import` statements |
227
- | `jsGlobs` | Compiles `import x from 'glob:./path/*.js'` into object mappings |
223
+ ### `aliases`
224
+
225
+ Resolves `$/` root aliases to absolute paths in both CSS and JS files. This
226
+ lets you reference assets and modules from the project root without worrying
227
+ about relative paths.
228
+
229
+ In CSS:
230
+
231
+ ```css
232
+ @import '$/app/assets/css/reset.css';
233
+
234
+ .logo {
235
+ background: url('$/app/assets/images/logo.png');
236
+ }
237
+ ```
238
+
239
+ In JS:
240
+
241
+ ```javascript
242
+ import utils from '$/lib/utils.js'
243
+ ```
244
+
245
+ All `$/` references are resolved to your project root.
246
+
247
+ ### `cssGlobs`
248
+
249
+ Expands glob patterns in CSS `@import` statements. Instead of manually listing
250
+ every file, you can import an entire directory at once:
251
+
252
+ ```css
253
+ @import './components/**/*.css';
254
+ ```
255
+
256
+ This will be expanded into individual `@import` lines for each matching file,
257
+ sorted alphabetically.
258
+
259
+ > [!NOTE]
260
+ > A warning is logged if the pattern matches no files.
261
+
262
+ ### `jsGlobs`
263
+
264
+ Compiles glob imports into an object that maps file paths to their default
265
+ exports. Use the special `glob:` prefix in an import statement:
266
+
267
+ ```javascript
268
+ import components from 'glob:./components/**/*.js'
269
+ ```
270
+
271
+ This will generate individual imports and builds an object mapping. For
272
+ example:
273
+
274
+ ```javascript
275
+ import _glob_components_theme from './components/theme.js'
276
+ import _glob_components_shared_tooltip from './components/shared/tooltip.js'
277
+ const components = {
278
+ 'components/theme': _glob_components_theme,
279
+ 'components/shared/tooltip': _glob_components_shared_tooltip
280
+ }
281
+ ```
282
+
283
+ > [!NOTE]
284
+ > If no files match the pattern, an empty object is assigned.
228
285
 
229
286
  ### Custom plugins
230
287
 
231
- Create a JS file that exports a factory function:
288
+ Custom plugins are JS files referenced by their path in the config. Each file
289
+ must export a factory function that receives a context object with `root`
290
+ (project root path) and `prod` (boolean indicating production mode). What the
291
+ factory returns determines the plugin type.
292
+
293
+ #### Simple transform plugins
294
+
295
+ A simple transform plugin returns a function that receives the file content as
296
+ a string and an `args` object with the file's `path`. It should return the
297
+ transformed content. The transform can be synchronous or asynchronous.
298
+
299
+ Transforms are chained in the order they appear in the config, so each
300
+ transform receives the output of the previous one.
232
301
 
233
302
  ```javascript
234
303
  // config/bun/banner.js
235
304
 
236
- export default function banner({ prod }) {
237
- return (content) => {
238
- const stamp = prod ? "" : ` (dev build ${new Date().toISOString()})`;
239
- return `/* My App${stamp} */\n${content}`;
240
- };
305
+ export default function banner({prod}) {
306
+ return content => {
307
+ const stamp = prod ? '' : ` (dev build ${new Date().toISOString()})`
308
+ return `/* My App${stamp} */\n${content}`
309
+ }
310
+ }
311
+ ```
312
+
313
+ #### Raw Bun plugins
314
+
315
+ If the factory returns an object with a `setup` method instead of a function,
316
+ it is treated as a raw
317
+ [Bun plugin](https://bun.sh/docs/bundler/plugins). This gives you full access
318
+ to Bun's plugin API, including `onLoad`, `onResolve`, and custom loaders.
319
+
320
+ ```javascript
321
+ // config/bun/svg.js
322
+
323
+ export default function svg() {
324
+ return {
325
+ name: 'svg-loader',
326
+ setup(build) {
327
+ build.onLoad({filter: /\.svg$/}, async args => {
328
+ const text = await Bun.file(args.path).text()
329
+ return {
330
+ contents: `export default ${JSON.stringify(text)}`,
331
+ loader: 'js'
332
+ }
333
+ })
334
+ }
335
+ }
241
336
  }
242
337
  ```
243
338
 
244
- Then reference it in your config:
339
+ #### Registering custom plugins
340
+
341
+ Reference custom plugins by their file path in your config:
245
342
 
246
343
  ```json
247
344
  {
248
345
  "plugins": {
249
- "css": ["cssAliases", "cssGlobs", "config/bun/banner.js"]
346
+ "css": ["aliases", "cssGlobs", "config/bun/banner.js"],
347
+ "js": ["aliases", "jsGlobs", "config/bun/svg.js"]
250
348
  }
251
349
  }
252
350
  ```
@@ -42,7 +42,7 @@ export default {
42
42
  loadConfig() {
43
43
  const defaults = {
44
44
  entryPoints: {js: ['app/assets/js/app.js'], css: ['app/assets/css/app.css']},
45
- plugins: {css: ['cssAliases', 'cssGlobs'], js: ['jsGlobs']},
45
+ plugins: {css: ['aliases', 'cssGlobs'], js: ['aliases', 'jsGlobs']},
46
46
  staticDirs: ['app/assets/images', 'app/assets/fonts'],
47
47
  outDir: 'public/assets',
48
48
  publicPath: '/assets',
@@ -0,0 +1,9 @@
1
+ const REGEX = /(url\(\s*['"]?|(?<!\w)['"])\$\//g
2
+
3
+ // Resolves `$/` root aliases in CSS url() references and JS/CSS imports.
4
+ // e.g. url('$/app/assets/images/foo.png') → url('/absolute/root/app/assets/images/foo.png')
5
+ // import x from '$/lib/utils.js' → import x from '/absolute/root/lib/utils.js'
6
+ // @import '$/app/assets/css/reset.css' → @import '/absolute/root/app/assets/css/reset.css'
7
+ export default function aliases({root}) {
8
+ return content => content.replace(REGEX, `$1${root}/`)
9
+ }
@@ -1,9 +1,9 @@
1
1
  import {join} from 'path'
2
- import cssAliases from './cssAliases.js'
2
+ import aliases from './aliases.js'
3
3
  import cssGlobs from './cssGlobs.js'
4
4
  import jsGlobs from './jsGlobs.js'
5
5
 
6
- const builtins = {cssAliases, cssGlobs, jsGlobs}
6
+ const builtins = {aliases, cssGlobs, jsGlobs}
7
7
  const TYPE_REGEXES = {
8
8
  css: /\.css$/,
9
9
  js: /\.(js|ts|jsx|tsx)$/
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BunBunBundle
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bun_bun_bundle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wout Fierens
@@ -36,7 +36,7 @@ files:
36
36
  - exe/bun_bun_bundle
37
37
  - lib/bun/bake.js
38
38
  - lib/bun/bun_bundle.js
39
- - lib/bun/plugins/cssAliases.js
39
+ - lib/bun/plugins/aliases.js
40
40
  - lib/bun/plugins/cssGlobs.js
41
41
  - lib/bun/plugins/index.js
42
42
  - lib/bun/plugins/jsGlobs.js
@@ -1,10 +0,0 @@
1
- import {join} from 'path'
2
-
3
- const REGEX = /url\(\s*['"]?\$\//g
4
-
5
- // Resolves `$` root aliases in CSS url() references.
6
- // e.g. url('$/images/foo.png') → url('/absolute/src/images/foo.png')
7
- export default function cssAliases({root}) {
8
- const srcDir = join(root, 'src')
9
- return content => content.replace(REGEX, `url('${srcDir}/`)
10
- }