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