svelte-bundle 0.0.22 → 0.0.23
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/bundle.js +22 -48
- package/cli.js +11 -0
- package/package.json +4 -9
package/bundle.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// bundle.js
|
|
1
2
|
import fs from 'fs/promises';
|
|
2
3
|
import path from 'path';
|
|
3
4
|
import { rollup } from 'rollup';
|
|
@@ -10,18 +11,13 @@ import postcss from 'postcss';
|
|
|
10
11
|
import tailwindcss from 'tailwindcss';
|
|
11
12
|
import autoprefixer from 'autoprefixer';
|
|
12
13
|
import cssnano from 'cssnano';
|
|
13
|
-
import { createRequire } from 'module';
|
|
14
14
|
import { fileURLToPath } from 'url';
|
|
15
|
+
import { createRequire } from 'module';
|
|
15
16
|
|
|
16
17
|
const require = createRequire(import.meta.url);
|
|
17
18
|
const __filename = fileURLToPath(import.meta.url);
|
|
18
19
|
const __dirname = path.dirname(__filename);
|
|
19
20
|
|
|
20
|
-
// Function to find the CLI's node_modules directory
|
|
21
|
-
const findCliNodeModules = () => {
|
|
22
|
-
return path.resolve(__dirname, 'node_modules');
|
|
23
|
-
};
|
|
24
|
-
|
|
25
21
|
export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
26
22
|
const { useTailwind = false, tailwindConfig = null } = options;
|
|
27
23
|
|
|
@@ -29,9 +25,6 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
|
29
25
|
// Ensure output directory exists
|
|
30
26
|
await fs.mkdir(outputDir, { recursive: true });
|
|
31
27
|
|
|
32
|
-
// Get CLI's node_modules path
|
|
33
|
-
const cliNodeModules = findCliNodeModules();
|
|
34
|
-
|
|
35
28
|
let cssText = '';
|
|
36
29
|
|
|
37
30
|
// Setup PostCSS plugins based on whether Tailwind is enabled
|
|
@@ -75,6 +68,9 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
|
75
68
|
.process(tailwindCss, { from: undefined });
|
|
76
69
|
globalCssText = processedCss.css;
|
|
77
70
|
}
|
|
71
|
+
|
|
72
|
+
// Get the absolute path to svelte/internal
|
|
73
|
+
const svelteInternalPath = require.resolve('svelte/internal');
|
|
78
74
|
|
|
79
75
|
// Create temporary SSR bundle
|
|
80
76
|
const ssrBundle = await rollup({
|
|
@@ -104,52 +100,31 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
|
104
100
|
resolve({
|
|
105
101
|
browser: true,
|
|
106
102
|
dedupe: ['svelte'],
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
modulePaths: [cliNodeModules],
|
|
110
|
-
rootDir: cliNodeModules
|
|
103
|
+
modulePaths: [path.join(__dirname, 'node_modules')],
|
|
104
|
+
rootDir: __dirname
|
|
111
105
|
}),
|
|
112
106
|
commonjs()
|
|
113
|
-
]
|
|
107
|
+
],
|
|
108
|
+
external: ['svelte/internal']
|
|
114
109
|
});
|
|
115
110
|
|
|
116
|
-
// Create a temporary directory
|
|
117
|
-
const tempDir = path.join(
|
|
111
|
+
// Create a temporary directory in the CLI package directory
|
|
112
|
+
const tempDir = path.join(__dirname, '.temp');
|
|
118
113
|
await fs.mkdir(tempDir, { recursive: true });
|
|
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');
|
|
114
|
+
const tempSSRFile = path.join(tempDir, 'ssr-bundle.js');
|
|
143
115
|
|
|
144
|
-
// Generate SSR bundle
|
|
116
|
+
// Generate SSR bundle as ESM
|
|
145
117
|
await ssrBundle.write({
|
|
146
118
|
file: tempSSRFile,
|
|
147
119
|
format: 'es',
|
|
148
|
-
exports: 'default'
|
|
120
|
+
exports: 'default',
|
|
121
|
+
paths: {
|
|
122
|
+
'svelte/internal': svelteInternalPath
|
|
123
|
+
}
|
|
149
124
|
});
|
|
150
125
|
|
|
151
|
-
// Import the SSR bundle
|
|
152
|
-
const { default: App } = await import(
|
|
126
|
+
// Import the SSR bundle using dynamic import
|
|
127
|
+
const { default: App } = await import(/* @vite-ignore */`file://${tempSSRFile}`);
|
|
153
128
|
const { html: initialHtml } = App.render();
|
|
154
129
|
|
|
155
130
|
// Clean up temp files
|
|
@@ -182,9 +157,8 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
|
182
157
|
resolve({
|
|
183
158
|
browser: true,
|
|
184
159
|
dedupe: ['svelte'],
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
rootDir: cliNodeModules
|
|
160
|
+
modulePaths: [path.join(__dirname, 'node_modules')],
|
|
161
|
+
rootDir: __dirname
|
|
188
162
|
}),
|
|
189
163
|
commonjs(),
|
|
190
164
|
terser()
|
|
@@ -200,7 +174,7 @@ export async function buildStaticFile(svelteFilePath, outputDir, options = {}) {
|
|
|
200
174
|
});
|
|
201
175
|
|
|
202
176
|
// Create the final HTML
|
|
203
|
-
const finalHtml = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Static Svelte App</title><style>${globalCssText}${cssText}</style></head><body><div id="app">${initialHtml}</div><script
|
|
177
|
+
const finalHtml = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Static Svelte App</title><style>${globalCssText}${cssText}</style></head><body><div id="app">${initialHtml}</div><script>${clientCode}const app=new App({target:document.getElementById("app"),hydrate:!0});</script></body></html>`;
|
|
204
178
|
|
|
205
179
|
// Write the output file
|
|
206
180
|
const outputPath = path.join(outputDir, 'output.html');
|
package/cli.js
CHANGED
|
@@ -26,6 +26,14 @@ const rl = createInterface({
|
|
|
26
26
|
|
|
27
27
|
const question = (query) => new Promise((resolve) => rl.question(query, resolve));
|
|
28
28
|
|
|
29
|
+
// Add check for node version
|
|
30
|
+
const nodeVersion = process.versions.node;
|
|
31
|
+
const [major] = nodeVersion.split('.').map(Number);
|
|
32
|
+
if (major < 18) {
|
|
33
|
+
console.error(chalk.red(`Error: Node.js version 18 or higher is required. Current version: ${nodeVersion}`));
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
29
37
|
program
|
|
30
38
|
.name('svelte-bundle')
|
|
31
39
|
.description(packageJson.description)
|
|
@@ -132,6 +140,9 @@ async function validateAndProcess() {
|
|
|
132
140
|
} catch (error) {
|
|
133
141
|
console.error(chalk.red('\nBuild failed:'));
|
|
134
142
|
console.error(chalk.red(error.message));
|
|
143
|
+
if (error.stack) {
|
|
144
|
+
console.error(chalk.gray(error.stack));
|
|
145
|
+
}
|
|
135
146
|
process.exit(1);
|
|
136
147
|
} finally {
|
|
137
148
|
rl.close();
|
package/package.json
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-bundle",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.23",
|
|
4
|
+
"description": "Bundle Svelte files into static HTML files",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"npm": "svelte-bundle",
|
|
7
6
|
"bin": {
|
|
8
|
-
"svelte-bundle": "
|
|
7
|
+
"svelte-bundle": "cli.js"
|
|
9
8
|
},
|
|
10
9
|
"files": [
|
|
11
10
|
"cli.js",
|
|
12
11
|
"bundle.js"
|
|
13
12
|
],
|
|
14
|
-
"categories": [
|
|
15
|
-
"build-plugins",
|
|
16
|
-
"integrations"
|
|
17
|
-
],
|
|
18
13
|
"keywords": [
|
|
19
14
|
"svelte",
|
|
20
15
|
"bundle",
|
|
@@ -64,4 +59,4 @@
|
|
|
64
59
|
"engines": {
|
|
65
60
|
"node": ">=18"
|
|
66
61
|
}
|
|
67
|
-
}
|
|
62
|
+
}
|