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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29function _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); }
30function _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; }
31const countLines = require('metro/src/lib/countLines');
32async function transform(config, projectRoot, filename, data, options) {
33  const isCss = options.type !== 'asset' && filename.endsWith('.css');
34  // If the file is not CSS, then use the default behavior.
35  if (!isCss) {
36    return _metroTransformWorker().default.transform(config, projectRoot, filename, data, options);
37  }
38
39  // If the platform is not web, then return an empty module.
40  if (options.platform !== 'web') {
41    const code = (0, _cssModules().matchCssModule)(filename) ? 'module.exports={};' : '';
42    return _metroTransformWorker().default.transform(config, projectRoot, filename,
43    // TODO: Native CSS Modules
44    Buffer.from(code), options);
45  }
46  const code = data.toString('utf8');
47
48  // If the file is a CSS Module, then transform it to a JS module
49  // in development and a static CSS file in production.
50  if ((0, _cssModules().matchCssModule)(filename)) {
51    const results = await (0, _cssModules().transformCssModuleWeb)({
52      filename,
53      src: code,
54      options: {
55        projectRoot,
56        dev: options.dev,
57        minify: options.minify,
58        sourceMap: false
59      }
60    });
61    if (options.dev) {
62      // Dev has the CSS appended to the JS file.
63      return _metroTransformWorker().default.transform(config, projectRoot, filename, Buffer.from(results.output), options);
64    }
65    const jsModuleResults = await _metroTransformWorker().default.transform(config, projectRoot, filename, Buffer.from(results.output), options);
66    const cssCode = results.css.toString();
67    const output = [{
68      type: 'js/module',
69      data: {
70        // @ts-expect-error
71        ...jsModuleResults.output[0].data,
72        // Append additional css metadata for static extraction.
73        css: {
74          code: cssCode,
75          lineCount: countLines(cssCode),
76          map: [],
77          functionMap: null
78        }
79      }
80    }];
81    return {
82      dependencies: jsModuleResults.dependencies,
83      output
84    };
85  }
86
87  // Global CSS:
88
89  if (options.dev) {
90    return _metroTransformWorker().default.transform(config, projectRoot, filename,
91    // In development, we use a JS file that appends a style tag to the
92    // document. This is necessary because we need to replace the style tag
93    // when the CSS changes.
94    // NOTE: We may change this to better support static rendering in the future.
95    Buffer.from((0, _css().wrapDevelopmentCSS)({
96      src: code,
97      filename
98    })), options);
99  }
100  const {
101    transform
102  } = await Promise.resolve().then(() => _interopRequireWildcard(require('lightningcss')));
103
104  // TODO: Add bundling to resolve imports
105  // https://lightningcss.dev/bundling.html#bundling-order
106
107  const cssResults = transform({
108    filename,
109    code: Buffer.from(code),
110    sourceMap: false,
111    cssModules: false,
112    projectRoot,
113    minify: options.minify
114  });
115
116  // TODO: Warnings:
117  // cssResults.warnings.forEach((warning) => {
118  // });
119
120  // Create a mock JS module that exports an empty object,
121  // this ensures Metro dependency graph is correct.
122  const jsModuleResults = await _metroTransformWorker().default.transform(config, projectRoot, filename, Buffer.from(''), options);
123  const cssCode = cssResults.code.toString();
124
125  // In production, we export the CSS as a string and use a special type to prevent
126  // it from being included in the JS bundle. We'll extract the CSS like an asset later
127  // and append it to the HTML bundle.
128  const output = [{
129    data: {
130      // @ts-expect-error
131      ...jsModuleResults.output[0].data,
132      // Append additional css metadata for static extraction.
133      css: {
134        code: cssCode,
135        lineCount: countLines(cssCode),
136        map: [],
137        functionMap: null
138      }
139    },
140    type: 'js/module'
141  }];
142  return {
143    dependencies: jsModuleResults.dependencies,
144    output
145  };
146}
147
148/**
149 * A custom Metro transformer that adds support for processing Expo-specific bundler features.
150 * - Global CSS files on web.
151 * - CSS Modules on web.
152 * - TODO: Tailwind CSS on web.
153 */
154module.exports = {
155  // Use defaults for everything that's not custom.
156  ..._metroTransformWorker().default,
157  transform
158};
159//# sourceMappingURL=transform-worker.js.map