YM
2025-08-07 e5c64590b336221f132ed28ae4ae6dca3e03055c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/index.ts
var _pluginutils = require('@rollup/pluginutils');
var _unplugin = require('unplugin');
 
// src/core/style.ts
var _esmodulelexer = require('es-module-lexer');
var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
var hyphenateRE = /\B([A-Z])/g;
var hyphenate = (str) => str.replaceAll(hyphenateRE, "-$1").toLowerCase();
var formatMap = {
  cjs: "lib",
  esm: "es"
};
var multilineCommentsRE = /\/\*\s(.|[\n\r])*?\*\//gm;
var singlelineCommentsRE = /\/\/\s.*/g;
function stripeComments(code) {
  return code.replaceAll(multilineCommentsRE, "").replaceAll(singlelineCommentsRE, "");
}
var transformImportStyle = (specifier, source, useSource = false, options) => {
  const { prefix, lib, format, ignoreComponents } = options;
  const statement = stripeComments(source.slice(specifier.ss, specifier.se));
  const leftBracket = statement.indexOf("{");
  if (leftBracket > -1) {
    const identifiers = statement.slice(leftBracket + 1, statement.indexOf("}"));
    const components = identifiers.split(",");
    const styleImports = [];
    components.forEach((c) => {
      const trimmed = c.replace(/\sas\s.+/, "").trim();
      if (trimmed.startsWith(prefix)) {
        const component = trimmed.slice(prefix.length);
        if (ignoreComponents.includes(component))
          return;
        if (useSource) {
          styleImports.push(
            `import '${lib}/${formatMap[format]}/components/${hyphenate(
              component
            )}/style/index'`
          );
        } else {
          styleImports.push(
            `import '${lib}/${formatMap[format]}/components/${hyphenate(
              component
            )}/style/css'`
          );
        }
      }
    });
    return styleImports.join("\n");
  }
};
var transformStyle = async (source, options) => {
  const { useSource, lib, prefix, format, ignoreComponents } = options;
  if (!source)
    return;
  await _esmodulelexer.init;
  const specifiers = _esmodulelexer.parse.call(void 0, source)[0].filter(({ n }) => {
    return n === lib || n === `${lib}/es/components` || n === `${lib}/lib/components`;
  });
  if (specifiers.length === 0)
    return;
  const styleImports = specifiers.map((s2) => {
    const ret = transformImportStyle(s2, source, useSource, {
      lib,
      prefix,
      format,
      ignoreComponents
    });
    return ret;
  }).filter((s2) => s2).join("\n");
  const lastSpecifier = specifiers.at(-1);
  const s = new (0, _magicstring2.default)(source);
  s.appendLeft(lastSpecifier.se + 1, `
${styleImports}
`);
  return {
    code: s.toString(),
    get map() {
      return s.generateMap({ hires: true, includeContent: true });
    }
  };
};
 
// node_modules/.pnpm/escape-string-regexp@5.0.0/node_modules/escape-string-regexp/index.js
function escapeStringRegexp(string) {
  if (typeof string !== "string") {
    throw new TypeError("Expected a string");
  }
  return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
}
 
// src/core/default-locale.ts
var getLocaleRE = (options) => new RegExp(
  `${escapeStringRegexp(`${options.lib}/`)}(es|lib)${escapeStringRegexp(
    "/hooks/use-locale/index"
  )}`
);
var transformDefaultLocale = (options, source, id) => {
  if (!id.match(getLocaleRE(options)))
    return;
  return source.replace(
    "locale/lang/en",
    `locale/lang/${options.defaultLocale}`
  );
};
var getViteDepPlugin = (options) => {
  const localeImporterRE = new RegExp(
    `${escapeStringRegexp(
      `node_modules/${options.lib}/`
    )}(es|lib)${escapeStringRegexp("/hooks/use-locale/index")}`
  );
  const localePath = "/locale/lang/en";
  const localePathFixed = `/locale/lang/${options.defaultLocale}`;
  return {
    name: "unplugin-element-plus:default-locale",
    setup(build) {
      build.onResolve(
        {
          filter: new RegExp(escapeStringRegexp(localePath)),
          namespace: "file"
        },
        ({ path, importer, kind, resolveDir }) => {
          if (localeImporterRE.test(importer))
            return build.resolve(path.replace(localePath, localePathFixed), {
              importer,
              kind,
              resolveDir
            });
        }
      );
    }
  };
};
 
// src/index.ts
var defaultOptions = {
  include: [
    "**/*.vue",
    "**/*.ts",
    "**/*.js",
    "**/*.tsx",
    "**/*.jsx",
    "**/*.vue?vue&type=script*"
  ],
  exclude: [/[/\\]node_modules[/\\]/, /[/\\]\.git[/\\]/, /[/\\]\.nuxt[/\\]/],
  lib: "element-plus",
  ignoreComponents: [],
  useSource: false,
  defaultLocale: "",
  format: "esm",
  prefix: "El",
  sourceMap: false
};
var src_default = _unplugin.createUnplugin.call(void 0, (userOptions = {}) => {
  const options = Object.assign(defaultOptions, userOptions);
  const filter = _pluginutils.createFilter.call(void 0, options.include, options.exclude);
  return {
    name: "unplugin-element-plus",
    enforce: "post",
    transformInclude(id) {
      return getLocaleRE(options).test(id) || filter(id);
    },
    transform(source, id) {
      if (options.defaultLocale) {
        const result = transformDefaultLocale(options, source, id);
        if (result)
          return result;
      }
      return transformStyle(source, options);
    },
    vite: {
      config() {
        if (options.defaultLocale) {
          return {
            optimizeDeps: {
              esbuildOptions: {
                plugins: [getViteDepPlugin(options)]
              }
            }
          };
        }
      }
    }
  };
});
 
 
 
exports.src_default = src_default;