Skip to content

Commit 0493b92

Browse files
authored
feat(browser): support browsers as well as node (#8)
feat(browser): support browsers as well as node For use in the browser, either load typed-html as a module, or use a bundler like webpack or rollup to bundle the package for you. ```ts // Direct ES import: import * as elements from './node_modules/typed-html/dist/elements.js'; // OR, when using a bundler like rollup or webpack import * as elements from 'typed-html'; ``` BREAKING CHANGE: emitted new lines in strings are no longer platform dependent. `\n` is now always used.
1 parent bdc4c15 commit 0493b92

File tree

11 files changed

+143
-118
lines changed

11 files changed

+143
-118
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
node_modules
2+
/dist
23
*.js
34
*.d.ts
45
*.map
5-
*.tsbuildinfo
6+
*.tsbuildinfo
7+
!src/jsx/*.d.ts

.npmignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
**/**
2-
!readme.md
3-
!src/**/*.js
4-
!src/**/*.d.ts
2+
!dist/*.?(js|d.ts)
3+
!README.md
4+
!src/**/*.?(js|d.ts)

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"**/*?.js": {
1111
"when": "$(basename).tsx"
1212
},
13-
"**/*.d.ts": true,
13+
"*/*.d.ts": true,
14+
"src/jsx/*.d.ts": false,
1415
"**/*.map": true,
1516
"**/*.tsbuildinfo": true
1617
},

readme.md renamed to README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ However, the following piece of code will **NOT** compile:
7979
<a foo="bar"></a>; // => Error: Property 'foo' does not exist on type 'HtmlAnchorTag'
8080
```
8181

82+
## Supported environments
83+
84+
Typed HTML supports both NodeJS and (since 2.0) the browser.
85+
86+
For use in the browser, either load typed-html as a module, or use a bundler like webpack or rollup to bundle the package for you.
87+
88+
```ts
89+
// Direct ES import:
90+
import * as elements from './node_modules/typed-html/dist/elements.js';
91+
// OR, when using a bundler like rollup or webpack
92+
import * as elements from 'typed-html';
93+
```
94+
8295
## Supported scenarios
8396

8497
All template scenarios are supported with plain TypeScript.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"description": "TypeSafe HTML templates using TypeScript. No need to learn a template library.",
55
"main": "src/elements.js",
66
"typings": "src/elements.d.ts",
7+
"module": "dist/elements.js",
78
"repository": {
89
"type": "git",
910
"url": "https://github.com/nicojs/typed-html"
1011
},
1112
"scripts": {
12-
"prebuild": "rimraf \"+(test|src)/**/*.+(js|map|tsbuildinfo|d.ts)\" dist",
13-
"build": "tsc -b",
13+
"prebuild": "rimraf \"+(test|src)/*.+(js|map|tsbuildinfo|d.ts)\" dist",
14+
"build": "tsc -b && tsc -p src --outDir dist --module esnext --target esnext",
1415
"pretest": "npm run build",
1516
"test": "mocha",
1617
"preversion": "npm test",

src/elements.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/// <reference path="./jsx/element-types.ts" />
2-
/// <reference path="./jsx/events.ts" />
3-
/// <reference path="./jsx/intrinsic-elements.ts" />
4-
5-
import * as os from 'os';
1+
/// <reference path="./jsx/element-types.d.ts" />
2+
/// <reference path="./jsx/events.d.ts" />
3+
/// <reference path="./jsx/intrinsic-elements.d.ts" />
64

75
type AttributeValue = number | string | Date | boolean;
86

@@ -79,8 +77,8 @@ const attributesToString = (attributes: Attributes | undefined): string => {
7977
const contentsToString = (contents: any[] | undefined) => {
8078
if (contents) {
8179
return contents
82-
.map(elements => Array.isArray(elements) ? elements.join(os.EOL) : elements)
83-
.join(os.EOL);
80+
.map(elements => Array.isArray(elements) ? elements.join('\n') : elements)
81+
.join('\n');
8482
} else {
8583
return '';
8684
}

0 commit comments

Comments
 (0)