From 3718cf3fe44815e16ccd22ffba9c0d9e3f60d68b Mon Sep 17 00:00:00 2001 From: Marine Dunstetter Date: Tue, 26 Mar 2024 13:48:31 +0100 Subject: [PATCH] feat(compile route templates): skip template-colocation-plugin for route templates --- packages/shared-internals/package.json | 2 ++ .../src/template-colocation-plugin.ts | 14 ++++++++++++++ pnpm-lock.yaml | 9 +++++++++ tests/scenarios/v2-addon-dev-test.ts | 8 ++++---- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/shared-internals/package.json b/packages/shared-internals/package.json index 24b503ff1..3c8509318 100644 --- a/packages/shared-internals/package.json +++ b/packages/shared-internals/package.json @@ -36,6 +36,7 @@ "typescript-memoize": "^1.0.1", "fs-extra": "^9.1.0", "lodash": "^4.17.21", + "minimatch": "^3.0.4", "semver": "^7.3.5" }, "devDependencies": { @@ -47,6 +48,7 @@ "@types/fs-extra": "^9.0.12", "@types/lodash": "^4.14.170", "@types/js-string-escape": "^1.0.0", + "@types/minimatch": "^3.0.4", "@types/semver": "^7.3.6", "@types/tmp": "^0.1.0", "fixturify": "^2.1.1", diff --git a/packages/shared-internals/src/template-colocation-plugin.ts b/packages/shared-internals/src/template-colocation-plugin.ts index 52f830b1e..4fcbaab0d 100644 --- a/packages/shared-internals/src/template-colocation-plugin.ts +++ b/packages/shared-internals/src/template-colocation-plugin.ts @@ -6,6 +6,7 @@ import { dirname } from 'path'; import { explicitRelative, PackageCache } from '.'; import { ImportUtil } from 'babel-import-util'; import makeDebug from 'debug'; +import minimatch from 'minimatch'; import { cleanUrl } from './paths'; const debug = makeDebug('embroider:template-colocation-plugin'); @@ -36,6 +37,14 @@ export interface Options { // This option is used by Embroider itself to help with v1 addon // compatibility, other users should probably not use it. templateExtensions?: string[]; + + // Default to [] + // + // Skip the plugin for files that match the specified globs. + // + // This option is used to prevent the plugin to transform the + // compiled output of hbs files that are not colocated components. + exclude?: string[]; } interface State { @@ -66,6 +75,11 @@ export default function main(babel: typeof Babel) { } } + if (state.opts.exclude?.some(glob => minimatch(filename, glob))) { + debug('not handling colocation for %s', filename); + return; + } + debug('handling colocation for %s', filename); let extensions = state.opts.templateExtensions ?? ['.hbs']; for (let ext of extensions) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1e36604ce..d4e95e206 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -683,6 +683,9 @@ importers: lodash: specifier: ^4.17.21 version: 4.17.21 + minimatch: + specifier: ^3.0.4 + version: 3.1.2 resolve-package-path: specifier: ^4.0.1 version: 4.0.3 @@ -714,6 +717,9 @@ importers: '@types/lodash': specifier: ^4.14.170 version: 4.14.202 + '@types/minimatch': + specifier: ^3.0.4 + version: 3.0.5 '@types/semver': specifier: ^7.3.6 version: 7.5.8 @@ -8374,6 +8380,9 @@ packages: /ajv-formats@2.1.1: resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==} + peerDependenciesMeta: + ajv: + optional: true dependencies: ajv: 8.12.0 diff --git a/tests/scenarios/v2-addon-dev-test.ts b/tests/scenarios/v2-addon-dev-test.ts index 280265319..75eac364b 100644 --- a/tests/scenarios/v2-addon-dev-test.ts +++ b/tests/scenarios/v2-addon-dev-test.ts @@ -29,7 +29,9 @@ appScenarios 'babel.config.json': ` { "plugins": [ - "@embroider/addon-dev/template-colocation-plugin", + ["@embroider/addon-dev/template-colocation-plugin", { + exclude: ['**/just-a-template.js'], + }], "@babel/plugin-transform-class-static-block", ["babel-plugin-ember-template-compilation", { targetFormat: 'hbs', @@ -286,9 +288,7 @@ appScenarios expectFile( 'dist/components/demo/just-a-template.js' ).equalsCode(`import { precompileTemplate } from '@ember/template-compilation'; -import { setComponentTemplate } from '@ember/component'; -var TEMPLATE = precompileTemplate("

I am not a component but a template.

"); -var justATemplate = setComponentTemplate(TEMPLATE, precompileTemplate("

I am not a component but a template.

")); +var justATemplate = precompileTemplate("

I am not a component but a template.

"); export { justATemplate as default }; //# sourceMappingURL=just-a-template.js.map`); });