vite-plugin-inline-multipage 1.0.10 → 1.0.12

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/dist/index.d.ts CHANGED
@@ -1,12 +1,6 @@
1
1
  import { type Plugin } from 'vite';
2
2
  export interface InlineEverythingOptions {
3
- /**
4
- * Dir where the build files are located (default: 'build')
5
- */
6
3
  buildDir?: string;
7
- /**
8
- * If the plugin should remove the _app directory after inlining (default: true)
9
- */
10
4
  cleanUp?: boolean;
11
5
  remap?: Record<string, string>;
12
6
  }
package/dist/index.js CHANGED
@@ -3,7 +3,8 @@ import path from 'path';
3
3
  import fs from 'fs';
4
4
  import * as cheerio from 'cheerio';
5
5
  export default function inlineEverythingPlugin(options) {
6
- const { buildDir = 'build', cleanUp = false, remap = {} } = options || {};
6
+ const { buildDir = 'build', cleanUp = false, remap = {}, } = options || {};
7
+ const remapKeys = new Set(Object.keys(remap));
7
8
  return {
8
9
  name: 'inline-everything',
9
10
  apply: 'build',
@@ -23,15 +24,15 @@ export default function inlineEverythingPlugin(options) {
23
24
  });
24
25
  for (const htmlFile of htmlFiles) {
25
26
  let html = fs.readFileSync(htmlFile, 'utf8');
26
- const load_html = cheerio.load(html);
27
- load_html('link[rel="stylesheet"]').each((_, el) => {
28
- const href = load_html(el).attr('href');
27
+ const $ = cheerio.load(html);
28
+ $('link[rel="stylesheet"]').each((_, el) => {
29
+ const href = $(el).attr('href');
29
30
  if (href) {
30
31
  const filename = path.basename(href);
31
32
  if (assetMap.has(filename)) {
32
33
  try {
33
- const cssContent = fs.readFileSync(assetMap.get(filename), 'utf8');
34
- load_html(el).replaceWith(`<style>${cssContent}</style>`);
34
+ const css = fs.readFileSync(assetMap.get(filename), 'utf8');
35
+ $(el).replaceWith(`<style>${css}</style>`);
35
36
  }
36
37
  catch (e) {
37
38
  console.warn(`Failed to inline CSS ${filename}:`, e.message);
@@ -39,14 +40,14 @@ export default function inlineEverythingPlugin(options) {
39
40
  }
40
41
  }
41
42
  });
42
- load_html('script[type="module"][src]').each((_, el) => {
43
- const src = load_html(el).attr('src');
43
+ $('script[type="module"][src]').each((_, el) => {
44
+ const src = $(el).attr('src');
44
45
  if (src) {
45
46
  const filename = path.basename(src);
46
47
  if (assetMap.has(filename)) {
47
48
  try {
48
- const jsContent = fs.readFileSync(assetMap.get(filename), 'utf8');
49
- load_html(el).replaceWith(`<script type="module">${jsContent}</script>`);
49
+ const js = fs.readFileSync(assetMap.get(filename), 'utf8');
50
+ $(el).replaceWith(`<script type="module">${js}</script>`);
50
51
  }
51
52
  catch (e) {
52
53
  console.warn(`Failed to inline JS ${filename}:`, e.message);
@@ -54,18 +55,21 @@ export default function inlineEverythingPlugin(options) {
54
55
  }
55
56
  }
56
57
  });
57
- load_html('link[rel="modulepreload"]').remove();
58
- load_html('link[rel="preload"]').remove();
59
- load_html('script[data-sveltekit-hydrate]').remove();
58
+ $('link[rel="modulepreload"]').remove();
59
+ $('link[rel="preload"]').remove();
60
+ $('script[data-sveltekit-hydrate]').remove();
60
61
  const originalName = path.basename(htmlFile, '.html');
61
- const targetName = remap[originalName];
62
- if (targetName) {
63
- const newPath = path.join(path.dirname(htmlFile), targetName);
64
- fs.writeFileSync(newPath, load_html.html());
65
- fs.rmSync(htmlFile);
62
+ const isRouteFile = remapKeys.has(originalName);
63
+ if (isRouteFile) {
64
+ const remappedName = remap[originalName];
65
+ const remappedPath = path.join(path.dirname(htmlFile), remappedName);
66
+ fs.writeFileSync(remappedPath, $.html());
67
+ if (remappedPath !== htmlFile) {
68
+ fs.unlinkSync(htmlFile);
69
+ }
66
70
  }
67
71
  else {
68
- fs.writeFileSync(htmlFile, load_html.html());
72
+ fs.writeFileSync(htmlFile, $.html());
69
73
  }
70
74
  }
71
75
  if (cleanUp) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-inline-multipage",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "A Vite plugin that inlines multi-paged applications (like in svelte) to multiple html files",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",