-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
운영용 스크립트에서 코드 재사용을 위해 `$lib/server` 안에 있는 파일을 import 해올 수 있도록 [esm loader hooks](https://nodejs.org/api/module.html#customization-hooks) 구현함. 이제 `pnpm run:script ./scripts/asdf.ts` 와 같은 식으로 로컬에서 스크립트를 실행할 수 있음
- Loading branch information
Showing
5 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const resolve = async (specifier, context, nextResolve) => { | ||
if (specifier.startsWith('$')) { | ||
return { | ||
url: `virtual://${specifier}`, | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
return nextResolve(specifier, context); | ||
}; | ||
|
||
export const load = (url, context, nextLoad) => { | ||
if (url.startsWith('virtual://')) { | ||
const mod = url.replace(/^virtual:\/\//, ''); | ||
let source; | ||
|
||
switch (mod) { | ||
case '$env/dynamic/public': | ||
case '$env/dynamic/private': | ||
source = `export const env = process.env;`; | ||
break; | ||
case '$app/environment': | ||
source = `export const building = false; export const dev = true; export const browser = false;`; | ||
break; | ||
default: | ||
throw new Error(`Unknown virtual module: ${mod}`); | ||
} | ||
|
||
return { | ||
format: 'module', | ||
source, | ||
shortCircuit: true, | ||
}; | ||
} | ||
|
||
return nextLoad(url, context); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { register } from 'node:module'; | ||
|
||
register('./hooks.js', import.meta.url); | ||
process.env.SCRIPTS = 'true'; |
This file contains 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
This file contains 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