react-lazy-data
Advanced tools
Comparing version 0.2.2 to 0.2.3
# Changelog | ||
## 0.2.3 | ||
Bug fixes: | ||
* Fix ESM export missing `preloadData` | ||
Improvements: | ||
* Build server code as ES6 | ||
Dependencies: | ||
* Update `react-async-ssr` dependency | ||
* Update `babel-unique-id` dependency | ||
Refactor: | ||
* Split client and server code into folders | ||
No code: | ||
* Code comments | ||
Dev: | ||
* Remove dev code when testing in production mode [fix] | ||
* Test ESM export directly | ||
## 0.2.2 | ||
@@ -4,0 +32,0 @@ |
@@ -17,8 +17,6 @@ 'use strict'; | ||
var PLUGIN_NAME = 'react-lazy-data/babel'; // Exports | ||
const PLUGIN_NAME = 'react-lazy-data/babel'; // Exports | ||
// Invariant function with prefix | ||
var invariant = function (condition, message) { | ||
return invariant$1(condition, PLUGIN_NAME + ": " + message); | ||
} ; | ||
const invariant = __DEV__ ? (condition, message) => invariant$1(condition, `${PLUGIN_NAME}: ${message}`) : invariant$1; | ||
/** | ||
@@ -57,15 +55,18 @@ * Babel plugin. | ||
// NB `babel-unique-id` validates the other options | ||
var cacheVar = options.cacheVar; | ||
!(cacheVar === undefined || isItType.isFullString(cacheVar)) ? invariant(false, "options.cacheVar must be a non-empty string if provided - got " + cacheVar) : void 0; // Disregard the default as it's unnecessary | ||
let { | ||
cacheVar | ||
} = options; | ||
invariant(cacheVar === undefined || isItType.isFullString(cacheVar), `options.cacheVar must be a non-empty string if provided - got ${cacheVar}`); // Disregard the default as it's unnecessary | ||
if (cacheVar === constants.DEFAULT_CACHE_VAR) cacheVar = undefined; // ID options | ||
var idOptions = lodash.pick(options, ['rootPath', 'isPackage', 'packageName', 'packageVersion', 'idLength']); | ||
idOptions.pluginName = PLUGIN_NAME; // Return plugin | ||
const idOptions = lodash.pick(options, ['rootPath', 'isPackage', 'packageName', 'packageVersion', 'idLength']); | ||
if (__DEV__) idOptions.pluginName = PLUGIN_NAME; // Return plugin | ||
return { | ||
visitor: { | ||
CallExpression: function CallExpression(path, state) { | ||
CallExpression(path, state) { | ||
transform(path, state, idOptions, cacheVar, api.types); | ||
} | ||
} | ||
@@ -91,3 +92,3 @@ }; | ||
var optionsPath, cacheVar; | ||
let optionsPath, cacheVar; | ||
@@ -97,14 +98,16 @@ if (callPath.node.arguments.length > 1) { | ||
optionsPath = callPath.get('arguments.1'); | ||
var propsPaths = getObjectProps(optionsPath); | ||
var idProp = propsPaths.id, | ||
serializeProp = propsPaths.serialize, | ||
noSsrProp = propsPaths.noSsr; | ||
var cacheVarProp = propsPaths.cacheVar; // Validate options and remove default options | ||
const propsPaths = getObjectProps(optionsPath); | ||
const { | ||
id: idProp, | ||
serialize: serializeProp, | ||
noSsr: noSsrProp | ||
} = propsPaths; | ||
let cacheVarProp = propsPaths.cacheVar; // Validate options and remove default options | ||
var serialize = true, | ||
let serialize = true, | ||
noSsr = false; | ||
!(!idProp || idProp.valuePath.isStringLiteral()) ? invariant(false, '`createResourceFactory()` `id` option must be a string literal if defined') : void 0; | ||
invariant(!idProp || idProp.valuePath.isStringLiteral(), '`createResourceFactory()` `id` option must be a string literal if defined'); | ||
if (noSsrProp) { | ||
!noSsrProp.valuePath.isBooleanLiteral() ? invariant(false, '`createResourceFactory()` `noSsr` option must be a boolean literal if defined') : void 0; | ||
invariant(noSsrProp.valuePath.isBooleanLiteral(), '`createResourceFactory()` `noSsr` option must be a boolean literal if defined'); | ||
noSsr = noSsrProp.value; // Remove `noSsr: false` as it's the default | ||
@@ -116,3 +119,5 @@ | ||
if (serializeProp) { | ||
var valuePath = serializeProp.valuePath; | ||
const { | ||
valuePath | ||
} = serializeProp; | ||
@@ -124,3 +129,3 @@ if (valuePath.isBooleanLiteral()) { | ||
} else { | ||
!(valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression()) ? invariant(false, '`createResourceFactory()` `serialize` option must be a boolean or function literal') : void 0; | ||
invariant(valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression(), '`createResourceFactory()` `serialize` option must be a boolean or function literal'); | ||
} | ||
@@ -130,5 +135,5 @@ } | ||
if (cacheVarProp) { | ||
!cacheVarProp.valuePath.isStringLiteral() ? invariant(false, '`createResourceFactory()` `cacheVar` option must be a string literal if defined') : void 0; | ||
invariant(cacheVarProp.valuePath.isStringLiteral(), '`createResourceFactory()` `cacheVar` option must be a string literal if defined'); | ||
cacheVar = cacheVarProp.value; | ||
!(cacheVar !== '') ? invariant(false, '`createResourceFactory()` `cacheVar` option must not be empty string') : void 0; // Remove `cacheVar: <default>` as it's the default | ||
invariant(cacheVar !== '', '`createResourceFactory()` `cacheVar` option must not be empty string'); // Remove `cacheVar: <default>` as it's the default | ||
@@ -153,3 +158,3 @@ if (cacheVar === constants.DEFAULT_CACHE_VAR) { | ||
// Create options object | ||
var optionsNode = t.objectExpression([]); | ||
const optionsNode = t.objectExpression([]); | ||
callPath.pushContainer('arguments', optionsNode); | ||
@@ -160,3 +165,3 @@ optionsPath = callPath.get('arguments.1'); | ||
var id = createId(state, idOptions); | ||
const id = createId(state, idOptions); | ||
addStringPropToObject(optionsPath, 'id', id, t); // Add cache var to options | ||
@@ -180,15 +185,17 @@ | ||
function getObjectProps(objectPath) { | ||
!objectPath.isObjectExpression() ? invariant(false, '`createResourceFactory()` options must be an object literal') : void 0; | ||
var propsPaths = {}; | ||
invariant(objectPath.isObjectExpression(), '`createResourceFactory()` options must be an object literal'); | ||
const propsPaths = {}; | ||
for (var i = 0; i < objectPath.node.properties.length; i++) { | ||
var propPath = objectPath.get("properties." + i); | ||
var keyPath = propPath.get('key'); | ||
!keyPath.isIdentifier() ? invariant(false, '`createResourceFactory()` options must contain only identifiers as keys') : void 0; | ||
var valuePath = propPath.get('value'), | ||
value = valuePath.node.value; | ||
for (let i = 0; i < objectPath.node.properties.length; i++) { | ||
const propPath = objectPath.get(`properties.${i}`); | ||
const keyPath = propPath.get('key'); | ||
invariant(keyPath.isIdentifier(), '`createResourceFactory()` options must contain only identifiers as keys'); | ||
const valuePath = propPath.get('value'), | ||
{ | ||
value | ||
} = valuePath.node; | ||
propsPaths[keyPath.node.name] = { | ||
propPath: propPath, | ||
valuePath: valuePath, | ||
value: value | ||
propPath, | ||
valuePath, | ||
value | ||
}; | ||
@@ -223,3 +230,3 @@ } | ||
function addPropToObject(objectPath, key, valuePath, t) { | ||
var propPath = t.objectProperty(t.identifier(key), valuePath); | ||
const propPath = t.objectProperty(t.identifier(key), valuePath); | ||
objectPath.pushContainer('properties', propPath); | ||
@@ -226,0 +233,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var r=require("is-it-type"),i=e(require("tiny-invariant")),t=require("./constants.min.js"),a=e(require("babel-unique-id")),n=require("lodash"),o=i;function s(e,r,i,t){!function(e,r,i,t){var a=t.objectProperty(t.identifier(r),i);e.pushContainer("properties",a)}(e,r,t.stringLiteral(i),t)}module.exports=function(e,i){var u=i.cacheVar;void 0===u||r.isFullString(u)||o(!1),u===t.DEFAULT_CACHE_VAR&&(u=void 0);var v=n.pick(i,["rootPath","isPackage","packageName","packageVersion","idLength"]);return{visitor:{CallExpression:function(r,i){!function(e,r,i,n,u){if(!e.get("callee").isIdentifier({name:"createResourceFactory"}))return;var v,l;if(e.node.arguments.length>1){var p=function(e){e.isObjectExpression()||o(!1);for(var r={},i=0;i<e.node.properties.length;i++){var t=e.get("properties."+i),a=t.get("key");a.isIdentifier()||o(!1);var n=t.get("value"),s=n.node.value;r[a.node.name]={propPath:t,valuePath:n,value:s}}return r}(v=e.get("arguments.1")),c=p.id,h=p.serialize,g=p.noSsr,d=p.cacheVar,f=!0,m=!1;if(c&&!c.valuePath.isStringLiteral()&&o(!1),g&&(g.valuePath.isBooleanLiteral()||o(!1),(m=g.value)||g.propPath.remove()),h){var P=h.valuePath;P.isBooleanLiteral()?(f=h.value)&&!m&&h.propPath.remove():P.isFunctionExpression()||P.isArrowFunctionExpression()||o(!1)}if(d&&(d.valuePath.isStringLiteral()||o(!1),""===(l=d.value)&&o(!1),l===t.DEFAULT_CACHE_VAR&&(d.propPath.remove(),d=void 0)),!f||m)return c&&c.propPath.remove(),void(d&&d.propPath.remove());if(c)return}else{var E=u.objectExpression([]);e.pushContainer("arguments",E),v=e.get("arguments.1")}var L=a(r,i);s(v,"id",L,u),l||(l=n)&&s(v,"cacheVar",l,u)}(r,i,v,u,e.types)}}}}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("is-it-type"),r=e(require("tiny-invariant")),o=require("./constants.min.js"),i=e(require("babel-unique-id")),a=require("lodash");const n=__DEV__?(e,t)=>r(e,`react-lazy-data/babel: ${t}`):r;function s(e,t,r,o){!function(e,t,r,o){const i=o.objectProperty(o.identifier(t),r);e.pushContainer("properties",i)}(e,t,o.stringLiteral(r),o)}module.exports=function(e,r){let{cacheVar:c}=r;n(void 0===c||t.isFullString(c),`options.cacheVar must be a non-empty string if provided - got ${c}`),c===o.DEFAULT_CACHE_VAR&&(c=void 0);const u=a.pick(r,["rootPath","isPackage","packageName","packageVersion","idLength"]);return __DEV__&&(u.pluginName="react-lazy-data/babel"),{visitor:{CallExpression(t,r){!function(e,t,r,a,c){if(!e.get("callee").isIdentifier({name:"createResourceFactory"}))return;let u,l;if(e.node.arguments.length>1){u=e.get("arguments.1");const t=function(e){n(e.isObjectExpression(),"`createResourceFactory()` options must be an object literal");const t={};for(let r=0;r<e.node.properties.length;r++){const o=e.get(`properties.${r}`),i=o.get("key");n(i.isIdentifier(),"`createResourceFactory()` options must contain only identifiers as keys");const a=o.get("value"),{value:s}=a.node;t[i.node.name]={propPath:o,valuePath:a,value:s}}return t}(u),{id:r,serialize:i,noSsr:a}=t;let s=t.cacheVar,c=!0,p=!1;if(n(!r||r.valuePath.isStringLiteral(),"`createResourceFactory()` `id` option must be a string literal if defined"),a&&(n(a.valuePath.isBooleanLiteral(),"`createResourceFactory()` `noSsr` option must be a boolean literal if defined"),p=a.value,p||a.propPath.remove()),i){const{valuePath:e}=i;e.isBooleanLiteral()?(c=i.value,c&&!p&&i.propPath.remove()):n(e.isFunctionExpression()||e.isArrowFunctionExpression(),"`createResourceFactory()` `serialize` option must be a boolean or function literal")}if(s&&(n(s.valuePath.isStringLiteral(),"`createResourceFactory()` `cacheVar` option must be a string literal if defined"),l=s.value,n(""!==l,"`createResourceFactory()` `cacheVar` option must not be empty string"),l===o.DEFAULT_CACHE_VAR&&(s.propPath.remove(),s=void 0)),!c||p)return r&&r.propPath.remove(),void(s&&s.propPath.remove());if(r)return}else{const t=c.objectExpression([]);e.pushContainer("arguments",t),u=e.get("arguments.1")}const p=i(t,r);s(u,"id",p,c),l||(l=a,l&&s(u,"cacheVar",l,c))}(t,r,u,c,e.types)}}}}; | ||
//# sourceMappingURL=babel.min.js.map |
@@ -17,4 +17,4 @@ 'use strict'; | ||
var DataExtractor = /*#__PURE__*/function () { | ||
function DataExtractor(options) { | ||
class DataExtractor { | ||
constructor(options) { | ||
// Get cache var from options | ||
@@ -26,30 +26,27 @@ this._cacheVar = shared.getCacheVarFromOptionsWithValidate(options); // Init data cache | ||
var _proto = DataExtractor.prototype; | ||
_proto.collectData = function collectData(children) { | ||
collectData(children) { | ||
return DataExtractorManager({ | ||
extractor: this, | ||
children: children | ||
children | ||
}); | ||
}; | ||
} | ||
_proto.getData = function getData() { | ||
getData() { | ||
return this._cache; | ||
}; | ||
} | ||
_proto.getScript = function getScript(options) { | ||
getScript(options) { | ||
// Get cache var from options, or constructor options | ||
var cacheVar = shared.getCacheVarFromOptionsWithValidate(options, this._cacheVar); // Get data | ||
const cacheVar = shared.getCacheVarFromOptionsWithValidate(options, this._cacheVar); // Get data | ||
var data = this.getData(); // Return script tag containing data | ||
const data = this.getData(); // Return script tag containing data | ||
var cacheVarStr = jsonify(cacheVar); | ||
return "<script>(window[" + cacheVarStr + "]=window[" + cacheVarStr + "]||{}).data=" + jsonify(data) + "</script>"; | ||
}; | ||
const cacheVarStr = jsonify(cacheVar); | ||
return `<script>(window[${cacheVarStr}]=window[${cacheVarStr}]||{}).data=${jsonify(data)}</script>`; | ||
} | ||
return DataExtractor; | ||
}(); | ||
} | ||
var Ctx = /*#__PURE__*/function () { | ||
function Ctx(extractor) { | ||
class Ctx { | ||
constructor(extractor) { | ||
this._cache = {}; | ||
@@ -59,16 +56,14 @@ this._ssrCache = extractor._cache; | ||
var _proto2 = Ctx.prototype; | ||
_proto2.getCache = function getCache(factory) { | ||
getCache(factory) { | ||
// Get factory ID | ||
var id = factory._id; | ||
const id = factory._id; | ||
if (!id) return undefined; | ||
return getValuesCache(this._cache, id); | ||
}; | ||
} | ||
_proto2.register = function register(resource, willRender) { | ||
register(resource, willRender) { | ||
// If will not render, exit - data is not needed | ||
if (!willRender) return; // Get factory ID | ||
var id = resource._factory._id; | ||
const id = resource._factory._id; | ||
if (!id) return; // Get master resource | ||
@@ -78,3 +73,3 @@ | ||
// eslint-disable-line no-constant-condition | ||
var parent = resource._parent; | ||
const parent = resource._parent; | ||
if (!parent) break; | ||
@@ -85,15 +80,15 @@ resource = parent; | ||
var valuesCache = getValuesCache(this._ssrCache, id); | ||
const valuesCache = getValuesCache(this._ssrCache, id); | ||
valuesCache[resource._cacheKey] = resource._value; | ||
}; | ||
} | ||
return Ctx; | ||
}(); | ||
} | ||
function DataExtractorManager(_ref) { | ||
var extractor = _ref.extractor, | ||
children = _ref.children; | ||
function DataExtractorManager({ | ||
extractor, | ||
children | ||
}) { | ||
return react.createElement(shared.ServerContext.Provider, { | ||
value: new Ctx(extractor), | ||
children: children | ||
children | ||
}); | ||
@@ -106,3 +101,3 @@ } | ||
function getValuesCache(cache, id) { | ||
var valuesCache = cache[id]; | ||
let valuesCache = cache[id]; | ||
@@ -109,0 +104,0 @@ if (!valuesCache) { |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");require("is-it-type"),require("tiny-invariant"),require("./constants.min.js");var r=require("./shared.min.js"),e=function(){function t(t){this._cacheVar=r.getCacheVarFromOptionsWithValidate(t),this._cache={}}var e=t.prototype;return e.collectData=function(t){return i({extractor:this,children:t})},e.getData=function(){return this._cache},e.getScript=function(t){var e=r.getCacheVarFromOptionsWithValidate(t,this._cacheVar),a=this.getData(),i=c(e);return"<script>(window["+i+"]=window["+i+"]||{}).data="+c(a)+"<\/script>"},t}(),a=function(){function t(t){this._cache={},this._ssrCache=t._cache}var r=t.prototype;return r.getCache=function(t){var r=t._id;if(r)return n(this._cache,r)},r.register=function(t,r){if(r){var e=t._factory._id;if(e){for(;;){var a=t._parent;if(!a)break;t=a}n(this._ssrCache,e)[t._cacheKey]=t._value}}},t}();function i(e){var i=e.extractor,n=e.children;return t.createElement(r.ServerContext.Provider,{value:new a(i),children:n})}function n(t,r){var e=t[r];return e||(e={},t[r]=e),e}function c(t){return JSON.stringify(t).replace(/<\//g,"<\\/")}exports.DataExtractor=e,exports.DataExtractorManager=i; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");require("is-it-type"),require("tiny-invariant"),require("./constants.min.js");var e=require("./shared.min.js");class r{constructor(t){this._cache={},this._ssrCache=t._cache}getCache(t){const e=t._id;if(e)return c(this._cache,e)}register(t,e){if(!e)return;const r=t._factory._id;if(r){for(;;){const e=t._parent;if(!e)break;t=e}c(this._ssrCache,r)[t._cacheKey]=t._value}}}function a({extractor:a,children:c}){return t.createElement(e.ServerContext.Provider,{value:new r(a),children:c})}function c(t,e){let r=t[e];return r||(r={},t[e]=r),r}function i(t){return JSON.stringify(t).replace(/<\//g,"<\\/")}exports.DataExtractor=class{constructor(t){this._cacheVar=e.getCacheVarFromOptionsWithValidate(t),this._cache={}}collectData(t){return a({extractor:this,children:t})}getData(){return this._cache}getScript(t){const r=e.getCacheVarFromOptionsWithValidate(t,this._cacheVar),a=this.getData(),c=i(r);return`<script>(window[${c}]=window[${c}]||{}).data=${i(a)}<\/script>`}},exports.DataExtractorManager=a; | ||
//# sourceMappingURL=server.min.js.map |
@@ -13,8 +13,6 @@ import { isFullString } from 'is-it-type'; | ||
var PLUGIN_NAME = 'react-lazy-data/babel'; // Exports | ||
const PLUGIN_NAME = 'react-lazy-data/babel'; // Exports | ||
// Invariant function with prefix | ||
var invariant = function (condition, message) { | ||
return invariant$1(condition, PLUGIN_NAME + ": " + message); | ||
} ; | ||
const invariant = __DEV__ ? (condition, message) => invariant$1(condition, `${PLUGIN_NAME}: ${message}`) : invariant$1; | ||
/** | ||
@@ -53,15 +51,18 @@ * Babel plugin. | ||
// NB `babel-unique-id` validates the other options | ||
var cacheVar = options.cacheVar; | ||
!(cacheVar === undefined || isFullString(cacheVar)) ? invariant(false, "options.cacheVar must be a non-empty string if provided - got " + cacheVar) : void 0; // Disregard the default as it's unnecessary | ||
let { | ||
cacheVar | ||
} = options; | ||
invariant(cacheVar === undefined || isFullString(cacheVar), `options.cacheVar must be a non-empty string if provided - got ${cacheVar}`); // Disregard the default as it's unnecessary | ||
if (cacheVar === DEFAULT_CACHE_VAR) cacheVar = undefined; // ID options | ||
var idOptions = pick(options, ['rootPath', 'isPackage', 'packageName', 'packageVersion', 'idLength']); | ||
idOptions.pluginName = PLUGIN_NAME; // Return plugin | ||
const idOptions = pick(options, ['rootPath', 'isPackage', 'packageName', 'packageVersion', 'idLength']); | ||
if (__DEV__) idOptions.pluginName = PLUGIN_NAME; // Return plugin | ||
return { | ||
visitor: { | ||
CallExpression: function CallExpression(path, state) { | ||
CallExpression(path, state) { | ||
transform(path, state, idOptions, cacheVar, api.types); | ||
} | ||
} | ||
@@ -87,3 +88,3 @@ }; | ||
var optionsPath, cacheVar; | ||
let optionsPath, cacheVar; | ||
@@ -93,14 +94,16 @@ if (callPath.node.arguments.length > 1) { | ||
optionsPath = callPath.get('arguments.1'); | ||
var propsPaths = getObjectProps(optionsPath); | ||
var idProp = propsPaths.id, | ||
serializeProp = propsPaths.serialize, | ||
noSsrProp = propsPaths.noSsr; | ||
var cacheVarProp = propsPaths.cacheVar; // Validate options and remove default options | ||
const propsPaths = getObjectProps(optionsPath); | ||
const { | ||
id: idProp, | ||
serialize: serializeProp, | ||
noSsr: noSsrProp | ||
} = propsPaths; | ||
let cacheVarProp = propsPaths.cacheVar; // Validate options and remove default options | ||
var serialize = true, | ||
let serialize = true, | ||
noSsr = false; | ||
!(!idProp || idProp.valuePath.isStringLiteral()) ? invariant(false, '`createResourceFactory()` `id` option must be a string literal if defined') : void 0; | ||
invariant(!idProp || idProp.valuePath.isStringLiteral(), '`createResourceFactory()` `id` option must be a string literal if defined'); | ||
if (noSsrProp) { | ||
!noSsrProp.valuePath.isBooleanLiteral() ? invariant(false, '`createResourceFactory()` `noSsr` option must be a boolean literal if defined') : void 0; | ||
invariant(noSsrProp.valuePath.isBooleanLiteral(), '`createResourceFactory()` `noSsr` option must be a boolean literal if defined'); | ||
noSsr = noSsrProp.value; // Remove `noSsr: false` as it's the default | ||
@@ -112,3 +115,5 @@ | ||
if (serializeProp) { | ||
var valuePath = serializeProp.valuePath; | ||
const { | ||
valuePath | ||
} = serializeProp; | ||
@@ -120,3 +125,3 @@ if (valuePath.isBooleanLiteral()) { | ||
} else { | ||
!(valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression()) ? invariant(false, '`createResourceFactory()` `serialize` option must be a boolean or function literal') : void 0; | ||
invariant(valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression(), '`createResourceFactory()` `serialize` option must be a boolean or function literal'); | ||
} | ||
@@ -126,5 +131,5 @@ } | ||
if (cacheVarProp) { | ||
!cacheVarProp.valuePath.isStringLiteral() ? invariant(false, '`createResourceFactory()` `cacheVar` option must be a string literal if defined') : void 0; | ||
invariant(cacheVarProp.valuePath.isStringLiteral(), '`createResourceFactory()` `cacheVar` option must be a string literal if defined'); | ||
cacheVar = cacheVarProp.value; | ||
!(cacheVar !== '') ? invariant(false, '`createResourceFactory()` `cacheVar` option must not be empty string') : void 0; // Remove `cacheVar: <default>` as it's the default | ||
invariant(cacheVar !== '', '`createResourceFactory()` `cacheVar` option must not be empty string'); // Remove `cacheVar: <default>` as it's the default | ||
@@ -149,3 +154,3 @@ if (cacheVar === DEFAULT_CACHE_VAR) { | ||
// Create options object | ||
var optionsNode = t.objectExpression([]); | ||
const optionsNode = t.objectExpression([]); | ||
callPath.pushContainer('arguments', optionsNode); | ||
@@ -156,3 +161,3 @@ optionsPath = callPath.get('arguments.1'); | ||
var id = createId(state, idOptions); | ||
const id = createId(state, idOptions); | ||
addStringPropToObject(optionsPath, 'id', id, t); // Add cache var to options | ||
@@ -176,15 +181,17 @@ | ||
function getObjectProps(objectPath) { | ||
!objectPath.isObjectExpression() ? invariant(false, '`createResourceFactory()` options must be an object literal') : void 0; | ||
var propsPaths = {}; | ||
invariant(objectPath.isObjectExpression(), '`createResourceFactory()` options must be an object literal'); | ||
const propsPaths = {}; | ||
for (var i = 0; i < objectPath.node.properties.length; i++) { | ||
var propPath = objectPath.get("properties." + i); | ||
var keyPath = propPath.get('key'); | ||
!keyPath.isIdentifier() ? invariant(false, '`createResourceFactory()` options must contain only identifiers as keys') : void 0; | ||
var valuePath = propPath.get('value'), | ||
value = valuePath.node.value; | ||
for (let i = 0; i < objectPath.node.properties.length; i++) { | ||
const propPath = objectPath.get(`properties.${i}`); | ||
const keyPath = propPath.get('key'); | ||
invariant(keyPath.isIdentifier(), '`createResourceFactory()` options must contain only identifiers as keys'); | ||
const valuePath = propPath.get('value'), | ||
{ | ||
value | ||
} = valuePath.node; | ||
propsPaths[keyPath.node.name] = { | ||
propPath: propPath, | ||
valuePath: valuePath, | ||
value: value | ||
propPath, | ||
valuePath, | ||
value | ||
}; | ||
@@ -219,3 +226,3 @@ } | ||
function addPropToObject(objectPath, key, valuePath, t) { | ||
var propPath = t.objectProperty(t.identifier(key), valuePath); | ||
const propPath = t.objectProperty(t.identifier(key), valuePath); | ||
objectPath.pushContainer('properties', propPath); | ||
@@ -222,0 +229,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import{isFullString as e}from"is-it-type";import r from"tiny-invariant";import{D as t}from"./constants.min.js";import a from"babel-unique-id";import{pick as i}from"lodash";var o=r;function n(e,r,t,a){!function(e,r,t,a){var i=a.objectProperty(a.identifier(r),t);e.pushContainer("properties",i)}(e,r,a.stringLiteral(t),a)}export default function(r,s){var p=s.cacheVar;void 0===p||e(p)||o(!1),p===t&&(p=void 0);var u=i(s,["rootPath","isPackage","packageName","packageVersion","idLength"]);return{visitor:{CallExpression:function(e,i){!function(e,r,i,s,p){if(!e.get("callee").isIdentifier({name:"createResourceFactory"}))return;var u,v;if(e.node.arguments.length>1){var l=function(e){e.isObjectExpression()||o(!1);for(var r={},t=0;t<e.node.properties.length;t++){var a=e.get("properties."+t),i=a.get("key");i.isIdentifier()||o(!1);var n=a.get("value"),s=n.node.value;r[i.node.name]={propPath:a,valuePath:n,value:s}}return r}(u=e.get("arguments.1")),c=l.id,m=l.serialize,f=l.noSsr,h=l.cacheVar,g=!0,d=!1;if(c&&!c.valuePath.isStringLiteral()&&o(!1),f&&(f.valuePath.isBooleanLiteral()||o(!1),(d=f.value)||f.propPath.remove()),m){var P=m.valuePath;P.isBooleanLiteral()?(g=m.value)&&!d&&m.propPath.remove():P.isFunctionExpression()||P.isArrowFunctionExpression()||o(!1)}if(h&&(h.valuePath.isStringLiteral()||o(!1),""===(v=h.value)&&o(!1),v===t&&(h.propPath.remove(),h=void 0)),!g||d)return c&&c.propPath.remove(),void(h&&h.propPath.remove());if(c)return}else{var x=p.objectExpression([]);e.pushContainer("arguments",x),u=e.get("arguments.1")}var y=a(r,i);n(u,"id",y,p),v||(v=s)&&n(u,"cacheVar",v,p)}(e,i,u,p,r.types)}}}} | ||
import{isFullString as e}from"is-it-type";import t from"tiny-invariant";import{D as o}from"./constants.min.js";import r from"babel-unique-id";import{pick as a}from"lodash";const i=__DEV__?(e,o)=>t(e,`react-lazy-data/babel: ${o}`):t;function n(e,t,o,r){!function(e,t,o,r){const a=r.objectProperty(r.identifier(t),o);e.pushContainer("properties",a)}(e,t,r.stringLiteral(o),r)}export default function(t,s){let{cacheVar:c}=s;i(void 0===c||e(c),`options.cacheVar must be a non-empty string if provided - got ${c}`),c===o&&(c=void 0);const l=a(s,["rootPath","isPackage","packageName","packageVersion","idLength"]);return __DEV__&&(l.pluginName="react-lazy-data/babel"),{visitor:{CallExpression(e,a){!function(e,t,a,s,c){if(!e.get("callee").isIdentifier({name:"createResourceFactory"}))return;let l,u;if(e.node.arguments.length>1){l=e.get("arguments.1");const t=function(e){i(e.isObjectExpression(),"`createResourceFactory()` options must be an object literal");const t={};for(let o=0;o<e.node.properties.length;o++){const r=e.get(`properties.${o}`),a=r.get("key");i(a.isIdentifier(),"`createResourceFactory()` options must contain only identifiers as keys");const n=r.get("value"),{value:s}=n.node;t[a.node.name]={propPath:r,valuePath:n,value:s}}return t}(l),{id:r,serialize:a,noSsr:n}=t;let s=t.cacheVar,c=!0,p=!1;if(i(!r||r.valuePath.isStringLiteral(),"`createResourceFactory()` `id` option must be a string literal if defined"),n&&(i(n.valuePath.isBooleanLiteral(),"`createResourceFactory()` `noSsr` option must be a boolean literal if defined"),p=n.value,p||n.propPath.remove()),a){const{valuePath:e}=a;e.isBooleanLiteral()?(c=a.value,c&&!p&&a.propPath.remove()):i(e.isFunctionExpression()||e.isArrowFunctionExpression(),"`createResourceFactory()` `serialize` option must be a boolean or function literal")}if(s&&(i(s.valuePath.isStringLiteral(),"`createResourceFactory()` `cacheVar` option must be a string literal if defined"),u=s.value,i(""!==u,"`createResourceFactory()` `cacheVar` option must not be empty string"),u===o&&(s.propPath.remove(),s=void 0)),!c||p)return r&&r.propPath.remove(),void(s&&s.propPath.remove());if(r)return}else{const t=c.objectExpression([]);e.pushContainer("arguments",t),l=e.get("arguments.1")}const p=r(t,a);n(l,"id",p,c),u||(u=s,u&&n(l,"cacheVar",u,c))}(e,a,l,c,t.types)}}}} | ||
//# sourceMappingURL=babel.min.js.map |
@@ -5,3 +5,3 @@ import { createElement } from 'react'; | ||
import './constants.js'; | ||
import { S as ServerContext, a as getCacheVarFromOptionsWithValidate } from './shared.js'; | ||
import { a as getCacheVarFromOptionsWithValidate, S as ServerContext } from './shared.js'; | ||
@@ -14,4 +14,4 @@ /* -------------------- | ||
var DataExtractor = /*#__PURE__*/function () { | ||
function DataExtractor(options) { | ||
class DataExtractor { | ||
constructor(options) { | ||
// Get cache var from options | ||
@@ -23,30 +23,27 @@ this._cacheVar = getCacheVarFromOptionsWithValidate(options); // Init data cache | ||
var _proto = DataExtractor.prototype; | ||
_proto.collectData = function collectData(children) { | ||
collectData(children) { | ||
return DataExtractorManager({ | ||
extractor: this, | ||
children: children | ||
children | ||
}); | ||
}; | ||
} | ||
_proto.getData = function getData() { | ||
getData() { | ||
return this._cache; | ||
}; | ||
} | ||
_proto.getScript = function getScript(options) { | ||
getScript(options) { | ||
// Get cache var from options, or constructor options | ||
var cacheVar = getCacheVarFromOptionsWithValidate(options, this._cacheVar); // Get data | ||
const cacheVar = getCacheVarFromOptionsWithValidate(options, this._cacheVar); // Get data | ||
var data = this.getData(); // Return script tag containing data | ||
const data = this.getData(); // Return script tag containing data | ||
var cacheVarStr = jsonify(cacheVar); | ||
return "<script>(window[" + cacheVarStr + "]=window[" + cacheVarStr + "]||{}).data=" + jsonify(data) + "</script>"; | ||
}; | ||
const cacheVarStr = jsonify(cacheVar); | ||
return `<script>(window[${cacheVarStr}]=window[${cacheVarStr}]||{}).data=${jsonify(data)}</script>`; | ||
} | ||
return DataExtractor; | ||
}(); | ||
} | ||
var Ctx = /*#__PURE__*/function () { | ||
function Ctx(extractor) { | ||
class Ctx { | ||
constructor(extractor) { | ||
this._cache = {}; | ||
@@ -56,16 +53,14 @@ this._ssrCache = extractor._cache; | ||
var _proto2 = Ctx.prototype; | ||
_proto2.getCache = function getCache(factory) { | ||
getCache(factory) { | ||
// Get factory ID | ||
var id = factory._id; | ||
const id = factory._id; | ||
if (!id) return undefined; | ||
return getValuesCache(this._cache, id); | ||
}; | ||
} | ||
_proto2.register = function register(resource, willRender) { | ||
register(resource, willRender) { | ||
// If will not render, exit - data is not needed | ||
if (!willRender) return; // Get factory ID | ||
var id = resource._factory._id; | ||
const id = resource._factory._id; | ||
if (!id) return; // Get master resource | ||
@@ -75,3 +70,3 @@ | ||
// eslint-disable-line no-constant-condition | ||
var parent = resource._parent; | ||
const parent = resource._parent; | ||
if (!parent) break; | ||
@@ -82,15 +77,15 @@ resource = parent; | ||
var valuesCache = getValuesCache(this._ssrCache, id); | ||
const valuesCache = getValuesCache(this._ssrCache, id); | ||
valuesCache[resource._cacheKey] = resource._value; | ||
}; | ||
} | ||
return Ctx; | ||
}(); | ||
} | ||
function DataExtractorManager(_ref) { | ||
var extractor = _ref.extractor, | ||
children = _ref.children; | ||
function DataExtractorManager({ | ||
extractor, | ||
children | ||
}) { | ||
return createElement(ServerContext.Provider, { | ||
value: new Ctx(extractor), | ||
children: children | ||
children | ||
}); | ||
@@ -103,3 +98,3 @@ } | ||
function getValuesCache(cache, id) { | ||
var valuesCache = cache[id]; | ||
let valuesCache = cache[id]; | ||
@@ -106,0 +101,0 @@ if (!valuesCache) { |
@@ -1,2 +0,2 @@ | ||
import{createElement as t}from"react";import"is-it-type";import"tiny-invariant";import"./constants.min.js";import{S as r,a as i}from"./shared.min.js";var n=function(){function t(t){this._cacheVar=i(t),this._cache={}}var r=t.prototype;return r.collectData=function(t){return a({extractor:this,children:t})},r.getData=function(){return this._cache},r.getScript=function(t){var r=i(t,this._cacheVar),n=this.getData(),e=o(r);return"<script>(window["+e+"]=window["+e+"]||{}).data="+o(n)+"<\/script>"},t}(),e=function(){function t(t){this._cache={},this._ssrCache=t._cache}var r=t.prototype;return r.getCache=function(t){var r=t._id;if(r)return c(this._cache,r)},r.register=function(t,r){if(r){var i=t._factory._id;if(i){for(;;){var n=t._parent;if(!n)break;t=n}c(this._ssrCache,i)[t._cacheKey]=t._value}}},t}();function a(i){var n=i.extractor,a=i.children;return t(r.Provider,{value:new e(n),children:a})}function c(t,r){var i=t[r];return i||(i={},t[r]=i),i}function o(t){return JSON.stringify(t).replace(/<\//g,"<\\/")}export{n as DataExtractor,a as DataExtractorManager}; | ||
import{createElement as t}from"react";import"is-it-type";import"tiny-invariant";import"./constants.min.js";import{a as r,S as c}from"./shared.min.js";class e{constructor(t){this._cacheVar=r(t),this._cache={}}collectData(t){return a({extractor:this,children:t})}getData(){return this._cache}getScript(t){const c=r(t,this._cacheVar),e=this.getData(),i=n(c);return`<script>(window[${i}]=window[${i}]||{}).data=${n(e)}<\/script>`}}class i{constructor(t){this._cache={},this._ssrCache=t._cache}getCache(t){const r=t._id;if(r)return s(this._cache,r)}register(t,r){if(!r)return;const c=t._factory._id;if(c){for(;;){const r=t._parent;if(!r)break;t=r}s(this._ssrCache,c)[t._cacheKey]=t._value}}}function a({extractor:r,children:e}){return t(c.Provider,{value:new i(r),children:e})}function s(t,r){let c=t[r];return c||(c={},t[r]=c),c}function n(t){return JSON.stringify(t).replace(/<\//g,"<\\/")}export{e as DataExtractor,a as DataExtractorManager}; | ||
//# sourceMappingURL=server.min.js.map |
@@ -11,3 +11,4 @@ /* -------------------- | ||
isResource as isResourceProd, | ||
withResources as withResourcesProd | ||
withResources as withResourcesProd, | ||
preloadData as preloadDataProd | ||
} from '../dist/esm/index.min.js'; | ||
@@ -18,3 +19,4 @@ | ||
isResource as isResourceDev, | ||
withResources as withResourcesDev | ||
withResources as withResourcesDev, | ||
preloadData as preloadDataDev | ||
} from '../dist/esm/index.js'; | ||
@@ -33,1 +35,4 @@ | ||
: withResourcesDev; | ||
export const preloadData = process.env.NODE_ENV === 'production' | ||
? preloadDataProd | ||
: preloadDataDev; |
{ | ||
"name": "react-lazy-data", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Lazy-load data with React Suspense", | ||
@@ -37,3 +37,3 @@ "main": "index.js", | ||
"@babel/runtime": "^7.9.2", | ||
"babel-unique-id": "^0.1.2", | ||
"babel-unique-id": "^0.1.4", | ||
"is-class-component": "^1.1.1", | ||
@@ -43,3 +43,3 @@ "is-it-type": "^3.1.1", | ||
"lodash": "^4.17.15", | ||
"react-async-ssr": "^0.7.0", | ||
"react-async-ssr": "^0.7.1", | ||
"tiny-invariant": "^1.1.0" | ||
@@ -46,0 +46,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
385283
2272
10
Updatedbabel-unique-id@^0.1.4
Updatedreact-async-ssr@^0.7.1