@ninetailed/experience.js-gatsby 4.2.2 → 4.2.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gatsby-browser.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _tslib = require('./_tslib.js');
5
+ var serialize = require('./serialize.js');
6
6
  var React = require('react');
7
7
  var experience_js = require('@ninetailed/experience.js');
8
8
  var experience_jsReact = require('@ninetailed/experience.js-react');
@@ -21,12 +21,21 @@ const WrapRootElement = _a => {
21
21
  var {
22
22
  children
23
23
  } = _a,
24
- options = _tslib.__rest(_a, ["children"]);
24
+ options = serialize.__rest(_a, ["children"]);
25
25
  if (!ninetailed) {
26
26
  const resolvedPlugins = loaders_ninetailedPlugins.plugins.map(({
27
27
  PluginCtor,
28
28
  options
29
- }) => new PluginCtor(options));
29
+ }) => {
30
+ if (!options) {
31
+ return new PluginCtor();
32
+ }
33
+ if (options && !options.serializedFunctionNames) {
34
+ return new PluginCtor(options);
35
+ }
36
+ const updatedOptions = Object.assign(Object.assign({}, options), serialize.deserializePluginOptionFns(options));
37
+ return new PluginCtor(updatedOptions);
38
+ });
30
39
  const {
31
40
  clientId,
32
41
  environment,
package/gatsby-node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PluginOptionsSchemaArgs, CreateWebpackConfigArgs, CreatePagesArgs } from 'gatsby';
1
+ import { CreatePagesArgs, CreateWebpackConfigArgs, PluginOptionsSchemaArgs } from 'gatsby';
2
2
  import { ObjectSchema } from 'gatsby-plugin-utils';
3
3
  import { PluginOptions } from './plugin-options';
4
4
  export declare const pluginOptionsSchema: ({ Joi, }: PluginOptionsSchemaArgs) => ObjectSchema<any>;
package/gatsby-node.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _tslib = require('./_tslib.js');
5
+ var serialize = require('./serialize.js');
6
6
  var require$$0 = require('path');
7
7
 
