Skip to content

Commit b1c7903

Browse files
committed
fix: save & prepend original imports manually
1 parent 9e4a95f commit b1c7903

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function decide() {
5757

5858
const isExternal = /^(https?:)?\/\//;
5959
const isString = (x: unknown): x is string => typeof x === 'string';
60+
const IMPORTS = /import(?:["'\s]*([\w*{}\n,\r\s\t]+)from\s*)?["'\s].*([@\w/_-]+)["'\s].*/g;
6061

6162
function isTypescript(attrs: Attributes): boolean | void {
6263
if (isString(attrs.lang)) return /^(ts|typescript)$/.test(attrs.lang);
@@ -89,6 +90,8 @@ async function transform(input: ProcessorInput, options: TransformOptions): Prom
8990
}
9091
}
9192

93+
let imports = input.content.match(IMPORTS);
94+
let preprend = Array.isArray(imports) ? imports.join('\n') : '';
9295
let output = await (service || esbuild).transform(input.content, config);
9396

9497
// TODO: format output.warnings
@@ -97,7 +100,7 @@ async function transform(input: ProcessorInput, options: TransformOptions): Prom
97100
}
98101

99102
return {
100-
code: output.code,
103+
code: preprend + output.code.replace(IMPORTS, ''),
101104
dependencies: deps,
102105
map: output.map,
103106
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
import Counter from './Counter.svelte';
3+
import { capitalize } from './utils';
4+
import {
5+
hello, world,
6+
howdy
7+
} from 'foobar';
8+
9+
export let name: string = 'hello';
10+
export const value = hello() + world();
11+
</script>
12+
13+
<div>{capitalize(name)}</div>
14+
<Counter {value} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">import Counter from './Counter.svelte';
2+
import { capitalize } from './utils';
3+
import {
4+
hello, world,
5+
howdy
6+
} from 'foobar';
7+
let name = "hello";
8+
const value = hello() + world();
9+
export {
10+
name,
11+
value
12+
};
13+
</script>
14+
15+
<div>{capitalize(name)}</div>
16+
<Counter {value} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { typescript } from '../../../src';
2+
3+
export const config = typescript({
4+
//
5+
})

0 commit comments

Comments
 (0)