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

Add the 'browser' condition for conditions when resolving #2170

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,13 @@ export class Resolver {
if (pkg.packageJSON.exports) {
let found = resolveExports(pkg.packageJSON, request.specifier, {
browser: true,
conditions: ['default', 'imports'],
/**
* Ideally, these would be exported by vite, but the constant
* that defines client conditions is not exposed to us
*
* See: https://vite.dev/config/shared-options.html#resolve-conditions
*/
conditions: ['import', 'module', 'browser' /*, development | production */, 'default'],
});
if (found?.[0]) {
return logTransition(
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/optimize-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export function optimizeDeps(): OptimizeDeps {
exclude: ['@embroider/macros'],
extensions: ['.hbs', '.gjs', '.gts'],
esbuildOptions: {
// When optimizing deps for development,
// always allow the latest featuers
// (such as top level await)
target: 'esnext',
plugins: [esBuildResolver()],
},
};
Expand Down
19 changes: 18 additions & 1 deletion tests/scenarios/v2-addon-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const { module: Qmodule, test } = QUnit;

appScenarios
.map('v2-addon-basics', project => {
let fakeContentTag = baseV2Addon();
fakeContentTag.pkg.name = 'fake-content-tag';
fakeContentTag.pkg.exports = {
browser: './browser.js',
default: './not-browser.js',
};
fakeContentTag.pkg.files = ['browser.js', 'not-browser.js'];
merge(fakeContentTag.files, {
'browser.js': `export const value = 'browser'`,
'not-browser.js': `export const value = 'not browser'`,
});

let addon = baseV2Addon();
addon.pkg.name = 'v2-addon';
(addon.pkg as any)['ember-addon']['app-js']['./components/example-component.js'] =
Expand Down Expand Up @@ -34,11 +46,16 @@ appScenarios
'import-from-npm.js': `
export default async function() {
let { message } = await import('third-party');
return message()
let { value } = await import('fake-content-tag');

if (value !== 'browser') throw new Error('Incorrect conditions for fake-content-tag');

return message();
}
`,
});

addon.addDependency(fakeContentTag);
addon.addDependency('third-party', {
files: {
'index.js': `
Expand Down
Loading