Skip to content

Commit 9946b4d

Browse files
author
Chris Joel
committed
rename and clang format
1 parent 2fd23e6 commit 9946b4d

8 files changed

+125
-136
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# 📺 HD HTML
2-
1+
# 🍢 HTML Modules Toolkit
32

43
Let's face it: if you publish stuff on the web, you probably noticed that
54
it is getting harder and harder to use HTML to do it. HTML Imports failed to
@@ -12,9 +11,10 @@ HTML has been around for a long time. I mean, sure, just a few decades. But,
1211
that's like a million software years. It is a testament to HTML's versatility
1312
that we still use it for so many things today. The next generation of web
1413
content will need to take advantage of a new generation of HTML capabilities.
15-
HD HTML is a collection of tools and libraries to help web authors write their
16-
apps and content the way they were meant to be written: in highly declarative
17-
HTML. HD HTML unlocks the potential of future HTML standards for the projects of today.
14+
HTML Modules Toolkit is a collection of tools and libraries to help web authors
15+
write their apps and content the way they were meant to be written: in highly
16+
declarative HTML. HTML Modules Toolkit unlocks the potential of future HTML
17+
standards for the projects of today.
1818

1919
## HTML Modules
2020

@@ -30,10 +30,10 @@ might look like once they are standard and implemented by browsers.
3030

3131
## Future HTML today
3232

33-
HD HTML intends to support workflows that incorporate future HTML standards like
34-
HTML Modules. Today, this project includes transforms for incorporating HTML
35-
Modules into build pipelines, and also middleware for using HTML Modules
36-
seamlessly inside of a dev server.
33+
HTML Modules Toolkit intends to support workflows that incorporate future HTML
34+
standards like HTML Modules. Today, this project includes transforms for
35+
incorporating HTML Modules into build pipelines, and also middleware for using
36+
HTML Modules seamlessly inside of a dev server.
3737

3838
The standards that this project builds upon are still at a very early stage,
3939
so these tools should be considered to be in a similar state. We will adapt and

src/document-view.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414

15-
import { parse as parseHtml, serialize as serializeDocument } from 'parse5';
1615
import * as dom from 'dom5';
16+
import {parse as parseHtml, serialize as serializeDocument} from 'parse5';
17+
import {ASTNode} from 'parse5';
1718
import * as File from 'vinyl';
18-
import { ASTNode } from 'parse5';
1919

20-
import { getFileContents } from './file.js';
20+
import {getFileContents} from './file.js';
2121

2222
const p = dom.predicates;
2323

2424
const scripts = p.hasTagName('script');
2525
const external = p.hasAttr('src');
2626
const inline = p.NOT(external);
27-
const standard = p.OR(p.hasAttrValue('type', 'text/javascript'),
28-
p.NOT(p.hasAttr('type')));
27+
const standard =
28+
p.OR(p.hasAttrValue('type', 'text/javascript'), p.NOT(p.hasAttr('type')));
2929
const moduleScript = p.hasAttrValue('type', 'module');
3030
const worker = p.hasAttrValue('type', 'worker');
3131

@@ -38,7 +38,8 @@ export class DocumentView {
3838
return this.fromSourceString(await getFileContents(file));
3939
}
4040

41-
constructor(public document: ASTNode) {}
41+
constructor(public document: ASTNode) {
42+
}
4243

4344
get inlineScripts() {
4445
return dom.queryAll(this.document, p.AND(scripts, inline));
@@ -76,4 +77,3 @@ export class DocumentView {
7677
return serializeDocument(this.document);
7778
}
7879
};
79-

src/file.ts

+34-29
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414

15+
import * as fs from 'fs';
16+
import * as nodePath from 'path';
17+
import {Transform} from 'stream';
18+
import {promisify} from 'util';
1519
import * as File from 'vinyl';
1620
import * as vfs from 'vinyl-fs';
17-
import * as nodePath from 'path';
18-
import * as fs from 'fs';
19-
import { promisify } from 'util';
20-
import { Transform } from 'stream';
2121

2222
const stat = promisify(fs.stat);
2323

