Socket
Socket
Sign inDemoInstall

drag-event-service

Package Overview
Dependencies
3
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.0.2

babel.config.js

83

dist/drag-event-service.cjs.js
/*!
* drag-event-service v1.0.1
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
* drag-event-service v1.0.2
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var hp = require('helper-js');
/*!
* helper-js v1.4.14
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
function onDOM(el, name, handler) {
for (var _len6 = arguments.length, args = new Array(_len6 > 3 ? _len6 - 3 : 0), _key8 = 3; _key8 < _len6; _key8++) {
args[_key8 - 3] = arguments[_key8];
}
if (el.addEventListener) {
// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.addEventListener.apply(el, [name, handler].concat(args));
} else if (el.attachEvent) {
// IE 8 及更早 IE 版本
el.attachEvent.apply(el, ["on".concat(name), handler].concat(args));
}
}
function offDOM(el, name, handler) {
for (var _len7 = arguments.length, args = new Array(_len7 > 3 ? _len7 - 3 : 0), _key9 = 3; _key9 < _len7; _key9++) {
args[_key9 - 3] = arguments[_key9];
}
if (el.removeEventListener) {
// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.removeEventListener.apply(el, [name, handler].concat(args));
} else if (el.detachEvent) {
// IE 8 及更早 IE 版本
el.detachEvent.apply(el, ["on".concat(name), handler].concat(args));
}
}
// support desktop and mobile
const events = {
var events = {
start: ['mousedown', 'touchstart'],

@@ -65,3 +30,3 @@ move: ['mousemove', 'touchmove'],

on(el, name, handler, options) {
const {
var {
args,

@@ -72,9 +37,9 @@ mouseArgs,

const store = this._getStore(el);
var store = this._getStore(el);
const ts = this;
var ts = this;
const wrapper = function (e) {
let mouse;
const isTouch = ts.isTouch(e);
var wrapper = function wrapper(e) {
var mouse;
var isTouch = ts.isTouch(e);

@@ -110,8 +75,8 @@ if (isTouch) {

onDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
onDOM.call(null, el, events[name][1], wrapper, ...[...args, ...touchArgs]);
hp.onDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
hp.onDOM.call(null, el, events[name][1], wrapper, ...[...args, ...touchArgs]);
},
off(el, name, handler, options) {
const {
var {
args,

@@ -122,6 +87,6 @@ mouseArgs,

const store = this._getStore(el);
var store = this._getStore(el);
for (let i = store.length - 1; i >= 0; i--) {
const {
for (var i = store.length - 1; i >= 0; i--) {
var {
handler: handler2,

@@ -132,4 +97,4 @@ wrapper

if (handler === handler2) {
offDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
offDOM.call(null, el, events[name][1], wrapper, ...[...args, ...mouseArgs]);
hp.offDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
hp.offDOM.call(null, el, events[name][1], wrapper, ...[...args, ...mouseArgs]);
store.splice(i, 1);

@@ -147,5 +112,5 @@ }

const args = options.args || [];
const mouseArgs = options.mouseArgs || [];
const touchArgs = options.touchArgs || [];
var args = options.args || [];
var mouseArgs = options.mouseArgs || [];
var touchArgs = options.touchArgs || [];
return {

@@ -158,2 +123,2 @@ args,

exports.default = index;
module.exports = index;
/*!
* drag-event-service v1.0.1
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
/*!
* helper-js v1.4.14
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
* drag-event-service v1.0.2
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
import { onDOM, offDOM } from 'helper-js';
function onDOM(el, name, handler) {
for (var _len6 = arguments.length, args = new Array(_len6 > 3 ? _len6 - 3 : 0), _key8 = 3; _key8 < _len6; _key8++) {
args[_key8 - 3] = arguments[_key8];
}
if (el.addEventListener) {
// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.addEventListener.apply(el, [name, handler].concat(args));
} else if (el.attachEvent) {
// IE 8 及更早 IE 版本
el.attachEvent.apply(el, ["on".concat(name), handler].concat(args));
}
}
function offDOM(el, name, handler) {
for (var _len7 = arguments.length, args = new Array(_len7 > 3 ? _len7 - 3 : 0), _key9 = 3; _key9 < _len7; _key9++) {
args[_key9 - 3] = arguments[_key9];
}
if (el.removeEventListener) {
// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.removeEventListener.apply(el, [name, handler].concat(args));
} else if (el.detachEvent) {
// IE 8 及更早 IE 版本
el.detachEvent.apply(el, ["on".concat(name), handler].concat(args));
}
}
// support desktop and mobile
const events = {
var events = {
start: ['mousedown', 'touchstart'],

@@ -61,3 +28,3 @@ move: ['mousemove', 'touchmove'],

on(el, name, handler, options) {
const {
var {
args,

@@ -68,9 +35,9 @@ mouseArgs,

const store = this._getStore(el);
var store = this._getStore(el);
const ts = this;
var ts = this;
const wrapper = function (e) {
let mouse;
const isTouch = ts.isTouch(e);
var wrapper = function wrapper(e) {
var mouse;
var isTouch = ts.isTouch(e);

@@ -111,3 +78,3 @@ if (isTouch) {

off(el, name, handler, options) {
const {
var {
args,

@@ -118,6 +85,6 @@ mouseArgs,

const store = this._getStore(el);
var store = this._getStore(el);
for (let i = store.length - 1; i >= 0; i--) {
const {
for (var i = store.length - 1; i >= 0; i--) {
var {
handler: handler2,

@@ -142,5 +109,5 @@ wrapper

const args = options.args || [];
const mouseArgs = options.mouseArgs || [];
const touchArgs = options.touchArgs || [];
var args = options.args || [];
var mouseArgs = options.mouseArgs || [];
var touchArgs = options.touchArgs || [];
return {

@@ -147,0 +114,0 @@ args,

/*!
* drag-event-service v1.0.1
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
* drag-event-service v1.0.2
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.dragEventService = {}));
}(this, (function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.dragEventService = factory());
}(this, (function () { 'use strict';
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
}
}
var arrayWithoutHoles = _arrayWithoutHoles;
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
var iterableToArray = _iterableToArray;
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
var nonIterableSpread = _nonIterableSpread;
function _toConsumableArray(arr) {
return arrayWithoutHoles(arr) || iterableToArray(arr) || nonIterableSpread();
}
var toConsumableArray = _toConsumableArray;
/*!
* helper-js v1.4.14
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
* helper-js v1.4.21
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
function onDOM(el, name, handler) {

@@ -26,9 +55,8 @@ for (var _len6 = arguments.length, args = new Array(_len6 > 3 ? _len6 - 3 : 0), _key8 = 3; _key8 < _len6; _key8++) {

// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.addEventListener.apply(el, [name, handler].concat(args));
el.addEventListener(name, handler, ...args);
} else if (el.attachEvent) {
// IE 8 及更早 IE 版本
el.attachEvent.apply(el, ["on".concat(name), handler].concat(args));
el.attachEvent("on".concat(name), handler, ...args);
}
}
function offDOM(el, name, handler) {

@@ -41,11 +69,10 @@ for (var _len7 = arguments.length, args = new Array(_len7 > 3 ? _len7 - 3 : 0), _key9 = 3; _key9 < _len7; _key9++) {

// 所有主流浏览器,除了 IE 8 及更早 IE版本
el.removeEventListener.apply(el, [name, handler].concat(args));
el.removeEventListener(name, handler, ...args);
} else if (el.detachEvent) {
// IE 8 及更早 IE 版本
el.detachEvent.apply(el, ["on".concat(name), handler].concat(args));
el.detachEvent("on".concat(name), handler, ...args);
}
}
// support desktop and mobile
const events = {
var events = {
start: ['mousedown', 'touchstart'],

@@ -56,7 +83,6 @@ move: ['mousemove', 'touchmove'],

var index = {
isTouch(e) {
isTouch: function isTouch(e) {
return e.type && e.type.startsWith('touch');
},
_getStore(el) {
_getStore: function _getStore(el) {
if (!el._wrapperStore) {

@@ -68,17 +94,17 @@ el._wrapperStore = [];

},
on: function on(el, name, handler, options) {
var _hp$onDOM, _hp$onDOM2;
on(el, name, handler, options) {
const {
args,
mouseArgs,
touchArgs
} = resolveOptions(options);
var _resolveOptions = resolveOptions(options),
args = _resolveOptions.args,
mouseArgs = _resolveOptions.mouseArgs,
touchArgs = _resolveOptions.touchArgs;
const store = this._getStore(el);
var store = this._getStore(el);
const ts = this;
var ts = this;
const wrapper = function (e) {
let mouse;
const isTouch = ts.isTouch(e);
var wrapper = function wrapper(e) {
var mouse;
var isTouch = ts.isTouch(e);

@@ -108,4 +134,4 @@ if (isTouch) {

store.push({
handler,
wrapper
handler: handler,
wrapper: wrapper
}); // follow format will cause big bundle size

@@ -115,24 +141,25 @@ // 以下写法将会使打包工具认为hp是上下文, 导致打包整个hp

onDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
onDOM.call(null, el, events[name][1], wrapper, ...[...args, ...touchArgs]);
(_hp$onDOM = onDOM).call.apply(_hp$onDOM, [null, el, events[name][0], wrapper].concat([].concat(toConsumableArray(args), toConsumableArray(mouseArgs))));
(_hp$onDOM2 = onDOM).call.apply(_hp$onDOM2, [null, el, events[name][1], wrapper].concat([].concat(toConsumableArray(args), toConsumableArray(touchArgs))));
},
off: function off(el, name, handler, options) {
var _resolveOptions2 = resolveOptions(options),
args = _resolveOptions2.args,
mouseArgs = _resolveOptions2.mouseArgs;
off(el, name, handler, options) {
const {
args,
mouseArgs,
touchArgs
} = resolveOptions(options);
var store = this._getStore(el);
const store = this._getStore(el);
for (var i = store.length - 1; i >= 0; i--) {
var _store$i = store[i],
handler2 = _store$i.handler,
wrapper = _store$i.wrapper;
for (let i = store.length - 1; i >= 0; i--) {
const {
handler: handler2,
wrapper
} = store[i];
if (handler === handler2) {
var _hp$offDOM, _hp$offDOM2;
if (handler === handler2) {
offDOM.call(null, el, events[name][0], wrapper, ...[...args, ...mouseArgs]);
offDOM.call(null, el, events[name][1], wrapper, ...[...args, ...mouseArgs]);
(_hp$offDOM = offDOM).call.apply(_hp$offDOM, [null, el, events[name][0], wrapper].concat([].concat(toConsumableArray(args), toConsumableArray(mouseArgs))));
(_hp$offDOM2 = offDOM).call.apply(_hp$offDOM2, [null, el, events[name][1], wrapper].concat([].concat(toConsumableArray(args), toConsumableArray(mouseArgs))));
store.splice(i, 1);

@@ -142,3 +169,2 @@ }

}
};

@@ -151,16 +177,14 @@

const args = options.args || [];
const mouseArgs = options.mouseArgs || [];
const touchArgs = options.touchArgs || [];
var args = options.args || [];
var mouseArgs = options.mouseArgs || [];
var touchArgs = options.touchArgs || [];
return {
args,
mouseArgs,
touchArgs
args: args,
mouseArgs: mouseArgs,
touchArgs: touchArgs
};
}
exports.default = index;
return index;
Object.defineProperty(exports, '__esModule', { value: true });
})));
/*!
* drag-event-service v1.0.1
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).dragEventService={})}(this,(function(e){"use strict";
* drag-event-service v1.0.2
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).dragEventService=e()}(this,(function(){"use strict";var t=function(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}};var e=function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)};var r=function(){throw new TypeError("Invalid attempt to spread non-iterable instance")};var n=function(n){return t(n)||e(n)||r()};
/*!
* helper-js v1.4.14
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/function t(e,t,n){for(var o=arguments.length,r=new Array(o>3?o-3:0),a=3;a<o;a++)r[a-3]=arguments[a];e.addEventListener?e.addEventListener.apply(e,[t,n].concat(r)):e.attachEvent&&e.attachEvent.apply(e,["on".concat(t),n].concat(r))}function n(e,t,n){for(var o=arguments.length,r=new Array(o>3?o-3:0),a=3;a<o;a++)r[a-3]=arguments[a];e.removeEventListener?e.removeEventListener.apply(e,[t,n].concat(r)):e.detachEvent&&e.detachEvent.apply(e,["on".concat(t),n].concat(r))}const o={start:["mousedown","touchstart"],move:["mousemove","touchmove"],end:["mouseup","touchend"]};var r={isTouch:e=>e.type&&e.type.startsWith("touch"),_getStore:e=>(e._wrapperStore||(e._wrapperStore=[]),e._wrapperStore),on(e,n,r,c){const{args:s,mouseArgs:u,touchArgs:l}=a(c),p=this._getStore(e),h=this,i=function(e){let t;if(h.isTouch(e))t={x:e.changedTouches[0].pageX,y:e.changedTouches[0].pageY};else if(t={x:e.pageX,y:e.pageY},"start"===n&&1!==e.which)return;return r.call(this,e,t)};p.push({handler:r,wrapper:i}),t.call(null,e,o[n][0],i,...s,...u),t.call(null,e,o[n][1],i,...s,...l)},off(e,t,r,c){const{args:s,mouseArgs:u,touchArgs:l}=a(c),p=this._getStore(e);for(let a=p.length-1;a>=0;a--){const{handler:c,wrapper:l}=p[a];r===c&&(n.call(null,e,o[t][0],l,...s,...u),n.call(null,e,o[t][1],l,...s,...u),p.splice(a,1))}}};function a(e){return e||(e={}),{args:e.args||[],mouseArgs:e.mouseArgs||[],touchArgs:e.touchArgs||[]}}e.default=r,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=drag-event-service.min.js.map
* helper-js v1.4.21
* (c) phphe <phphe@outlook.com> (https://github.com/phphe)
* Released under the MIT License.
*/function o(t,e,r){for(var n=arguments.length,o=new Array(n>3?n-3:0),a=3;a<n;a++)o[a-3]=arguments[a];t.addEventListener?t.addEventListener(e,r,...o):t.attachEvent&&t.attachEvent("on".concat(e),r,...o)}function a(t,e,r){for(var n=arguments.length,o=new Array(n>3?n-3:0),a=3;a<n;a++)o[a-3]=arguments[a];t.removeEventListener?t.removeEventListener(e,r,...o):t.detachEvent&&t.detachEvent("on".concat(e),r,...o)}var c={start:["mousedown","touchstart"],move:["mousemove","touchmove"],end:["mouseup","touchend"]};function u(t){return t||(t={}),{args:t.args||[],mouseArgs:t.mouseArgs||[],touchArgs:t.touchArgs||[]}}return{isTouch:function(t){return t.type&&t.type.startsWith("touch")},_getStore:function(t){return t._wrapperStore||(t._wrapperStore=[]),t._wrapperStore},on:function(t,e,r,a){var s,i,l=u(a),p=l.args,f=l.mouseArgs,h=l.touchArgs,g=this._getStore(t),v=this,d=function(t){var n;if(v.isTouch(t))n={x:t.changedTouches[0].pageX,y:t.changedTouches[0].pageY};else if(n={x:t.pageX,y:t.pageY},"start"===e&&1!==t.which)return;return r.call(this,t,n)};g.push({handler:r,wrapper:d}),(s=o).call.apply(s,[null,t,c[e][0],d].concat([].concat(n(p),n(f)))),(i=o).call.apply(i,[null,t,c[e][1],d].concat([].concat(n(p),n(h))))},off:function(t,e,r,o){for(var s=u(o),i=s.args,l=s.mouseArgs,p=this._getStore(t),f=p.length-1;f>=0;f--){var h,g,v=p[f],d=v.handler,y=v.wrapper;if(r===d)(h=a).call.apply(h,[null,t,c[e][0],y].concat([].concat(n(i),n(l)))),(g=a).call.apply(g,[null,t,c[e][1],y].concat([].concat(n(i),n(l)))),p.splice(f,1)}}}}));
{
"name": "drag-event-service",
"version": "1.0.1",
"version": "1.0.2",
"description": "",

@@ -22,8 +22,9 @@ "main": "dist/drag-event-service.cjs.js",

"devDependencies": {
"rollup-helper": "^2.0.1"
"rollup-helper": "^2.0.12"
},
"dependencies": {
"helper-js": "^1.4.14"
"@babel/runtime": "^7.7.7",
"helper-js": "^1.4.21"
},
"license": "MIT"
}

