From 1d6dbc70ce72cea0286b9cdfe93c6c020a959ae2 Mon Sep 17 00:00:00 2001 From: Fabian Cook Date: Wed, 16 Oct 2024 09:12:11 +1300 Subject: [PATCH] Drop rollup replacements where no longer used --- example/polyfill-rollup.js | 293 +----------------------------------- example/rollup.js | 299 ++----------------------------------- scripts/post-build.js | 36 +---- 3 files changed, 21 insertions(+), 607 deletions(-) diff --git a/example/polyfill-rollup.js b/example/polyfill-rollup.js index c721aa6..278071f 100644 --- a/example/polyfill-rollup.js +++ b/example/polyfill-rollup.js @@ -1756,25 +1756,13 @@ function getNavigation() { return (navigation$1 = new Navigation()); } - -const getStructuredClone = () => json; - -async function getStructuredCloneModule() { - const { stringify, parse } = await Promise.resolve().then(function () { return json; }); - return { stringify, parse }; -} -function structuredCloneFallback() { - const stringify = JSON.stringify.bind(JSON), parse = JSON.parse.bind(JSON); - return { - stringify, - parse - }; -} -function stringify$1(value) { - return getStructuredClone().stringify(value); +let _stringify = JSON.stringify.bind(JSON); +let _parse = JSON.parse.bind(JSON); +function stringify(value) { + return _stringify(value); } -function parse$1(value) { - return getStructuredClone().parse(value); +function parse(value) { + return _parse(value); } const AppLocationCheckChange = Symbol.for("@virtualstate/navigation/location/checkChange"); @@ -2095,7 +2083,7 @@ function setHistoryState(navigation, history, entry, persist, limit) { if (typeof sessionStorage === "undefined") return; try { - const raw = stringify$1(getSerializableState()); + const raw = stringify(getSerializableState()); sessionStorage.setItem(entry.key, raw); } catch { } @@ -2132,7 +2120,7 @@ function getHistoryState(history, entry) { const raw = sessionStorage.getItem(entry.key); if (!raw) return undefined; - const state = parse$1(raw); + const state = parse(raw); if (!isStateHistoryWithMeta(state)) return undefined; return state[NavigationKey].state; @@ -2686,268 +2674,3 @@ if (shouldApplyPolyfill(navigation)) { console.error(error); } } - -const VOID = -1; -const PRIMITIVE = 0; -const ARRAY = 1; -const OBJECT = 2; -const DATE = 3; -const REGEXP = 4; -const MAP = 5; -const SET = 6; -const ERROR = 7; -const BIGINT = 8; -// export const SYMBOL = 9; - -const env = typeof self === 'object' ? self : globalThis; - -const deserializer = ($, _) => { - const as = (out, index) => { - $.set(index, out); - return out; - }; - - const unpair = index => { - if ($.has(index)) - return $.get(index); - - const [type, value] = _[index]; - switch (type) { - case PRIMITIVE: - case VOID: - return as(value, index); - case ARRAY: { - const arr = as([], index); - for (const index of value) - arr.push(unpair(index)); - return arr; - } - case OBJECT: { - const object = as({}, index); - for (const [key, index] of value) - object[unpair(key)] = unpair(index); - return object; - } - case DATE: - return as(new Date(value), index); - case REGEXP: { - const {source, flags} = value; - return as(new RegExp(source, flags), index); - } - case MAP: { - const map = as(new Map, index); - for (const [key, index] of value) - map.set(unpair(key), unpair(index)); - return map; - } - case SET: { - const set = as(new Set, index); - for (const index of value) - set.add(unpair(index)); - return set; - } - case ERROR: { - const {name, message} = value; - return as(new env[name](message), index); - } - case BIGINT: - return as(BigInt(value), index); - case 'BigInt': - return as(Object(BigInt(value)), index); - } - return as(new env[type](value), index); - }; - - return unpair; -}; - -/** - * @typedef {Array} Record a type representation - */ - -/** - * Returns a deserialized value from a serialized array of Records. - * @param {Record[]} serialized a previously serialized value. - * @returns {any} - */ -const deserialize = serialized => deserializer(new Map, serialized)(0); - -const EMPTY = ''; - -const {toString} = {}; -const {keys} = Object; - -const typeOf = value => { - const type = typeof value; - if (type !== 'object' || !value) - return [PRIMITIVE, type]; - - const asString = toString.call(value).slice(8, -1); - switch (asString) { - case 'Array': - return [ARRAY, EMPTY]; - case 'Object': - return [OBJECT, EMPTY]; - case 'Date': - return [DATE, EMPTY]; - case 'RegExp': - return [REGEXP, EMPTY]; - case 'Map': - return [MAP, EMPTY]; - case 'Set': - return [SET, EMPTY]; - } - - if (asString.includes('Array')) - return [ARRAY, asString]; - - if (asString.includes('Error')) - return [ERROR, asString]; - - return [OBJECT, asString]; -}; - -const shouldSkip = ([TYPE, type]) => ( - TYPE === PRIMITIVE && - (type === 'function' || type === 'symbol') -); - -const serializer = (strict, json, $, _) => { - - const as = (out, value) => { - const index = _.push(out) - 1; - $.set(value, index); - return index; - }; - - const pair = value => { - if ($.has(value)) - return $.get(value); - - let [TYPE, type] = typeOf(value); - switch (TYPE) { - case PRIMITIVE: { - let entry = value; - switch (type) { - case 'bigint': - TYPE = BIGINT; - entry = value.toString(); - break; - case 'function': - case 'symbol': - if (strict) - throw new TypeError('unable to serialize ' + type); - entry = null; - break; - case 'undefined': - return as([VOID], value); - } - return as([TYPE, entry], value); - } - case ARRAY: { - if (type) - return as([type, [...value]], value); - - const arr = []; - const index = as([TYPE, arr], value); - for (const entry of value) - arr.push(pair(entry)); - return index; - } - case OBJECT: { - if (type) { - switch (type) { - case 'BigInt': - return as([type, value.toString()], value); - case 'Boolean': - case 'Number': - case 'String': - return as([type, value.valueOf()], value); - } - } - - if (json && ('toJSON' in value)) - return pair(value.toJSON()); - - const entries = []; - const index = as([TYPE, entries], value); - for (const key of keys(value)) { - if (strict || !shouldSkip(typeOf(value[key]))) - entries.push([pair(key), pair(value[key])]); - } - return index; - } - case DATE: - return as([TYPE, value.toISOString()], value); - case REGEXP: { - const {source, flags} = value; - return as([TYPE, {source, flags}], value); - } - case MAP: { - const entries = []; - const index = as([TYPE, entries], value); - for (const [key, entry] of value) { - if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry)))) - entries.push([pair(key), pair(entry)]); - } - return index; - } - case SET: { - const entries = []; - const index = as([TYPE, entries], value); - for (const entry of value) { - if (strict || !shouldSkip(typeOf(entry))) - entries.push(pair(entry)); - } - return index; - } - } - - const {message} = value; - return as([TYPE, {name: type, message}], value); - }; - - return pair; -}; - -/** - * @typedef {Array} Record a type representation - */ - -/** - * Returns an array of serialized Records. - * @param {any} value a serializable value. - * @param {{lossy?: boolean}?} options an object with a `lossy` property that, - * if `true`, will not throw errors on incompatible types, and behave more - * like JSON stringify would behave. Symbol and Function will be discarded. - * @returns {Record[]} - */ - const serialize = (value, {json, lossy} = {}) => { - const _ = []; - return serializer(!(json || lossy), !!json, new Map, _)(value), _; -}; - -/*! (c) Andrea Giammarchi - ISC */ - -const {parse: $parse, stringify: $stringify} = JSON; -const options = {json: true, lossy: true}; - -/** - * Revive a previously stringified structured clone. - * @param {string} str previously stringified data as string. - * @returns {any} whatever was previously stringified as clone. - */ -const parse = str => deserialize($parse(str)); - -/** - * Represent a structured clone value as string. - * @param {any} any some clone-able value to stringify. - * @returns {string} the value stringified. - */ -const stringify = any => $stringify(serialize(any, options)); - -var json = /*#__PURE__*/Object.freeze({ - __proto__: null, - parse: parse, - stringify: stringify -}); diff --git a/example/rollup.js b/example/rollup.js index a1de8bb..6ac61b0 100644 --- a/example/rollup.js +++ b/example/rollup.js @@ -2016,27 +2016,17 @@ async function transition(navigation) { return finalPromise; } -/** post rollup replace json **/ -const structuredClone = (await getStructuredCloneModule() - .catch(structuredCloneFallback)); -const getStructuredClone = () => structuredClone; -/** post rollup replace json **/ -async function getStructuredCloneModule() { - const { stringify, parse } = await Promise.resolve().then(function () { return json; }); - return { stringify, parse }; -} -function structuredCloneFallback() { - const stringify = JSON.stringify.bind(JSON), parse = JSON.parse.bind(JSON); - return { - stringify, - parse - }; +let _stringify = JSON.stringify.bind(JSON); +let _parse = JSON.parse.bind(JSON); +function configureSerialization(stringify, parse) { + _stringify = stringify; + _parse = parse; } -function stringify$1(value) { - return getStructuredClone().stringify(value); +function stringify(value) { + return _stringify(value); } -function parse$1(value) { - return getStructuredClone().parse(value); +function parse(value) { + return _parse(value); } const globalWindow = typeof window === "undefined" ? undefined : window; @@ -2098,7 +2088,7 @@ function setHistoryState(navigation, history, entry, persist, limit) { if (typeof sessionStorage === "undefined") return; try { - const raw = stringify$1(getSerializableState()); + const raw = stringify(getSerializableState()); sessionStorage.setItem(entry.key, raw); } catch { } @@ -2135,7 +2125,7 @@ function getHistoryState(history, entry) { const raw = sessionStorage.getItem(entry.key); if (!raw) return undefined; - const state = parse$1(raw); + const state = parse(raw); if (!isStateHistoryWithMeta(state)) return undefined; return state[NavigationKey].state; @@ -2686,270 +2676,5 @@ function applyPolyfill(options = DEFAULT_POLYFILL_OPTIONS) { return navigation; } -const VOID = -1; -const PRIMITIVE = 0; -const ARRAY = 1; -const OBJECT = 2; -const DATE = 3; -const REGEXP = 4; -const MAP = 5; -const SET = 6; -const ERROR = 7; -const BIGINT = 8; -// export const SYMBOL = 9; - -const env = typeof self === 'object' ? self : globalThis; - -const deserializer = ($, _) => { - const as = (out, index) => { - $.set(index, out); - return out; - }; - - const unpair = index => { - if ($.has(index)) - return $.get(index); - - const [type, value] = _[index]; - switch (type) { - case PRIMITIVE: - case VOID: - return as(value, index); - case ARRAY: { - const arr = as([], index); - for (const index of value) - arr.push(unpair(index)); - return arr; - } - case OBJECT: { - const object = as({}, index); - for (const [key, index] of value) - object[unpair(key)] = unpair(index); - return object; - } - case DATE: - return as(new Date(value), index); - case REGEXP: { - const {source, flags} = value; - return as(new RegExp(source, flags), index); - } - case MAP: { - const map = as(new Map, index); - for (const [key, index] of value) - map.set(unpair(key), unpair(index)); - return map; - } - case SET: { - const set = as(new Set, index); - for (const index of value) - set.add(unpair(index)); - return set; - } - case ERROR: { - const {name, message} = value; - return as(new env[name](message), index); - } - case BIGINT: - return as(BigInt(value), index); - case 'BigInt': - return as(Object(BigInt(value)), index); - } - return as(new env[type](value), index); - }; - - return unpair; -}; - -/** - * @typedef {Array} Record a type representation - */ - -/** - * Returns a deserialized value from a serialized array of Records. - * @param {Record[]} serialized a previously serialized value. - * @returns {any} - */ -const deserialize = serialized => deserializer(new Map, serialized)(0); - -const EMPTY = ''; - -const {toString} = {}; -const {keys} = Object; - -const typeOf = value => { - const type = typeof value; - if (type !== 'object' || !value) - return [PRIMITIVE, type]; - - const asString = toString.call(value).slice(8, -1); - switch (asString) { - case 'Array': - return [ARRAY, EMPTY]; - case 'Object': - return [OBJECT, EMPTY]; - case 'Date': - return [DATE, EMPTY]; - case 'RegExp': - return [REGEXP, EMPTY]; - case 'Map': - return [MAP, EMPTY]; - case 'Set': - return [SET, EMPTY]; - } - - if (asString.includes('Array')) - return [ARRAY, asString]; - - if (asString.includes('Error')) - return [ERROR, asString]; - - return [OBJECT, asString]; -}; - -const shouldSkip = ([TYPE, type]) => ( - TYPE === PRIMITIVE && - (type === 'function' || type === 'symbol') -); - -const serializer = (strict, json, $, _) => { - - const as = (out, value) => { - const index = _.push(out) - 1; - $.set(value, index); - return index; - }; - - const pair = value => { - if ($.has(value)) - return $.get(value); - - let [TYPE, type] = typeOf(value); - switch (TYPE) { - case PRIMITIVE: { - let entry = value; - switch (type) { - case 'bigint': - TYPE = BIGINT; - entry = value.toString(); - break; - case 'function': - case 'symbol': - if (strict) - throw new TypeError('unable to serialize ' + type); - entry = null; - break; - case 'undefined': - return as([VOID], value); - } - return as([TYPE, entry], value); - } - case ARRAY: { - if (type) - return as([type, [...value]], value); - - const arr = []; - const index = as([TYPE, arr], value); - for (const entry of value) - arr.push(pair(entry)); - return index; - } - case OBJECT: { - if (type) { - switch (type) { - case 'BigInt': - return as([type, value.toString()], value); - case 'Boolean': - case 'Number': - case 'String': - return as([type, value.valueOf()], value); - } - } - - if (json && ('toJSON' in value)) - return pair(value.toJSON()); - - const entries = []; - const index = as([TYPE, entries], value); - for (const key of keys(value)) { - if (strict || !shouldSkip(typeOf(value[key]))) - entries.push([pair(key), pair(value[key])]); - } - return index; - } - case DATE: - return as([TYPE, value.toISOString()], value); - case REGEXP: { - const {source, flags} = value; - return as([TYPE, {source, flags}], value); - } - case MAP: { - const entries = []; - const index = as([TYPE, entries], value); - for (const [key, entry] of value) { - if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry)))) - entries.push([pair(key), pair(entry)]); - } - return index; - } - case SET: { - const entries = []; - const index = as([TYPE, entries], value); - for (const entry of value) { - if (strict || !shouldSkip(typeOf(entry))) - entries.push(pair(entry)); - } - return index; - } - } - - const {message} = value; - return as([TYPE, {name: type, message}], value); - }; - - return pair; -}; - -/** - * @typedef {Array} Record a type representation - */ - -/** - * Returns an array of serialized Records. - * @param {any} value a serializable value. - * @param {{lossy?: boolean}?} options an object with a `lossy` property that, - * if `true`, will not throw errors on incompatible types, and behave more - * like JSON stringify would behave. Symbol and Function will be discarded. - * @returns {Record[]} - */ - const serialize = (value, {json, lossy} = {}) => { - const _ = []; - return serializer(!(json || lossy), !!json, new Map, _)(value), _; -}; - -/*! (c) Andrea Giammarchi - ISC */ - -const {parse: $parse, stringify: $stringify} = JSON; -const options = {json: true, lossy: true}; - -/** - * Revive a previously stringified structured clone. - * @param {string} str previously stringified data as string. - * @returns {any} whatever was previously stringified as clone. - */ -const parse = str => deserialize($parse(str)); - -/** - * Represent a structured clone value as string. - * @param {any} any some clone-able value to stringify. - * @returns {string} the value stringified. - */ -const stringify = any => $stringify(serialize(any, options)); - -var json = /*#__PURE__*/Object.freeze({ - __proto__: null, - parse: parse, - stringify: stringify -}); - -export { AppLocationAwaitFinished, AppLocationCheckChange, AppLocationTransitionURL, AppLocationUrl, EventTarget, NAVIGATION_LOCATION_DEFAULT_URL, Navigation, NavigationCanIntercept, NavigationCurrentEntryChangeEvent, NavigationDisposeState, NavigationFormData, NavigationGetState, NavigationHistory, NavigationLocation, NavigationSetCurrentIndex, NavigationSetCurrentKey, NavigationSetEntries, NavigationSetOptions, NavigationSetState, NavigationSync, NavigationTransitionFinally, NavigationUserInitiated, applyPolyfill, getCompletePolyfill, getPolyfill, isInterceptEvent, isNavigationNavigationType, transition }; +export { AppLocationAwaitFinished, AppLocationCheckChange, AppLocationTransitionURL, AppLocationUrl, EventTarget, NAVIGATION_LOCATION_DEFAULT_URL, Navigation, NavigationCanIntercept, NavigationCurrentEntryChangeEvent, NavigationDisposeState, NavigationFormData, NavigationGetState, NavigationHistory, NavigationLocation, NavigationSetCurrentIndex, NavigationSetCurrentKey, NavigationSetEntries, NavigationSetOptions, NavigationSetState, NavigationSync, NavigationTransitionFinally, NavigationUserInitiated, applyPolyfill, configureSerialization, getCompletePolyfill, getPolyfill, isInterceptEvent, isNavigationNavigationType, transition }; //# sourceMappingURL=rollup.js.map diff --git a/scripts/post-build.js b/scripts/post-build.js index 2bec979..86219f3 100644 --- a/scripts/post-build.js +++ b/scripts/post-build.js @@ -213,43 +213,11 @@ if (!process.env.NO_COVERAGE_BADGE_UPDATE) { { - async function rollupReplacements(fileName) { - let file = await fs.readFile(fileName, "utf-8"); - - const importJsonMarker = "/** post rollup replace json **/"; - - function replaceInsideMarkers(marker, replacement) { - const startIndex = file.indexOf(marker), - endIndex = file.lastIndexOf(marker) + marker.length; - - const fileStart = file.slice(0, startIndex - 1), - fileEnd = file.slice(endIndex + 1); - - const replacing = file.slice(startIndex, endIndex); - - if (typeof replacement === "function") { - replacement = replacement(replacing.replaceAll(marker, "")); - } - - file = `${fileStart}\n\n${replacement}\n\n${fileEnd}` - } - - replaceInsideMarkers(importJsonMarker, "const getStructuredClone = () => json;"); - - await fs.writeFile(fileName, file); - } - - await rollupReplacements("esnext/polyfill-rollup.js"); - - await fs.cp("esnext/rollup.js", "esnext/rollup-input.cjs"); - - await rollupReplacements("esnext/rollup-input.cjs"); - { const bundle = await rollup({ - input: "./esnext/rollup-input.cjs", + input: "./esnext/rollup.js", plugins: [ ignore([ `${cwd}/esnext/tests/navigation.playwright.js`, @@ -277,8 +245,6 @@ if (!process.env.NO_COVERAGE_BADGE_UPDATE) { }); } - await fs.rm("./esnext/rollup-input.cjs"); - await fs.cp("esnext/polyfill-rollup.js", "example/polyfill-rollup.js"); await fs.cp("esnext/routes-rollup.js", "example/routes-rollup.js"); await fs.cp("esnext/rollup.js", "example/rollup.js");