Skip to content

Commit 037be50

Browse files
committed
feat(compartment-mapper): json support in bundler
1 parent 054256f commit 037be50

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
runtime: '',
3+
getBundlerKit({ index, record: { sourceText } }) {
4+
return {
5+
getFunctor: () => `\
6+
// === functors[${index}] ===
7+
${sourceText},
8+
`,
9+
getCells: () => `\
10+
{ default: cell('default') },
11+
`,
12+
getReexportsWiring: () => '',
13+
getFunctorCall: () => `\
14+
cells[${index}].default.set(functors[${index}]);
15+
`,
16+
};
17+
},
18+
};

packages/compartment-mapper/src/bundle.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { parseLocatedJson } from './json.js';
2626

2727
import mjsSupport from './bundle-mjs.js';
2828
import cjsSupport from './bundle-cjs.js';
29+
import jsonSupport from './bundle-json.js';
2930

3031
const textEncoder = new TextEncoder();
3132

@@ -130,6 +131,7 @@ const sortedModules = (
130131
const implementationPerParser = {
131132
'pre-mjs-json': mjsSupport,
132133
'pre-cjs-json': cjsSupport,
134+
json: jsonSupport,
133135
};
134136

135137
function getRuntime(parser) {
@@ -141,13 +143,7 @@ function getRuntime(parser) {
141143
function getBundlerKitForModule(module) {
142144
const parser = module.parser;
143145
if (!implementationPerParser[parser]) {
144-
const warning = `/*unknown parser:${parser}*/`;
145-
// each item is a function to avoid creating more in-memory copies of the source text etc.
146-
return {
147-
getFunctor: () => `(()=>{${warning}})`,
148-
getCells: `{${warning}}`,
149-
getFunctorCall: warning,
150-
};
146+
throw Error(`unknown parser:${parser}`);
151147
}
152148
const getBundlerKit = implementationPerParser[parser].getBundlerKit;
153149
return getBundlerKit(module);

packages/compartment-mapper/src/parse-json.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const parseJson = async (
3535
imports,
3636
exports: freeze(['default']),
3737
execute: freeze(execute),
38+
sourceText: source,
3839
}),
3940
};
4041
};

packages/compartment-mapper/test/fixtures-0/node_modules/bundle/main.js

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compartment-mapper/test/test-bundle.js

+3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ const expectedLog = [
4949
fromMjs: 'foo',
5050
zzz: 1,
5151
},
52+
'bundle',
5253
];
5354

55+
// If you're looking at this test hoping to modify it to see bundling results,
56+
// run `yarn dev:livebundle` instead
5457
test('bundles work', async t => {
5558
const bundle = await makeBundle(read, fixture);
5659
const log = [];

0 commit comments

Comments
 (0)