@@ -52,9 +52,11 @@ export async function getFileContents(file: File): Promise<string> {
5252

5353
export class SyntheticFileMap {
5454
protected fileMap: Map<string, File> = new Map();
55-
protected watcher: fs.FSWatcher | null = null;
55+
protected watcher: fs.FSWatcher|null = null;
5656

57-
constructor(readonly basePath: string, protected makeTransform: () => Transform) {
57+
constructor(
58+
readonly basePath: string,
59+
protected makeTransform: () => Transform) {
5860
this.watchFilesystem();
5961
}
6062

@@ -63,11 +65,11 @@ export class SyntheticFileMap {
6365
return;
6466
}
6567

66-
this.watcher = fs.watch(this.basePath, {
67-
recursive: true,
68-
persistent: false
69-
}, (eventType: string, filename: string) =>
70-
this.onFsEvent(eventType, filename));
68+
this.watcher = fs.watch(
69+
this.basePath,
70+
{recursive: true, persistent: false},
71+
(eventType: string, filename: string) =>
72+
this.onFsEvent(eventType, filename));
7173
}
7274

7375
stopWatchingFilesystem() {
@@ -91,7 +93,8 @@ export class SyntheticFileMap {
9193
if (!stats.isDirectory()) {
9294
return true;
9395
}
94-
} catch (e) {}
96+
} catch (e) {
97+
}
9598

9699
return false;
97100
}
@@ -108,23 +111,25 @@ export class SyntheticFileMap {
108111

109112
vfs.src([filePath])
110113
.pipe(this.makeTransform())
111-
.on('error', (error) => {
112-
console.error(error);
113-
reject(error);
114-
})
115-
.on('data', (file: File) => {
116-
const newFilename = nodePath.basename(file.path);
117-
// The new filename of the original module will have `.js` appended
118-
// if it originally ended in `.html`. Newly generated files can be
119-
// referenced by their suggested paths.
120-
const storedName = newFilename === `${originalFilename}.js`
121-
? filePath
122-
: file.path;
123-
124-
// NOTE(cdata): A transform may emit more than one file here, as is
125-
// the case for HTML Modules => JS Modules
126-
this.fileMap.set(storedName, file);
127-
})
114+
.on('error',
115+
(error) => {
116+
console.error(error);
117+
reject(error);
118+
})
119+
.on('data',
120+
(file: File) => {
121+
const newFilename = nodePath.basename(file.path);
122+
// The new filename of the original module will have `.js`
123+
// appended if it originally ended in `.html`. Newly generated
124+
// files can be referenced by their suggested paths.
125+
const storedName = newFilename === `${originalFilename}.js` ?
126+
filePath :
127+
file.path;
128+
129+
// NOTE(cdata): A transform may emit more than one file here, as
130+
// is the case for HTML Modules => JS Modules
131+
this.fileMap.set(storedName, file);
132+
})
128133
.on('end', () => {
129134
if (this.fileMap.has(filePath)) {
130135
resolve(this.fileMap.get(filePath));

src/html-module-specifier-transform.ts

+39-46
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414

15-
import * as File from 'vinyl';
1615
import * as dom from 'dom5';
17-
import { Transform } from 'stream';
16+
import {Transform} from 'stream';
17+
import * as File from 'vinyl';
1818

19-
import { getFileContents } from './file.js';
20-
import { transformStream } from './stream.js';
21-
import { DocumentView } from './document-view.js';
22-
import { ScriptView } from './script-view.js';
19+
import {DocumentView} from './document-view.js';
20+
import {getFileContents} from './file.js';
21+
import {ScriptView} from './script-view.js';
22+
import {transformStream} from './stream.js';
2323

2424

2525
export const htmlModuleSpecifierTransform = (): Transform =>
26-
transformStream<File, File>(async (file: File): Promise<File> => {
26+
transformStream<File, File>(async(file: File): Promise<File> => {
2727
if (/.html$/.test(file.path)) {
2828
return await transformSpecifiersInHtmlFile(file);
2929
} else if (/.js$/.test(file.path)) {
@@ -35,59 +35,53 @@ export const htmlModuleSpecifierTransform = (): Transform =>
3535

3636

3737
export const transformSpecifiersInHtmlFile =
38-
async (file: File): Promise<File> => {
39-
const htmlString = transformSpecifiersInHtmlString(
40-
await getFileContents(file));
38+
async(file: File): Promise<File> => {
39+
const htmlString =
40+
transformSpecifiersInHtmlString(await getFileContents(file));
4141

42-
file.contents = Buffer.from(htmlString);
42+
file.contents = Buffer.from(htmlString);
4343

44-
return file;
45-
};
44+
return file;
45+
};
4646

4747

48-
export const transformSpecifiersInJsFile =
49-
async (file: File): Promise<File> => {
50-
const jsString = transformSpecifiersInJsString(
51-
await getFileContents(file));
48+
export const transformSpecifiersInJsFile = async(file: File): Promise<File> => {
49+
const jsString = transformSpecifiersInJsString(await getFileContents(file));
5250

53-
file.contents = Buffer.from(jsString);
51+
file.contents = Buffer.from(jsString);
5452

55-
return file;
56-
};
57-
58-
59-
export const transformSpecifiersInHtmlString =
60-
(htmlString: string): string => {
61-
const documentView = DocumentView.fromSourceString(htmlString);
62-
const {
63-
inlineModuleScripts,
64-
externalModuleScripts
65-
} = documentView;
66-
67-
for (const script of externalModuleScripts) {
68-
const src = dom.getAttribute(script, 'src');
69-
if (/.html$/.test(src)) {
70-
dom.setAttribute(script, 'src', `${src}.js`);
71-
}
72-
}
53+
return file;
54+
};
7355

74-
for (const script of inlineModuleScripts) {
75-
const jsString = transformSpecifiersInJsString(
76-
dom.getTextContent(script));
7756

78-
dom.setTextContent(script, jsString);
79-
}
57+
export const transformSpecifiersInHtmlString = (htmlString: string): string => {
58+
const documentView = DocumentView.fromSourceString(htmlString);
59+
const {inlineModuleScripts, externalModuleScripts} = documentView;
8060

81-
return documentView.toString();
82-
};;
61+
for (const script of externalModuleScripts) {
62+
const src = dom.getAttribute(script, 'src');
63+
if (/.html$/.test(src)) {
64+
dom.setAttribute(script, 'src', `${src}.js`);
65+
}
66+
}
67+
68+
for (const script of inlineModuleScripts) {
69+
const jsString = transformSpecifiersInJsString(dom.getTextContent(script));
70+
71+
dom.setTextContent(script, jsString);
72+
}
73+
74+
return documentView.toString();
75+
};
76+
;
8377

8478

8579
export const transformSpecifiersInJsString = (jsString: string): string => {
8680
const scriptView = ScriptView.fromSourceString(jsString);
87-
const { importDeclarations } = scriptView;
81+
const {importDeclarations} = scriptView;
8882

8983
for (const declaration of importDeclarations) {
90-
const { source } = declaration;
84+
const {source} = declaration;
9185
if (/.html$/.test(source.value)) {
9286
source.value = source.value.replace(/\.html$/, '.html.js');
9387
source.raw = source.raw.replace(/\.html$/, '.html.js');
@@ -96,4 +90,3 @@ export const transformSpecifiersInJsString = (jsString: string): string => {
9690

9791
return scriptView.toString();
9892
};
99-

src/html-module-transform/express-middleware.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414

15+
import {Request, RequestHandler, Response} from 'express';
1516
import * as File from 'vinyl';
16-
import { Request, Response, RequestHandler } from 'express';
1717

18-
import { SyntheticFileMap } from '../file.js';
19-
import { htmlModuleTransform } from '../html-module-transform.js';
18+
import {SyntheticFileMap} from '../file.js';
19+
import {htmlModuleTransform} from '../html-module-transform.js';
2020

2121
export const htmlModulesMiddleware = (root: string = './'): RequestHandler => {
22-
2322
const syntheticFileMap =
2423
new SyntheticFileMap(root, () => htmlModuleTransform());
2524

2625
return async (request: Request, response: Response, next: () => void) => {
27-
const { path } = request;
26+
const {path} = request;
2827
const hasFile = await syntheticFileMap.hasFile(path);
2928

3029
if (!hasFile) {
@@ -46,4 +45,3 @@ export const htmlModulesMiddleware = (root: string = './'): RequestHandler => {
4645
response.send(file.contents);
4746
};
4847
};
49-

src/html-module-transform/koa-middleware.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414

15+
import {Context, Middleware} from 'koa';
1516
import * as File from 'vinyl';
16-
import { Context, Middleware } from 'koa';
1717

18-
import { SyntheticFileMap } from '../file.js';
19-
import { htmlModuleTransform } from '../html-module-transform.js';
18+
import {SyntheticFileMap} from '../file.js';
19+
import {htmlModuleTransform} from '../html-module-transform.js';
2020

2121
export const htmlModulesMiddleware = (root: string = './'): Middleware => {
22-
2322
const syntheticFileMap =
2423
new SyntheticFileMap(root, () => htmlModuleTransform());
2524

2625
return async (ctx: Context, next: Function) => {
27-
const { request, response } = ctx;
28-
const { path } = request;
26+
const {request, response} = ctx;
27+
const {path} = request;
2928
const hasFile = await syntheticFileMap.hasFile(path);
3029

3130
if (!hasFile) {

src/script-view.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ import * as walk from 'acorn/dist/walk';
1717
import * as escodegen from 'escodegen';
1818
import * as File from 'vinyl';
1919

20-
import { getFileContents } from './file.js';
20+
import {getFileContents} from './file.js';
2121

2222
const injectAcornImportMeta = require('acorn-import-meta/inject');
2323

2424
const acorn = injectAcornImportMeta(acornBase);
2525

26-
const {
27-
parse: parseJs
28-
} = acorn;
26+
const {parse: parseJs} = acorn;
2927
const serializeJs = escodegen.generate;
3028

3129
const $verbatim = Symbol('verbatim');
@@ -40,9 +38,7 @@ export class ScriptView {
4038
sourceType: 'module',
4139
ecmaVersion: 9,
4240
allowImportExportEverywhere: true,
43-
plugins: {
44-
importMeta: true
45-
}
41+
plugins: {importMeta: true}
4642
}));
4743
}
4844

@@ -76,9 +72,6 @@ export class ScriptView {
7672
}
7773

7874
toString() {
79-
return serializeJs(this.script, <any>{
80-
verbatim: $verbatim
81-
});
75+
return serializeJs(this.script, <any>{verbatim: $verbatim});
8276
}
8377
};
84-

0 commit comments

Comments
 (0)