Skip to content

Instantly share code, notes, and snippets.

@tanhauhau
Created May 30, 2021 15:30
Show Gist options
  • Save tanhauhau/af9e6f8bdbc796d4905f2c372e6c064a to your computer and use it in GitHub Desktop.
Save tanhauhau/af9e6f8bdbc796d4905f2c372e6c064a to your computer and use it in GitHub Desktop.
svelte/register
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
SECRET_A=XXX
SECRET_B=YYY
// require('svelte/register');
const fs = require('fs');
const svelteCompiler = require('svelte/compiler');
require.extensions['.svelte'] = function (module, filename) {
const content = fs.readFileSync(filename, 'utf-8');
const { js } = svelteCompiler.compile(content, {
generate: 'ssr',
format: 'cjs'
});
return module._compile(js.code, filename);
}
require.extensions['.env'] = function (module, filename) {
const content = fs.readFileSync(filename, 'utf-8');
const obj = {};
content.split('\n').forEach((line) => {
const [key, value] = line.split('=');
obj[key] = value;
});
return module._compile(`module.exports = ${JSON.stringify(obj)};`, filename);
// module.exports = { secret_a: 'xxx', secret_b = 'yyy' }
};
const envVariables = require('./dev.env');
console.log('envVariables', envVariables);
require.extensions['.svelte'] = function (module, filename) {
const content = fs.readFileSync(filename, 'utf-8');
const { js } = svelteCompiler.compile(content, {
generate: 'ssr',
format: 'cjs'
});
return module._compile(js.code, filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment