diff --git a/packages/addon-dev/src/rollup-hbs-plugin.ts b/packages/addon-dev/src/rollup-hbs-plugin.ts index b93b03e0b..db35a35fd 100644 --- a/packages/addon-dev/src/rollup-hbs-plugin.ts +++ b/packages/addon-dev/src/rollup-hbs-plugin.ts @@ -1,14 +1,13 @@ import { createFilter } from '@rollup/pluginutils'; import type { Plugin, PluginContext, CustomPluginOptions } from 'rollup'; import { readFileSync } from 'fs'; -import { hbsToJS } from '@embroider/core'; +import { correspondingTemplate, hbsToJS } from '@embroider/core'; import minimatch from 'minimatch'; -import { parse as pathParse } from 'path'; export default function rollupHbsPlugin({ - templates, + excludeColocation, }: { - templates?: string[]; + excludeColocation?: string[]; }): Plugin { return { name: 'rollup-hbs-plugin', @@ -26,7 +25,7 @@ export default function rollupHbsPlugin({ source, importer, options, - templates + excludeColocation ); } }, @@ -74,30 +73,22 @@ function getHbsToJSCode(file: string): { code: string } { }; } -function correspondingTemplate(filename: string): string { - let { ext } = pathParse(filename); - return filename.slice(0, filename.length - ext.length) + '.hbs'; -} - async function maybeSynthesizeComponentJS( context: PluginContext, source: string, importer: string | undefined, options: { custom?: CustomPluginOptions; isEntry: boolean }, - templates: string[] | undefined + excludeColocation: string[] | undefined ) { - let templateResolution = await context.resolve( - correspondingTemplate(source), - importer, - { - skipSelf: true, - ...options, - } - ); + let hbsFilename = correspondingTemplate(source); + let templateResolution = await context.resolve(hbsFilename, importer, { + skipSelf: true, + ...options, + }); if (!templateResolution) { return null; } - let type = templates?.some((glob) => minimatch(source, glob)) + let type = excludeColocation?.some((glob) => minimatch(hbsFilename, glob)) ? 'template-js' : 'template-only-component-js'; // we're trying to resolve a JS module but only the corresponding HBS diff --git a/packages/shared-internals/src/index.ts b/packages/shared-internals/src/index.ts index cbf236beb..2cb7f5b42 100644 --- a/packages/shared-internals/src/index.ts +++ b/packages/shared-internals/src/index.ts @@ -1,5 +1,5 @@ export { AppMeta, AddonMeta, PackageInfo } from './metadata'; -export { explicitRelative, extensionsPattern, unrelativize, cleanUrl } from './paths'; +export { explicitRelative, extensionsPattern, unrelativize, cleanUrl, correspondingTemplate } from './paths'; export { getOrCreate } from './get-or-create'; export { default as Package, V2AddonPackage as AddonPackage, V2AppPackage as AppPackage, V2Package } from './package'; export { default as PackageCache } from './package-cache'; diff --git a/packages/shared-internals/src/paths.ts b/packages/shared-internals/src/paths.ts index a1e0ed123..5d5ea6dbe 100644 --- a/packages/shared-internals/src/paths.ts +++ b/packages/shared-internals/src/paths.ts @@ -1,4 +1,4 @@ -import { relative, isAbsolute, dirname, join, basename, resolve, sep } from 'path'; +import { relative, isAbsolute, dirname, join, basename, resolve, sep, parse as pathParse } from 'path'; import type Package from './package'; // by "explicit", I mean that we want "./local/thing" instead of "local/thing" @@ -49,3 +49,10 @@ const postfixRE = /[?#].*$/s; export function cleanUrl(url: string): string { return url.replace(postfixRE, ''); } + +// given a filename, returns it with the hbs extension +// for instance, passing filename.js returns filename.hbs +export function correspondingTemplate(filename: string): string { + let { ext } = pathParse(filename); + return filename.slice(0, filename.length - ext.length) + '.hbs'; +} diff --git a/packages/shared-internals/src/template-colocation-plugin.ts b/packages/shared-internals/src/template-colocation-plugin.ts index 4fcbaab0d..8004dbadb 100644 --- a/packages/shared-internals/src/template-colocation-plugin.ts +++ b/packages/shared-internals/src/template-colocation-plugin.ts @@ -7,7 +7,7 @@ import { explicitRelative, PackageCache } from '.'; import { ImportUtil } from 'babel-import-util'; import makeDebug from 'debug'; import minimatch from 'minimatch'; -import { cleanUrl } from './paths'; +import { cleanUrl, correspondingTemplate } from './paths'; const debug = makeDebug('embroider:template-colocation-plugin'); @@ -75,7 +75,7 @@ export default function main(babel: typeof Babel) { } } - if (state.opts.exclude?.some(glob => minimatch(filename, glob))) { + if (state.opts.exclude?.some(glob => minimatch(correspondingTemplate(filename), glob))) { debug('not handling colocation for %s', filename); return; } diff --git a/tests/scenarios/v2-addon-dev-test.ts b/tests/scenarios/v2-addon-dev-test.ts index a68673073..b08435f93 100644 --- a/tests/scenarios/v2-addon-dev-test.ts +++ b/tests/scenarios/v2-addon-dev-test.ts @@ -30,7 +30,7 @@ appScenarios { "plugins": [ ["@embroider/addon-dev/template-colocation-plugin", { - exclude: ['**/just-a-template.js'], + exclude: ['**/just-a-template.hbs'], }], "@babel/plugin-transform-class-static-block", ["babel-plugin-ember-template-compilation", { @@ -74,7 +74,7 @@ appScenarios }), addon.hbs({ - templates: ['**/just-a-template.js'], + excludeColocation: ['**/just-a-template.hbs'], }), addon.gjs(), addon.dependencies(),