Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.0-alpha.8

- fix(dev-tools): preserve external workspace packages in `ocular-bundle` (#56)

## v2.0.0-alpha.7

- fix(dev-tools): parse `ocular-bundle` entry points, targets and externals correctly (#52)
Expand Down
36 changes: 34 additions & 2 deletions modules/dev-tools/src/configuration/get-esbuild-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ function getExternalGlobalsIIFE(externalPackages: string[], mapping: Record<stri
return externals;
}

/**
* Removes workspace aliases that would otherwise take precedence over esbuild externals.
* @param aliases Ocular aliases resolved for the current monorepo.
* @param externalPackages Package names that must remain external to the bundle.
*/
function removeExternalAliases(
aliases: Record<string, string>,
externalPackages: string[]
): Record<string, string> {
const filteredAliases = {...aliases};

for (const externalPackage of externalPackages) {
const packageName = externalPackage.endsWith('/*')
? externalPackage.slice(0, -2)
: externalPackage;
if (packageName.includes('*')) {
continue;
}

for (const alias of Object.keys(filteredAliases)) {
if (alias === packageName || alias.startsWith(`${packageName}/`)) {
delete filteredAliases[alias];
Comment on lines +59 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove parent aliases for externalized subpaths

When a caller externalizes only a package subpath, such as --externals=@scope/pkg/worker, this condition leaves the generated parent alias @scope/pkg intact. That parent alias also rewrites imports of @scope/pkg/worker, and because esbuild applies alias substitutions before its other path-resolution logic, the requested external rule never sees the original package path and the subpath is bundled. Remove aliases that are ancestors of an external package path as well.

Useful? React with 👍 / 👎.

}
}
}

return filteredAliases;
}

// esbuild does not support umd format
// Work around from https://github.com/evanw/esbuild/issues/819
// Template: https://webpack.js.org/configuration/output/#type-umd
Expand Down Expand Up @@ -97,6 +126,7 @@ export type BundleOptions = {
globals?: {[pattern: string]: string};
debug?: boolean;
sourcemap?: boolean;
sourcesContent?: boolean;
watch?: boolean;
};

Expand Down Expand Up @@ -154,7 +184,8 @@ export async function getBundleConfig(opts: BundleOptions): Promise<BuildOptions
externals,
globalName,
debug,
sourcemap = false
sourcemap = false,
sourcesContent = true
} = opts;

const normalizedExternals =
Expand All @@ -170,11 +201,12 @@ export async function getBundleConfig(opts: BundleOptions): Promise<BuildOptions
// @ts-expect-error umd is not supported by esbuild, will be overwritten below
format,
minify: !devMode,
alias: ocularConfig.aliases,
alias: removeExternalAliases(ocularConfig.aliases, externalPackages),
platform: 'browser',
target,
logLevel: 'info',
sourcemap,
sourcesContent,
plugins: []
};
if (globalName) {
Expand Down
22 changes: 19 additions & 3 deletions modules/dev-tools/test/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ test('dev-tools#parseBundleArguments normalizes CLI values', () => {
'--externals=@vis.gl/tangram-renderer,zod',
'--target=chrome110,safari15',
'--sourcemap',
'--sourcesContent=false',
'--debug=false'
])
).toEqual({
Expand All @@ -92,6 +93,7 @@ test('dev-tools#parseBundleArguments normalizes CLI values', () => {
externals: ['@vis.gl/tangram-renderer', 'zod'],
target: ['chrome110', 'safari15'],
sourcemap: true,
sourcesContent: false,
debug: false
});
});
Expand All @@ -106,11 +108,15 @@ test('dev-tools#getBundleConfig passes normalized externals to esbuild', async (
const config = await getBundleConfig({
input: './bundle.ts',
format: 'esm',
externals: ['@vis.gl/tangram-renderer', 'zod']
externals: ['@vis.gl/dev-tools', 'zod'],
sourcesContent: false
});

expect(config.entryPoints).toEqual(['./bundle.ts']);
expect(config.external).toEqual(['@vis.gl/tangram-renderer', 'zod']);
expect(config.external).toEqual(['@vis.gl/dev-tools', 'zod']);
expect(config.sourcesContent).toBe(false);
expect(config.alias).not.toHaveProperty('@vis.gl/dev-tools');
expect(config.alias).not.toHaveProperty('@vis.gl/dev-tools/test');
});

test('ocular-bundle CLI bundles the requested entry point', () => {
Expand All @@ -122,7 +128,16 @@ test('ocular-bundle CLI bundles the requested entry point', () => {
path.join(fixtureDirectory, 'package.json'),
JSON.stringify({name: 'ocular-bundle-fixture', type: 'module'})
);
writeFileSync(path.join(fixtureDirectory, '.ocularrc.js'), 'export default {};\n');
writeFileSync(
path.join(fixtureDirectory, '.ocularrc.js'),
`export default {aliases: {'external-package': ${JSON.stringify(
path.join(fixtureDirectory, 'aliased-external.js')
)}}};\n`
);
writeFileSync(
path.join(fixtureDirectory, 'aliased-external.js'),
"export default 'incorrectly bundled';\n"
);
writeFileSync(
path.join(fixtureDirectory, 'entry.js'),
"import externalValue from 'external-package';\nexport const value = `fixture:${externalValue}`;\n"
Expand All @@ -144,6 +159,7 @@ test('ocular-bundle CLI bundles the requested entry point', () => {
const bundle = readFileSync(path.join(fixtureDirectory, 'bundle.js'), 'utf8');
expect(bundle).toContain('fixture:');
expect(bundle).toContain('from "external-package"');
expect(bundle).not.toContain('incorrectly bundled');
expect(bundle).not.toContain('@vis.gl/dev-tools/scripts/bundle.js');
} finally {
rmSync(fixtureDirectory, {recursive: true, force: true});
Expand Down
Loading