Skip to content

feat: resolve __SST_DIRNAME and __SST_FILENAME at built time #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/sst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@babel/core": "^7.0.0-0",
"@babel/generator": "^7.20.5",
"@babel/plugin-syntax-typescript": "^7.21.4",
"@babel/types": "^7.27.0",
"@smithy/signature-v4": "^2.0.16",
"@trpc/server": "9.16.0",
"adm-zip": "0.5.14",
Expand Down
11 changes: 8 additions & 3 deletions packages/sst/src/constructs/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ export class App extends CDKApp {
` }`,
`}`,
``,
`declare global {`,
` const __SST_DIRNAME: string;`,
` const __SST_FILENAME: string;`,
`}`,
``,
``,
].join("\n")
);
Expand Down Expand Up @@ -375,9 +380,9 @@ export class App extends CDKApp {
new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"s3:GetObject",
"s3:GetObjectTagging",
"s3:PutObject",
"s3:GetObject",
"s3:GetObjectTagging",
"s3:PutObject",
"s3:PutObjectTagging"
],
resources: [
Expand Down
37 changes: 37 additions & 0 deletions packages/sst/src/stacks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dynamicImport } from "../util/module.js";
import { findAbove } from "../util/fs.js";
import { VisibleError } from "../error.js";
import babel from "@babel/core";
import { stringLiteral } from "@babel/types";
const _ = await import("@babel/generator");
const generate = _.default?.default ?? _.default;
// @ts-expect-error
Expand Down Expand Up @@ -86,6 +87,42 @@ export async function load(input: string, shallow?: boolean) {
});
},
},
{
name: "pathReplacer",
setup(build) {
build.onLoad({ filter: /.*/ }, async (args) => {
let contents = await fs
.readFile(args.path)
.then((x) => x.toString());
const ast = babel.parse(contents, {
sourceType: "module",
babelrc: false,
configFile: false,
plugins: [ts],
});
if (ast === null) {
console.error(`Error parsing ${args.path} as AST`);
return
}
babel.traverse(ast, {
Identifier(astPath) {
if (astPath.node.name === "__SST_FILENAME") {
astPath.replaceWith(stringLiteral(args.path));
}
if (astPath.node.name === "__SST_DIRNAME") {
astPath.replaceWith(
stringLiteral(path.parse(args.path).dir)
);
}
},
});
return {
contents: generate(ast!).code,
loader: "ts",
};
});
},
},
],
absWorkingDir: root,
outfile,
Expand Down
64 changes: 39 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.