Skip to content

Commit 031701f

Browse files
committed
feat: added browser support
1 parent bdb452a commit 031701f

File tree

11 files changed

+2815
-72
lines changed

11 files changed

+2815
-72
lines changed

.browserslistrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extends @ladjs/browserslist-config

.dist.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["@babel/env", {
4+
"targets": {
5+
"browsers": [ "extends @ladjs/browserslist-config" ]
6+
}
7+
}]
8+
]
9+
}

.dist.eslintrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"extends": ["eslint:recommended"],
3+
"env": {
4+
"node": false,
5+
"browser": true,
6+
"amd": true,
7+
"es6": true,
8+
"commonjs": true
9+
},
10+
"plugins": ["compat"],
11+
"rules": {
12+
"compat/compat": "error",
13+
"no-func-assign": "off",
14+
"no-unused-vars": "off",
15+
"no-empty": "off"
16+
},
17+
"settings": {
18+
"polyfills": [
19+
"Array.from",
20+
"Symbol.iterator",
21+
"Symbol.prototype"
22+
]
23+
}
24+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
node_modules
55
coverage
66
.nyc_output
7+
lib
8+
dist

.lib.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
["@babel/env", {
4+
"targets": {
5+
"node": "6.4.0",
6+
"browsers": [ "extends @ladjs/browserslist-config" ]
7+
}
8+
}]
9+
],
10+
"sourceMaps": "both"
11+
}

