Skip to content

Commit 1f2010a

Browse files
authored
feat: distribute files for ES modules (also support browser) (#105)
Build result: ``` dist/ ├── bem-ts.js (NEW) ├── bem-ts.min.js (NEW) ├── bem-ts.min.js.map (NEW) ├── index.d.ts └── index.js ``` For browser, supports source-map and minified version. And add `docs/` directory for demo, which will be published GitHub Pages.
1 parent e3ebd48 commit 1f2010a

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ before_script:
1616
- commitlint-travis
1717
- npm run lint
1818

19+
after_success:
20+
- npm run build
21+
1922
branches:
2023
only:
2124
- master

docs/index.html

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>bem-ts Demo</title>
7+
<link rel="stylesheet" href="//unpkg.com/[email protected]/css/bulma.min.css">
8+
<style>
9+
body {
10+
max-width: 40em;
11+
margin: 0 auto;
12+
padding: 1em;
13+
}
14+
h2 {
15+
margin-top: 2em;
16+
}
17+
pre, p {
18+
border: 1px solid lightgray;
19+
padding: 1em 1.25em;
20+
overflow-x: auto;
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
<h2>Input:</h2>
26+
<pre><code></code></pre>
27+
28+
<h2>Output:</h2>
29+
<p><samp></samp></p>
30+
31+
<script type="module">
32+
import block from "../dist/bem-ts.js";
33+
const b = block("block");
34+
const output = document.querySelector("samp");
35+
output.innerHTML = b();
36+
output.innerHTML += "<br>" + b({ modifier: true });
37+
output.innerHTML += "<br>" + b("element");
38+
output.innerHTML += "<br>" + b("element", { modifier: true });
39+
</script>
40+
41+
<script>
42+
document.querySelector("code").textContent =
43+
document.querySelector("script[type='module']").textContent.replace(/ {2,}/g, "").trim();
44+
</script>
45+
</body>
46+
</html>

package-lock.json

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@
1111
],
1212
"license": "MIT",
1313
"main": "dist/index.js",
14+
"module": "dist/bem-ts.js",
1415
"types": "dist/index.d.ts",
1516
"files": [
1617
"dist",
1718
"!*test*"
1819
],
1920
"scripts": {
2021
"prebuild": "rimraf dist/",
21-
"build": "tsc",
22-
"build:watch": "npm run build -- --watch",
22+
"build": "npm-run-all --print-label --parallel build:*",
23+
"build:cjs": "tsc",
24+
"build:esm": "tsc --module es2015 --outDir dist/module/ && mv dist/module/index.js dist/bem-ts.js && rimraf dist/module/",
25+
"postbuild:esm": "cd dist && uglifyjs bem-ts.js -o bem-ts.min.js --source-map url=bem-ts.min.js.map",
2326
"test": "nyc tape -r ts-node/register test.ts",
2427
"test:coverage": "nyc report --reporter=html && opn coverage/index.html",
2528
"lint:ts": "tslint --project .",
2629
"lint:ts:fix": "prettier --write \"**/*.ts\" && npm run lint:ts -- --fix",
2730
"lint:md": "markdownlint --ignore node_modules --ignore CHANGELOG.md \"**/*.md\"",
2831
"lint:md:fix": "prettier --write \"**/*.md\"",
29-
"lint": "npm-run-all --print-name --print-label --parallel lint:*",
32+
"lint": "npm-run-all --print-label --parallel lint:*",
3033
"commitmsg": "commitlint -E GIT_PARAMS",
3134
"precommit": "lint-staged",
3235
"release": "standard-version",
@@ -42,6 +45,7 @@
4245
"tslint": "5.10.0",
4346
"tslint-config-prettier": "1.13.0",
4447
"typescript": "2.9.2",
48+
"uglify-es": "3.3.9",
4549
"ybiq": "3.3.0"
4650
},
4751
"lint-staged": {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "es2017",
44
"module": "commonjs",
55
"declaration": true,
66
"outDir": "./dist",

0 commit comments

Comments
 (0)