-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1836 from BlueCutOfficial/replace-content-for-wit…
…h-valid-html Replace content-for using a Vite plugin
- Loading branch information
Showing
10 changed files
with
277 additions
and
5 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
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Plugin from 'broccoli-plugin'; | ||
import type { Node } from 'broccoli-node-api'; | ||
import { readFileSync } from 'fs-extra'; | ||
import { join } from 'path'; | ||
|
||
export default class ContentForConfig extends Plugin { | ||
// The object keys are the content types and each value is the HTML | ||
// code that should replace the corresponding {{content-for}} | ||
// Example: { body: '<p>This snippet replaces content-for \"body\" in the app index.html</p>' } | ||
private contentFor: any; | ||
|
||
private defaultContentForTypes = [ | ||
'head', | ||
'test-head', | ||
'head-footer', | ||
'test-head-footer', | ||
'body', | ||
'test-body', | ||
'body-footer', | ||
'test-body-footer', | ||
'config-module', | ||
'app-boot', | ||
]; | ||
|
||
constructor(configTree: Node, private options: any) { | ||
super([configTree], { | ||
annotation: 'embroider:content-for-config', | ||
persistentOutput: true, | ||
needsCache: false, | ||
}); | ||
} | ||
|
||
readContents() { | ||
if (!this.contentFor) { | ||
throw new Error(`ContentForConfig not available until after the build`); | ||
} | ||
return this.contentFor; | ||
} | ||
|
||
build() { | ||
if (!this.contentFor) this.contentFor = {}; | ||
const availableContentForTypes = this.options.availableContentForTypes ?? []; | ||
const extendedContentTypes = new Set([...this.defaultContentForTypes, ...availableContentForTypes]); | ||
|
||
let appConfig = this.getAppConfig(); | ||
appConfig.forEach((configPath: { file: string; json: any }) => { | ||
extendedContentTypes.forEach(contentType => { | ||
const matchExp = this.options.pattern.match; | ||
if (!this.contentFor[configPath.file]) this.contentFor[configPath.file] = {}; | ||
if (!this.contentFor[configPath.file][contentType]) { | ||
let contents = this.options.pattern.replacement.call(null, configPath.json, matchExp, contentType); | ||
this.contentFor[configPath.file][contentType] = contents; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
getAppConfig() { | ||
return this.options.configPaths.map((configPath: { file: string; path: string }) => { | ||
let config = readFileSync(join(this.inputPaths[0], configPath.path), { encoding: 'utf8' }); | ||
return { | ||
file: configPath.file, | ||
json: JSON.parse(config), | ||
}; | ||
}); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Plugin } from 'vite'; | ||
import { readJSONSync } from 'fs-extra'; | ||
import { join } from 'path'; | ||
import { locateEmbroiderWorkingDir } from '@embroider/core'; | ||
|
||
export function contentFor(): Plugin { | ||
return { | ||
name: 'embroider-content-for', | ||
|
||
transformIndexHtml(html, { path }) { | ||
let config: any = readJSONSync(join(locateEmbroiderWorkingDir(process.cwd()), 'content-for.json')); | ||
let contentsForConfig = config[path]; | ||
for (const [contentType, htmlContent] of Object.entries(contentsForConfig)) { | ||
html = html.replace(`{{content-for "${contentType}"}}`, `${htmlContent}`); | ||
} | ||
return html; | ||
}, | ||
}; | ||
} |
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
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
Oops, something went wrong.