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