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