Skip to content
Snippets Groups Projects
Select Git revision
  • 928993390c7c28abe49b6925a2973298d1521b43
  • master default protected
  • 365-ouvrir-l-onglet-employeur-ou-taxes-carburant-quand-c-est-le-cas-pour-un-dispositif
  • 381-pb-affichage-labels-des-parametres-sur-plus-de-3-lignes
  • ajoute-duplicate-aide-logement
  • poc_castype_ia
  • parametres-editables-budget
  • ui-parametres
  • 366-signe-a-cote-du-droit-en-vigueur-sur-l-ui-pour-indiquer-que-la-reforme-a-eu-lieu-mais-qu-elle-n
  • 355-les-dispositifs-prestations-sociales-du-graphique-se-cachent-montrent-en-meme-temps-2
  • 358-les-variables-dont-le-montant-est-nul-apparaissent-en-bleu-et-non-cliquables
  • 356-ajuster-la-largeur-sur-les-graphiques-budgetaires
  • incoherence_cas_type_0
  • fix-ui-suppression-tranches-baremes
  • ajout-agregat-cehr-version-plf
  • impact_carbone
  • xlsx
  • header_revamp
  • 270-concevoir-la-page-d-accueil-leximpact
  • 219-conversion-des-montants-min-et-max-de-l-axe-des-x-en-smic
  • 294-afficher-le-salaire-des-cas-types-en-nombre-de-smic
  • 0.0.1129
  • 0.0.1128
  • 0.0.1127
  • 0.0.1126
  • 0.0.1125
  • 0.0.1124
  • 0.0.1123
  • 0.0.1122
  • 0.0.1121
  • 0.0.1120
  • 0.0.1119
  • 0.0.1118
  • 0.0.1117
  • 0.0.1116
  • 0.0.1115
  • 0.0.1114
  • 0.0.1113
  • 0.0.1112
  • 0.0.1111
  • 0.0.1110
41 results

plugin-yaml-patched.ts

Blame
  • plugin-yaml-patched.ts 2.07 KiB
    /// Code taken from https://github.com/rollup/plugins/blob/master/packages/yaml/src/index.js
    
    import YAML, { type LoadOptions } from "js-yaml"
    import toSource from "tosource"
    import {
      createFilter,
      makeLegalIdentifier,
      type FilterPattern,
    } from "@rollup/pluginutils"
    import type { PluginOption } from "vite"
    
    interface Options extends LoadOptions {
      documentMode?: "multi" | "single"
      exclude?: FilterPattern | undefined
      include?: FilterPattern | undefined
      transform?: ((data: unknown, id: string) => unknown) | null
    }
    const defaults: Options = {
      documentMode: "single",
      transform: null,
    }
    const ext = /\.ya?ml$/
    
    export default function yaml(opts: Options = {}): PluginOption {
      const options = Object.assign({}, defaults, opts)
      const { documentMode } = options
      const filter = createFilter(options.include, options.exclude)
      let loadMethod: (str: string, opts?: LoadOptions) => unknown
    
      if (documentMode === "single") {
        loadMethod = YAML.load
      } else if (documentMode === "multi") {
        loadMethod = YAML.loadAll as (str: string, opts?: LoadOptions) => unknown
      } else {
        this.error(
          `plugin-yaml → documentMode: '${documentMode}' is not a valid value. Please choose 'single' or 'multi'`,
        )
      }
    
      return {
        name: "yaml",
    
        transform(content: string, id: string) {
          if (!ext.test(id)) return null
          if (!filter(id)) return null
    
          // The pach is here: it adds `options` argument.
          let data = loadMethod(content, options)
    
          if (typeof options.transform === "function") {
            const result = options.transform(data, id)
            // eslint-disable-next-line no-undefined
            if (result !== undefined) {
              data = result
            }
          }
    
          const keys = Object.keys(data).filter(
            (key) => key === makeLegalIdentifier(key),
          )
          const code = `var data = ${toSource(data)};\n\n`
          const exports = ["export default data;"]
            .concat(keys.map((key) => `export var ${key} = data.${key};`))
            .join("\n")
    
          return {
            code: code + exports,
            map: { mappings: "" },
          }
        },
      }
    }