1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6Object.defineProperty(exports, "SerialAsset", {
7  enumerable: true,
8  get: function () {
9    return _serializerAssets().SerialAsset;
10  }
11});
12exports.createSerializerFromSerialProcessors = createSerializerFromSerialProcessors;
13exports.withExpoSerializers = withExpoSerializers;
14exports.withSerializerPlugins = withSerializerPlugins;
15function _jscSafeUrl() {
16  const data = require("jsc-safe-url");
17  _jscSafeUrl = function () {
18    return data;
19  };
20  return data;
21}
22function _baseJSBundle() {
23  const data = _interopRequireDefault(require("metro/src/DeltaBundler/Serializers/baseJSBundle"));
24  _baseJSBundle = function () {
25    return data;
26  };
27  return data;
28}
29function _sourceMapString() {
30  const data = _interopRequireDefault(require("metro/src/DeltaBundler/Serializers/sourceMapString"));
31  _sourceMapString = function () {
32    return data;
33  };
34  return data;
35}
36function _bundleToString() {
37  const data = _interopRequireDefault(require("metro/src/lib/bundleToString"));
38  _bundleToString = function () {
39    return data;
40  };
41  return data;
42}
43function _path() {
44  const data = _interopRequireDefault(require("path"));
45  _path = function () {
46    return data;
47  };
48  return data;
49}
50function _environmentVariableSerializerPlugin() {
51  const data = require("./environmentVariableSerializerPlugin");
52  _environmentVariableSerializerPlugin = function () {
53    return data;
54  };
55  return data;
56}
57function _getCssDeps() {
58  const data = require("./getCssDeps");
59  _getCssDeps = function () {
60    return data;
61  };
62  return data;
63}
64function _serializerAssets() {
65  const data = require("./serializerAssets");
66  _serializerAssets = function () {
67    return data;
68  };
69  return data;
70}
71function _env() {
72  const data = require("../env");
73  _env = function () {
74    return data;
75  };
76  return data;
77}
78function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
79/**
80 * Copyright © 2022 650 Industries.
81 *
82 * This source code is licensed under the MIT license found in the
83 * LICENSE file in the root directory of this source tree.
84 */
85
86// @ts-expect-error
87
88function withExpoSerializers(config) {
89  const processors = [];
90  processors.push(_environmentVariableSerializerPlugin().serverPreludeSerializerPlugin);
91  if (!_env().env.EXPO_NO_CLIENT_ENV_VARS) {
92    processors.push(_environmentVariableSerializerPlugin().environmentVariableSerializerPlugin);
93  }
94  return withSerializerPlugins(config, processors);
95}
96
97// There can only be one custom serializer as the input doesn't match the output.
98// Here we simply run
99function withSerializerPlugins(config, processors) {
100  var _config$serializer;
101  const originalSerializer = (_config$serializer = config.serializer) === null || _config$serializer === void 0 ? void 0 : _config$serializer.customSerializer;
102  return {
103    ...config,
104    serializer: {
105      ...config.serializer,
106      customSerializer: createSerializerFromSerialProcessors(processors, originalSerializer)
107    }
108  };
109}
110function getDefaultSerializer(fallbackSerializer) {
111  const defaultSerializer = fallbackSerializer !== null && fallbackSerializer !== void 0 ? fallbackSerializer : async (...params) => {
112    const bundle = (0, _baseJSBundle().default)(...params);
113    const outputCode = (0, _bundleToString().default)(bundle).code;
114    return outputCode;
115  };
116  return async (...props) => {
117    const [entryPoint, preModules, graph, options] = props;
118    const jsCode = await defaultSerializer(entryPoint, preModules, graph, options);
119    if (!options.sourceUrl) {
120      return jsCode;
121    }
122    const sourceUrl = (0, _jscSafeUrl().isJscSafeUrl)(options.sourceUrl) ? (0, _jscSafeUrl().toNormalUrl)(options.sourceUrl) : options.sourceUrl;
123    const url = new URL(sourceUrl, 'https://expo.dev');
124    if (url.searchParams.get('platform') !== 'web' || url.searchParams.get('serializer.output') !== 'static') {
125      // Default behavior if `serializer.output=static` is not present in the URL.
126      return jsCode;
127    }
128    const includeSourceMaps = url.searchParams.get('serializer.map') === 'true';
129    const cssDeps = (0, _getCssDeps().getCssSerialAssets)(graph.dependencies, {
130      projectRoot: options.projectRoot,
131      processModuleFilter: options.processModuleFilter
132    });
133    const jsAssets = [];
134    if (jsCode) {
135      const stringContents = typeof jsCode === 'string' ? jsCode : jsCode.code;
136      const jsFilename = (0, _getCssDeps().fileNameFromContents)({
137        filepath: url.pathname,
138        src: stringContents
139      });
140      jsAssets.push({
141        filename: options.dev ? 'index.js' : `_expo/static/js/web/${jsFilename}.js`,
142        originFilename: 'index.js',
143        type: 'js',
144        metadata: {},
145        source: stringContents
146      });
147      if (
148      // Only include the source map if the `options.sourceMapUrl` option is provided and we are exporting a static build.
149      includeSourceMaps && options.sourceMapUrl) {
150        const sourceMap = typeof jsCode === 'string' ? serializeToSourceMap(...props) : jsCode.map;
151
152        // Make all paths relative to the server root to prevent the entire user filesystem from being exposed.
153        const parsed = JSON.parse(sourceMap);
154        // TODO: Maybe we can do this earlier.
155        parsed.sources = parsed.sources.map(
156        // TODO: Maybe basePath support
157        value => {
158          if (value.startsWith('/')) {
159            var _options$serverRoot;
160            return '/' + _path().default.relative((_options$serverRoot = options.serverRoot) !== null && _options$serverRoot !== void 0 ? _options$serverRoot : options.projectRoot, value);
161          }
162          // Prevent `__prelude__` from being relative.
163          return value;
164        });
165        jsAssets.push({
166          filename: options.dev ? 'index.map' : `_expo/static/js/web/${jsFilename}.js.map`,
167          originFilename: 'index.map',
168          type: 'map',
169          metadata: {},
170          source: JSON.stringify(parsed)
171        });
172      }
173    }
174    return JSON.stringify([...jsAssets, ...cssDeps]);
175  };
176}
177function getSortedModules(graph, {
178  createModuleId
179}) {
180  const modules = [...graph.dependencies.values()];
181  // Assign IDs to modules in a consistent order
182  for (const module of modules) {
183    createModuleId(module.path);
184  }
185  // Sort by IDs
186  return modules.sort((a, b) => createModuleId(a.path) - createModuleId(b.path));
187}
188function serializeToSourceMap(...props) {
189  const [, prepend, graph, options] = props;
190  const modules = [...prepend, ...getSortedModules(graph, {
191    createModuleId: options.createModuleId
192  })];
193  return (0, _sourceMapString().default)(modules, {
194    ...options
195  });
196}
197function createSerializerFromSerialProcessors(processors, originalSerializer) {
198  const finalSerializer = getDefaultSerializer(originalSerializer);
199  return (...props) => {
200    for (const processor of processors) {
201      if (processor) {
202        props = processor(...props);
203      }
204    }
205    return finalSerializer(...props);
206  };
207}
208//# sourceMappingURL=withExpoSerializers.js.map