Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix domain sharding for ESM output #292

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion packages/core/integration-tests/test/domain-sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import assert from 'assert';
import path from 'path';

import {fsFixture, overlayFS, bundle} from '@atlaspack/test-utils';
import {fsFixture, overlayFS, bundle, run} from '@atlaspack/test-utils';
import {domainShardingKey} from '@atlaspack/domain-sharding';

const maxShards = 8;

Expand Down Expand Up @@ -120,5 +121,70 @@ describe('domain-sharding', () => {
'Expected generated code for shardUrl was not found',
);
});

it('for ESM loaded bundle manifest', async () => {
const maxShards = 8;
await fsFixture(overlayFS)`
package.json:
{
"name": "bundle-sharding-test",
"@atlaspack/runtime-js": {
"domainSharding": {
"maxShards": ${maxShards}
}
}
}

src/index.js:
async function fn() {
const a = await import('./a.js');
const b = await import('./b.js');
console.log('a', a, b);
}
fn();

src/a.js:
export const a = async () => {
const b = await import('./b');
return b + 'A';
}
src/b.js:
export const b = 'B';

yarn.lock:
`;

const bundleGraph = await bundle('src/index.js', {
inputFS: overlayFS,
mode: 'production',
defaultTargetOptions: {
shouldOptimize: false,
outputFormat: 'esmodule',
},
});

const mainBundle = bundleGraph
.getBundles()
.find((b) => b.name === 'index.js');

if (!mainBundle) {
return assert(mainBundle);
}

// $FlowFixMe - Flow doesn't seem to know about doesNotReject
await assert.doesNotReject(
run(bundleGraph, {[domainShardingKey]: true}),
'Expected bundle to be able to execute',
);

const code = await overlayFS.readFile(mainBundle.filePath, 'utf-8');

const expectedCode = `var$load = (parcelRequire("cfSxn"))(${maxShards});`
.replaceAll('$', '\\$')
.replaceAll('(', '\\(')
.replaceAll(')', '\\)');

assert.match(code, new RegExp(expectedCode));
});
});
});
6 changes: 5 additions & 1 deletion packages/core/test-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,11 @@ export async function runESM(

function load(inputSpecifier, referrer, code = null) {
// ESM can request bundles with an absolute URL. Normalize this to the baseDir.
let specifier = inputSpecifier.replace('http://localhost', baseDir);
// Any digits after the - can be ignored, for domain sharding tests
let specifier = inputSpecifier.replace(
/http:\/\/localhost(-\d+)?/,
baseDir,
);

if (path.isAbsolute(specifier) || specifier.startsWith('.')) {
let extname = path.extname(specifier);
Expand Down
6 changes: 5 additions & 1 deletion packages/runtimes/js/src/JSRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,11 @@ function getLoaderRuntime({
let code = [];

if (needsEsmLoadPrelude) {
code.push(`let load = require('./helpers/browser/esm-js-loader');`);
let preludeLoad = shardingConfig
? `let load = require('./helpers/browser/esm-js-loader-shards')(${shardingConfig.maxShards});`
: `let load = require('./helpers/browser/esm-js-loader');`;

code.push(preludeLoad);
}

code.push(`module.exports = ${loaderCode};`);
Expand Down
11 changes: 11 additions & 0 deletions packages/runtimes/js/src/helpers/browser/esm-js-loader-shards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let load = (maxShards) => (id) => {
// eslint-disable-next-line no-undef
return __parcel__import__(
require('@atlaspack/domain-sharding').shardUrl(
require('../bundle-manifest').resolve(id),
maxShards,
),
);
};

module.exports = load;
Loading