.lib.eslintrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": ["eslint:recommended", "plugin:node/recommended"],
3+
"env": { "browser": true" },
4+
"plugins": ["compat"],
5+
"rules": {
6+
"compat/compat": "error"
7+
},
8+
"settings": {
9+
"polyfills": [
10+
]
11+
}
12+
}

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
* [Install](#install)
1717
* [Usage](#usage)
18+
* [Node](#node)
1819
* [Contributors](#contributors)
1920
* [License](#license)
2021

@@ -36,12 +37,46 @@ yarn add dayjs dayjs-with-plugins
3637

3738
## Usage
3839

40+
### Node
41+
3942
```js
4043
const dayjs = require('dayjs-with-plugins');
4144

42-
// ...
45+
console.log('M/D/YY', dayjs().format('M/D/YY'));
46+
```
47+
48+
#### VanillaJS
49+
50+
This is the solution for you if you're just using `<script>` tags everywhere!
51+
52+
```html
53+
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6,es7,Array.from,Symbol.iterator,Symbol.prototype"></script>
54+
<script src="https://unpkg.com/dayjs-with-plugins"></script>
55+
<script type="text/javascript">
56+
(function() {
57+
console.log('M/D/YY', dayjs().format('M/D/YY'));
58+
})();
59+
</script>
60+
```
61+
62+
##### Required Browser Features
63+
64+
We recommend using <https://polyfill.io> (specifically with the bundle mentioned in [VanillaJS](#vanillajs) above):
65+
66+
```html
67+
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6,es7,Array.from,Symbol.iterator,Symbol.prototype"></script>
4368
```
4469

70+
* Array.from() is not supported in IE 11
71+
* Symbol.iterator() is not supported in IE 11
72+
* Symbol.prototype() is not supported in IE 11
73+
74+
#### Bundler
75+
76+
This assumes you are using [browserify][], [webpack][], [rollup][], or another bundler.
77+
78+
See [Node](#node) usage above for how to use in a bundler environment.
79+
4580

4681
## Contributors
4782

@@ -60,3 +95,9 @@ const dayjs = require('dayjs-with-plugins');
6095
[npm]: https://www.npmjs.com/
6196

6297
[yarn]: https://yarnpkg.com/
98+
99+
[browserify]: https://github.com/browserify/browserify
100+
101+
[webpack]: https://github.com/webpack/webpack
102+
103+
[rollup]: https://github.com/rollup/rollup

package.json

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,47 @@
2222
"debug": "^4.1.1"
2323
},
2424
"devDependencies": {
25+
"@babel/cli": "^7.6.4",
26+
"@babel/core": "^7.6.4",
27+
"@babel/preset-env": "^7.6.3",
2528
"@commitlint/cli": "latest",
2629
"@commitlint/config-conventional": "latest",
30+
"@ladjs/browserslist-config": "^0.0.1",
2731
"ava": "latest",
32+
"babelify": "^10.0.0",
33+
"browserify": "^16.5.0",
2834
"codecov": "latest",
29-
"cross-env": "latest",
35+
"cross-env": "^6.0.3",
3036
"dayjs": "^1.8.16",
31-
"eslint": "latest",
32-
"eslint-config-xo-lass": "latest",
37+
"eslint": "^6.5.1",
38+
"eslint-config-xo-lass": "^1.0.3",
39+
"eslint-plugin-compat": "^3.3.0",
40+
"eslint-plugin-node": "^10.0.0",
3341
"fixpack": "latest",
3442
"husky": "latest",
43+
"jsdom": "^15.1.1",
3544
"lint-staged": "latest",
3645
"nyc": "latest",
3746
"remark-cli": "latest",
3847
"remark-preset-github": "latest",
48+
"tinyify": "^2.5.1",
3949
"xo": "latest"
4050
},
4151
"engines": {
42-
"node": ">=8.3"
52+
"node": ">=6.4.0"
4353
},
54+
"files": [
55+
"lib",
56+
"dist"
57+
],
4458
"homepage": "https://github.com/niftylettuce/dayjs-with-plugins",
4559
"husky": {
4660
"hooks": {
4761
"pre-commit": "npm test",
4862
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
4963
}
5064
},
65+
"jsdelivr": "dist/dayjs-with-plugins.min.js",
5166
"keywords": [
5267
"all",
5368
"alternative",
@@ -80,7 +95,10 @@
8095
"git add"
8196
]
8297
},
83-
"main": "index.js",
98+
"main": "lib/index.js",
99+
"peerDependencies": {
100+
"dayjs": "*"
101+
},
84102
"prettier": {
85103
"singleQuote": true,
86104
"bracketSpacing": true,
@@ -97,20 +115,24 @@
97115
},
98116
"scripts": {
99117
"ava": "cross-env NODE_ENV=test ava",
118+
"browserify": "browserify src/index.js -o dist/dayjs-with-plugins.js -s dayjs -g [ babelify --configFile ./.dist.babelrc ]",
119+
"build": "npm run build:clean && npm run build:lib && npm run build:dist",
120+
"build:clean": "rimraf lib dist",
121+
"build:dist": "npm run browserify && npm run minify",
122+
"build:lib": "babel --config-file ./.lib.babelrc src --out-dir lib",
100123
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
101-
"lint": "xo && remark . -qfo",
124+
"lint": "xo && remark . -qfo && eslint -c .lib.eslintrc lib && eslint --no-inline-config -c .dist.eslintrc dist",
125+
"minify": "cross-env NODE_ENV=production browserify src/index.js -o dist/dayjs-with-plugins.min.js -s dayjs -g [ babelify --configFile ./.dist.babelrc ] -p tinyify",
102126
"nyc": "cross-env NODE_ENV=test nyc ava",
103-
"test": "npm run lint && npm run ava",
104-
"test-coverage": "npm run lint && npm run nyc"
127+
"test": "npm run build && npm run lint && npm run ava",
128+
"test-coverage": "npm run build && npm run lint && npm run nyc"
105129
},
130+
"unpkg": "dist/dayjs-with-plugins.min.js",
106131
"xo": {
107132
"prettier": true,
108133
"space": true,
109134
"extends": [
110135
"xo-lass"
111136
]
112-
},
113-
"peerDependencies": {
114-
"dayjs": "*"
115137
}
116138
}
File renamed without changes.

test/browser.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const path = require('path');
2+
const { readFileSync } = require('fs');
3+
const { Script } = require('vm');
4+
const test = require('ava');
5+
const { JSDOM, VirtualConsole } = require('jsdom');
6+
7+
const virtualConsole = new VirtualConsole();
8+
virtualConsole.sendTo(console);
9+
10+
const script = new Script(
11+
readFileSync(path.join(__dirname, '..', 'dist', 'dayjs-with-plugins.min.js'))
12+
);
13+
14+
const dom = new JSDOM(``, {
15+
url: 'http://localhost:3000/',
16+
referrer: 'http://localhost:3000/',
17+
contentType: 'text/html',
18+
includeNodeLocations: true,
19+
resources: 'usable',
20+
runScripts: 'dangerously',
21+
virtualConsole
22+
});
23+
24+
dom.runVMScript(script);
25+
26+
test('should expose dayjs global funciton', t => {
27+
t.true(typeof dom.window.dayjs === 'function');
28+
});

0 commit comments

Comments
 (0)