-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: move inline resources script to tools #4894
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* tslint:disable:no-eval */ | ||
|
|
||
| import {dirname, join} from 'path'; | ||
| import {readFileSync, writeFileSync} from 'fs'; | ||
| import {sync as glob} from 'glob'; | ||
|
|
||
| /** Finds all JavaScript files and inlines all external resources of Angular components. */ | ||
| export function inlineResourcesFolder(folderPath: string) { | ||
| glob(join(folderPath, '**/*.js')).forEach(filePath => inlineResources(filePath)); | ||
| } | ||
|
|
||
| /** Inlines the external resources of Angular components of a file. */ | ||
| export function inlineResources(filePath: string) { | ||
| let fileContent = readFileSync(filePath, 'utf-8'); | ||
|
|
||
| fileContent = inlineTemplate(fileContent, filePath); | ||
| fileContent = inlineStyles(fileContent, filePath); | ||
| fileContent = removeModuleId(fileContent); | ||
|
|
||
| writeFileSync(filePath, fileContent, 'utf-8'); | ||
| } | ||
|
|
||
| /** Inlines the templates of Angular components for a specified source file. */ | ||
| function inlineTemplate(fileContent: string, filePath: string) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the inlined resources here minified?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the minifying happens before by Gulp. |
||
| return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (match, templateUrl) => { | ||
| const templateFile = join(dirname(filePath), templateUrl); | ||
| const templateContent = loadResourceFile(templateFile); | ||
|
||
| return `template: "${templateContent}"`; | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** Inlines the external styles of Angular components for a specified source file. */ | ||
| function inlineStyles(fileContent: string, filePath: string) { | ||
| return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (match, styleUrls) => { | ||
|
||
| // The RegExp matches the array of external style files. This is a string right now and | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expand comment to include something like // styleUrls will be a string that looks something like "['button.css', 'other.css']" |
||
| // can to be parsed using the `eval` method. | ||
| const parsedUrls = eval(styleUrls) as string[]; | ||
|
|
||
| return 'styles: [' + parsedUrls.map(styleUrl => { | ||
| const stylePath = join(dirname(filePath), styleUrl); | ||
| const styleContent = loadResourceFile(stylePath); | ||
| return `"${styleContent}"`; | ||
| }).join(',\n') + ']'; | ||
|
||
| }); | ||
| } | ||
|
|
||
| /** Remove every mention of `moduleId: module.id` */ | ||
| function removeModuleId(fileContent: string) { | ||
| return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, ''); | ||
| } | ||
|
|
||
| /** Loads the specified resource file and drops line-breaks of the content. */ | ||
| function loadResourceFile(filePath: string): string { | ||
| return readFileSync(filePath, 'utf-8') | ||
| .replace(/([\n\r]\s*)+/gm, ' ') | ||
| .replace(/"/g, '\\"'); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about just
inlineAllResources?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that but
AllResourcessounds like it inlines all resources a given file.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then maybe
inlineAllResourceForDirectory(and theninlineResourcesForFile)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
inlineAllResourceForDirectorysounds goodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually why not just
inlineResourcesForDirectory?