edges-svelte 2.0.7 → 2.1.0
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.
|
@@ -90,12 +90,39 @@ export function edgesPlugin(options) {
|
|
|
90
90
|
const compressionOptions = compression.enabled ? `, { compress: true, compressionThreshold: ${compression.threshold || 1024} }` : '';
|
|
91
91
|
// Build silent devtools option
|
|
92
92
|
const silentOption = silentChromeDevtools ? '' : `, false`;
|
|
93
|
+
// Find the position after the last import statement to preserve import order
|
|
94
|
+
const findImportInsertPosition = (sourceCode) => {
|
|
95
|
+
const lines = sourceCode.split('\n');
|
|
96
|
+
let lastImportIndex = -1;
|
|
97
|
+
for (let i = 0; i < lines.length; i++) {
|
|
98
|
+
const line = lines[i].trim();
|
|
99
|
+
// Check for import statements (including type imports and dynamic imports)
|
|
100
|
+
if (line.startsWith('import ') || (line.startsWith('export ') && line.includes(' from '))) {
|
|
101
|
+
lastImportIndex = i;
|
|
102
|
+
}
|
|
103
|
+
// Stop at first non-import, non-comment, non-empty line after imports started
|
|
104
|
+
else if (lastImportIndex !== -1 && line && !line.startsWith('//') && !line.startsWith('/*') && !line.startsWith('*')) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (lastImportIndex === -1) {
|
|
109
|
+
// No imports found, insert at the beginning
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
// Calculate character position after the last import line
|
|
113
|
+
const position = lines.slice(0, lastImportIndex + 1).join('\n').length;
|
|
114
|
+
return position > 0 ? position + 1 : 0; // +1 for the newline after last import
|
|
115
|
+
};
|
|
93
116
|
if (!hasHandleExport) {
|
|
94
117
|
// No handle defined - create default with compression options
|
|
118
|
+
const insertPos = findImportInsertPosition(code);
|
|
119
|
+
const beforeImports = code.slice(0, insertPos);
|
|
120
|
+
const afterImports = code.slice(insertPos);
|
|
95
121
|
return {
|
|
96
|
-
code:
|
|
122
|
+
code: beforeImports +
|
|
123
|
+
`// __EDGES_AUTO_WRAPPED__\n` +
|
|
97
124
|
`import { edgesHandle } from '${importPath}';\n\n` +
|
|
98
|
-
|
|
125
|
+
afterImports +
|
|
99
126
|
`\n\n` +
|
|
100
127
|
`export const handle = edgesHandle(({ serialize, edgesEvent, resolve }) => ` +
|
|
101
128
|
`resolve(edgesEvent, { transformPageChunk: ({ html }) => serialize(html${compressionOptions}) })${silentOption});`,
|
|
@@ -103,9 +130,13 @@ export function edgesPlugin(options) {
|
|
|
103
130
|
};
|
|
104
131
|
}
|
|
105
132
|
// User defined a handle - wrap it with options
|
|
106
|
-
const
|
|
133
|
+
const insertPos = findImportInsertPosition(code);
|
|
134
|
+
const beforeImports = code.slice(0, insertPos);
|
|
135
|
+
const afterImports = code.slice(insertPos);
|
|
136
|
+
const wrappedCode = beforeImports +
|
|
137
|
+
`// __EDGES_AUTO_WRAPPED__\n` +
|
|
107
138
|
`import { __autoWrapHandle } from '${importPath}';\n\n` +
|
|
108
|
-
|
|
139
|
+
afterImports.replace(/export\s+const\s+handle/, 'const __userHandle') +
|
|
109
140
|
`\n\n` +
|
|
110
141
|
`const __compressionOptions = ${JSON.stringify({ compress: compression.enabled, compressionThreshold: compression.threshold })};\n` +
|
|
111
142
|
`const __silentChromeDevtools = ${silentChromeDevtools};\n` +
|