Skip to content

Commit 1b7e94c

Browse files
committed
transcribe code with module context
1 parent 6358ea5 commit 1b7e94c

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

cli/literator.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -137,46 +137,48 @@ export function TranscribeTypeDraftSync(source: string)
137137

138138
export async function TranscribeSvelteDraftAsync(source: string)
139139
{
140+
//
140141
const code = await readFile(source, "utf8");
141-
const transcriber = new SvelteTranscriber(code);
142-
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
143-
const { import_section, script_section, template_section } = transcriber.TranscribeToSections();
142+
const { import_section, script_section, template_section, module_context } = Transcribe(code);
144143

145144
//
146145
const style = source.replace(".svelte.tsx", ".css");
147146
const style_section = await pathExists(style) ? await readFile(style, "utf8") : "";
148147

149148
//
150-
const component = [
151-
"<script>",
152-
import_section,
153-
"\n",
154-
script_section,
155-
"</script>",
156-
"\n",
157-
template_section,
158-
"\n",
159-
"<style>",
160-
style_section,
161-
"</style>"
162-
].join("\n");
163-
149+
const component = AssembleComponent(import_section, script_section, template_section, style_section, module_context);
164150
return component;
165151
}
166152

167153
export function TranscribeSvelteDraftSync(source: string)
168154
{
155+
//
169156
const code = readFileSync(source, "utf8");
170-
const transcriber = new SvelteTranscriber(code);
171-
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
172-
const { import_section, script_section, template_section } = transcriber.TranscribeToSections();
157+
const { import_section, script_section, template_section, module_context } = Transcribe(code);
173158

174159
//
175160
const style = source.replace(".svelte.tsx", ".css");
176161
const style_section = existsSync(style) ? readFileSync(style, "utf8") : "";
177162

178163
//
164+
const component = AssembleComponent(import_section, script_section, template_section, style_section, module_context);
165+
return component;
166+
}
167+
168+
function Transcribe(code: string)
169+
{
170+
const transcriber = new SvelteTranscriber(code);
171+
config.dsls.forEach(dsl => transcriber.AddDSL(dsl.name, dsl.dsl));
172+
const module_context = transcriber.ExtractModuleContext();
173+
const { import_section, script_section, template_section } = transcriber.TranscribeToSections();
174+
return { import_section, script_section, template_section, module_context }
175+
}
176+
177+
function AssembleComponent(import_section: string, script_section: string, template_section: string, style_section: string, module_context: string)
178+
{
179+
const module_context_section = module_context ? `<script context="module">\n${module_context}\n</script>` : "";
179180
const component = [
181+
module_context_section,
180182
"<script>",
181183
import_section,
182184
"\n",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-draft",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Develop svelte app in typedraft",
55
"author": "mistlog",
66
"license": "MIT",

0 commit comments

Comments
 (0)