Skip to content

Commit e30222c

Browse files
author
Guy Bedford
committed
better error for incorrect import specifiers
1 parent a4e66bc commit e30222c

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/componentize.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export async function componentize(jsSource, witWorld, opts) {
134134
curIdx = 0;
135135
for (const jsImpt of jsImports) {
136136
if (jsImpt.t !== 1 && jsImpt.t !== 2) continue;
137+
if (!jsImpt.n) continue;
138+
if (!imports.some(([impt]) => jsImpt.n === impt)) {
139+
throw new Error(`Import '${jsImpt.n}' is not defined by the WIT world. Make sure to use a bundler for JS dependencies such as esbuild or RollupJS. Future ComponentizeJS versions may include Node.js builtins and bundling.`);
140+
}
137141
const specifier = jsSource.slice(jsImpt.s, jsImpt.e);
138142
source += jsSource.slice(curIdx, jsImpt.s);
139143
source += `./${specifier.replace(':', '__').replace('/', '$')}.js`;

test/cases/bad-binding/source.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import blah from 'not:world-defined';

test/cases/bad-binding/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { strictEqual } from 'node:assert';
2+
3+
export function err (error) {
4+
strictEqual(error.message, "Import 'not:world-defined' is not defined by the WIT world. Make sure to use a bundler for JS dependencies such as esbuild or RollupJS. Future ComponentizeJS versions may include Node.js builtins and bundling.");
5+
}

test/cases/bad-binding/world.wit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package local:%char;
2+
3+
interface chars {
4+
/// A function that returns a character
5+
return-char: func() -> char;
6+
/// A function that accepts a character
7+
take-char: func(x: char);
8+
}
9+
10+
world the-world {
11+
import chars;
12+
}

0 commit comments

Comments
 (0)