vite-plugin-inline-multipage 1.0.11 → 1.0.13

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 (2) hide show
  1. package/dist/index.js +38 -30
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import fs from 'fs';
4
4
  import * as cheerio from 'cheerio';
5
5
  export default function inlineEverythingPlugin(options) {
6
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',
@@ -11,14 +12,14 @@ export default function inlineEverythingPlugin(options) {
11
12
  const resolvedBuildDir = path.resolve(process.cwd(), buildDir);
12
13
  const htmlFiles = await glob('**/*.html', {
13
14
  cwd: resolvedBuildDir,
14
- absolute: true,
15
+ absolute: true
15
16
  });
16
17
  const assetFiles = await glob('**/_app/immutable/**/*.{css,js}', {
17
18
  cwd: resolvedBuildDir,
18
- absolute: true,
19
+ absolute: true
19
20
  });
20
21
  const assetMap = new Map();
21
- assetFiles.forEach((file) => {
22
+ assetFiles.forEach(file => {
22
23
  assetMap.set(path.basename(file), file);
23
24
  });
24
25
  for (const htmlFile of htmlFiles) {
@@ -26,44 +27,51 @@ export default function inlineEverythingPlugin(options) {
26
27
  const $ = cheerio.load(html);
27
28
  $('link[rel="stylesheet"]').each((_, el) => {
28
29
  const href = $(el).attr('href');
29
- const filename = href && path.basename(href);
30
- if (filename && assetMap.has(filename)) {
31
- try {
32
- const css = fs.readFileSync(assetMap.get(filename), 'utf8');
33
- $(el).replaceWith(`<style>${css}</style>`);
34
- }
35
- catch (e) {
36
- console.warn(`Failed to inline CSS ${filename}:`, e.message);
30
+ if (href) {
31
+ const filename = path.basename(href);
32
+ if (assetMap.has(filename)) {
33
+ try {
34
+ const css = fs.readFileSync(assetMap.get(filename), 'utf8');
35
+ $(el).replaceWith(`<style>${css}</style>`);
36
+ }
37
+ catch (e) {
38
+ console.warn(`Failed to inline CSS ${filename}:`, e.message);
39
+ }
37
40
  }
38
41
  }
39
42
  });
40
43
  $('script[type="module"][src]').each((_, el) => {
41
44
  const src = $(el).attr('src');
42
- const filename = src && path.basename(src);
43
- if (filename && assetMap.has(filename)) {
44
- try {
45
- const js = fs.readFileSync(assetMap.get(filename), 'utf8');
46
- $(el).replaceWith(`<script type="module">${js}</script>`);
47
- }
48
- catch (e) {
49
- console.warn(`Failed to inline JS ${filename}:`, e.message);
45
+ if (src) {
46
+ const filename = path.basename(src);
47
+ if (assetMap.has(filename)) {
48
+ try {
49
+ const js = fs.readFileSync(assetMap.get(filename), 'utf8');
50
+ $(el).replaceWith(`<script type="module">${js}</script>`);
51
+ }
52
+ catch (e) {
53
+ console.warn(`Failed to inline JS ${filename}:`, e.message);
54
+ }
50
55
  }
51
56
  }
52
57
  });
53
58
  $('link[rel="modulepreload"]').remove();
54
59
  $('link[rel="preload"]').remove();
55
60
  $('script[data-sveltekit-hydrate]').remove();
56
- const outputPath = (() => {
57
- const original = path.basename(htmlFile);
58
- const remapKey = original.replace(/\.html$/, '');
59
- if (remap[remapKey]) {
60
- return path.join(path.dirname(htmlFile), remap[remapKey]);
61
+ const relPath = path.relative(resolvedBuildDir, htmlFile);
62
+ const match = relPath.match(/^(.*?)\/index\.html$/);
63
+ const routeKey = match ? match[1] : path.basename(htmlFile, '.html');
64
+ const isRouteFile = remapKeys.has(routeKey);
65
+ if (isRouteFile) {
66
+ const remappedName = remap[routeKey];
67
+ const remappedPath = path.join(path.dirname(htmlFile), remappedName);
68
+ fs.writeFileSync(remappedPath, $.html());
69
+ if (remappedPath !== htmlFile) {
70
+ fs.unlinkSync(htmlFile);
61
71
  }
62
- return htmlFile;
63
- })();
64
- fs.writeFileSync(outputPath, $.html());
65
- if (outputPath !== htmlFile) {
66
- fs.unlinkSync(htmlFile);
72
+ }
73
+ else {
74
+ fs.writeFileSync(htmlFile, $.html());
67
75
  }
68
76
  }
69
77
  if (cleanUp) {
@@ -74,6 +82,6 @@ export default function inlineEverythingPlugin(options) {
74
82
  console.warn('Could not clean up _app directory:', e.message);
75
83
  }
76
84
  }
77
- },
85
+ }
78
86
  };
79
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-inline-multipage",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
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",