8
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -52,7 +52,7 @@ const createPages = ({
52
52
  actions,
53
53
  store,
54
54
  graphql
55
- }) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
55
+ }) => serialize.__awaiter(void 0, void 0, void 0, function* () {
56
56
  const ninetailedGatsbyPlugin = store.getState().flattenedPlugins.find(plugin => plugin.name === '@ninetailed/experience.js-gatsby');
57
57
  if (!ninetailedGatsbyPlugin) {
58
58
  return;
@@ -105,17 +105,26 @@ const createPages = ({
105
105
  throw new Error('Failed to query experience data. Check query configuration or data retrieval by the source plugin.');
106
106
  }
107
107
  const experiences = experienceOptionsSet ? experienceMapper(experienceData) : [];
108
- const ninetailedPreviewPluginOverride = Object.assign(Object.assign({}, ninetailedPreviewPlugin), {
109
- options: Object.assign(Object.assign({}, ninetailedPreviewPlugin.options), {
110
- audiences,
111
- experiences
112
- })
113
- });
114
- const ninetailedPluginsOverwrite = ninetailedGatsbyPlugin.pluginOptions.ninetailedPlugins.map(plugin => {
115
- return plugin === ninetailedPreviewPlugin ? ninetailedPreviewPluginOverride : plugin;
108
+ const ninetailedPluginsOverride = ninetailedGatsbyPlugin.pluginOptions.ninetailedPlugins.map(plugin => {
109
+ const pluginOptions = plugin.options;
110
+ if (!pluginOptions) {
111
+ return plugin;
112
+ }
113
+ const {
114
+ serializedFunctions,
115
+ serializedFunctionNames
116
+ } = serialize.serializePluginOptionFns(pluginOptions);
117
+ return Object.assign(Object.assign({}, plugin), {
118
+ options: Object.assign(Object.assign(Object.assign(Object.assign({}, plugin.options), serializedFunctions), {
119
+ serializedFunctionNames
120
+ }), plugin.name === '@ninetailed/experience.js-plugin-preview' && {
121
+ audiences,
122
+ experiences
123
+ })
124
+ });
116
125
  });
117
126
  ninetailedGatsbyPlugin.pluginOptions = Object.assign(Object.assign({}, ninetailedGatsbyPlugin.pluginOptions), {
118
- ninetailedPlugins: ninetailedPluginsOverwrite
127
+ ninetailedPlugins: ninetailedPluginsOverride
119
128
  });
120
129
  setPluginStatus({
121
130
  pluginOptions: ninetailedGatsbyPlugin.pluginOptions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-gatsby",
3
- "version": "4.2.2",
3
+ "version": "4.2.3-beta.0",
4
4
  "keywords": [
5
5
  "gatsby",
6
6
  "gatsby-plugin",
@@ -18,8 +18,8 @@
18
18
  "type": "commonjs",
19
19
  "types": "./index.d.ts",
20
20
  "dependencies": {
21
- "@ninetailed/experience.js": "4.2.2",
22
- "@ninetailed/experience.js-react": "4.2.2",
21
+ "@ninetailed/experience.js": "4.2.3-beta.0",
22
+ "@ninetailed/experience.js-react": "4.2.3-beta.0",
23
23
  "gatsby-plugin-utils": "3.19.0"
24
24
  }
25
25
  }
@@ -6,3 +6,12 @@ export type PluginOptions = GatsbyPluginOptions & NinetailedProviderInstantiatio
6
6
  options: unknown;
7
7
  }[];
8
8
  };
9
+ export type NinetailedPluginOptions = Record<string, unknown>;
10
+ export type SerializedFunctions = {
11
+ serializedFunctionNames: string[];
12
+ };
13
+ export type ResolvedPluginOptions = Record<string, unknown> & SerializedFunctions;
14
+ export type ResolvedPlugin = {
15
+ PluginCtor: any;
16
+ options?: ResolvedPluginOptions;
17
+ };
@@ -37,5 +37,44 @@ function __awaiter(thisArg, _arguments, P, generator) {
37
37
  });
38
38
  }
39
39
 
40
+ const createFunctionBody = serializedFunction => {
41
+ return '"use strict";\n return ' + serializedFunction + ';';
42
+ };
43
+ const deserializeFunction = serializedFunction => {
44
+ return new Function(createFunctionBody(serializedFunction))();
45
+ };
46
+ const deserializePluginOptionFns = options => {
47
+ var _a;
48
+ return (_a = options.serializedFunctionNames) === null || _a === void 0 ? void 0 : _a.reduce((acc, currFuncName) => {
49
+ const currentFunction = options[currFuncName];
50
+ if (currentFunction && typeof currentFunction === 'string') {
51
+ return Object.assign(Object.assign({}, acc), {
52
+ [currFuncName]: deserializeFunction(currentFunction)
53
+ });
54
+ }
55
+ return acc;
56
+ }, {});
57
+ };
58
+ const serializePluginOptionFns = pluginOptions => {
59
+ const serializedFunctionNames = [];
60
+ const serializedFunctions = Object.keys(pluginOptions).reduce((acc, optionKey) => {
61
+ const optionValue = pluginOptions[optionKey];
62
+ if (optionValue && typeof optionValue === 'function') {
63
+ const serializedFunction = optionValue.toString();
64
+ serializedFunctionNames.push(optionKey);
65
+ return Object.assign(Object.assign({}, acc), {
66
+ [optionKey]: serializedFunction
67
+ });
68
+ }
69
+ return acc;
70
+ }, {});
71
+ return {
72
+ serializedFunctions,
73
+ serializedFunctionNames
74
+ };
75
+ };
76
+
40
77
  exports.__awaiter = __awaiter;
41
78
  exports.__rest = __rest;
79
+ exports.deserializePluginOptionFns = deserializePluginOptionFns;
80
+ exports.serializePluginOptionFns = serializePluginOptionFns;
@@ -0,0 +1,6 @@
1
+ import { NinetailedPluginOptions, ResolvedPluginOptions } from '../plugin-options';
2
+ export declare const deserializePluginOptionFns: (options: ResolvedPluginOptions) => Record<string, (...args: unknown[]) => void>;
3
+ export declare const serializePluginOptionFns: (pluginOptions: NinetailedPluginOptions) => {
4
+ serializedFunctions: {};
5
+ serializedFunctionNames: string[];
6
+ };