Skip to content

Commit 78b2f16

Browse files
committed
update http templates to use esbuild
Signed-off-by: Karthik Ganeshram <[email protected]>
1 parent cc30aca commit 78b2f16

File tree

9 files changed

+95
-85
lines changed

9 files changed

+95
-85
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// build.mjs
2+
import { build } from 'esbuild';
3+
import path from 'path';
4+
import { SpinEsbuildPlugin } from "@spinframework/build-tools/plugins/esbuild/index.js";
5+
import fs from 'fs';
6+
7+
const spinPlugin = await SpinEsbuildPlugin();
8+
9+
// plugin to handle vendor files in node_modules that may not be bundled.
10+
// Instead of generating a real source map for these files, it appends a minimal
11+
// inline source map pointing to an empty source. This avoids errors and ensures
12+
// source maps exist even for unbundled vendor code.
13+
let SourceMapPlugin = {
14+
name: 'excludeVendorFromSourceMap',
15+
setup(build) {
16+
build.onLoad({ filter: /node_modules/ }, args => {
17+
return {
18+
contents: fs.readFileSync(args.path, 'utf8')
19+
+ '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
20+
loader: 'default',
21+
}
22+
})
23+
},
24+
}
25+
26+
await build({
27+
entryPoints: ['./src/index.ts'],
28+
outfile: './build/bundle.js',
29+
bundle: true,
30+
format: 'esm',
31+
platform: 'node',
32+
sourcemap: true,
33+
minify: false,
34+
plugins: [spinPlugin, SourceMapPlugin],
35+
logLevel: 'error',
36+
loader: {
37+
'.ts': 'ts',
38+
'.tsx': 'tsx',
39+
},
40+
resolveExtensions: ['.ts', '.tsx', '.js'],
41+
// This prevents sourcemaps from traversing into node_modules
42+
sourceRoot: path.resolve(process.cwd(), 'src'),
43+
});

templates/http-js/content/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
"description": "{{project-description}}",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm",
7+
"build": "node build.mjs && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
1111
"author": "",
1212
"license": "ISC",
1313
"devDependencies": {
1414
"mkdirp": "^3.0.1",
15-
"webpack": "^5.74.0",
16-
"webpack-cli": "^4.10.0"
15+
"esbuild": "^0.25.8"
1716
},
1817
"dependencies": {
1918
{%- case http-router -%}

templates/http-js/content/webpack.config.js

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// build.mjs
2+
import { build } from 'esbuild';
3+
import path from 'path';
4+
import { SpinEsbuildPlugin } from "@spinframework/build-tools/plugins/esbuild/index.js";
5+
import fs from 'fs';
6+
7+
const spinPlugin = await SpinEsbuildPlugin();
8+
9+
// plugin to handle vendor files in node_modules that may not be bundled.
10+
// Instead of generating a real source map for these files, it appends a minimal
11+
// inline source map pointing to an empty source. This avoids errors and ensures
12+
// source maps exist even for unbundled vendor code.
13+
let SourceMapPlugin = {
14+
name: 'excludeVendorFromSourceMap',
15+
setup(build) {
16+
build.onLoad({ filter: /node_modules/ }, args => {
17+
return {
18+
contents: fs.readFileSync(args.path, 'utf8')
19+
+ '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
20+
loader: 'default',
21+
}
22+
})
23+
},
24+
}
25+
26+
await build({
27+
entryPoints: ['./src/index.ts'],
28+
outfile: './build/bundle.js',
29+
bundle: true,
30+
format: 'esm',
31+
platform: 'node',
32+
sourcemap: true,
33+
minify: false,
34+
plugins: [spinPlugin, SourceMapPlugin],
35+
logLevel: 'error',
36+
loader: {
37+
'.ts': 'ts',
38+
'.tsx': 'tsx',
39+
},
40+
resolveExtensions: ['.ts', '.tsx', '.js'],
41+
// This prevents sourcemaps from traversing into node_modules
42+
sourceRoot: path.resolve(process.cwd(), 'src'),
43+
});

templates/http-ts/content/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "{{project-description}}",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm",
7+
"build": "node build.mjs && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
@@ -14,8 +14,7 @@
1414
"mkdirp": "^3.0.1",
1515
"ts-loader": "^9.4.1",
1616
"typescript": "^4.8.4",
17-
"webpack": "^5.74.0",
18-
"webpack-cli": "^4.10.0"
17+
"esbuild": "^0.25.8"
1918
},
2019
"dependencies": {
2120
{%- case http-router -%}

templates/http-ts/content/webpack.config.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

test/test-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"keywords": [],
88
"license": "Apache-2.0",
99
"scripts": {
10-
"build": "node build.mjs && mkdirp dist && j2w -i build/bundle.js -o dist/test-app.wasm",
10+
"build": "node build.mjs && mkdirp dist && j2w -d -i build/bundle.js -o dist/test-app.wasm",
1111
"test": "echo \"Error: no test specified\" && exit 1"
1212
},
1313
"devDependencies": {

test/test-app/spin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ component = "test-app"
1313
[component.test-app]
1414
source = "dist/test-app.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ["http://localhost:3000"]
16+
allowed_outbound_hosts = ["http://localhost:3000", "tcp://127.0.0.1:*"]
1717
key_value_stores = ["default"]
1818
[component.test-app.build]
1919
command = ["npm install", "npm run build"]

test/test-app/src/test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const decoder = new TextDecoder()
55
const streamingChunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
66

77
function health(req: Request) {
8-
return new Response("Healthy", { status: 200 })
8+
let a = 5;
9+
console.log(a)
10+
return new Response("Healthy" + a, { status: 200 })
911
}
1012

1113
function stream(req: Request) {

0 commit comments

Comments
 (0)