svelte-bundle 0.0.21 → 0.0.22

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 (3) hide show
  1. package/ReadMe.md +4 -3
  2. package/bundle.js +49 -7
  3. package/package.json +8 -3
package/ReadMe.md CHANGED
@@ -1,8 +1,11 @@
1
+ > ⚠️ **NOTICE:** While this tool is currently functional, it has not nearly been battle-tested enough to ensure it works in most use-cases.
2
+
1
3
  ## Usage
2
4
  This tool can be used with `npx`:
3
5
  ```bash
4
6
  npx svelte-bundle -i <input-dir> -o <output-dir>
5
7
  ```
8
+ ### Full Documentation: [https://github.com/uhteddy/svelte-bundle/wiki](https://github.com/uhteddy/svelte-bundle/wiki)
6
9
 
7
10
  # Svelte Bundle CLI
8
11
 
@@ -12,9 +15,7 @@ Just note, the purpose of this tool is **NOT** to bundle an entire Svelte app, i
12
15
 
13
16
  Utilizing this you will be able to develop a page with the joy of svelte and be able to directly get a single `.html` file that you can utilize getting full use-case of svelte.
14
17
 
15
- ⚠️ **Note**: This tool is **NOT** made to function with SvelteKit, this takes a single `.svelte` file as input and utilizes vite to compile it into a single `.html` file.
16
-
17
- ⚠️ **Note**: This tool is currently in early development and is not fully complete. The roadmap and additional features will be added over time. There is currently **NO** testing on this, meaning there is no guarentee it will work for most usecases.
18
+ ⚠️ **Note**: This tool is **NOT** made to function with SvelteKit natively, this takes a single `.svelte` file as input and will output a single `.html` file.
18
19
 
19
20
  ## Inspiration
20
21
  This tool was inspired by the need I had when it came to updating the CMS for a company I worked for. They were looking for more custom content on their website which used an outdated CMS. Pages were only able to include HTML, CSS, and JS.
package/bundle.js CHANGED
@@ -1,4 +1,3 @@
1
- // bundle.js
2
1
  import fs from 'fs/promises';
3
2
  import path from 'path';
4
3
  import { rollup } from 'rollup';
@@ -11,6 +10,17 @@ import postcss from 'postcss';
11
10
  import tailwindcss from 'tailwindcss';
12
11
  import autoprefixer from 'autoprefixer';
13
12
  import cssnano from 'cssnano';
13
+ import { createRequire } from 'module';
14
+ import { fileURLToPath } from 'url';
15
+
16
+ const require = createRequire(import.meta.url);
17
+ const __filename = fileURLToPath(import.meta.url);
18
+ const __dirname = path.dirname(__filename);
19
+
20
+ // Function to find the CLI's node_modules directory
21
+ const findCliNodeModules = () => {
22
+ return path.resolve(__dirname, 'node_modules');
23
+ };
14
24
 
15
25
  export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
16
26
  const { useTailwind = false, tailwindConfig = null } = options;
@@ -19,6 +29,9 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
19
29
  // Ensure output directory exists
20
30
  await fs.mkdir(outputDir, { recursive: true });
21
31
 
32
+ // Get CLI's node_modules path
33
+ const cliNodeModules = findCliNodeModules();
34
+
22
35
  let cssText = '';
23
36
 
24
37
  // Setup PostCSS plugins based on whether Tailwind is enabled
@@ -90,17 +103,43 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
90
103
  }),
91
104
  resolve({
92
105
  browser: true,
93
- dedupe: ['svelte']
106
+ dedupe: ['svelte'],
107
+ preferBuiltins: false,
108
+ moduleDirectories: ['node_modules'],
109
+ modulePaths: [cliNodeModules],
110
+ rootDir: cliNodeModules
94
111
  }),
95
112
  commonjs()
96
- ],
97
- external: ['svelte/internal']
113
+ ]
98
114
  });
99
115
 
100
116
  // Create a temporary directory for SSR
101
117
  const tempDir = path.join(path.dirname(svelteFilePath), '.temp');
102
118
  await fs.mkdir(tempDir, { recursive: true });
103
- const tempSSRFile = path.join(tempDir, 'ssr-bundle.js');
119
+
120
+ // Create package.json for the temp directory
121
+ await fs.writeFile(
122
+ path.join(tempDir, 'package.json'),
123
+ JSON.stringify({
124
+ type: 'module',
125
+ dependencies: {
126
+ svelte: require(path.join(cliNodeModules, 'svelte', 'package.json')).version
127
+ }
128
+ }),
129
+ 'utf-8'
130
+ );
131
+
132
+ // Create symlink to CLI's node_modules
133
+ const tempNodeModules = path.join(tempDir, 'node_modules');
134
+ try {
135
+ await fs.symlink(cliNodeModules, tempNodeModules, 'junction');
136
+ } catch (error) {
137
+ if (error.code !== 'EEXIST') {
138
+ throw error;
139
+ }
140
+ }
141
+
142
+ const tempSSRFile = path.join(tempDir, 'ssr-bundle.mjs');
104
143
 
105
144
  // Generate SSR bundle
106
145
  await ssrBundle.write({
@@ -110,7 +149,7 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
110
149
  });
111
150
 
112
151
  // Import the SSR bundle
113
- const { default: App } = await import(tempSSRFile);
152
+ const { default: App } = await import(`file://${tempSSRFile}`);
114
153
  const { html: initialHtml } = App.render();
115
154
 
116
155
  // Clean up temp files
@@ -142,7 +181,10 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
142
181
  }),
143
182
  resolve({
144
183
  browser: true,
145
- dedupe: ['svelte']
184
+ dedupe: ['svelte'],
185
+ moduleDirectories: ['node_modules'],
186
+ modulePaths: [cliNodeModules],
187
+ rootDir: cliNodeModules
146
188
  }),
147
189
  commonjs(),
148
190
  terser()
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "svelte-bundle",
3
- "version": "0.0.21",
4
- "description": "Bundle Svelte files into static HTML files",
3
+ "version": "0.0.22",
4
+ "description": "CLI tool to easily bundle a .svelte file into a single .html file",
5
5
  "type": "module",
6
+ "npm": "svelte-bundle",
6
7
  "bin": {
7
8
  "svelte-bundle": "./cli.js"
8
9
  },
@@ -10,6 +11,10 @@
10
11
  "cli.js",
11
12
  "bundle.js"
12
13
  ],
14
+ "categories": [
15
+ "build-plugins",
16
+ "integrations"
17
+ ],
13
18
  "keywords": [
14
19
  "svelte",
15
20
  "bundle",
@@ -59,4 +64,4 @@
59
64
  "engines": {
60
65
  "node": ">=18"
61
66
  }
62
- }
67
+ }