-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e5b1fe
commit 3c45615
Showing
2 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,90 @@ | ||
import { createFilter } from '@rollup/pluginutils'; | ||
import type { Plugin } from 'rollup'; | ||
import { pathExists } from 'fs-extra'; | ||
import { readFileSync } from 'fs'; | ||
import path from 'path'; | ||
|
||
import type { Plugin } from 'rollup'; | ||
|
||
const backtick = '`'; | ||
|
||
export default function rollupHbsPlugin(): Plugin { | ||
const filter = createFilter('**/*.hbs'); | ||
|
||
return { | ||
name: 'rollup-hbs-plugin', | ||
load(id: string) { | ||
async load(id: string) { | ||
if (!filter(id)) return; | ||
let input = readFileSync(id, 'utf8'); | ||
let code = | ||
`import { hbs } from 'ember-cli-htmlbars';\n` + | ||
`export default hbs${backtick}${input}${backtick};`; | ||
|
||
if (await isTemplateOnly(id)) { | ||
return { code: getTemplateOnly(id), id }; | ||
} | ||
|
||
// For templates with a backing class | ||
return { | ||
code, | ||
code: getTemplateFactory(id), | ||
id: id + '.js', | ||
}; | ||
}, | ||
|
||
// template-only components may be imported | ||
async resolveId(source, importer, options) { | ||
let resolution = await this.resolve(source, importer, { | ||
...options, | ||
skipSelf: true, | ||
}); | ||
|
||
let ext = path.extname(source); | ||
|
||
// For hbs files, we _might_ have a template-only situation | ||
if (resolution && ext !== '.hbs') return resolution; | ||
if (!importer) return null; | ||
|
||
// source may or may not have '.hbs' on the path | ||
let dir = path.dirname(importer); | ||
let nameWithExt = ext ? source : source + '.hbs'; | ||
let maybeHbs = path.join(dir, nameWithExt); | ||
|
||
resolution = await this.resolve(maybeHbs, importer, { | ||
...options, | ||
skipSelf: true, | ||
}); | ||
|
||
if (resolution) return resolution; | ||
|
||
return null; | ||
}, | ||
}; | ||
} | ||
|
||
async function isTemplateOnly(hbsPath: string) { | ||
let jsPath = hbsPath.replace(/\.hbs$/, '.js'); | ||
let tsPath = hbsPath.replace(/\.hbs$/, '.ts'); | ||
|
||
let hasJs = await pathExists(jsPath); | ||
let hasTs = await pathExists(tsPath); | ||
|
||
let hasClass = hasJs || hasTs; | ||
|
||
return !hasClass; | ||
} | ||
|
||
function getTemplateOnly(hbsPath: string) { | ||
let input = readFileSync(hbsPath, 'utf8'); | ||
let code = | ||
`import { hbs } from 'ember-cli-htmlbars';\n` + | ||
`import templateOnly from '@ember/component/template-only';\n` + | ||
`import { setComponentTemplate } from '@ember/component';\n` + | ||
`export default setComponentTemplate(\n` + | ||
`hbs${backtick}${input.trim()}${backtick}, templateOnly());`; | ||
|
||
return code; | ||
} | ||
|
||
function getTemplateFactory(hbsPath: string) { | ||
let input = readFileSync(hbsPath, 'utf8'); | ||
let code = | ||
`import { hbs } from 'ember-cli-htmlbars';\n` + | ||
`export default hbs${backtick}${input.trim()}${backtick};`; | ||
|
||
return code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters