Skip to content

Commit 1f46415

Browse files
authored
fix: local compiler (#131)
* include d.ts * added local compiler * better typing
1 parent db16b6c commit 1f46415

7 files changed

Lines changed: 639 additions & 64 deletions

File tree

FormData.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
7575
}
7676
}
7777

78+
/**
79+
* @param {string} name
80+
* @param {string | undefined} filename
81+
* @returns {[string, File|string]}
82+
*/
7883
function normalizeArgs (name, value, filename) {
7984
if (value instanceof Blob) {
8085
filename = filename !== undefined
@@ -97,6 +102,11 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
97102
return value.replace(/\r?\n|\r/g, '\r\n')
98103
}
99104

105+
/**
106+
* @template T
107+
* @param {ArrayLike<T>} arr
108+
* @param {{ (elm: T): void; }} cb
109+
*/
100110
function each (arr, cb) {
101111
for (let i = 0; i < arr.length; i++) {
102112
cb(arr[i])
@@ -112,14 +122,14 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
112122
/**
113123
* FormData class
114124
*
115-
* @param {HTMLElement=} form
125+
* @param {HTMLFormElement=} form
116126
*/
117127
constructor (form) {
128+
/** @type {[string, string|File][]} */
118129
this._data = []
119130

120131
const self = this
121-
122-
form && each(form.elements, elm => {
132+
form && each(form.elements, (/** @type {HTMLInputElement} */ elm) => {
123133
if (
124134
!elm.name ||
125135
elm.disabled ||
@@ -196,7 +206,6 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
196206
*
197207
* @param {Function} callback Executed for each item with parameters (value, name, thisArg)
198208
* @param {Object=} thisArg `this` context for callback function
199-
* @return {undefined}
200209
*/
201210
forEach (callback, thisArg) {
202211
ensureArgs(arguments, 1)
@@ -275,11 +284,11 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
275284
* @param {string} name Filed name
276285
* @param {string} value Field value
277286
* @param {string=} filename Filename (optional)
278-
* @return {undefined}
279287
*/
280288
set (name, value, filename) {
281289
ensureArgs(arguments, 2)
282290
name = String(name)
291+
/** @type {[string, string|File][]} */
283292
const result = []
284293
const args = normalizeArgs(name, value, filename)
285294
let replace = true
@@ -347,7 +356,7 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
347356
* The class itself is iterable
348357
* alias for formdata.entries()
349358
*
350-
* @return {Iterator}
359+
* @return {Iterator}
351360
*/
352361
[Symbol.iterator] () {
353362
return this.entries()

build.js

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
1-
import https from 'https'
2-
import fs from 'fs'
3-
import { URLSearchParams } from 'url'
1+
import { writeFileSync } from 'node:fs'
2+
import closure from 'google-closure-compiler'
43

5-
// This is an async file read
6-
const code = fs.readFileSync('./FormData.js', 'utf8').toString()
4+
const ClosureCompiler = closure.compiler
75

8-
// Build the post string from an object
9-
const postData = new URLSearchParams({
10-
compilation_level: 'ADVANCED_OPTIMIZATIONS',
11-
output_format: 'text',
12-
output_info: 'compiled_code',
6+
const closureCompiler = new ClosureCompiler({
7+
js: 'FormData.js',
138
warning_level: 'QUIET',
149
output_wrapper: '/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */\n;(function(){%output%})();',
15-
js_code: code
16-
}).toString()
17-
18-
// An object of options to indicate where to post to
19-
const options = {
20-
host: 'closure-compiler.appspot.com',
21-
path: '/compile',
22-
method: 'POST',
23-
headers: {
24-
'Content-Type': 'application/x-www-form-urlencoded',
25-
'Content-Length': Buffer.byteLength(postData)
26-
}
27-
}
28-
29-
// Set up the request
30-
const req = https.request(options, res => {
31-
res.setEncoding('utf8')
10+
compilation_level: 'ADVANCED'
11+
})
3212

33-
if (res.statusCode !== 200) {
13+
closureCompiler.run((exitCode, stdOut, stdErr) => {
14+
if (exitCode) {
3415
console.log('FATAL An error occurred trying to use closure compiler')
16+
console.log(stdErr)
3517
process.exit(-2)
3618
}
3719

38-
res.pipe(fs.createWriteStream('formdata.min.js'))
20+
writeFileSync('formdata.min.js', stdOut)
3921
})
40-
41-
// post the data
42-
req.write(postData)
43-
req.end()

0 commit comments

Comments
 (0)