Skip to content

Commit

Permalink
feat(compile route templates): improve API so users can specify their…
Browse files Browse the repository at this point in the history
… hbs files instead of js that don't exist in their codebase
  • Loading branch information
BlueCutOfficial committed Apr 3, 2024
1 parent e870de5 commit 566925f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
31 changes: 11 additions & 20 deletions packages/addon-dev/src/rollup-hbs-plugin.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -26,7 +25,7 @@ export default function rollupHbsPlugin({
source,
importer,
options,
templates
excludeColocation
);
}
},
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-internals/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
9 changes: 8 additions & 1 deletion packages/shared-internals/src/paths.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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';
}
4 changes: 2 additions & 2 deletions packages/shared-internals/src/template-colocation-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/scenarios/v2-addon-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down Expand Up @@ -74,7 +74,7 @@ appScenarios
}),
addon.hbs({
templates: ['**/just-a-template.js'],
excludeColocation: ['**/just-a-template.hbs'],
}),
addon.gjs(),
addon.dependencies(),
Expand Down

0 comments on commit 566925f

Please sign in to comment.