Skip to content

Commit

Permalink
Merge pull request #1202 from dfreeman/remap-reexports
Browse files Browse the repository at this point in the history
Accept optional `mapFilename` config for `rollup-app-reexports`
  • Loading branch information
ef4 authored May 16, 2022
2 parents c757450 + 0ad415e commit fbbaad7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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

0 comments on commit fbbaad7

Please sign in to comment.