1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.transform = transform;
7function _metroTransformWorker() {
8  const data = _interopRequireDefault(require("metro-transform-worker"));
9  _metroTransformWorker = function () {
10    return data;
11  };
12  return data;
13}
14function _css() {
15  const data = require("./css");
16  _css = function () {
17    return data;
18  };
19  return data;
20}
21function _cssModules() {
22  const data = require("./css-modules");
23  _cssModules = function () {
24    return data;
25  };
26  return data;
27}
28function _postcss() {
29  const data = require("./postcss");
30  _postcss = function () {
31    return data;
32  };
33  return data;
34}
35function _sass() {
36  const data = require("./sass");
37  _sass = function () {
38    return data;
39  };
40  return data;
41}
42function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43/**
44 * Copyright 2023-present 650 Industries (Expo). All rights reserved.
45 * Copyright (c) Meta Platforms, Inc. and affiliates.
46 *
47 * This source code is licensed under the MIT license found in the
48 * LICENSE file in the root directory of this source tree.
49 */
50
51const countLines = require('metro/src/lib/countLines');
52async function transform(config, projectRoot, filename, data, options) {
53  var _jsModuleResults$outp2;
54  const isCss = options.type !== 'asset' && /\.(s?css|sass)$/.test(filename);
55  // If the file is not CSS, then use the default behavior.
56  if (!isCss) {
57    var _options$customTransf;
58    const environment = (_options$customTransf = options.customTransformOptions) === null || _options$customTransf === void 0 ? void 0 : _options$customTransf.environment;
59    if (environment !== 'node' && (
60    // TODO: Ensure this works with windows.
61    filename.match(new RegExp(`^app/\\+html(\\.${options.platform})?\\.([tj]sx?|[cm]js)?$`)) ||
62    // Strip +api files.
63    filename.match(/\+api(\.(native|ios|android|web))?\.[tj]sx?$/))) {
64      // Remove the server-only +html file and API Routes from the bundle when bundling for a client environment.
65      return _metroTransformWorker().default.transform(config, projectRoot, filename, !options.minify ? Buffer.from(
66      // Use a string so this notice is visible in the bundle if the user is
67      // looking for it.
68      '"> The server-only file was removed from the client JS bundle by Expo CLI."') : Buffer.from(''), options);
69    }
70    if (environment !== 'node' && !filename.match(/\/node_modules\//) && filename.match(/\+api(\.(native|ios|android|web))?\.[tj]sx?$/)) {
71      // Clear the contents of +api files when bundling for the client.
72      // This ensures that the client doesn't accidentally use the server-only +api files.
73      return _metroTransformWorker().default.transform(config, projectRoot, filename, Buffer.from(''), options);
74    }
75    return _metroTransformWorker().default.transform(config, projectRoot, filename, data, options);
76  }
77
78  // If the platform is not web, then return an empty module.
79  if (options.platform !== 'web') {
80    const code = (0, _cssModules().matchCssModule)(filename) ? 'module.exports={ unstable_styles: {} };' : '';
81    return _metroTransformWorker().default.transform(config, projectRoot, filename,
82    // TODO: Native CSS Modules
83    Buffer.from(code), options);
84  }
85  let code = data.toString('utf8');
86
87  // Apply postcss transforms
88  code = await (0, _postcss().transformPostCssModule)(projectRoot, {
89    src: code,
90    filename
91  });
92
93  // TODO: When native has CSS support, this will need to move higher up.
94  const syntax = (0, _sass().matchSass)(filename);
95  if (syntax) {
96    code = (0, _sass().compileSass)(projectRoot, {
97      filename,
98      src: code
99    }, {
100      syntax
101    }).src;
102  }
103
104  // If the file is a CSS Module, then transform it to a JS module
105  // in development and a static CSS file in production.
106  if ((0, _cssModules().matchCssModule)(filename)) {
107    var _jsModuleResults$outp;
108    const results = await (0, _cssModules().transformCssModuleWeb)({
109      filename,
110      src: code,
111      options: {
112        projectRoot,
113        dev: options.dev,
114        minify: options.minify,
115        sourceMap: false
116      }
117    });
118    const jsModuleResults = await _metroTransformWorker().default.transform(config, projectRoot, filename, Buffer.from(results.output), options);
119    const cssCode = results.css.toString();
120    const output = [{
121      type: 'js/module',
122      data: {
123        // @ts-expect-error
124        ...((_jsModuleResults$outp = jsModuleResults.output[0]) === null || _jsModuleResults$outp === void 0 ? void 0 : _jsModuleResults$outp.data),
125        // Append additional css metadata for static extraction.
126        css: {
127          code: cssCode,
128          lineCount: countLines(cssCode),
129          map: [],
130          functionMap: null
131        }
132      }
133    }];
134    return {
135      dependencies: jsModuleResults.dependencies,
136      output
137    };
138  }
139
140  // Global CSS:
141
142  const {
143    transform
144  } = require('lightningcss');
145
146  // TODO: Add bundling to resolve imports
147  // https://lightningcss.dev/bundling.html#bundling-order
148
149  const cssResults = transform({
150    filename,
151    code: Buffer.from(code),
152    sourceMap: false,
153    cssModules: false,
154    projectRoot,
155    minify: options.minify
156  });
157
158  // TODO: Warnings:
159  // cssResults.warnings.forEach((warning) => {
160  // });
161
162  // Create a mock JS module that exports an empty object,
163  // this ensures Metro dependency graph is correct.
164  const jsModuleResults = await _metroTransformWorker().default.transform(config, projectRoot, filename, options.dev ? Buffer.from((0, _css().wrapDevelopmentCSS)({
165    src: code,
166    filename
167  })) : Buffer.from(''), options);
168  const cssCode = cssResults.code.toString();
169
170  // In production, we export the CSS as a string and use a special type to prevent
171  // it from being included in the JS bundle. We'll extract the CSS like an asset later
172  // and append it to the HTML bundle.
173  const output = [{
174    type: 'js/module',
175    data: {
176      // @ts-expect-error
177      ...((_jsModuleResults$outp2 = jsModuleResults.output[0]) === null || _jsModuleResults$outp2 === void 0 ? void 0 : _jsModuleResults$outp2.data),
178      // Append additional css metadata for static extraction.
179      css: {
180        code: cssCode,
181        lineCount: countLines(cssCode),
182        map: [],
183        functionMap: null
184      }
185    }
186  }];
187  return {
188    dependencies: jsModuleResults.dependencies,
189    output
190  };
191}
192
193/**
194 * A custom Metro transformer that adds support for processing Expo-specific bundler features.
195 * - Global CSS files on web.
196 * - CSS Modules on web.
197 * - TODO: Tailwind CSS on web.
198 */
199module.exports = {
200  // Use defaults for everything that's not custom.
201  ..._metroTransformWorker().default,
202  transform
203};
204//# sourceMappingURL=transform-worker.js.map