Skip to content
Merged
Show file tree
Hide file tree
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
138 changes: 0 additions & 138 deletions scripts/release/inline-resources.js

This file was deleted.

4 changes: 2 additions & 2 deletions tools/gulp/packaging/build-tasks-gulp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {SOURCE_ROOT, DIST_ROOT, HTML_MINIFIER_OPTIONS} from '../build-config';
import {sequenceTask, sassBuildTask, copyTask, triggerLivereload} from '../util/task_helpers';
import {composeRelease} from './build-release';
import {buildPackageBundles} from './build-bundles';
import {inlineResourcesFolder} from './inline-resources';

// There are no type definitions available for these imports.
const inlineResources = require('../../../scripts/release/inline-resources');
const htmlmin = require('gulp-htmlmin');

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ export function createPackageBuildTasks(packageName: string, requiredPackages: s
return src(htmlGlob).pipe(htmlmin(HTML_MINIFIER_OPTIONS)).pipe(dest(packageOut));
});

task(`${packageName}:assets:inline`, () => inlineResources(packageOut));
task(`${packageName}:assets:inline`, () => inlineResourcesFolder(packageOut));

/**
* Watch tasks, that will rebuild the package whenever TS, SCSS, or HTML files change.
Expand Down
58 changes: 58 additions & 0 deletions tools/gulp/packaging/inline-resources.ts
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about just inlineAllResources?

Copy link
Member Author

@devversion devversion May 31, 2017

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 AllResources sounds like it inlines all resources a given file.

Copy link
Member

@jelbourn jelbourn May 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe inlineAllResourceForDirectory (and then inlineResourcesForFile)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah inlineAllResourceForDirectory sounds good

Copy link
Member Author

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?

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the inlined resources here minified?

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename variables to templatePath and template?

Copy link
Member Author

@devversion devversion May 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

templatePath sounds good. For the template I'd like to keep the content to indicate it's the text.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine too

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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename styleUrls to styleUrlsValue to make it more clear that it's just yet the real array of styleUrl strings. Then prsedUrls below could just change to styleUrls.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I like that.

// The RegExp matches the array of external style files. This is a string right now and
Copy link
Member

Choose a reason for hiding this comment

The 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') + ']';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe refactor this to

const styles = parsedUrls
    .map(url => join(dirname(filePath), url))
    .map(path => loadResourceFile(path));

return `styles: [${styleContents.join(',')}]`;

(the newline shouldn't be necessary AFAICT)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. Not sure about the line break. I didn't write it but I will test it.

});
}

/** 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, '\\"');
}