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

Backport #1642 to stable: Allow for more flexible addon-dev appReexports #1777

Merged
merged 2 commits into from
Feb 1, 2024
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
14 changes: 10 additions & 4 deletions packages/addon-dev/src/rollup-app-reexports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function appReexports(opts: {
to: string;
include: string[];
mapFilename?: (filename: string) => string;
exports?: (filename: string) => string[] | string | undefined;
}): Plugin {
return {
name: 'app-reexports',
Expand All @@ -16,6 +17,12 @@ export default function appReexports(opts: {
let appJS: Record<string, string> = {};
for (let addonFilename of Object.keys(bundle)) {
let appFilename = opts.mapFilename?.(addonFilename) ?? addonFilename;
let appExports = opts.exports?.(addonFilename) || ['default'];

let computedExports =
typeof appExports === 'string'
? appExports
: `{ ${appExports.join(', ')} }`;

if (
opts.include.some((glob) => minimatch(addonFilename, glob)) &&
Expand All @@ -25,10 +32,9 @@ export default function appReexports(opts: {
this.emitFile({
type: 'asset',
fileName: `_app_/${appFilename}`,
source: `export { default } from "${pkg.name}/${addonFilename.slice(
0,
-extname(addonFilename).length
)}";\n`,
source: `export ${computedExports} from "${
pkg.name
}/${addonFilename.slice(0, -extname(addonFilename).length)}";\n`,
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/addon-dev/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export class Addon {
// package.json metadata to list them all.
appReexports(
patterns: string[],
opts: { mapFilename?: (fileName: string) => string } = {}
opts: {
mapFilename?: (fileName: string) => string;
exports?: (filename: string) => string[] | string | undefined;
} = {}
): Plugin {
return appReexports({
from: this.#srcDir,
to: this.#destDir,
include: patterns,
mapFilename: opts.mapFilename,
exports: opts.exports,
});
}

Expand Down
37 changes: 37 additions & 0 deletions tests/scenarios/v2-addon-dev-typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ appScenarios
addon.pkg.scripts = {
build: 'node ./node_modules/rollup/dist/bin/rollup -c ./rollup.config.mjs',
};
addon.pkg['ember-addon']['app-js'] = {
'./components/previously-added.js': './dist/_app_/components/previously-added.js',
};

merge(addon.files, {
'babel.config.json': `
Expand Down Expand Up @@ -91,14 +94,25 @@ appScenarios
plugins: [
addon.publicEntrypoints([
'components/**/*.js',
'initializers/**/*.js',
'utils/**/*.js',
]),

addon.appReexports([
'components/demo/index.js',
'components/demo/out.js',
'components/demo/namespace-me.js',
'initializers/**/*.js',
'utils/**/*.js',
], {
mapFilename: (name) => reexportMappings[name] || name,
exports: (name) => {
if (name.startsWith('initializers/')) {
return ['default', 'initialize'];
} else if (name.startsWith('utils/')) {
return '*';
}
}
}),

addon.dependencies(),
Expand Down Expand Up @@ -167,6 +181,25 @@ appScenarios
`,
},
},
initializers: {
'demo.js': `
export function initialize() {
// Wow, we're doing the init in the app
}

export default {
name: 'demo',
initialize,
};
`,
},
utils: {
'demo-util.js': `
export function demoUtil() {
return 42;
}
`,
},
},
});

Expand Down Expand Up @@ -250,6 +283,8 @@ appScenarios
'./components/demo/index.js': './dist/_app_/components/demo/index.js',
'./components/demo/out.js': './dist/_app_/components/demo/out.js',
'./components/demo/namespace/namespace-me.js': './dist/_app_/components/demo/namespace/namespace-me.js',
'./initializers/demo.js': './dist/_app_/initializers/demo.js',
'./utils/demo-util.js': './dist/_app_/utils/demo-util.js',
});
});

Expand All @@ -260,6 +295,8 @@ appScenarios
'./dist/_app_/components/demo/out.js': 'export { default } from "v2-addon/components/demo/out";\n',
'./dist/_app_/components/demo/namespace/namespace-me.js':
'export { default } from "v2-addon/components/demo/namespace-me";\n',
'./dist/_app_/initializers/demo.js': 'export { default, initialize } from "v2-addon/initializers/demo";\n',
'./dist/_app_/utils/demo-util.js': 'export * from "v2-addon/utils/demo-util";\n',
};

assert.strictEqual(
Expand Down
Loading