diff --git a/packages/sst/package.json b/packages/sst/package.json index 12317e6d0..531932b31 100644 --- a/packages/sst/package.json +++ b/packages/sst/package.json @@ -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", diff --git a/packages/sst/src/constructs/App.ts b/packages/sst/src/constructs/App.ts index fa2c172ac..33cbe7e7e 100644 --- a/packages/sst/src/constructs/App.ts +++ b/packages/sst/src/constructs/App.ts @@ -280,6 +280,11 @@ export class App extends CDKApp { ` }`, `}`, ``, + `declare global {`, + ` const __SST_DIRNAME: string;`, + ` const __SST_FILENAME: string;`, + `}`, + ``, ``, ].join("\n") ); @@ -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: [ diff --git a/packages/sst/src/stacks/build.ts b/packages/sst/src/stacks/build.ts index eb31551e3..364b90216 100644 --- a/packages/sst/src/stacks/build.ts +++ b/packages/sst/src/stacks/build.ts @@ -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 @@ -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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b4b3fac76..4ffc93f98 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -347,6 +347,9 @@ importers: '@babel/plugin-syntax-typescript': specifier: ^7.21.4 version: 7.25.9(@babel/core@7.26.0) + '@babel/types': + specifier: ^7.27.0 + version: 7.27.0 '@smithy/signature-v4': specifier: ^2.0.16 version: 2.3.0 @@ -405,8 +408,8 @@ importers: specifier: ^4.18.2 version: 4.21.2 fast-jwt: - specifier: ^3.1.1 - version: 3.3.3 + specifier: ^5.0.5 + version: 5.0.6 get-port: specifier: ^6.1.2 version: 6.1.2 @@ -2546,6 +2549,10 @@ packages: resolution: {integrity: sha512-L6mZmwFDK6Cjh1nRCLXpa6no13ZIioJDz7mdkzHv399pThrTa/k0nUlNaenOeh2kWu/iaOQYElEpKPUswUa9Vg==} engines: {node: '>=6.9.0'} + '@babel/types@7.27.0': + resolution: {integrity: sha512-H45s8fVLYjbhFH62dIJ3WtmJ6RSPt/3DRO0ZcT2SUiYiQyz3BLVb9ADEnLl91m74aQPS3AzzeajZHYOalWe3bg==} + engines: {node: '>=6.9.0'} + '@bcoe/v8-coverage@0.2.3': resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} @@ -7168,9 +7175,9 @@ packages: fast-json-stable-stringify@2.1.0: resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==} - fast-jwt@3.3.3: - resolution: {integrity: sha512-oS3P8bRI24oPLJUePt2OgF64FBQib5TlgHLFQxYNoHYEEZe0gU3cKjJAVqpB5XKV/zjxmq4Hzbk3fgfW/wRz8Q==} - engines: {node: '>=16 <22'} + fast-jwt@5.0.6: + resolution: {integrity: sha512-LPE7OCGUl11q3ZgW681cEU2d0d2JZ37hhJAmetCgNyW8waVaJVZXhyFF6U2so1Iim58Yc7pfxJe2P7MNetQH2g==} + engines: {node: '>=20'} fast-querystring@1.1.2: resolution: {integrity: sha512-g6KuKWmFXc0fID8WWH0jit4g0AGBoJhCkJMb1RmbsSEUNvQ+ZC8D6CUZ+GtF8nMzSPXnhiePyyqqipzNNEnHjg==} @@ -8577,6 +8584,7 @@ packages: lodash.get@4.4.2: resolution: {integrity: sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==} + deprecated: This package is deprecated. Use the optional chaining (?.) operator instead. lodash.includes@4.3.0: resolution: {integrity: sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==} @@ -8586,6 +8594,7 @@ packages: lodash.isequal@4.5.0: resolution: {integrity: sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==} + deprecated: This package is deprecated. Use require('node:util').isDeepStrictEqual instead. lodash.isinteger@4.0.4: resolution: {integrity: sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==} @@ -8909,8 +8918,8 @@ packages: mnemonist@0.38.3: resolution: {integrity: sha512-2K9QYubXx/NAjv4VLq1d1Ly8pWNC5L3BrixtdkyTegXWJIqY+zLNDhhX/A+ZwWt70tB1S8H4BE8FLYEFyNoOBw==} - mnemonist@0.39.8: - resolution: {integrity: sha512-vyWo2K3fjrUw8YeeZ1zF0fy6Mu59RHokURlld8ymdUPjMlD9EC9ov1/YPqTgqRvUN9nTr3Gqfz29LYAmu0PHPQ==} + mnemonist@0.40.3: + resolution: {integrity: sha512-Vjyr90sJ23CKKH/qPAgUKicw/v6pRoamxIEDFOF8uSgFME7DqPRpHgRTejWVjkdGg5dXj0/NyxZHZ9bcjH+2uQ==} module-definition@3.4.0: resolution: {integrity: sha512-XxJ88R1v458pifaSkPNLUTdSPNVGMP2SXVncVmApGO+gAfrLANiYe6JofymCzVceGOMwQE2xogxBSc8uB7XegA==} @@ -12499,7 +12508,7 @@ snapshots: dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sso-oidc': 3.699.0(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0) + '@aws-sdk/client-sso-oidc': 3.699.0(@aws-sdk/client-sts@3.726.1(aws-crt@1.25.0))(aws-crt@1.25.0) '@aws-sdk/client-sts': 3.699.0(aws-crt@1.25.0) '@aws-sdk/core': 3.696.0 '@aws-sdk/credential-provider-node': 3.699.0(@aws-sdk/client-sso-oidc@3.699.0(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0))(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0) @@ -12642,7 +12651,7 @@ snapshots: dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sso-oidc': 3.699.0(@aws-sdk/client-sts@3.726.1(aws-crt@1.25.0))(aws-crt@1.25.0) + '@aws-sdk/client-sso-oidc': 3.699.0(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0) '@aws-sdk/client-sts': 3.699.0(aws-crt@1.25.0) '@aws-sdk/core': 3.696.0 '@aws-sdk/credential-provider-node': 3.699.0(@aws-sdk/client-sso-oidc@3.699.0(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0))(@aws-sdk/client-sts@3.699.0(aws-crt@1.25.0))(aws-crt@1.25.0) @@ -16356,7 +16365,7 @@ snapshots: '@babel/parser': 7.26.5 '@babel/template': 7.25.9 '@babel/traverse': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 convert-source-map: 2.0.0 debug: 4.4.0 gensync: 1.0.0-beta.2 @@ -16368,7 +16377,7 @@ snapshots: '@babel/generator@7.26.5': dependencies: '@babel/parser': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@jridgewell/gen-mapping': 0.3.8 '@jridgewell/trace-mapping': 0.3.25 jsesc: 3.1.0 @@ -16430,7 +16439,7 @@ snapshots: '@babel/helper-module-imports@7.25.9': dependencies: '@babel/traverse': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 transitivePeerDependencies: - supports-color @@ -16502,7 +16511,7 @@ snapshots: '@babel/helpers@7.26.0': dependencies: '@babel/template': 7.25.9 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@babel/highlight@7.25.9': dependencies: @@ -16513,7 +16522,7 @@ snapshots: '@babel/parser@7.26.5': dependencies: - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@babel/plugin-bugfix-firefox-class-in-computed-class-key@7.25.9(@babel/core@7.26.0)': dependencies: @@ -17190,7 +17199,7 @@ snapshots: dependencies: '@babel/code-frame': 7.26.2 '@babel/parser': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@babel/traverse@7.26.5': dependencies: @@ -17198,7 +17207,7 @@ snapshots: '@babel/generator': 7.26.5 '@babel/parser': 7.26.5 '@babel/template': 7.25.9 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 debug: 4.4.0 globals: 11.12.0 transitivePeerDependencies: @@ -17214,6 +17223,11 @@ snapshots: '@babel/helper-string-parser': 7.25.9 '@babel/helper-validator-identifier': 7.25.9 + '@babel/types@7.27.0': + dependencies: + '@babel/helper-string-parser': 7.25.9 + '@babel/helper-validator-identifier': 7.25.9 + '@bcoe/v8-coverage@0.2.3': {} '@cdklabs/tskb@0.0.3': {} @@ -20844,23 +20858,23 @@ snapshots: '@types/babel__core@7.20.5': dependencies: '@babel/parser': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@types/babel__generator': 7.6.8 '@types/babel__template': 7.4.4 '@types/babel__traverse': 7.20.6 '@types/babel__generator@7.6.8': dependencies: - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@types/babel__template@7.4.4': dependencies: '@babel/parser': 7.26.5 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@types/babel__traverse@7.20.6': dependencies: - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@types/body-parser@1.19.5': dependencies: @@ -21797,7 +21811,7 @@ snapshots: babel-plugin-jest-hoist@29.6.3: dependencies: '@babel/template': 7.25.9 - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@types/babel__core': 7.20.5 '@types/babel__traverse': 7.20.6 @@ -23679,12 +23693,12 @@ snapshots: fast-json-stable-stringify@2.1.0: {} - fast-jwt@3.3.3: + fast-jwt@5.0.6: dependencies: '@lukeed/ms': 2.0.2 asn1.js: 5.4.1 ecdsa-sig-formatter: 1.0.11 - mnemonist: 0.39.8 + mnemonist: 0.40.3 fast-querystring@1.1.2: dependencies: @@ -25103,7 +25117,7 @@ snapshots: '@babel/generator': 7.26.5 '@babel/plugin-syntax-jsx': 7.25.9(@babel/core@7.26.0) '@babel/plugin-syntax-typescript': 7.25.9(@babel/core@7.26.0) - '@babel/types': 7.26.5 + '@babel/types': 7.27.0 '@jest/expect-utils': 29.7.0 '@jest/transform': 29.7.0 '@jest/types': 29.6.3 @@ -25711,7 +25725,7 @@ snapshots: dependencies: obliterator: 1.6.1 - mnemonist@0.39.8: + mnemonist@0.40.3: dependencies: obliterator: 2.0.5