Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: rollup-hbs-plugin add resolveId hook #1171

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions packages/addon-dev/src/rollup-hbs-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,48 @@ import { readFileSync } from 'fs';
const backtick = '`';

export default function rollupHbsPlugin(): Plugin {
const filter = createFilter('**/*.hbs');
const templateFilter = createFilter('**/*.hbs');
const moduleFilter = createFilter('**/*.hbs.js');

return {
name: 'rollup-hbs-plugin',
async resolveId(source: string, importer: string | undefined, options) {
const resolution = await this.resolve(source, importer, {
skipSelf: true,
...options,
});

const id = resolution?.id;

if (!templateFilter(id)) return null;
wondersloth marked this conversation as resolved.
Show resolved Hide resolved
wondersloth marked this conversation as resolved.
Show resolved Hide resolved

// This creates an `*.hbs.js` that we will populate in `load()` hook.
return {
...resolution,
id: id + '.js',
meta: {
'rollup-hbs-plugin': {
originalId: id,
},
},
};
},
load(id: string) {
if (!filter(id)) return;
let input = readFileSync(id, 'utf8');
if (!moduleFilter(id)) return null;

const meta = this.getModuleInfo(id)?.meta;
const originalId = meta?.['rollup-hbs-plugin']?.originalId;

if (!originalId) {
return;
}
wondersloth marked this conversation as resolved.
Show resolved Hide resolved

let input = readFileSync(originalId, 'utf8');
let code =
`import { hbs } from 'ember-cli-htmlbars';\n` +
`export default hbs${backtick}${input}${backtick};`;
return {
code,
id: id + '.js',
};
},
};
Expand Down