Skip to content

Commit 29540f2

Browse files
committed
Refactor: Update build process and add browser tests
1 parent 4cc0e21 commit 29540f2

File tree

20 files changed

+1102
-49
lines changed

20 files changed

+1102
-49
lines changed

build/compability.js renamed to build/other.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const __filename = fileURLToPath(import.meta.url);
77
const __dirname = dirname(__filename);
88

99
let project = dirname(__dirname);
10-
let input = fs.readFileSync(project + '/stage/Test.js', 'utf-8');
10+
let input = fs.readFileSync(project + '/dist/Test.mjs', 'utf-8');
1111

12-
async function compile(data) {
12+
async function compileSYS(data) {
1313
return new Promise((resolve)=> {
1414
let output = babel.transform(data, {
1515
presets: ['@babel/preset-env'],
@@ -18,16 +18,7 @@ async function compile(data) {
1818
resolve(output)
1919
})
2020
}
21-
async function compileToCJS(data) {
22-
return new Promise((resolve)=> {
23-
let output = babel.transform(data, {
24-
presets: ['@babel/preset-env'],
25-
moduleIds: true,
26-
moduleId: 'Test'
27-
}).code;
28-
resolve(output)
29-
})
30-
}
21+
3122
async function compileToAMD(data) {
3223
return new Promise((resolve)=> {
3324
let output = babel.transform(data, {
@@ -37,13 +28,12 @@ async function compileToAMD(data) {
3728
resolve(output)
3829
})
3930
}
31+
4032
try {
41-
let code = await compile(input);
42-
fs.writeFileSync(project + '/dist/Test.js', code, 'utf-8');
43-
let code_cjs = await compileToCJS(input);
44-
fs.writeFileSync(project+'/dist/Test.cjs', code_cjs, 'utf-8');
33+
let code_sys = await compileSYS(input);
34+
fs.writeFileSync(project + '/dist/system/Test.js', code_sys, 'utf-8');
4535
let code_amd = await compileToAMD(input);
46-
fs.writeFileSync(project+'/dist/Test.amd.js', code_amd, 'utf-8');
36+
fs.writeFileSync(project+'/dist/amd/Test.js', code_amd, 'utf-8');
4737
} catch (e) {
4838
console.error(e)
4939
}

build/pretty.js

Whitespace-only changes.

build/test/browser.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { fileURLToPath } from 'url';
2+
import { dirname } from 'path';
3+
import fs from 'fs-plus';
4+
import babel from '@babel/core';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
let project = dirname(dirname(__dirname));
10+
let input = fs.readFileSync(project + '/test/html/test.js', 'utf-8');
11+
12+
async function compile(data) {
13+
return new Promise((resolve)=> {
14+
let output = babel.transform(data, {
15+
presets: ["@babel/preset-env"]
16+
}).code;
17+
resolve(output)
18+
})
19+
}
20+
21+
try {
22+
let code = await compile(input);
23+
fs.writeFileSync(project+'/test/html/legacy/test.js', code, 'utf-8');
24+
} catch (e) {
25+
console.error(e)
26+
}
27+
28+
try {
29+
let code_test = fs.readFileSync(project + '/test/html/legacy/test.js', 'utf-8');
30+
let code_replace = code_test.replace('Test()', 'Test.default()');
31+
let code_amd = `define(["Test"], function (Test) {\n${code_replace}\n})`;
32+
fs.writeFileSync(project+'/test/html/legacy/test.amd.js', code_amd, 'utf-8');
33+
} catch (e) {
34+
console.error(e)
35+
}
36+
37+
try {
38+
let code_test = fs.readFileSync(project + '/test/html/legacy/test.js', 'utf-8');
39+
let code_replace = code_test.replace('Test()', 'Test.default()');
40+
let code_system = `System.import(["Test"]).then(function (Test) {\n${code_replace}\n})`;
41+
fs.writeFileSync(project+'/test/html/legacy/test.system.js', code_system, 'utf-8');
42+
} catch(e) {
43+
console.error(e)
44+
}

build/test/directly.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

build/test/terminal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const __filename = fileURLToPath(import.meta.url);
66
const __dirname = dirname(__filename);
77

88
let project = dirname(dirname(__dirname));
9-
let input = fs.readFileSync(project + '/test/html/directly.js', 'utf-8');
9+
let input = fs.readFileSync(project + '/test/html/test.js', 'utf-8');
1010
let output = {
1111
cjs: `const Test = require('../../dist/Test.cjs');\n${input}`,
1212
mjs: `import Test from '../../dist/Test.mjs';\n${input}`

dist/amd/Test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
define(["exports"], function (_exports) {
2+
"use strict";
3+
4+
Object.defineProperty(_exports, "__esModule", {
5+
value: true
6+
});
7+
_exports["default"] = void 0;
8+
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); }
9+
function Test() {
10+
var cases = [];
11+
var currentDescription = '';
12+
function run() {
13+
if (!cases || cases.length === 0) {
14+
console.warn("Zero cases");
15+
return;
16+
}
17+
var currentDescribe = null;
18+
if (typeof window !== "undefined") {
19+
var resultsContainer = document.getElementById("test-results") || document.createElement("pre");
20+
resultsContainer.id = "test-results";
21+
resultsContainer.style.fontSize = "110%";
22+
if (!document.getElementById("test-results")) {
23+
document.body.appendChild(resultsContainer);
24+
}
25+
cases.forEach(function (testCase) {
26+
var describeText = testCase.description.split(" - ")[0];
27+
var itText = testCase.description.split(" - ")[1];
28+
if (currentDescribe !== describeText) {
29+
currentDescribe = describeText;
30+
var groupHeader = document.createElement("h2");
31+
groupHeader.textContent = currentDescribe;
32+
resultsContainer.appendChild(groupHeader);
33+
}
34+
var resultElement = document.createElement("p");
35+
try {
36+
testCase.fn();
37+
resultElement.textContent = "\u2714 ".concat(itText);
38+
resultElement.style.color = "green";
39+
} catch (error) {
40+
resultElement.textContent = "\u2716 ".concat(itText, " - ").concat(error.message);
41+
resultElement.style.color = "red";
42+
}
43+
resultsContainer.appendChild(resultElement);
44+
});
45+
return;
46+
}
47+
cases.forEach(function (testCase) {
48+
if (currentDescribe !== testCase.description.split(" - ")[0]) {
49+
if (currentDescribe !== null) {
50+
console.groupEnd();
51+
}
52+
currentDescribe = testCase.description.split(" - ")[0];
53+
console.group(currentDescribe);
54+
}
55+
try {
56+
testCase.fn();
57+
console.log("\x1B[32m\u2714 ".concat(testCase.description, "\x1B[0m"));
58+
} catch (error) {
59+
console.error("\x1B[31m\u2716 ".concat(testCase.description, " - ").concat(error.message, "\x1B[0m"));
60+
}
61+
});
62+
if (currentDescribe !== null) {
63+
console.groupEnd();
64+
}
65+
}
66+
function deepCompare(obj1, obj2) {
67+
if (_typeof(obj1) !== "object" || obj1 === null || _typeof(obj2) !== "object" || obj2 === null) {
68+
return obj1 === obj2;
69+
}
70+
var keys1 = Object.keys(obj1);
71+
var keys2 = Object.keys(obj2);
72+
if (keys1.length !== keys2.length) {
73+
return false;
74+
}
75+
for (var _i = 0, _keys = keys1; _i < _keys.length; _i++) {
76+
var key = _keys[_i];
77+
if (!obj2.hasOwnProperty(key) || !deepCompare(obj1[key], obj2[key])) {
78+
return false;
79+
}
80+
}
81+
return true;
82+
}
83+
function describe(description, fn) {
84+
cases = [];
85+
currentDescription = description;
86+
fn();
87+
run();
88+
}
89+
function it(description, fn) {
90+
cases.push({
91+
description: currentDescription + " - " + description,
92+
fn: fn
93+
});
94+
}
95+
function assert(condition) {
96+
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
97+
if (typeof window !== "undefined") {
98+
if (Array.isArray(condition) && condition.length === 2) {
99+
if (!deepCompare(condition[0], condition[1])) {
100+
throw new Error(message + " Expected ".concat(JSON.stringify(condition[0]), " equals ").concat(JSON.stringify(condition[1])));
101+
}
102+
} else {
103+
if (!condition) {
104+
throw new Error(message + " Expected ".concat(String(condition)));
105+
}
106+
}
107+
} else {
108+
if (Array.isArray(condition) && condition.length === 2) {
109+
if (!deepCompare(condition[0], condition[1])) {
110+
throw new Error(message + " Expected ".concat(JSON.stringify(condition[0]), " equals ").concat(JSON.stringify(condition[1])));
111+
}
112+
} else {
113+
if (!condition) {
114+
throw new Error(message + " Expected ".concat(String(condition)));
115+
}
116+
}
117+
}
118+
}
119+
return {
120+
describe: describe,
121+
it: it,
122+
assert: assert
123+
};
124+
}
125+
var _default = _exports["default"] = Test;
126+
});

dist/system/Test.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"use strict";
2+
3+
System.register([], function (_export, _context) {
4+
"use strict";
5+
6+
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); }
7+
function Test() {
8+
var cases = [];
9+
var currentDescription = '';
10+
function run() {
11+
if (!cases || cases.length === 0) {
12+
console.warn("Zero cases");
13+
return;
14+
}
15+
var currentDescribe = null;
16+
if (typeof window !== "undefined") {
17+
var resultsContainer = document.getElementById("test-results") || document.createElement("pre");
18+
resultsContainer.id = "test-results";
19+
resultsContainer.style.fontSize = "110%";
20+
if (!document.getElementById("test-results")) {
21+
document.body.appendChild(resultsContainer);
22+
}
23+
cases.forEach(function (testCase) {
24+
var describeText = testCase.description.split(" - ")[0];
25+
var itText = testCase.description.split(" - ")[1];
26+
if (currentDescribe !== describeText) {
27+
currentDescribe = describeText;
28+
var groupHeader = document.createElement("h2");
29+
groupHeader.textContent = currentDescribe;
30+
resultsContainer.appendChild(groupHeader);
31+
}
32+
var resultElement = document.createElement("p");
33+
try {
34+
testCase.fn();
35+
resultElement.textContent = "\u2714 ".concat(itText);
36+
resultElement.style.color = "green";
37+
} catch (error) {
38+
resultElement.textContent = "\u2716 ".concat(itText, " - ").concat(error.message);
39+
resultElement.style.color = "red";
40+
}
41+
resultsContainer.appendChild(resultElement);
42+
});
43+
return;
44+
}
45+
cases.forEach(function (testCase) {
46+
if (currentDescribe !== testCase.description.split(" - ")[0]) {
47+
if (currentDescribe !== null) {
48+
console.groupEnd();
49+
}
50+
currentDescribe = testCase.description.split(" - ")[0];
51+
console.group(currentDescribe);
52+
}
53+
try {
54+
testCase.fn();
55+
console.log("\x1B[32m\u2714 ".concat(testCase.description, "\x1B[0m"));
56+
} catch (error) {
57+
console.error("\x1B[31m\u2716 ".concat(testCase.description, " - ").concat(error.message, "\x1B[0m"));
58+
}
59+
});
60+
if (currentDescribe !== null) {
61+
console.groupEnd();
62+
}
63+
}
64+
function deepCompare(obj1, obj2) {
65+
if (_typeof(obj1) !== "object" || obj1 === null || _typeof(obj2) !== "object" || obj2 === null) {
66+
return obj1 === obj2;
67+
}
68+
var keys1 = Object.keys(obj1);
69+
var keys2 = Object.keys(obj2);
70+
if (keys1.length !== keys2.length) {
71+
return false;
72+
}
73+
for (var _i = 0, _keys = keys1; _i < _keys.length; _i++) {
74+
var key = _keys[_i];
75+
if (!obj2.hasOwnProperty(key) || !deepCompare(obj1[key], obj2[key])) {
76+
return false;
77+
}
78+
}
79+
return true;
80+
}
81+
function describe(description, fn) {
82+
cases = [];
83+
currentDescription = description;
84+
fn();
85+
run();
86+
}
87+
function it(description, fn) {
88+
cases.push({
89+
description: currentDescription + " - " + description,
90+
fn: fn
91+
});
92+
}
93+
function assert(condition) {
94+
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
95+
if (typeof window !== "undefined") {
96+
if (Array.isArray(condition) && condition.length === 2) {
97+
if (!deepCompare(condition[0], condition[1])) {
98+
throw new Error(message + " Expected ".concat(JSON.stringify(condition[0]), " equals ").concat(JSON.stringify(condition[1])));
99+
}
100+
} else {
101+
if (!condition) {
102+
throw new Error(message + " Expected ".concat(String(condition)));
103+
}
104+
}
105+
} else {
106+
if (Array.isArray(condition) && condition.length === 2) {
107+
if (!deepCompare(condition[0], condition[1])) {
108+
throw new Error(message + " Expected ".concat(JSON.stringify(condition[0]), " equals ").concat(JSON.stringify(condition[1])));
109+
}
110+
} else {
111+
if (!condition) {
112+
throw new Error(message + " Expected ".concat(String(condition)));
113+
}
114+
}
115+
}
116+
}
117+
return {
118+
describe: describe,
119+
it: it,
120+
assert: assert
121+
};
122+
}
123+
return {
124+
setters: [],
125+
execute: function execute() {
126+
_export("default", Test);
127+
}
128+
};
129+
});

0 commit comments

Comments
 (0)