Skip to content

Commit f19b530

Browse files
authored
feat: use rollup as build tool (#153)
* feat: use rollup as build tool * feat: avoid polluting window and document of global * chore: tweak .npmignore * chore: update devDependencies
1 parent 650a64c commit f19b530

File tree

13 files changed

+1003
-13227
lines changed

13 files changed

+1003
-13227
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ bower_components
44
.vscode
55
.DS_Store
66

7+
# ouput dir
78
dist/
9+
lib/
10+
es/

.npmignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
.github/
22
example/
3-
test/
3+
src/
44
tests/
5-
bower.json
65
CONTRIBUTING.md
76
index.html
87
package-lock.json
98
playwright.config.ts
10-
webpack.config.js
9+
rollup.config.js

CONTRIBUTING.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
* Install deps with `npm install`
88

9-
* Watch *src* for changes and will then compile and minify into *dist* with `npm start`
9+
* Watch *src* for changes and will then compile into *dist*, *lib* and *es* with `npm start`
1010

1111
* Make sure you have [prettier](https://prettier.io/) installed, and you are reformatting on save
1212

13-
* Open Chrome `open http://localhost:3000/`
14-
15-
* Run tests `http://localhost:3000/test/`
13+
* Run tests `npm run test`
1614

1715
* Build distribution files with `npm run build`

package-lock.json

Lines changed: 925 additions & 13163 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"name": "xhook",
33
"version": "0.0.0-git-tag",
44
"description": "Easily intercept and modify XHR request and response",
5-
"browser": "dist/xhook.js",
6-
"main": "src/main.js",
5+
"main": "lib/main.js",
6+
"module": "es/main.js",
77
"scripts": {
8-
"start": "webpack-dev-server --mode development",
9-
"build": "webpack-cli --mode development && webpack-cli --mode production --output-filename xhook.min.js",
8+
"start": "rollup -c -w",
9+
"build": "rollup -c",
1010
"test": "playwright test",
1111
"test:server": "http-server --port 8080",
1212
"test:all": "testcafe all tests"
@@ -42,11 +42,10 @@
4242
],
4343
"license": "MIT",
4444
"devDependencies": {
45-
"@playwright/test": "^1.23.0",
45+
"@playwright/test": "^1.24.2",
4646
"http-server": "^14.1.1",
47-
"webpack": "^4.42.0",
48-
"webpack-cli": "^3.3.11",
49-
"webpack-dev-server": "^3.10.3"
47+
"rollup": "^2.77.2",
48+
"rollup-plugin-terser": "^7.0.2"
5049
},
5150
"prettier": {}
5251
}

rollup.config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { defineConfig } from 'rollup';
2+
import { terser } from "rollup-plugin-terser";
3+
import { version } from './package.json'
4+
5+
const year = new Date().getFullYear();
6+
7+
const banner = `//XHook - v${version} - ` +
8+
"https://github.com/jpillora/xhook\n" +
9+
`//Jaime Pillora <[email protected]> - ` +
10+
`MIT Copyright ${year}`
11+
12+
const baseIifeConfig = {
13+
banner,
14+
format: 'iife',
15+
name: 'xhook',
16+
sourcemap: true,
17+
}
18+
19+
export default defineConfig({
20+
input: 'src/main.js',
21+
output: [
22+
{
23+
...baseIifeConfig,
24+
file: 'dist/xhook.js',
25+
},
26+
{
27+
...baseIifeConfig,
28+
file: 'dist/xhook.min.js',
29+
plugins: [terser({
30+
format: {
31+
comments: /^(XHook|Jaime)/,
32+
},
33+
})]
34+
},
35+
{
36+
dir: 'lib',
37+
format: 'cjs',
38+
exports: 'auto'
39+
},
40+
{
41+
dir: 'es',
42+
format: 'esm',
43+
}
44+
]
45+
})

src/main.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { EventEmitter } from "./misc/event-emitter";
2-
import { window } from "./misc/window";
32
import headers from "./misc/headers";
43

54
//patchable types
@@ -49,11 +48,5 @@ xhook.headers = headers.convert;
4948
//enable by default
5049
xhook.enable();
5150

52-
//publicise (amd+commonjs+window)
53-
if (typeof define === "function" && define.amd) {
54-
define("xhook", [], () => xhook);
55-
} else if (module && typeof module === "object" && module.exports) {
56-
module.exports = { xhook };
57-
} else if (window) {
58-
window.xhook = xhook;
59-
}
51+
52+
export default xhook;

src/misc/events.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { document } from "./window";
1+
import { documentRef } from "./window";
22

33
export const UPLOAD_EVENTS = ["load", "loadend", "loadstart"];
44
export const COMMON_EVENTS = ["progress", "abort", "error", "timeout"];
@@ -45,8 +45,8 @@ export const proxyEvents = function(events, src, dst) {
4545

4646
//create fake event
4747
export const fakeEvent = function(type) {
48-
if (document && document.createEventObject != null) {
49-
const msieEventObject = document.createEventObject();
48+
if (documentRef && documentRef.createEventObject != null) {
49+
const msieEventObject = documentRef.createEventObject();
5050
msieEventObject.type = type;
5151
return msieEventObject;
5252
}

src/misc/window.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ if (
2626
msie = parseInt(RegExp.$1, 10);
2727
}
2828

29-
export const window = result;
30-
export const document = result.document;
29+
export const windowRef = result;
30+
export const documentRef = result.document;

src/patch/fetch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { window } from "../misc/window";
1+
import { windowRef } from "../misc/window";
22
import { mergeObjects } from "../misc/events";
33
import hooks from "../misc/hooks";
44
import formData from "./formdata";
55

66
//browser's fetch
7-
const Native = window.fetch;
7+
const Native = windowRef.fetch;
88

99
//xhook's fetch
1010
const Xhook = function(url, options) {
@@ -105,12 +105,12 @@ const Xhook = function(url, options) {
105105
export default {
106106
patch() {
107107
if (Native) {
108-
window.fetch = Xhook;
108+
windowRef.fetch = Xhook;
109109
}
110110
},
111111
unpatch() {
112112
if (Native) {
113-
window.fetch = Native;
113+
windowRef.fetch = Native;
114114
}
115115
},
116116
Native,

0 commit comments

Comments
 (0)