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

Accept optional mapFilename config for rollup-app-reexports #1202

Merged
merged 1 commit into from
May 16, 2022
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
15 changes: 9 additions & 6 deletions packages/addon-dev/src/rollup-app-reexports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ export default function appReexports(opts: {
from: string;
to: string;
include: string[];
mapFilename?: (filename: string) => string;
}): Plugin {
return {
name: 'app-reexports',
generateBundle(_, bundle) {
let pkg = readJsonSync('package.json');
let appJS: Record<string, string> = {};
for (let filename of Object.keys(bundle)) {
for (let addonFilename of Object.keys(bundle)) {
let appFilename = opts.mapFilename?.(addonFilename) ?? addonFilename;

if (
opts.include.some((glob) => minimatch(filename, glob)) &&
!minimatch(filename, '**/*.d.ts')
opts.include.some((glob) => minimatch(addonFilename, glob)) &&
!minimatch(addonFilename, '**/*.d.ts')
) {
appJS[`./${filename}`] = `./dist/_app_/${filename}`;
appJS[`./${appFilename}`] = `./dist/_app_/${appFilename}`;
this.emitFile({
type: 'asset',
fileName: `_app_/${filename}`,
source: `export { default } from "${pkg.name}/${filename}";\n`,
fileName: `_app_/${appFilename}`,
source: `export { default } from "${pkg.name}/${addonFilename}";\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 @@ -18,11 +18,15 @@ export class Addon {
// Given a list of globs describing modules in your srcDir, this generates
// corresponding appTree modules that contain reexports, and updates your
// package.json metadata to list them all.
appReexports(patterns: string[]): Plugin {
appReexports(
patterns: string[],
opts: { mapFilename?: (fileName: string) => string } = {}
): Plugin {
return appReexports({
from: this.#srcDir,
to: this.#destDir,
include: patterns,
mapFilename: opts.mapFilename,
});
}

Expand Down
19 changes: 18 additions & 1 deletion tests/scenarios/v2-addon-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ appScenarios
destDir: 'dist',
});

const reexportMappings = {
'components/demo/namespace-me.js': 'components/demo/namespace/namespace-me.js',
};

export default {
output: addon.output(),

Expand All @@ -62,7 +66,10 @@ appScenarios
addon.appReexports([
'components/demo/index.js',
'components/demo/out.js',
]),
'components/demo/namespace-me.js',
], {
mapFilename: (name) => reexportMappings[name] || name,
}),

addon.hbs(),
addon.dependencies(),
Expand All @@ -84,6 +91,9 @@ appScenarios
'out.hbs': `
<out>{{yield}}</out>
`,
'namespace-me.hbs': `
namespaced component
`,
'index.js': `
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
Expand Down Expand Up @@ -149,6 +159,12 @@ appScenarios

assert.dom('out').containsText('hi');
});

test('<Demo::Namespace::NamespaceMe />', async function (assert) {
await render(hbs\`<Demo::Namespace::NamespaceMe />\`);

assert.dom().containsText('namespaced component');
});
});
`,
},
Expand Down Expand Up @@ -180,6 +196,7 @@ appScenarios
assert.deepEqual(reExports, {
'./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',
});
});

Expand Down