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