Skip to content

Commit

Permalink
feat(browser): support browsers as well as node (#8)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicojs authored Jun 27, 2019
1 parent bdc4c15 commit 0493b92
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 118 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules
/dist
*.js
*.d.ts
*.map
*.tsbuildinfo
*.tsbuildinfo
!src/jsx/*.d.ts
6 changes: 3 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**/**
!readme.md
!src/**/*.js
!src/**/*.d.ts
!dist/*.?(js|d.ts)
!README.md
!src/**/*.?(js|d.ts)
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"**/*?.js": {
"when": "$(basename).tsx"
},
"**/*.d.ts": true,
"*/*.d.ts": true,
"src/jsx/*.d.ts": false,
"**/*.map": true,
"**/*.tsbuildinfo": true
},
Expand Down
13 changes: 13 additions & 0 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ However, the following piece of code will **NOT** compile:
<a foo="bar"></a>; // => Error: Property 'foo' does not exist on type 'HtmlAnchorTag'
```

## Supported environments

Typed HTML supports both NodeJS and (since 2.0) the browser.

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';
```

## Supported scenarios

All template scenarios are supported with plain TypeScript.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"description": "TypeSafe HTML templates using TypeScript. No need to learn a template library.",
"main": "src/elements.js",
"typings": "src/elements.d.ts",
"module": "dist/elements.js",
"repository": {
"type": "git",
"url": "https://github.com/nicojs/typed-html"
},
"scripts": {
"prebuild": "rimraf \"+(test|src)/**/*.+(js|map|tsbuildinfo|d.ts)\" dist",
"build": "tsc -b",
"prebuild": "rimraf \"+(test|src)/*.+(js|map|tsbuildinfo|d.ts)\" dist",
"build": "tsc -b && tsc -p src --outDir dist --module esnext --target esnext",
"pretest": "npm run build",
"test": "mocha",
"preversion": "npm test",
Expand Down
12 changes: 5 additions & 7 deletions src/elements.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/// <reference path="./jsx/element-types.ts" />
/// <reference path="./jsx/events.ts" />
/// <reference path="./jsx/intrinsic-elements.ts" />

import * as os from 'os';
/// <reference path="./jsx/element-types.d.ts" />
/// <reference path="./jsx/events.d.ts" />
/// <reference path="./jsx/intrinsic-elements.d.ts" />

type AttributeValue = number | string | Date | boolean;

Expand Down Expand Up @@ -79,8 +77,8 @@ const attributesToString = (attributes: Attributes | undefined): string => {
const contentsToString = (contents: any[] | undefined) => {
if (contents) {
return contents
.map(elements => Array.isArray(elements) ? elements.join(os.EOL) : elements)
.join(os.EOL);
.map(elements => Array.isArray(elements) ? elements.join('\n') : elements)
.join('\n');
} else {
return '';
}
Expand Down
Loading

0 comments on commit 0493b92

Please sign in to comment.