@@ -1,257 +0,13 @@

const gzipSize = require('gzip-size');
const rollup = require('rollup');
const JsonPlugin = require('@rollup/plugin-json');
const NodeResolvePlugin = require('@rollup/plugin-node-resolve');
const CommonjsPlugin = require('@rollup/plugin-commonjs');
const BabelPlugin = require('rollup-plugin-babel');
const TerserPlugin = require('rollup-plugin-terser'); // minify file
const path = require('path');
const fs = require('fs');
const package = require('../package.json');
const {build} = require('rollup-helper')
const program = require('commander');
program.option('-w, --watch', 'Watch files change and build cjs, umd, esm')
program.parse(process.argv);
let builds = require('./config').getAllBuilds()
// The default entry is src/index.js or src/{package.name}.js
if (program.watch) {
// watch
watchFile()
} else {
// build
buildFile()
}
// No subdirectories; 不包含子目录
async function buildDir(inputDir, opt={}, eachOpt={}) {
const files = fs.readdirSync(inputDir)
.filter(item => {
const filePath = path.join(inputDir, item)
return fs.statSync(filePath).isFile() && (!opt.exclude || !opt.exclude(item, filePath))
// filter builds via command line arg
if (process.argv[2]) {
const filters = process.argv[2].split(',')
builds = builds.filter(b => {
return filters.some(f => b.output.file.indexOf(f) > -1 || b._name.indexOf(f) > -1)
})
const report = []
for (const item of files) {
const filePath = path.join(inputDir, item)
const [name, suffix] = item.split('.')
const pathWithoutSuffix = item.slice(0, item.length - 1 - suffix.length)
report.push(...await buildFile({
input: filePath,
moduleName: camelCase(name),
outFileName: pathWithoutSuffix,
...eachOpt,
}))
}
}
// No subdirectories; 不包含子目录
function watchDir(inputDir, opt={}, eachOpt={}) {
const files = fs.readdirSync(inputDir)
.filter(item => {
const filePath = path.join(inputDir, item)
return fs.statSync(filePath).isFile() && (!opt.exclude || !opt.exclude(item, filePath))
})
for (const item of files) {
const filePath = path.join(inputDir, item)
const [name, suffix] = item.split('.')
const pathWithoutSuffix = item.slice(0, item.length - 1 - suffix.length)
watchFile({
input: filePath,
moduleName: camelCase(name),
outFileName: pathWithoutSuffix,
...eachOpt,
})
}
}
/*
opt.plugins: Your plugin config will override default by name, or be pushed to end of plugins.
hooks:
opt.afterOptionsResolved(inputOptions, outputOptions)
*/
async function buildFile(opt={}) {
opt = {
input: get_default_input(),
moduleName: camelCase(package.name),
outFileName: package.name, // without suffix
outputDir: './dist',
formats: ['cjs', 'esm', 'umd', 'umd.min'],
...opt,
plugins: resolvePlugins(opt.plugins),
}
//
const report = []
for (const format of opt.formats) {
const inputOptions = {
input: opt.input,
plugins: [...opt.plugins],
}
const outputOptions = {
format: format,
name: opt.moduleName,
exports: 'named', // reason: Entry module "xxx.js" is using named and default exports together. Consumers of your bundle will have to use `helperJs["default"]` to access the default export, which may not be what you want. Use `output.exports: "named"` to disable this warning
}
if (opt.banner !== false) {
if (typeof opt.banner === 'string') {
outputOptions.banner = opt.banner
} else {
outputOptions.banner = `
/*!
* ${package.name} v${package.version}
* (c) ${package.author}
* Released under the ${package.license} License.
*/`.trim()
}
}
switch (format) {
case 'umd':
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.js`)
break;
case 'umd.min':
inputOptions.plugins.push(TerserPlugin.terser())
outputOptions.format = 'umd'
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.min.js`)
outputOptions.sourcemap = true
break;
default:
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.${format}.js`)
}
opt.afterOptionsResolved && opt.afterOptionsResolved(inputOptions, outputOptions)
const bundle = await rollup.rollup(inputOptions)
// generate code
await bundle.write(outputOptions)
reportOne(outputOptions.file)
if (format === 'umd.min') {
const sourceMapPath = outputOptions.file + '.map'
if (fs.existsSync(sourceMapPath)) {
reportOne(sourceMapPath)
}
}
}
console.table(report);
return report
function reportOne(file) {
const {sizeKiB, sizeKiBGzipped} = getFileInfo(file)
console.log(`Done: ${file}`);
report.push({Output: file, Size: `${sizeKiB} KiB`, Gzipped: `${sizeKiBGzipped} KiB`})
}
}
/*
opt.plugins: Your plugin config will override default by name, or be pushed to end of plugins.
hooks:
opt.afterOptionsResolved(watchOptions)
*/
async function watchFile(opt={}) {
opt = {
input: get_default_input(),
moduleName: camelCase(package.name),
outFileName: package.name, // without suffix
outputDir: './dist',
formats: ['cjs', 'esm', 'umd'],
plugins: resolvePlugins(opt.plugins),
...opt,
}
const inputOptions = {
input: opt.input,
plugins: [...opt.plugins],
}
const outputOptionsArr = []
//
for (const format of opt.formats) {
const outputOptions = {
format: format,
name: opt.moduleName,
exports: 'named', // reason: Entry module "xxx.js" is using named and default exports together. Consumers of your bundle will have to use `helperJs["default"]` to access the default export, which may not be what you want. Use `output.exports: "named"` to disable this warning
}
outputOptionsArr.push(outputOptions)
switch (format) {
case 'umd':
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.js`)
break;
case 'umd.min':
inputOptions.plugins.push(TerserPlugin.terser())
outputOptions.format = 'umd'
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.min.js`)
outputOptions.sourcemap = true
break;
default:
outputOptions.file = path.join(opt.outputDir, `${opt.outFileName}.${format}.js`)
}
}
const watchOptions = {
...inputOptions,
output: outputOptionsArr,
// watch: {
// chokidar,
// clearScreen,
// exclude,
// include
// }
};
opt.afterOptionsResolved && opt.afterOptionsResolved(watchOptions)
console.log(`Start to watch ${inputOptions.input}`);
const watcher = rollup.watch(watchOptions);
watcher.on('event', event => {
if (['ERROR', 'FATAL'].includes(event.code)) {
console.log('error', '======================================================');
console.log(event.error);
}
if (['BUNDLE_END'].includes(event.code)) {
console.log('success', '======================================================');
console.log(event);
}
// event.code can be one of:
// START — the watcher is (re)starting
// BUNDLE_START — building an individual bundle
// BUNDLE_END — finished building a bundle
// END — finished building all bundles
// ERROR — encountered an error while bundling
// FATAL — encountered an unrecoverable error
});
}
function get_default_input() {
let input = './src/index.js'
if (!fs.existsSync(input)) {
input = `./src/${package.name.js}`
}
return input
}
function resolvePlugins(inputPlugins) {
const defaultPlugins = [
BabelPlugin({
runtimeHelpers: true,
}),
NodeResolvePlugin(),
CommonjsPlugin(),
JsonPlugin(),
];
if (inputPlugins) {
inputPlugins.forEach(plugin => {
const index = defaultPlugins.findIndex(v => v.name === plugin.name)
if (index > -1) {
// replace with input plugin
defaultPlugins.splice(index, 1, plugin)
} else {
defaultPlugins.push(plugin)
}
})
}
return defaultPlugins
}
function studlyCase (str) {
return str && (str[0].toUpperCase() + str.substr(1))
}
function camelCase (str) {
const temp = str.toString().split(/[-_]/)
for (let i = 1; i < temp.length; i++) {
temp[i] = studlyCase(temp[i])
}
return temp.join('')
}
function getFileInfo(file) {
// get size
const stats = fs.statSync(file)
const sizeKiB = parseFloat((stats["size"] / 1024).toFixed(2))
// get gzipped size
const sizeKiBGzipped = parseFloat((gzipSize.fileSync(file) / 1024).toFixed(2))
return {file, sizeKiB, sizeKiBGzipped}
}
build(builds)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc