(
cwd,
generate,
compileOptions = {},
output_map = false,
preprocessor
)
| 61 | * @param {any} [preprocessor] |
| 62 | */ |
| 63 | export async function compile_directory( |
| 64 | cwd, |
| 65 | generate, |
| 66 | compileOptions = {}, |
| 67 | output_map = false, |
| 68 | preprocessor |
| 69 | ) { |
| 70 | const output_dir = `${cwd}/_output/${generate}`; |
| 71 | |
| 72 | fs.rmSync(output_dir, { recursive: true, force: true }); |
| 73 | |
| 74 | for (let file of globSync('**', { cwd, onlyFiles: true })) { |
| 75 | if (file.startsWith('_')) continue; |
| 76 | |
| 77 | let text = fs.readFileSync(`${cwd}/${file}`, 'utf-8').replace(/\r\n/g, '\n'); |
| 78 | let opts = { |
| 79 | filename: path.join(cwd, file), |
| 80 | ...compileOptions, |
| 81 | generate |
| 82 | }; |
| 83 | |
| 84 | if (file.endsWith('.js')) { |
| 85 | const out = `${output_dir}/${file}`; |
| 86 | if (file.endsWith('.svelte.js')) { |
| 87 | const compiled = compileModule(text, { |
| 88 | filename: opts.filename, |
| 89 | generate: opts.generate, |
| 90 | dev: opts.dev, |
| 91 | experimental: opts.experimental |
| 92 | }); |
| 93 | write(out, compiled.js.code.replace(`v${VERSION}`, 'VERSION')); |
| 94 | } else { |
| 95 | // for non-runes tests, just re-export from the original source file — this |
| 96 | // allows the `_config.js` module to import shared state to use in tests |
| 97 | const source = path |
| 98 | .relative(path.dirname(out), path.resolve(cwd, file)) |
| 99 | .replace(/\\/g, '/'); |
| 100 | let result = `export * from '${source}';`; |
| 101 | if (text.includes('export default')) { |
| 102 | result += `\nexport { default } from '${source}';`; |
| 103 | } |
| 104 | |
| 105 | write(out, result); |
| 106 | } |
| 107 | } else if ( |
| 108 | file.endsWith('.svelte') && |
| 109 | // Make it possible to compile separate versions for client and server to simulate |
| 110 | // cases where `{browser ? 'foo' : 'bar'}` is turning into `{'foo'}` on the server |
| 111 | // and `{bar}` on the client, assuming we have sophisticated enough treeshaking |
| 112 | // in the future to make this a thing. |
| 113 | (!file.endsWith('.server.svelte') || generate === 'server') && |
| 114 | (!file.endsWith('.client.svelte') || generate === 'client') |
| 115 | ) { |
| 116 | file = file.replace(/\.client\.svelte$/, '.svelte').replace(/\.server\.svelte$/, '.svelte'); |
| 117 | |
| 118 | if (preprocessor?.preprocess) { |
| 119 | const preprocessed = await preprocess( |
| 120 | text, |
no test coverage detected