Skip to content

Commit e9bd5ca

Browse files
committed
transpile to es5
1 parent d3b68e6 commit e9bd5ca

File tree

7 files changed

+243
-2
lines changed

7 files changed

+243
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
ie: '11', // target es5 -- for example, react-native's QuickJS does not support class
8+
},
9+
},
10+
],
11+
],
12+
}

packages/signals/signals-runtime/build-signals-runtime-global.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const esbuild = require('esbuild')
22
const path = require('path')
33
const fs = require('fs')
44
const fsPromises = fs.promises
5+
const { execSync } = require('child_process')
56

67
const getBanner = (entryPoint) => {
78
const content = [
@@ -63,6 +64,14 @@ const buildRuntimeAsString = async (platform) => {
6364
console.log(`wrote: ${generatedTsFile}`)
6465
}
6566

67+
// while esbuild supports target: es5 natively, it chokes with errors like]
68+
// ✘ [ERROR] Transforming const to the configured target environment ("es5") is not supported yet
69+
const compileToEs5WithBabel = (outFile) => {
70+
return execSync(
71+
`npx babel ${outFile} --out-file ${outFile} --config-file ./babel.config.js`
72+
)
73+
}
74+
6675
const buildRuntime = async (platform) => {
6776
const entryPoint = getEntryPoint(platform)
6877
const { outfileUnminified, outfileMinified } = getOutFiles(platform)
@@ -85,6 +94,11 @@ const buildRuntime = async (platform) => {
8594
minify: false,
8695
banner: { js: getBanner(entryPoint) },
8796
})
97+
98+
// Compile to ES5
99+
compileToEs5WithBabel(outfileUnminified)
100+
compileToEs5WithBabel(outfileMinified)
101+
88102
console.log(`wrote: ${outfileUnminified}`)
89103
}
90104

packages/signals/signals-runtime/bundle-build-tests/runtime-env.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ describe('Global Scope Test: Web', () => {
2626
expect(dom.window).toBeDefined()
2727
// @ts-ignore
2828
expect(typeof dom.window.signals).toBe('object')
29+
expect(typeof dom.window.signals.find).toBe('function')
30+
expect(typeof dom.window.signals.filter).toBe('function')
2931
})
3032

3133
test('should expose constants', () => {
@@ -59,6 +61,8 @@ describe('Global Scope Test: Mobile', () => {
5961
expect(dom.window).toBeDefined()
6062
// @ts-ignore
6163
expect(typeof dom.window.signals).toBe('object')
64+
expect(typeof dom.window.signals.find).toBe('function')
65+
expect(typeof dom.window.signals.filter).toBe('function')
6266
})
6367

6468
test('should expose constants', () => {

packages/signals/signals-runtime/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"url": "https://github.com/segmentio/analytics-next"
4545
},
4646
"devDependencies": {
47+
"@babel/cli": "^7.22.10",
48+
"@babel/core": "^7.22.11",
49+
"@babel/preset-env": "^7.22.10",
4750
"@internal/test-helpers": "workspace:^",
4851
"@microsoft/api-extractor": "^7.47.9"
4952
}

packages/signals/signals-runtime/src/mobile/get-runtime-code.generated.ts

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,125 @@
22
// GENERATED, DO NOT EDIT
33
// Entry point: src/mobile/index.signals-runtime.ts
44
export const getRuntimeCode = (): string => `
5-
"use strict";(()=>{var g=Object.defineProperty;var f=(r,n)=>{for(var e in n)g(r,e,{get:n[e],enumerable:!0})};var l={};f(l,{EventType:()=>d,LocalDataAction:()=>c,NavigationAction:()=>S,NetworkAction:()=>u,SignalType:()=>p});var p=Object.freeze({Interaction:"interaction",Navigation:"navigation",Network:"network",LocalData:"localData",Instrumentation:"instrumentation",UserDefined:"userDefined"}),d=Object.freeze({Track:"track",Page:"page",Screen:"screen",Identify:"identify",Group:"group",Alias:"alias"}),S=Object.freeze({Forward:"forward",Backward:"backward",Modal:"modal",Entering:"entering",Leaving:"leaving",Page:"page",Popup:"popup"}),u=Object.freeze({Request:"request",Response:"response"}),c=Object.freeze({Loaded:"loaded",Updated:"updated",Saved:"saved",Deleted:"deleted",Undefined:"undefined"});var a=class{constructor(n=[]){this.find=(n,e,i)=>this.filter(n,e,i)[0];this.filter=(n,e,i)=>{let o=s=>s.type===e;return this.signalBuffer.slice(this.signalBuffer.indexOf(n)+1).filter(o).filter(s=>i?i(s):()=>!0)};this.signalBuffer=n}};var t=class extends a{constructor(e=[]){super(e);this.add=e=>{this.signalCounter<0&&(this.signalCounter=0),"index"in e&&e.index==-1&&(e.index=this.getNextIndex()),this.signalBuffer.unshift(e),this.signalBuffer.length>this.maxBufferSize&&this.signalBuffer.pop()};this.getNextIndex=()=>{let e=this.signalCounter;return this.signalCounter+=1,e};this.signalCounter=0,this.maxBufferSize=1e3}};Object.assign(globalThis,{signals:new t},l);})();
5+
"use strict";
6+
7+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
9+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
10+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
11+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
12+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
13+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
14+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
15+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
16+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
17+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
18+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
19+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20+
(function () {
21+
var g = Object.defineProperty;
22+
var f = function f(r, n) {
23+
for (var e in n) g(r, e, {
24+
get: n[e],
25+
enumerable: !0
26+
});
27+
};
28+
var l = {};
29+
f(l, {
30+
EventType: function EventType() {
31+
return d;
32+
},
33+
LocalDataAction: function LocalDataAction() {
34+
return c;
35+
},
36+
NavigationAction: function NavigationAction() {
37+
return S;
38+
},
39+
NetworkAction: function NetworkAction() {
40+
return u;
41+
},
42+
SignalType: function SignalType() {
43+
return p;
44+
}
45+
});
46+
var p = Object.freeze({
47+
Interaction: "interaction",
48+
Navigation: "navigation",
49+
Network: "network",
50+
LocalData: "localData",
51+
Instrumentation: "instrumentation",
52+
UserDefined: "userDefined"
53+
}),
54+
d = Object.freeze({
55+
Track: "track",
56+
Page: "page",
57+
Screen: "screen",
58+
Identify: "identify",
59+
Group: "group",
60+
Alias: "alias"
61+
}),
62+
S = Object.freeze({
63+
Forward: "forward",
64+
Backward: "backward",
65+
Modal: "modal",
66+
Entering: "entering",
67+
Leaving: "leaving",
68+
Page: "page",
69+
Popup: "popup"
70+
}),
71+
u = Object.freeze({
72+
Request: "request",
73+
Response: "response"
74+
}),
75+
c = Object.freeze({
76+
Loaded: "loaded",
77+
Updated: "updated",
78+
Saved: "saved",
79+
Deleted: "deleted",
80+
Undefined: "undefined"
81+
});
82+
var a = /*#__PURE__*/_createClass(function a() {
83+
var _this = this;
84+
var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
85+
_classCallCheck(this, a);
86+
this.find = function (n, e, i) {
87+
return _this.filter(n, e, i)[0];
88+
};
89+
this.filter = function (n, e, i) {
90+
var o = function o(s) {
91+
return s.type === e;
92+
};
93+
return _this.signalBuffer.slice(_this.signalBuffer.indexOf(n) + 1).filter(o).filter(function (s) {
94+
return i ? i(s) : function () {
95+
return !0;
96+
};
97+
});
98+
};
99+
this.signalBuffer = n;
100+
});
101+
var t = /*#__PURE__*/function (_a) {
102+
_inherits(t, _a);
103+
var _super = _createSuper(t);
104+
function t() {
105+
var _this2;
106+
var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
107+
_classCallCheck(this, t);
108+
_this2 = _super.call(this, e);
109+
_this2.add = function (e) {
110+
_this2.signalCounter < 0 && (_this2.signalCounter = 0), "index" in e && e.index == -1 && (e.index = _this2.getNextIndex()), _this2.signalBuffer.unshift(e), _this2.signalBuffer.length > _this2.maxBufferSize && _this2.signalBuffer.pop();
111+
};
112+
_this2.getNextIndex = function () {
113+
var e = _this2.signalCounter;
114+
return _this2.signalCounter += 1, e;
115+
};
116+
_this2.signalCounter = 0, _this2.maxBufferSize = 1e3;
117+
return _this2;
118+
}
119+
return _createClass(t);
120+
}(a);
121+
Object.assign(globalThis, {
122+
signals: new t()
123+
}, l);
124+
})();
6125
`
7126

packages/signals/signals-runtime/src/web/get-runtime-code.generated.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,92 @@
22
// GENERATED, DO NOT EDIT
33
// Entry point: src/web/index.signals-runtime.ts
44
export const getRuntimeCode = (): string => `
5-
"use strict";(()=>{var o=Object.defineProperty;var S=(l,e)=>{for(var n in e)o(l,n,{get:e[n],enumerable:!0})};var i=class{constructor(e=[]){this.find=(e,n,a)=>this.filter(e,n,a)[0];this.filter=(e,n,a)=>{let s=g=>g.type===n;return this.signalBuffer.slice(this.signalBuffer.indexOf(e)+1).filter(s).filter(g=>a?a(g):()=>!0)};this.signalBuffer=e}};var t=class extends i{};var r={};S(r,{EventType:()=>f,NavigationAction:()=>p,SignalType:()=>y});var f=Object.freeze({Track:"track",Page:"page",Screen:"screen",Identify:"identify",Group:"group",Alias:"alias"}),p=Object.freeze({URLChange:"urlChange",PageLoad:"pageLoad"}),y=Object.freeze({Interaction:"interaction",Navigation:"navigation",Network:"network",LocalData:"localData",Instrumentation:"instrumentation",UserDefined:"userDefined"});Object.assign(globalThis,{signals:new t},r);})();
5+
"use strict";
6+
7+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
9+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
10+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
11+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
12+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
13+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
14+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
15+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
16+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
17+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
18+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
19+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20+
(function () {
21+
var o = Object.defineProperty;
22+
var S = function S(l, e) {
23+
for (var n in e) o(l, n, {
24+
get: e[n],
25+
enumerable: !0
26+
});
27+
};
28+
var i = /*#__PURE__*/_createClass(function i() {
29+
var _this = this;
30+
var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
31+
_classCallCheck(this, i);
32+
this.find = function (e, n, a) {
33+
return _this.filter(e, n, a)[0];
34+
};
35+
this.filter = function (e, n, a) {
36+
var s = function s(g) {
37+
return g.type === n;
38+
};
39+
return _this.signalBuffer.slice(_this.signalBuffer.indexOf(e) + 1).filter(s).filter(function (g) {
40+
return a ? a(g) : function () {
41+
return !0;
42+
};
43+
});
44+
};
45+
this.signalBuffer = e;
46+
});
47+
var t = /*#__PURE__*/function (_i) {
48+
_inherits(t, _i);
49+
var _super = _createSuper(t);
50+
function t() {
51+
_classCallCheck(this, t);
52+
return _super.apply(this, arguments);
53+
}
54+
return _createClass(t);
55+
}(i);
56+
var r = {};
57+
S(r, {
58+
EventType: function EventType() {
59+
return f;
60+
},
61+
NavigationAction: function NavigationAction() {
62+
return p;
63+
},
64+
SignalType: function SignalType() {
65+
return y;
66+
}
67+
});
68+
var f = Object.freeze({
69+
Track: "track",
70+
Page: "page",
71+
Screen: "screen",
72+
Identify: "identify",
73+
Group: "group",
74+
Alias: "alias"
75+
}),
76+
p = Object.freeze({
77+
URLChange: "urlChange",
78+
PageLoad: "pageLoad"
79+
}),
80+
y = Object.freeze({
81+
Interaction: "interaction",
82+
Navigation: "navigation",
83+
Network: "network",
84+
LocalData: "localData",
85+
Instrumentation: "instrumentation",
86+
UserDefined: "userDefined"
87+
});
88+
Object.assign(globalThis, {
89+
signals: new t()
90+
}, r);
91+
})();
692
`
793

yarn.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6001,6 +6001,9 @@ __metadata:
60016001
version: 0.0.0-use.local
60026002
resolution: "@segment/analytics-signals-runtime@workspace:packages/signals/signals-runtime"
60036003
dependencies:
6004+
"@babel/cli": ^7.22.10
6005+
"@babel/core": ^7.22.11
6006+
"@babel/preset-env": ^7.22.10
60046007
"@internal/test-helpers": "workspace:^"
60056008
"@microsoft/api-extractor": ^7.47.9
60066009
tslib: ^2.4.1

0 commit comments

Comments
 (0)