vite-plugin-inline-multipage 1.0.15 → 1.0.16

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,6 +1,12 @@
1
1
  import { type Plugin } from 'vite';
2
2
  export interface InlineEverythingOptions {
3
+ /**
4
+ * Dir where the build files are located (default: 'build')
5
+ */
3
6
  buildDir?: string;
7
+ /**
8
+ * If the plugin should remove the _app directory after inlining (default: true)
9
+ */
4
10
  cleanUp?: boolean;
5
11
  remap?: Record<string, string>;
6
12
  }
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ 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
7
  return {
8
8
  name: 'inline-everything',
9
9
  apply: 'build',
@@ -23,15 +23,15 @@ export default function inlineEverythingPlugin(options) {
23
23
  });
24
24
  for (const htmlFile of htmlFiles) {
25
25
  let html = fs.readFileSync(htmlFile, 'utf8');
26
- const $ = cheerio.load(html);
27
- $('link[rel="stylesheet"]').each((_, el) => {
28
- const href = $(el).attr('href');
26
+ const load_html = cheerio.load(html);
27
+ load_html('link[rel="stylesheet"]').each((_, el) => {
28
+ const href = load_html(el).attr('href');
29
29
  if (href) {
30
30
  const filename = path.basename(href);
31
31
  if (assetMap.has(filename)) {
32
32
  try {
33
- const css = fs.readFileSync(assetMap.get(filename), 'utf8');
34
- $(el).replaceWith(`<style>${css}</style>`);
33
+ const cssContent = fs.readFileSync(assetMap.get(filename), 'utf8');
34
+ load_html(el).replaceWith(`<style>${cssContent}</style>`);
35
35
  }
36
36
  catch (e) {
37
37
  console.warn(`Failed to inline CSS ${filename}:`, e.message);
@@ -39,14 +39,14 @@ export default function inlineEverythingPlugin(options) {
39
39
  }
40
40
  }
41
41
  });
42
- $('script[type="module"][src]').each((_, el) => {
43
- const src = $(el).attr('src');
42
+ load_html('script[type="module"][src]').each((_, el) => {
43
+ const src = load_html(el).attr('src');
44
44
  if (src) {
45
45
  const filename = path.basename(src);
46
46
  if (assetMap.has(filename)) {
47
47
  try {
48
- const js = fs.readFileSync(assetMap.get(filename), 'utf8');
49
- $(el).replaceWith(`<script type="module">${js}</script>`);
48
+ const jsContent = fs.readFileSync(assetMap.get(filename), 'utf8');
49
+ load_html(el).replaceWith(`<script type="module">${jsContent}</script>`);
50
50
  }
51
51
  catch (e) {
52
52
  console.warn(`Failed to inline JS ${filename}:`, e.message);
@@ -54,29 +54,19 @@ export default function inlineEverythingPlugin(options) {
54
54
  }
55
55
  }
56
56
  });
57
- $('link[rel="modulepreload"]').remove();
58
- $('link[rel="preload"]').remove();
59
- $('script[data-sveltekit-hydrate]').remove();
60
- // Determine relative path and parts
61
- const relativePath = path.relative(resolvedBuildDir, htmlFile);
62
- const pathParts = relativePath.split(path.sep);
57
+ load_html('link[rel="modulepreload"]').remove();
58
+ load_html('link[rel="preload"]').remove();
59
+ load_html('script[data-sveltekit-hydrate]').remove();
63
60
  const originalName = path.basename(htmlFile, '.html');
64
- // Apply remap ONLY for index.html inside one-level subfolders (routes)
65
- const isIndexInSubfolder = pathParts.length === 2 && originalName === 'index';
66
- if (isIndexInSubfolder) {
67
- const routeName = pathParts[0]; // e.g. 'main'
68
- if (remap.hasOwnProperty(routeName)) {
69
- const remappedName = remap[routeName];
70
- const remappedPath = path.join(path.dirname(htmlFile), remappedName);
71
- fs.writeFileSync(remappedPath, $.html());
72
- if (remappedPath !== htmlFile) {
73
- fs.unlinkSync(htmlFile);
74
- }
75
- continue; // skip writing original file below
76
- }
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);
66
+ }
67
+ else {
68
+ fs.writeFileSync(htmlFile, load_html.html());
77
69
  }
78
- // Write processed html back for all other files
79
- fs.writeFileSync(htmlFile, $.html());
80
70
  }
81
71
  if (cleanUp) {
82
72
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-inline-multipage",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
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",