UNPKG

@storybook/addon-webpack5-compiler-babel

Version:
231 lines (220 loc) 8.07 kB
import { createRequire } from 'node:module'; import { fileURLToPath } from 'url'; import path4, { join } from 'path'; import process2 from 'process'; import fs3 from 'fs'; const require$1 = createRequire(import.meta.url); var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __require = /* @__PURE__ */ ((x) => typeof require$1 !== "undefined" ? require$1 : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require$1 !== "undefined" ? require$1 : a)[b] }) : x)(function(x) { if (typeof require$1 !== "undefined") return require$1.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); var __commonJS = (cb, mod) => function __require2() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. __defProp(target, "default", { value: mod, enumerable: true }) , mod )); // node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js var require_common_path_prefix = __commonJS({ "node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js"(exports$1, module) { var { sep: DEFAULT_SEPARATOR } = __require("path"); var determineSeparator = (paths) => { for (const path5 of paths) { const match = /(\/|\\)/.exec(path5); if (match !== null) return match[0]; } return DEFAULT_SEPARATOR; }; module.exports = function commonPathPrefix2(paths, sep = determineSeparator(paths)) { const [first = "", ...remaining] = paths; if (first === "" || remaining.length === 0) return ""; const parts = first.split(sep); let endOfPrefix = parts.length; for (const path5 of remaining) { const compare = path5.split(sep); for (let i = 0; i < endOfPrefix; i++) { if (compare[i] !== parts[i]) { endOfPrefix = i; } } if (endOfPrefix === 0) return ""; } const prefix = parts.slice(0, endOfPrefix).join(sep); return prefix.endsWith(sep) ? prefix : prefix + sep; }; } }); // node_modules/.pnpm/find-cache-dir@6.0.0/node_modules/find-cache-dir/index.js var import_common_path_prefix = __toESM(require_common_path_prefix()); var typeMappings = { directory: "isDirectory", file: "isFile" }; function checkType(type) { if (Object.hasOwnProperty.call(typeMappings, type)) { return; } throw new Error(`Invalid type specified: ${type}`); } var matchType = (type, stat) => stat[typeMappings[type]](); var toPath = (urlOrPath) => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; function locatePathSync(paths, { cwd: cwd2 = process2.cwd(), type = "file", allowSymlinks = true } = {}) { checkType(type); cwd2 = toPath(cwd2); const statFunction = allowSymlinks ? fs3.statSync : fs3.lstatSync; for (const path_ of paths) { try { const stat = statFunction(path4.resolve(cwd2, path_), { throwIfNoEntry: false }); if (!stat) { continue; } if (matchType(type, stat)) { return path_; } } catch { } } } // node_modules/.pnpm/find-up@6.3.0/node_modules/find-up/index.js var toPath2 = (urlOrPath) => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; var findUpStop = Symbol("findUpStop"); function findUpMultipleSync(name, options = {}) { let directory = path4.resolve(toPath2(options.cwd) || ""); const { root } = path4.parse(directory); const stopAt = options.stopAt || root; const limit = options.limit || Number.POSITIVE_INFINITY; const paths = [name].flat(); const runMatcher = (locateOptions) => { { return locatePathSync(paths, locateOptions); } }; const matches = []; while (true) { const foundPath = runMatcher({ ...options, cwd: directory }); if (foundPath === findUpStop) { break; } if (foundPath) { matches.push(path4.resolve(directory, foundPath)); } if (directory === stopAt || matches.length >= limit) { break; } directory = path4.dirname(directory); } return matches; } function findUpSync(name, options = {}) { const matches = findUpMultipleSync(name, { ...options, limit: 1 }); return matches[0]; } // node_modules/.pnpm/pkg-dir@7.0.0/node_modules/pkg-dir/index.js function packageDirectorySync({ cwd: cwd2 } = {}) { const filePath = findUpSync("package.json", { cwd: cwd2 }); return filePath && path4.dirname(filePath); } // node_modules/.pnpm/find-cache-dir@6.0.0/node_modules/find-cache-dir/index.js var { env, cwd } = process2; var isWritable = (path5) => { try { fs3.accessSync(path5, fs3.constants.W_OK); return true; } catch { return false; } }; function useDirectory(directory, options) { if (options.create) { fs3.mkdirSync(directory, { recursive: true }); } return directory; } function getNodeModuleDirectory(directory) { const nodeModules = path4.join(directory, "node_modules"); if (!isWritable(nodeModules) && (fs3.existsSync(nodeModules) || !isWritable(path4.join(directory)))) { return; } return nodeModules; } function findCacheDirectory(options = {}) { if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) { return useDirectory(path4.join(env.CACHE_DIR, options.name), options); } let { cwd: directory = cwd(), files } = options; if (files) { if (!Array.isArray(files)) { throw new TypeError(`Expected \`files\` option to be an array, got \`${typeof files}\`.`); } directory = (0, import_common_path_prefix.default)(files.map((file) => path4.resolve(directory, file))); } directory = packageDirectorySync({ cwd: directory }); if (!directory) { return; } const nodeModules = getNodeModuleDirectory(directory); if (!nodeModules) { return; } return useDirectory(path4.join(directory, "node_modules", ".cache", options.name), options); } // src/utils.ts function resolvePathInStorybookCache(fileOrDirectoryName, sub = "default") { let cacheDirectory = findCacheDirectory({ name: "storybook" }); cacheDirectory ||= join(process.cwd(), ".cache", "storybook"); return join(cacheDirectory, sub, fileOrDirectoryName); } // src/preset.ts var virtualModuleFiles = [/storybook-config-entry\.js$/, /storybook-stories\.js$/]; var webpackFinal = async (config, options) => { const babelOptions = await options.presets.apply("babel", {}, options); const typescriptOptions = await options.presets.apply("typescript", {}, options); config.module = { ...config.module || {}, rules: [ ...config.module?.rules || [], { test: typescriptOptions.skipCompiler ? /\.((c|m)?jsx?)$/ : /\.((c|m)?(j|t)sx?)$/, use: [ { loader: fileURLToPath(import.meta.resolve("babel-loader")), options: { cacheDirectory: resolvePathInStorybookCache("babel"), ...babelOptions } } ], exclude: [/node_modules/, ...virtualModuleFiles] } ] }; return config; }; export { webpackFinal };