Skip to content

Commit

Permalink
Merge pull request #1785 from embroider-build/backport-addondev-exclude
Browse files Browse the repository at this point in the history
Backport #1760 to stable: Add exclude option to appReexports and publicEntrypoints rollup plugins
  • Loading branch information
NullVoxPopuli authored Feb 1, 2024
2 parents 79963f2 + 77f6ed8 commit 062d357
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/addon-dev/src/rollup-app-reexports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function appReexports(opts: {
from: string;
to: string;
include: string[];
exclude?: string[];
mapFilename?: (filename: string) => string;
exports?: (filename: string) => string[] | string | undefined;
}): Plugin {
Expand All @@ -26,7 +27,8 @@ export default function appReexports(opts: {

if (
opts.include.some((glob) => minimatch(addonFilename, glob)) &&
!minimatch(addonFilename, '**/*.d.ts')
!minimatch(addonFilename, '**/*.d.ts') &&
opts.exclude?.some((glob) => minimatch(addonFilename, glob)) !== true
) {
appJS[`./${appFilename}`] = `./dist/_app_/${appFilename}`;
this.emitFile({
Expand Down
2 changes: 2 additions & 0 deletions packages/addon-dev/src/rollup-public-entrypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function normalizeFileExt(fileName: string) {
export default function publicEntrypoints(args: {
srcDir: string;
include: string[];
exclude?: string[];
}): Plugin {
return {
name: 'addon-modules',
Expand All @@ -19,6 +20,7 @@ export default function publicEntrypoints(args: {

let matches = walkSync(args.srcDir, {
globs: [...args.include, '**/*.hbs', '**/*.ts', '**/*.gts', '**/*.gjs'],
ignore: args.exclude,
});

for (let name of matches) {
Expand Down
10 changes: 8 additions & 2 deletions packages/addon-dev/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Addon {
opts: {
mapFilename?: (fileName: string) => string;
exports?: (filename: string) => string[] | string | undefined;
exclude?: string[];
} = {}
): Plugin {
return appReexports({
Expand All @@ -34,14 +35,19 @@ export class Addon {
include: patterns,
mapFilename: opts.mapFilename,
exports: opts.exports,
exclude: opts.exclude,
});
}

// This configures rollup to emit public entrypoints for each module in your
// srcDir that matches one of the given globs. Typical addons will want to
// match patterns like "components/**/*.js", "index.js", and "test-support.js".
publicEntrypoints(patterns: string[]) {
return publicEntrypoints({ srcDir: this.#srcDir, include: patterns });
publicEntrypoints(patterns: string[], opts: { exclude?: string[] } = {}) {
return publicEntrypoints({
srcDir: this.#srcDir,
include: patterns,
exclude: opts.exclude,
});
}

// This wraps standalone .hbs files as Javascript files using inline
Expand Down
20 changes: 17 additions & 3 deletions tests/scenarios/v2-addon-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ appScenarios
plugins: [
addon.publicEntrypoints([
'components/**/*.js',
]),
], {
exclude: ['**/-excluded/**/*'],
}),
addon.appReexports(['components/**/*.js'], {
mapFilename: (name) => reexportMappings[name] || name,
exclude: ['**/-excluded/**/*'],
}),
addon.hbs(),
Expand Down Expand Up @@ -141,6 +144,11 @@ appScenarios
<div data-test="should-transform">{{transformMe}}</div>
`,
},
'-excluded': {
'never-import-this.js': `
throw new Exception('This should never have been imported!');
`,
},
},
},
});
Expand Down Expand Up @@ -241,11 +249,17 @@ appScenarios
});
});

test('the addon was built successfully', async function () {
test('the addon has expected public entrypoints', async function () {
expectFile('dist/components/demo/index.js').exists();
expectFile('dist/components/demo/out.js').exists();
expectFile('dist/components/demo/namespace-me.js').exists();
expectFile('dist/components/-excluded/never-import-this.js').doesNotExist();
});

test('the addon has expected app-reexports', async function () {
expectFile('dist/_app_/components/demo/index.js').matches(
'export { default } from "v2-addon/components/demo/index"'
);

expectFile('dist/_app_/components/demo/out.js').matches(
'export { default } from "v2-addon/components/demo/out"'
);
Expand Down

0 comments on commit 062d357

Please sign in to comment.