Skip to content

Commit

Permalink
Support template-only imports
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Feb 12, 2022
1 parent 7e5b1fe commit 59b64ba
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/addon-dev/src/rollup-hbs-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createFilter } from '@rollup/pluginutils';
import type { Plugin } from 'rollup';
import { readFileSync } from 'fs';
import path from 'path';

import type { Plugin } from 'rollup';

const backtick = '`';

export default function rollupHbsPlugin(): Plugin {
Expand All @@ -19,5 +22,40 @@ export default function rollupHbsPlugin(): Plugin {
id: id + '.js',
};
},

// template-only components may be imported
async resolveId(source, importer, options) {
console.log(options.isEntry, source);

if (!options.isEntry) {
let isLocal = source.startsWith('.');
let hasExt = path.extname(source);

// imports with extensions will resolve automatically
if (!isLocal || hasExt) return;
}

// for relative paths inside our own package, first try normal resolving.
// This is calling https://www.rollupjs.org/guide/en/#thisresolve
let resolution = await this.resolve(source, importer, {
...options,
skipSelf: true,
});

if (resolution) {
return resolution;
}

// only if that doesn't work, try to rewrite to .hbs
resolution = await this.resolve(`${source}.hbs`, importer, {
isEntry: true,
});

if (resolution) {
return resolution;
}

return null;
},
};
}

0 comments on commit 59b64ba

Please sign in to comment.