Skip to content

Commit bbc9f58

Browse files
authored
fix: precompile stacks to commonjs (#7)
Avoid the hell of Pulumi's typescript support.
1 parent 70f84ef commit bbc9f58

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ Pulumi.
3939

4040
```javascript
4141
import { adapter } from 'sveltekit-adapter-aws-pulumi'
42-
import preprocess from 'svelte-preprocess'
42+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
4343

4444
/** @type {import('@sveltejs/kit').Config} */
4545
const config = {
46-
preprocess: preprocess(),
46+
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
47+
// for more information about preprocessors
48+
preprocess: vitePreprocess(),
49+
4750
kit: {
4851
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
49-
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
52+
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
5053
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
5154
adapter: adapter({
5255
autoDeploy: true,

adapter.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function adapter({
5858
builder,
5959
artifactPath,
6060
esbuildOptions,
61-
serverStreaming
61+
serverStreaming,
6262
)
6363

6464
const options_directory = await buildOptions(builder, artifactPath)
@@ -74,16 +74,7 @@ export function adapter({
7474
stackName: stackName,
7575
workDir: serverPath,
7676
}
77-
const serverStack = await LocalWorkspace.createOrSelectStack(
78-
serverArgs,
79-
{
80-
envVars: {
81-
TS_NODE_IGNORE: '^(?!.*(sveltekit-adapter-aws-pulumi)).*',
82-
TS_NODE_TYPE_CHECK: '0',
83-
PULUMI_NODEJS_TRANSPILE_ONLY: 'true',
84-
},
85-
}
86-
)
77+
const serverStack = await LocalWorkspace.createOrSelectStack(serverArgs)
8778

8879
await serverStack.setAllConfig({
8980
'aws:region': { value: region },
@@ -112,7 +103,7 @@ export function adapter({
112103
prerendered_directory,
113104
serverStackUpResult.outputs.serverDomain.value,
114105
serverStackUpResult.outputs.optionsDomain.value,
115-
artifactPath
106+
artifactPath,
116107
)
117108

118109
// Setup main stack.
@@ -121,13 +112,7 @@ export function adapter({
121112
stackName: stackName,
122113
workDir: mainPath,
123114
}
124-
const mainStack = await LocalWorkspace.createOrSelectStack(mainArgs, {
125-
envVars: {
126-
TS_NODE_IGNORE: '^(?!.*(sveltekit-adapter-aws-pulumi)).*',
127-
TS_NODE_TYPE_CHECK: '0',
128-
PULUMI_NODEJS_TRANSPILE_ONLY: 'true',
129-
},
130-
})
115+
const mainStack = await LocalWorkspace.createOrSelectStack(mainArgs)
131116

132117
await mainStack.setAllConfig({
133118
'aws:region': { value: region },
@@ -161,7 +146,7 @@ export function adapter({
161146
await mainStack.refresh()
162147
const mainStackUpResult = await mainStack.up({ onOutput: console.info })
163148
const mainAllowedOrigins = JSON.stringify(
164-
mainStackUpResult.outputs.allowedOrigins.value
149+
mainStackUpResult.outputs.allowedOrigins.value,
165150
)
166151

167152
// Call the server stack setting the allowed origins
@@ -177,7 +162,7 @@ export function adapter({
177162
adapterProps.stackName = stackName
178163
writeFileSync(
179164
join(artifactPath, '.adapterprops.json'),
180-
JSON.stringify(adapterProps)
165+
JSON.stringify(adapterProps),
181166
)
182167

183168
builder.log.minor('Pulumi deployment done.')

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@
3131
"adapter-stack-destroy": "./bin/destroy.js"
3232
},
3333
"scripts": {
34-
"build": "npm run clean && tsc --project tsconfig.build.json && npm run copy-files",
34+
"build": "npm run clean && tsc --project tsconfig.build.json && tsc --project stacks/tsconfig.json && npm run copy-files",
3535
"watch": "tsc -w --excludeFiles test",
3636
"format": "prettier --write .",
3737
"test": "vitest",
3838
"clean": "rimraf dist",
39-
"copy-files": "copyfiles stacks/* stacks/**/* package.json README.md LICENSE dist",
39+
"copy-files": "copyfiles package.json LICENSE README.md stacks/package.json stacks/*/Pulumi.yaml dist",
4040
"coverage": "vitest run --coverage"
4141
},
4242
"devDependencies": {
4343
"@types/folder-hash": "^4.0.4",
4444
"@types/lodash": "^4.17.1",
45+
"@types/mime-types": "^2.1.4",
4546
"@types/minimist": "^1.2.5",
4647
"@types/node": "20.12.12",
4748
"@types/yargs": "^17.0.32",
@@ -59,7 +60,7 @@
5960
"dotenv": "^16.4.5",
6061
"folder-hash": "^4.0.4",
6162
"lodash": "^4.17.21",
62-
"mime": "^4.0.3",
63+
"mime-types": "^2.1.35",
6364
"sveltekit-adapter-aws-base": "^3.0.1",
6465
"yargs": "^17.7.2"
6566
},

stacks/main/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
buildCDN,
99
createAliasRecord,
1010
buildInvalidator,
11-
} from './resources'
11+
} from './resources.js'
1212

1313
const pulumiConfig = new pulumi.Config()
1414
const edgePath = pulumiConfig.get('edgePath')
@@ -42,7 +42,7 @@ const distribution = buildCDN(
4242
bucket,
4343
serverHeaders,
4444
FQDN,
45-
certificateArn
45+
certificateArn,
4646
)
4747

4848
if (FQDN) {

stacks/main/resources.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3-
import mime from 'mime'
3+
import * as mime from 'mime-types'
44

55
import * as aws from '@pulumi/aws'
66
import * as pulumi from '@pulumi/pulumi'
@@ -195,7 +195,7 @@ export function uploadStatic(dirPath: string, bucket: aws.s3.Bucket) {
195195
{
196196
key: posixFilePath,
197197
bucket: bucket.id,
198-
contentType: mime.getType(filePath) || undefined,
198+
contentType: mime.lookup(filePath) || undefined,
199199
source: new pulumi.asset.FileAsset(filePath),
200200
},
201201
{

stacks/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "sveltekit-adapter-aws-pulumi-stacks",
3+
"version": "0.0.0"
4+
}

stacks/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"outDir": "../dist/stacks",
5+
"target": "es2016",
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"sourceMap": true,
9+
"experimentalDecorators": true,
10+
"pretty": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true
14+
}
15+
}

0 commit comments

Comments
 (0)