UNPKG

lightningcss-loader

Version:

Speed up your Webpack build with lightningcss

89 lines (88 loc) 4.81 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LightningCssLoader = LightningCssLoader; const interface_1 = require("./interface"); const lightningcss_1 = __importDefault(require("lightningcss")); const buffer_1 = require("buffer"); const utils_1 = require("./utils"); const LOADER_NAME = `lightningcss-loader`; // match `Custom media query {} is not defined` // https://github.com/parcel-bundler/lightningcss/blob/master/src/error.rs#L375 const CUSTOM_MEDIA_ERROR_REG = /Custom media query (.+?) is not defined/; const isCustomMediaError = (err) => { const msg = err === null || err === void 0 ? void 0 : err.message; if (!(msg === null || msg === void 0 ? void 0 : msg.length)) { return false; } const isMatch = CUSTOM_MEDIA_ERROR_REG.test(msg); return isMatch; }; function LightningCssLoader(source, prevMap) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const done = this.async(); const options = this.getOptions(); const { implementation, targets: userTargets } = options, opts = __rest(options, ["implementation", "targets"]); if (implementation && typeof implementation.transform !== 'function') { done(new TypeError(`[${LOADER_NAME}]: options.implementation.transform must be an 'lightningcss' transform function. Received ${typeof implementation.transform}`)); return; } const transform = (_a = implementation === null || implementation === void 0 ? void 0 : implementation.transform) !== null && _a !== void 0 ? _a : lightningcss_1.default.transform; const bundle = (_b = implementation === null || implementation === void 0 ? void 0 : implementation.bundle) !== null && _b !== void 0 ? _b : lightningcss_1.default.bundle; const filename = this.resourcePath; const enableSourceMap = this.sourceMap; const targets = (0, utils_1.getTargets)({ default: userTargets, key: interface_1.ECacheKey.loader }); const inputSourceMap = enableSourceMap && prevMap ? JSON.stringify(prevMap) : undefined; try { const codeBuffer = buffer_1.Buffer.from(source); const { code, map } = transform(Object.assign({ filename, code: codeBuffer, sourceMap: enableSourceMap, targets, inputSourceMap }, opts)); const codeAsString = code.toString(); done(null, codeAsString, map && JSON.parse(map.toString())); } catch (error) { // support @custom-media queries const isCustomMediaEnabled = ((_c = opts === null || opts === void 0 ? void 0 : opts.drafts) === null || _c === void 0 ? void 0 : _c.customMedia) === true; if (isCustomMediaEnabled) { const canBundle = typeof bundle === 'function' && isCustomMediaError(error) && filename; if (canBundle) { // fallback to bundle API try { const { code, map } = bundle(Object.assign({ filename, sourceMap: enableSourceMap, targets, inputSourceMap }, opts)); const codeAsString = code.toString(); done(null, codeAsString, map && JSON.parse(map.toString())); return; } catch (_d) { } } } done(error); } }); }