nuxt-content-assets 1.4.1 → 1.4.3
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/README.md +66 -43
- package/cache/nuxt.config.ts +1 -0
- package/cache/public/.gitignore +2 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +56 -25
- package/dist/runtime/assets/public.d.ts +2 -1
- package/dist/runtime/assets/public.mjs +19 -6
- package/dist/runtime/utils/fs.d.ts +2 -0
- package/dist/runtime/utils/fs.mjs +12 -0
- package/package.json +17 -9
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
> Enable locally-located assets in Nuxt Content
|
|
9
9
|
|
|
10
10
|
<p align="center">
|
|
11
|
-
<img src="https://raw.githubusercontent.com/davestewart/nuxt-content-assets/main/playground/
|
|
11
|
+
<img src="https://raw.githubusercontent.com/davestewart/nuxt-content-assets/main/playground/public/splash.png" alt="Nuxt Content Assets logo">
|
|
12
12
|
</p>
|
|
13
13
|
|
|
14
14
|
## Overview
|
|
@@ -155,11 +155,16 @@ If you delete an asset, it will be greyed out in the browser until you replace t
|
|
|
155
155
|
|
|
156
156
|
If you edit an image, video, embed or iframe source, the content will update immediately, which is useful if you're looking to get that design just right!
|
|
157
157
|
|
|
158
|
+
> [!NOTE]
|
|
159
|
+
> Live reload does not currently work with Nuxt Image (see Issue [#77](https://github.com/davestewart/nuxt-content-assets/issues/77)).
|
|
160
|
+
>
|
|
161
|
+
> If you need to iterate on image design, consider disabling Nuxt Image in development.
|
|
162
|
+
|
|
158
163
|
### Image sizing
|
|
159
164
|
|
|
160
165
|
#### HTML
|
|
161
166
|
|
|
162
|
-
The module
|
|
167
|
+
The module can pass image size hints to generated `<img>` tags:
|
|
163
168
|
|
|
164
169
|
```html
|
|
165
170
|
<!-- imageSize: 'style' -->
|
|
@@ -169,7 +174,10 @@ The module is [preconfigured](#image-size) to pass image size hints (by default
|
|
|
169
174
|
<img src="/image.jpg" width="640" height="480">
|
|
170
175
|
```
|
|
171
176
|
|
|
172
|
-
|
|
177
|
+
Turning this on prevents content jumps as your page loads.
|
|
178
|
+
|
|
179
|
+
> [!CAUTION]
|
|
180
|
+
> Don't use `imageSize: 'src'` in conjunction with Nuxt Image as it prevents the IPX module from correctly serving images, which causes static site generation to fail
|
|
173
181
|
|
|
174
182
|
#### Prose components
|
|
175
183
|
|
|
@@ -207,27 +215,17 @@ See playground component [here](playground/components/content/ContentImage.vue).
|
|
|
207
215
|
|
|
208
216
|
### Nuxt Image
|
|
209
217
|
|
|
210
|
-
|
|
218
|
+
[Nuxt Image](https://image.nuxtjs.org/) is supported by adding Nuxt Content Asset's cache folder as a Nuxt Layer:
|
|
211
219
|
|
|
212
220
|
```ts
|
|
213
221
|
// nuxt.config.ts
|
|
214
222
|
export default defineNuxtConfig({
|
|
215
|
-
modules: [
|
|
216
|
-
// Nuxt Image should be placed before Nuxt Content Assets
|
|
217
|
-
'@nuxt/image',
|
|
218
|
-
'nuxt-content-assets',
|
|
219
|
-
'@nuxt/content',
|
|
220
|
-
],
|
|
221
|
-
|
|
222
223
|
extends: [
|
|
223
|
-
|
|
224
|
-
'.nuxt/content-assets',
|
|
224
|
+
'node_modules/nuxt-content-assets/cache',
|
|
225
225
|
],
|
|
226
226
|
}
|
|
227
227
|
```
|
|
228
228
|
|
|
229
|
-
> Note that the new Layers setup enables Nuxt Image to load images from both the project's `public` folder and from `content`.
|
|
230
|
-
|
|
231
229
|
To serve all images as Nuxt Image images, create a `ProseImg` component like so:
|
|
232
230
|
|
|
233
231
|
```vue
|
|
@@ -239,7 +237,6 @@ To serve all images as Nuxt Image images, create a `ProseImg` component like so:
|
|
|
239
237
|
|
|
240
238
|
See the playground folder for both the [global](playground/components/temp/ProseImg.vue) and a [per image](playground/components/content/NuxtImg.ts) solution.
|
|
241
239
|
|
|
242
|
-
|
|
243
240
|
## Configuration
|
|
244
241
|
|
|
245
242
|
The module has the following options:
|
|
@@ -248,7 +245,7 @@ The module has the following options:
|
|
|
248
245
|
// nuxt.config.ts
|
|
249
246
|
export default defineNuxtConfig({
|
|
250
247
|
contentAssets: {
|
|
251
|
-
// inject image
|
|
248
|
+
// inject image size hints into the rendered html
|
|
252
249
|
imageSize: 'style',
|
|
253
250
|
|
|
254
251
|
// treat these extensions as content
|
|
@@ -262,7 +259,7 @@ export default defineNuxtConfig({
|
|
|
262
259
|
|
|
263
260
|
### Image size
|
|
264
261
|
|
|
265
|
-
You can add one
|
|
262
|
+
You can add one _or more_ image size hints to the generated images:
|
|
266
263
|
|
|
267
264
|
```ts
|
|
268
265
|
{
|
|
@@ -277,7 +274,6 @@ Pick from the following switches:
|
|
|
277
274
|
| `'style'` | Adds `style="aspect-ratio:..."` to any `<img>` tag |
|
|
278
275
|
| `'attrs'` | Adds `width` and `height` attributes to any `<img>` tag |
|
|
279
276
|
| `'src'` | Adds `?width=...&height=...` to `src` attribute (frontmatter only) |
|
|
280
|
-
| `false` | Disable image size hints |
|
|
281
277
|
|
|
282
278
|
Note: if you add *only* `attrs`, include the following CSS in your app:
|
|
283
279
|
|
|
@@ -288,8 +284,13 @@ img {
|
|
|
288
284
|
}
|
|
289
285
|
```
|
|
290
286
|
|
|
287
|
+
> [!Note]
|
|
288
|
+
>
|
|
289
|
+
> Since `v1.4.1` image size hints are now opt-in. This was done to maximise compatibiility with Nuxt Image.
|
|
290
|
+
|
|
291
291
|
### Content extensions
|
|
292
292
|
|
|
293
|
+
> [!NOTE]
|
|
293
294
|
> Generally, you shouldn't need to touch this setting
|
|
294
295
|
|
|
295
296
|
This setting tells Nuxt Content to ignore anything that is **not** one of these file extensions:
|
|
@@ -312,65 +313,85 @@ If you want to see what the module does as it runs, set `debug` to true:
|
|
|
312
313
|
|
|
313
314
|
## How it works
|
|
314
315
|
|
|
315
|
-
When Nuxt builds, the module scans all content sources for assets, copies them to
|
|
316
|
+
When Nuxt builds, the module scans all content sources for assets, copies them to a temporary layer folder (`nuxt_modules/nuxt-content-assets/cache`), and indexes path and image metadata.
|
|
316
317
|
|
|
317
|
-
After Nuxt Content has run, the parsed content is traversed, and both element attributes and frontmatter properties are checked to see if they resolve to the indexed asset paths.
|
|
318
|
+
After Nuxt Content has run, the parsed content (`.nuxt/content-cache`) is traversed, and both element attributes and frontmatter properties are checked to see if they resolve to the previously-indexed asset paths.
|
|
318
319
|
|
|
319
|
-
If they do, then the attribute or property is rewritten with the absolute path. If the asset is an image, then the element or metadata is optionally updated with size attributes or a query string.
|
|
320
|
+
If they do, then the attribute or property in Nuxt Content's cache is rewritten with the absolute path. If the asset is an image, then the element or metadata is optionally updated with size attributes or a query string.
|
|
320
321
|
|
|
321
322
|
Finally, Nitro serves the site, and any requests made to the transformed asset paths should be picked up and the *copied* asset served by the browser.
|
|
322
323
|
|
|
323
|
-
In development,
|
|
324
|
+
In development, a watch process propagates asset changes to the cache, updates the asset index, and notifies the browser via web sockets to refresh any loaded images.
|
|
325
|
+
|
|
326
|
+
If Nuxt Image is used, the `_ipx/` endpoint serves images directly from the cache's public folder.
|
|
324
327
|
|
|
325
328
|
## Development
|
|
326
329
|
|
|
327
|
-
Should you wish to develop the project, the
|
|
330
|
+
Should you wish to develop the project, you'll work with the following entities:
|
|
331
|
+
|
|
332
|
+
- [src](./src)<br>The module code itself
|
|
333
|
+
- [playground](./playground)<br>A standalone Nuxt app that reads the live module code
|
|
334
|
+
- [scripts](package.json)<br>A set of scripts to develop and publish the module
|
|
335
|
+
|
|
336
|
+
### Setup
|
|
328
337
|
|
|
329
|
-
|
|
338
|
+
To set up the project, run each of these scripts once:
|
|
330
339
|
|
|
331
340
|
```bash
|
|
332
341
|
# install dependencies
|
|
333
342
|
npm install
|
|
334
343
|
|
|
335
|
-
#
|
|
336
|
-
npm run dev:
|
|
344
|
+
# copy the cache folder to the playground's node_modules (workaround required in development)
|
|
345
|
+
npm run dev:setup
|
|
337
346
|
|
|
338
|
-
#
|
|
339
|
-
npm run dev
|
|
340
|
-
|
|
341
|
-
# run eslint
|
|
342
|
-
npm run lint
|
|
343
|
-
|
|
344
|
-
# run vitest
|
|
345
|
-
npm run test
|
|
346
|
-
npm run test:watch
|
|
347
|
+
# generate types for the module and playground (re-run if you install new packages)
|
|
348
|
+
npm run dev:prepare
|
|
347
349
|
```
|
|
348
350
|
|
|
349
|
-
|
|
351
|
+
### Development
|
|
352
|
+
|
|
353
|
+
To develop the module, utilise the supplied playground app:
|
|
350
354
|
|
|
351
355
|
```bash
|
|
356
|
+
# compile the module, run and serve the playground
|
|
357
|
+
npm run dev
|
|
358
|
+
|
|
352
359
|
# generate the playground
|
|
353
360
|
npm run dev:generate
|
|
354
361
|
|
|
355
362
|
# build the playground
|
|
356
363
|
npm run dev:build
|
|
357
364
|
|
|
358
|
-
# serve the generated
|
|
365
|
+
# serve the generated/built playground
|
|
359
366
|
npm run dev:preview
|
|
360
367
|
```
|
|
361
368
|
|
|
362
|
-
|
|
369
|
+
Check your code quality using these tools:
|
|
363
370
|
|
|
364
371
|
```bash
|
|
365
|
-
#
|
|
372
|
+
# lint your code with eslint
|
|
373
|
+
npm run lint
|
|
374
|
+
|
|
375
|
+
# runs tests with vitest
|
|
376
|
+
npm run test
|
|
377
|
+
npm run test:watch
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Publishing
|
|
381
|
+
|
|
382
|
+
> [!IMPORTANT]
|
|
383
|
+
> Before publishing, be sure to update the [version](package.json) and [changelog](CHANGELOG.md)!
|
|
384
|
+
|
|
385
|
+
To build and publish, run following scripts as required:
|
|
386
|
+
|
|
387
|
+
```bash
|
|
388
|
+
# lint, test, build, and dry-run publish
|
|
366
389
|
npm run release:dry
|
|
367
390
|
|
|
368
|
-
#
|
|
391
|
+
# lint, test, build and publish
|
|
369
392
|
npm run release
|
|
370
393
|
```
|
|
371
394
|
|
|
372
|
-
Make sure to edit changelog and update `package.json` version before releasing!
|
|
373
|
-
|
|
374
395
|
## Maintenance
|
|
375
396
|
|
|
376
397
|
This module was created using the Nuxt [Module Builder](https://github.com/nuxt/module-builder) command:
|
|
@@ -385,6 +406,8 @@ This created the module code from the starter template found here:
|
|
|
385
406
|
|
|
386
407
|
Both [Nuxi](https://github.com/nuxt/cli) and the module's dependencies and scripts are updated fairly regularly, so from time to time this module may need to be updated to keep in sync. So far, this has meant just updating the dependencies and scripts, which are found in the starter template code mentioned above.
|
|
387
408
|
|
|
409
|
+
Note that the build/release scripts are slightly modified from the originals; build is now separated, and release now doesn't use [changelogen](https://github.com/unjs/changelogen), or automatically add tags and push to GitHub.
|
|
410
|
+
|
|
388
411
|
<!-- Badges -->
|
|
389
412
|
[npm-version-src]: https://img.shields.io/npm/v/nuxt-content-assets/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
390
413
|
[npm-version-href]: https://npmjs.com/package/nuxt-content-assets
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default {}
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import Fs from 'fs';
|
|
1
|
+
import * as Fs from 'fs';
|
|
2
|
+
import Fs__default from 'fs';
|
|
2
3
|
import Path from 'crosspath';
|
|
3
4
|
import { useNuxt, createResolver, defineNuxtModule, addPlugin } from '@nuxt/kit';
|
|
4
5
|
import { visit, SKIP, CONTINUE } from 'unist-util-visit';
|
|
@@ -153,35 +154,47 @@ function buildQuery(...expr) {
|
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
function readFile(path, asJson = false) {
|
|
156
|
-
const text =
|
|
157
|
+
const text = Fs__default.readFileSync(path, { encoding: "utf8" });
|
|
157
158
|
return asJson ? JSON.parse(text) : text;
|
|
158
159
|
}
|
|
159
160
|
function writeFile(path, data) {
|
|
160
161
|
const text = typeof data === "object" ? JSON.stringify(data, null, " ") : String(data);
|
|
161
162
|
createFolder(Path.dirname(path));
|
|
162
|
-
|
|
163
|
+
Fs__default.writeFileSync(path, text, { encoding: "utf8" });
|
|
163
164
|
}
|
|
164
165
|
async function writeBlob(path, data) {
|
|
165
166
|
const buffer = Buffer.from(await data.arrayBuffer());
|
|
166
167
|
createFolder(Path.dirname(path));
|
|
167
|
-
|
|
168
|
+
Fs__default.writeFileSync(path, buffer);
|
|
168
169
|
}
|
|
169
170
|
function copyFile(src, trg) {
|
|
170
171
|
createFolder(Path.dirname(trg));
|
|
171
|
-
|
|
172
|
+
Fs__default.copyFileSync(src, trg);
|
|
172
173
|
}
|
|
173
174
|
function removeFile(src) {
|
|
174
|
-
|
|
175
|
+
Fs__default.rmSync(src);
|
|
175
176
|
}
|
|
176
177
|
function createFolder(path) {
|
|
177
|
-
|
|
178
|
+
Fs__default.mkdirSync(path, { recursive: true });
|
|
178
179
|
}
|
|
179
180
|
function removeFolder(path) {
|
|
180
181
|
const isDownstream = path.startsWith(Path.resolve());
|
|
181
182
|
if (isDownstream) {
|
|
182
|
-
|
|
183
|
+
Fs__default.rmSync(path, { recursive: true, force: true });
|
|
183
184
|
}
|
|
184
185
|
}
|
|
186
|
+
function removeEntry(path) {
|
|
187
|
+
if (Fs__default.existsSync(path)) {
|
|
188
|
+
if (isFile(path)) {
|
|
189
|
+
removeFile(path);
|
|
190
|
+
} else {
|
|
191
|
+
removeFolder(path);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function isFile(path) {
|
|
196
|
+
return Fs__default.lstatSync(path).isFile();
|
|
197
|
+
}
|
|
185
198
|
|
|
186
199
|
function createWebSocket() {
|
|
187
200
|
const wss = new WebSocketServer({ noServer: true });
|
|
@@ -390,22 +403,23 @@ function makeSourceManager(key, source, publicPath, callback) {
|
|
|
390
403
|
}
|
|
391
404
|
|
|
392
405
|
function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
393
|
-
const
|
|
394
|
-
const
|
|
406
|
+
const assetsKey = "assets.json";
|
|
407
|
+
const assetsPath = Path.join(publicPath, "..");
|
|
408
|
+
const storage = makeSourceStorage(assetsPath);
|
|
395
409
|
if (shouldWatch) {
|
|
396
410
|
void storage.watch(async (event, key) => {
|
|
397
|
-
if (event === "update" && key ===
|
|
411
|
+
if (event === "update" && key === assetsKey) {
|
|
398
412
|
await load();
|
|
399
413
|
}
|
|
400
414
|
});
|
|
401
415
|
}
|
|
402
416
|
const assets = {};
|
|
403
417
|
async function load() {
|
|
404
|
-
const data = await storage.getItem(
|
|
418
|
+
const data = await storage.getItem(assetsKey);
|
|
405
419
|
Object.assign(assets, data || {});
|
|
406
420
|
}
|
|
407
421
|
const save = debounce(function() {
|
|
408
|
-
void storage.setItem(
|
|
422
|
+
void storage.setItem(assetsKey, assets);
|
|
409
423
|
}, 50);
|
|
410
424
|
function resolveAsset(content, relAsset, registerContent = false) {
|
|
411
425
|
const srcDir = Path.dirname(content._file);
|
|
@@ -447,8 +461,19 @@ function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
|
447
461
|
}
|
|
448
462
|
return asset;
|
|
449
463
|
}
|
|
464
|
+
const init = () => {
|
|
465
|
+
if (Fs.existsSync(publicPath)) {
|
|
466
|
+
const names = Fs.readdirSync(publicPath);
|
|
467
|
+
for (const name of names) {
|
|
468
|
+
if (!/^\.git(ignore|keep)$/.test(name)) {
|
|
469
|
+
removeEntry(Path.join(publicPath, name));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
};
|
|
450
474
|
void load();
|
|
451
475
|
return {
|
|
476
|
+
init,
|
|
452
477
|
setAsset,
|
|
453
478
|
getAsset,
|
|
454
479
|
removeAsset,
|
|
@@ -521,24 +546,29 @@ const meta = {
|
|
|
521
546
|
const module = defineNuxtModule({
|
|
522
547
|
meta,
|
|
523
548
|
defaults: {
|
|
524
|
-
imageSize: "
|
|
549
|
+
imageSize: "",
|
|
525
550
|
contentExtensions: "md csv ya?ml json",
|
|
526
551
|
debug: false
|
|
527
552
|
},
|
|
528
553
|
async setup(options, nuxt) {
|
|
529
554
|
const buildPath = nuxt.options.buildDir;
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
555
|
+
const modulesPath = nuxt.options.modulesDir.find((path) => Fs.existsSync(`${path}/nuxt-content-assets/cache`)) || "";
|
|
556
|
+
if (!modulesPath) {
|
|
557
|
+
warn("Unable to find cache folder!");
|
|
558
|
+
if (nuxt.options.srcDir.endsWith("/playground")) {
|
|
559
|
+
warn('Run "npm run dev:setup" to generate a new cache folder');
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
const cachePath = modulesPath ? Path.resolve(modulesPath, "nuxt-content-assets/cache") : Path.resolve(buildPath, "content-assets");
|
|
563
|
+
const publicPath = Path.join(cachePath, "public");
|
|
564
|
+
const contentPath = Path.join(buildPath, "content-cache");
|
|
533
565
|
const isDev = !!nuxt.options.dev;
|
|
534
566
|
const isDebug = !!options.debug;
|
|
535
567
|
if (isDebug) {
|
|
536
|
-
log("
|
|
568
|
+
log("Cleaning content-cache");
|
|
569
|
+
log(`Cache path: "${Path.relative(".", cachePath)}"`);
|
|
537
570
|
}
|
|
538
|
-
|
|
539
|
-
removeFolder(assetsPath);
|
|
540
|
-
createFolder(`${assetsPath}/public`);
|
|
541
|
-
writeFile(`${assetsPath}/nuxt.config.ts`, "export default {}");
|
|
571
|
+
removeEntry(contentPath);
|
|
542
572
|
const { contentExtensions } = options;
|
|
543
573
|
if (contentExtensions) {
|
|
544
574
|
nuxt.options.content ||= {};
|
|
@@ -550,7 +580,7 @@ const module = defineNuxtModule({
|
|
|
550
580
|
}
|
|
551
581
|
const imageSizes = matchTokens(options.imageSize);
|
|
552
582
|
const sources = Array.from(nuxt.options._layers).map((layer) => layer.config?.content?.sources).reduce((output, sources2) => {
|
|
553
|
-
if (sources2) {
|
|
583
|
+
if (sources2 && !Array.isArray(sources2)) {
|
|
554
584
|
Object.assign(output, sources2);
|
|
555
585
|
}
|
|
556
586
|
return output;
|
|
@@ -565,6 +595,7 @@ const module = defineNuxtModule({
|
|
|
565
595
|
}
|
|
566
596
|
}
|
|
567
597
|
const assets = makeAssetsManager(publicPath, isDev);
|
|
598
|
+
assets.init();
|
|
568
599
|
function onAssetChange(event, absTrg) {
|
|
569
600
|
let src = "";
|
|
570
601
|
let width;
|
|
@@ -577,7 +608,7 @@ const module = defineNuxtModule({
|
|
|
577
608
|
if (oldAsset) {
|
|
578
609
|
if (oldAsset.width !== newAsset.width || oldAsset.height !== newAsset.height) {
|
|
579
610
|
newAsset.content.forEach(async (id) => {
|
|
580
|
-
const path = Path.join(contentPath, toPath(id));
|
|
611
|
+
const path = Path.join(contentPath, "parsed", toPath(id));
|
|
581
612
|
rewriteContent(path, newAsset);
|
|
582
613
|
});
|
|
583
614
|
}
|
|
@@ -594,7 +625,7 @@ const module = defineNuxtModule({
|
|
|
594
625
|
}
|
|
595
626
|
}
|
|
596
627
|
addPlugin(resolve("./runtime/sockets/plugin"));
|
|
597
|
-
const socket = isDev ? await setupSocketServer("content-assets") : null;
|
|
628
|
+
const socket = isDev && nuxt.options.content?.watch !== false ? await setupSocketServer("content-assets") : null;
|
|
598
629
|
const managers = {};
|
|
599
630
|
for (const [key, source] of Object.entries(sources)) {
|
|
600
631
|
if (isDebug) {
|
|
@@ -3,10 +3,11 @@ import type { ParsedContent, AssetConfig } from '../../types';
|
|
|
3
3
|
* Manages the public assets
|
|
4
4
|
*/
|
|
5
5
|
export declare function makeAssetsManager(publicPath: string, shouldWatch?: boolean): {
|
|
6
|
+
init: () => void;
|
|
6
7
|
setAsset: (path: string) => AssetConfig;
|
|
7
8
|
getAsset: (path: string) => AssetConfig | undefined;
|
|
8
9
|
removeAsset: (path: string) => AssetConfig | undefined;
|
|
9
|
-
resolveAsset: (content: ParsedContent, relAsset: string, registerContent?: boolean) => AssetConfig
|
|
10
|
+
resolveAsset: (content: ParsedContent, relAsset: string, registerContent?: boolean) => Partial<AssetConfig>;
|
|
10
11
|
dispose: () => Promise<void>;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
@@ -1,26 +1,28 @@
|
|
|
1
|
+
import * as Fs from "fs";
|
|
1
2
|
import Path from "crosspath";
|
|
2
3
|
import getImageSize from "image-size";
|
|
3
4
|
import debounce from "debounce";
|
|
4
5
|
import { hash } from "ohash";
|
|
5
6
|
import { makeSourceStorage } from "./source.mjs";
|
|
6
|
-
import { isImage, warn, log } from "../utils/index.mjs";
|
|
7
|
+
import { isImage, warn, log, removeEntry } from "../utils/index.mjs";
|
|
7
8
|
export function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
8
|
-
const
|
|
9
|
-
const
|
|
9
|
+
const assetsKey = "assets.json";
|
|
10
|
+
const assetsPath = Path.join(publicPath, "..");
|
|
11
|
+
const storage = makeSourceStorage(assetsPath);
|
|
10
12
|
if (shouldWatch) {
|
|
11
13
|
void storage.watch(async (event, key) => {
|
|
12
|
-
if (event === "update" && key ===
|
|
14
|
+
if (event === "update" && key === assetsKey) {
|
|
13
15
|
await load();
|
|
14
16
|
}
|
|
15
17
|
});
|
|
16
18
|
}
|
|
17
19
|
const assets = {};
|
|
18
20
|
async function load() {
|
|
19
|
-
const data = await storage.getItem(
|
|
21
|
+
const data = await storage.getItem(assetsKey);
|
|
20
22
|
Object.assign(assets, data || {});
|
|
21
23
|
}
|
|
22
24
|
const save = debounce(function() {
|
|
23
|
-
void storage.setItem(
|
|
25
|
+
void storage.setItem(assetsKey, assets);
|
|
24
26
|
}, 50);
|
|
25
27
|
function resolveAsset(content, relAsset, registerContent = false) {
|
|
26
28
|
const srcDir = Path.dirname(content._file);
|
|
@@ -62,8 +64,19 @@ export function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
|
62
64
|
}
|
|
63
65
|
return asset;
|
|
64
66
|
}
|
|
67
|
+
const init = () => {
|
|
68
|
+
if (Fs.existsSync(publicPath)) {
|
|
69
|
+
const names = Fs.readdirSync(publicPath);
|
|
70
|
+
for (const name of names) {
|
|
71
|
+
if (!/^\.git(ignore|keep)$/.test(name)) {
|
|
72
|
+
removeEntry(Path.join(publicPath, name));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
65
77
|
void load();
|
|
66
78
|
return {
|
|
79
|
+
init,
|
|
67
80
|
setAsset,
|
|
68
81
|
getAsset,
|
|
69
82
|
removeAsset,
|
|
@@ -5,3 +5,5 @@ export declare function copyFile(src: string, trg: string): void;
|
|
|
5
5
|
export declare function removeFile(src: string): void;
|
|
6
6
|
export declare function createFolder(path: string): void;
|
|
7
7
|
export declare function removeFolder(path: string): void;
|
|
8
|
+
export declare function removeEntry(path: string): void;
|
|
9
|
+
export declare function isFile(path: string): any;
|
|
@@ -30,3 +30,15 @@ export function removeFolder(path) {
|
|
|
30
30
|
Fs.rmSync(path, { recursive: true, force: true });
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
+
export function removeEntry(path) {
|
|
34
|
+
if (Fs.existsSync(path)) {
|
|
35
|
+
if (isFile(path)) {
|
|
36
|
+
removeFile(path);
|
|
37
|
+
} else {
|
|
38
|
+
removeFolder(path);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function isFile(path) {
|
|
43
|
+
return Fs.lstatSync(path).isFile();
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-content-assets",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Enable locally-located assets in Nuxt Content",
|
|
5
5
|
"repository": "davestewart/nuxt-content-assets",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,16 +15,23 @@
|
|
|
15
15
|
"main": "./dist/module.cjs",
|
|
16
16
|
"types": "./dist/types.d.ts",
|
|
17
17
|
"files": [
|
|
18
|
+
"cache",
|
|
18
19
|
"dist"
|
|
19
20
|
],
|
|
20
21
|
"scripts": {
|
|
22
|
+
"dev:setup": "cpy cache playground/node_modules/nuxt-content-assets && cpy \"cache/**\" playground/.nuxt/content-assets",
|
|
23
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare",
|
|
24
|
+
|
|
21
25
|
"dev": "nuxi dev playground",
|
|
22
|
-
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
23
26
|
"dev:generate": "nuxi generate playground",
|
|
24
27
|
"dev:build": "nuxi build playground",
|
|
25
28
|
"dev:preview": "nuxi preview playground",
|
|
26
|
-
|
|
27
|
-
"
|
|
29
|
+
|
|
30
|
+
"build": "nuxt-module-build build",
|
|
31
|
+
"release:setup": "npm run lint && npm run test && npm run build",
|
|
32
|
+
"release:dry": "npm run release:setup && npm publish --dry-run",
|
|
33
|
+
"release": "npm run release:setup && npm publish",
|
|
34
|
+
|
|
28
35
|
"lint": "eslint .",
|
|
29
36
|
"test": "vitest run",
|
|
30
37
|
"test:watch": "vitest watch"
|
|
@@ -44,20 +51,21 @@
|
|
|
44
51
|
"@nuxt/content": "^2.0.0"
|
|
45
52
|
},
|
|
46
53
|
"devDependencies": {
|
|
47
|
-
"@types/node": "^20.12.4",
|
|
48
54
|
"@nuxt/devtools": "latest",
|
|
49
55
|
"@nuxt/eslint-config": "^0.2.0",
|
|
50
56
|
"@nuxt/module-builder": "^0.5.5",
|
|
51
57
|
"@nuxt/schema": "^3.11.2",
|
|
52
58
|
"@nuxt/test-utils": "^3.12.0",
|
|
59
|
+
"@types/debounce": "^1.2.1",
|
|
60
|
+
"@types/node": "^20.12.4",
|
|
61
|
+
"@types/ws": "^8.5.10",
|
|
53
62
|
"changelogen": "^0.5.5",
|
|
63
|
+
"cpy-cli": "^5.0.0",
|
|
54
64
|
"eslint": "^8.57.0",
|
|
55
65
|
"nuxt": "^3.11.2",
|
|
56
|
-
"vitest": "^1.4.0"
|
|
57
|
-
"@types/debounce": "^1.2.1",
|
|
58
|
-
"@types/ws": "^8.5.10"
|
|
66
|
+
"vitest": "^1.4.0"
|
|
59
67
|
},
|
|
60
68
|
"engines": {
|
|
61
69
|
"node": ">=16.0.0"
|
|
62
70
|
}
|
|
63
|
-
}
|
|
71
|
+
}
|