Skip to content

Commit bca9766

Browse files
committed
Flatten fromSources
1 parent a178419 commit bca9766

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,11 @@ import { bundleFile } from 'subscript/util/bundle.js'
143143
console.log(await bundleFile('jessie.js'))
144144

145145
// Browser / custom sources
146-
import { bundle, fromSources } from 'subscript/util/bundle.js'
147-
const read = fromSources({
146+
import { bundle } from 'subscript/util/bundle.js'
147+
console.log(await bundle('main.js', {
148148
'main.js': `import { x } from './lib.js'; export default x * 2`,
149149
'lib.js': `export const x = 21`
150-
})
151-
console.log(await bundle('main.js', read))
150+
}))
152151
// → "const x = 21;\nexport { x as default }"
153152
```
154153

todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595

9696
- [x] ~~Warn Jessie on bad ASI~~ no complaints
9797

98-
- [ ] Better error pointers
98+
- [x] Better error pointers
9999

100100
- [ ] Bundle itself instead of esbuild
101101

util/bundle.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,17 @@ const resolvePath = (from, to) => {
292292
/**
293293
* Bundle ES modules into single file
294294
* @param {string} entry - Entry file path
295-
* @param {(path: string) => string|Promise<string>} read - File reader
295+
* @param {Object|Function} read - Source map {path: code} or reader function
296296
*/
297297
export async function bundle(entry, read) {
298+
// Accept object as source map
299+
if (typeof read === 'object') {
300+
const sources = read;
301+
read = path => {
302+
if (!(path in sources)) throw Error(`Module not found: ${path}`);
303+
return sources[path];
304+
};
305+
}
298306
const modules = new Map();
299307
const order = [];
300308

@@ -485,16 +493,6 @@ export async function bundle(entry, read) {
485493
return result;
486494
}
487495

488-
/**
489-
* Create a read function from a map of path → source code
490-
* @param {Object<string, string>} sources - Map of path to source code
491-
* @returns {(path: string) => string}
492-
*/
493-
export const fromSources = sources => path => {
494-
if (!(path in sources)) throw Error(`Module not found: ${path}`);
495-
return sources[path];
496-
};
497-
498496
/**
499497
* Bundle with Node.js fs (only available in Node.js environment)
500498
* Dynamically imports fs/path to avoid browser errors

0 commit comments

Comments
 (0)