Skip to content

Commit

Permalink
feat: use rollup as build tool (#153)
Browse files Browse the repository at this point in the history
* feat: use rollup as build tool

* feat: avoid polluting window and document of global

* chore: tweak .npmignore

* chore: update devDependencies
  • Loading branch information
TrickyPi authored Aug 4, 2022
1 parent 650a64c commit f19b530
Show file tree
Hide file tree
Showing 13 changed files with 1,003 additions and 13,227 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ bower_components
.vscode
.DS_Store

# ouput dir
dist/
lib/
es/
5 changes: 2 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
.github/
example/
test/
src/
tests/
bower.json
CONTRIBUTING.md
index.html
package-lock.json
playwright.config.ts
webpack.config.js
rollup.config.js
6 changes: 2 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

* Install deps with `npm install`

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

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

* Open Chrome `open http://localhost:3000/`

* Run tests `http://localhost:3000/test/`
* Run tests `npm run test`

* Build distribution files with `npm run build`
14,088 changes: 925 additions & 13,163 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "xhook",
"version": "0.0.0-git-tag",
"description": "Easily intercept and modify XHR request and response",
"browser": "dist/xhook.js",
"main": "src/main.js",
"main": "lib/main.js",
"module": "es/main.js",
"scripts": {
"start": "webpack-dev-server --mode development",
"build": "webpack-cli --mode development && webpack-cli --mode production --output-filename xhook.min.js",
"start": "rollup -c -w",
"build": "rollup -c",
"test": "playwright test",
"test:server": "http-server --port 8080",
"test:all": "testcafe all tests"
Expand Down Expand Up @@ -42,11 +42,10 @@
],
"license": "MIT",
"devDependencies": {
"@playwright/test": "^1.23.0",
"@playwright/test": "^1.24.2",
"http-server": "^14.1.1",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
"rollup": "^2.77.2",
"rollup-plugin-terser": "^7.0.2"
},
"prettier": {}
}
45 changes: 45 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from 'rollup';
import { terser } from "rollup-plugin-terser";
import { version } from './package.json'

const year = new Date().getFullYear();

const banner = `//XHook - v${version} - ` +
"https://github.com/jpillora/xhook\n" +
`//Jaime Pillora <[email protected]> - ` +
`MIT Copyright ${year}`

const baseIifeConfig = {
banner,
format: 'iife',
name: 'xhook',
sourcemap: true,
}

export default defineConfig({
input: 'src/main.js',
output: [
{
...baseIifeConfig,
file: 'dist/xhook.js',
},
{
...baseIifeConfig,
file: 'dist/xhook.min.js',
plugins: [terser({
format: {
comments: /^(XHook|Jaime)/,
},
})]
},
{
dir: 'lib',
format: 'cjs',
exports: 'auto'
},
{
dir: 'es',
format: 'esm',
}
]
})
11 changes: 2 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventEmitter } from "./misc/event-emitter";
import { window } from "./misc/window";
import headers from "./misc/headers";

//patchable types
Expand Down Expand Up @@ -49,11 +48,5 @@ xhook.headers = headers.convert;
//enable by default
xhook.enable();

//publicise (amd+commonjs+window)
if (typeof define === "function" && define.amd) {
define("xhook", [], () => xhook);
} else if (module && typeof module === "object" && module.exports) {
module.exports = { xhook };
} else if (window) {
window.xhook = xhook;
}

export default xhook;
6 changes: 3 additions & 3 deletions src/misc/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { document } from "./window";
import { documentRef } from "./window";

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

//create fake event
export const fakeEvent = function(type) {
if (document && document.createEventObject != null) {
const msieEventObject = document.createEventObject();
if (documentRef && documentRef.createEventObject != null) {
const msieEventObject = documentRef.createEventObject();
msieEventObject.type = type;
return msieEventObject;
}
Expand Down
4 changes: 2 additions & 2 deletions src/misc/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ if (
msie = parseInt(RegExp.$1, 10);
}

export const window = result;
export const document = result.document;
export const windowRef = result;
export const documentRef = result.document;
8 changes: 4 additions & 4 deletions src/patch/fetch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { window } from "../misc/window";
import { windowRef } from "../misc/window";
import { mergeObjects } from "../misc/events";
import hooks from "../misc/hooks";
import formData from "./formdata";

//browser's fetch
const Native = window.fetch;
const Native = windowRef.fetch;

//xhook's fetch
const Xhook = function(url, options) {
Expand Down Expand Up @@ -105,12 +105,12 @@ const Xhook = function(url, options) {
export default {
patch() {
if (Native) {
window.fetch = Xhook;
windowRef.fetch = Xhook;
}
},
unpatch() {
if (Native) {
window.fetch = Native;
windowRef.fetch = Native;
}
},
Native,
Expand Down
8 changes: 4 additions & 4 deletions src/patch/formdata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window } from "../misc/window";
import { windowRef } from "../misc/window";
import { slice } from "../misc/array";

//note:
Expand All @@ -7,7 +7,7 @@ import { slice } from "../misc/array";
// object is used on send

//browser's FormData
var Native = window.FormData;
var Native = windowRef.FormData;

//xhooks's FormData
const Xhook = function(form) {
Expand Down Expand Up @@ -37,12 +37,12 @@ const Xhook = function(form) {
export default {
patch() {
if (Native) {
window.FormData = Xhook;
windowRef.FormData = Xhook;
}
},
unpatch() {
if (Native) {
window.FormData = Native;
windowRef.FormData = Native;
}
},
Native,
Expand Down
8 changes: 4 additions & 4 deletions src/patch/xmlhttprequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, msie } from "../misc/window";
import { windowRef, msie } from "../misc/window";
import {
proxyEvents,
mergeObjects,
Expand All @@ -12,7 +12,7 @@ import formData from "./formdata";
const nullify = res => (res === undefined ? null : res);

//browser's XMLHttpRequest
const Native = window.XMLHttpRequest;
const Native = windowRef.XMLHttpRequest;

//xhook's XMLHttpRequest
const Xhook = function() {
Expand Down Expand Up @@ -413,12 +413,12 @@ Xhook.DONE = 4;
export default {
patch() {
if (Native) {
window.XMLHttpRequest = Xhook;
windowRef.XMLHttpRequest = Xhook;
}
},
unpatch() {
if (Native) {
window.XMLHttpRequest = Native;
windowRef.XMLHttpRequest = Native;
}
},
Native,
Expand Down
23 changes: 0 additions & 23 deletions webpack.config.js

This file was deleted.

0 comments on commit f19b530

Please sign in to comment.