Created
May 30, 2021 15:30
-
-
Save tanhauhau/af9e6f8bdbc796d4905f2c372e6c064a to your computer and use it in GitHub Desktop.
svelte/register
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
let name = 'world'; | |
</script> | |
<h1>Hello {name}!</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SECRET_A=XXX | |
SECRET_B=YYY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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