unplugin-env 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1914,7 +1914,7 @@ var require_scan = __commonJS({
1914
1914
  isExtglob = false;
1915
1915
  isGlob = false;
1916
1916
  }
1917
- let base2 = str;
1917
+ let base = str;
1918
1918
  let prefix = "";
1919
1919
  let glob = "";
1920
1920
  if (start > 0) {
@@ -1922,31 +1922,31 @@ var require_scan = __commonJS({
1922
1922
  str = str.slice(start);
1923
1923
  lastIndex -= start;
1924
1924
  }
1925
- if (base2 && isGlob === true && lastIndex > 0) {
1926
- base2 = str.slice(0, lastIndex);
1925
+ if (base && isGlob === true && lastIndex > 0) {
1926
+ base = str.slice(0, lastIndex);
1927
1927
  glob = str.slice(lastIndex);
1928
1928
  } else if (isGlob === true) {
1929
- base2 = "";
1929
+ base = "";
1930
1930
  glob = str;
1931
1931
  } else {
1932
- base2 = str;
1932
+ base = str;
1933
1933
  }
1934
- if (base2 && base2 !== "" && base2 !== "/" && base2 !== str) {
1935
- if (isPathSeparator(base2.charCodeAt(base2.length - 1))) {
1936
- base2 = base2.slice(0, -1);
1934
+ if (base && base !== "" && base !== "/" && base !== str) {
1935
+ if (isPathSeparator(base.charCodeAt(base.length - 1))) {
1936
+ base = base.slice(0, -1);
1937
1937
  }
1938
1938
  }
1939
1939
  if (opts.unescape === true) {
1940
1940
  if (glob) glob = utils.removeBackslashes(glob);
1941
- if (base2 && backslashes === true) {
1942
- base2 = utils.removeBackslashes(base2);
1941
+ if (base && backslashes === true) {
1942
+ base = utils.removeBackslashes(base);
1943
1943
  }
1944
1944
  }
1945
1945
  const state = {
1946
1946
  prefix,
1947
1947
  input,
1948
1948
  start,
1949
- base: base2,
1949
+ base,
1950
1950
  glob,
1951
1951
  isBrace,
1952
1952
  isBracket,
@@ -3499,28 +3499,28 @@ var require_tasks = __commonJS({
3499
3499
  function groupPatternsByBaseDirectory(patterns) {
3500
3500
  const group = {};
3501
3501
  return patterns.reduce((collection, pattern) => {
3502
- const base2 = utils.pattern.getBaseDirectory(pattern);
3503
- if (base2 in collection) {
3504
- collection[base2].push(pattern);
3502
+ const base = utils.pattern.getBaseDirectory(pattern);
3503
+ if (base in collection) {
3504
+ collection[base].push(pattern);
3505
3505
  } else {
3506
- collection[base2] = [pattern];
3506
+ collection[base] = [pattern];
3507
3507
  }
3508
3508
  return collection;
3509
3509
  }, group);
3510
3510
  }
3511
3511
  exports2.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
3512
3512
  function convertPatternGroupsToTasks(positive, negative, dynamic) {
3513
- return Object.keys(positive).map((base2) => {
3514
- return convertPatternGroupToTask(base2, positive[base2], negative, dynamic);
3513
+ return Object.keys(positive).map((base) => {
3514
+ return convertPatternGroupToTask(base, positive[base], negative, dynamic);
3515
3515
  });
3516
3516
  }
3517
3517
  exports2.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
3518
- function convertPatternGroupToTask(base2, positive, negative, dynamic) {
3518
+ function convertPatternGroupToTask(base, positive, negative, dynamic) {
3519
3519
  return {
3520
3520
  dynamic,
3521
3521
  positive,
3522
3522
  negative,
3523
- base: base2,
3523
+ base,
3524
3524
  patterns: [].concat(positive, negative.map(utils.pattern.convertToNegativePattern))
3525
3525
  };
3526
3526
  }
@@ -4460,8 +4460,8 @@ var require_async3 = __commonJS({
4460
4460
  onEnd(callback) {
4461
4461
  this._emitter.once("end", callback);
4462
4462
  }
4463
- _pushToQueue(directory, base2) {
4464
- const queueItem = { directory, base: base2 };
4463
+ _pushToQueue(directory, base) {
4464
+ const queueItem = { directory, base };
4465
4465
  this._queue.push(queueItem, (error) => {
4466
4466
  if (error !== null) {
4467
4467
  this._handleError(error);
@@ -4488,19 +4488,19 @@ var require_async3 = __commonJS({
4488
4488
  this._isDestroyed = true;
4489
4489
  this._emitter.emit("error", error);
4490
4490
  }
4491
- _handleEntry(entry, base2) {
4491
+ _handleEntry(entry, base) {
4492
4492
  if (this._isDestroyed || this._isFatalError) {
4493
4493
  return;
4494
4494
  }
4495
4495
  const fullpath = entry.path;
4496
- if (base2 !== void 0) {
4497
- entry.path = common.joinPathSegments(base2, entry.name, this._settings.pathSegmentSeparator);
4496
+ if (base !== void 0) {
4497
+ entry.path = common.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
4498
4498
  }
4499
4499
  if (common.isAppliedFilter(this._settings.entryFilter, entry)) {
4500
4500
  this._emitEntry(entry);
4501
4501
  }
4502
4502
  if (entry.dirent.isDirectory() && common.isAppliedFilter(this._settings.deepFilter, entry)) {
4503
- this._pushToQueue(fullpath, base2 === void 0 ? void 0 : entry.path);
4503
+ this._pushToQueue(fullpath, base === void 0 ? void 0 : entry.path);
4504
4504
  }
4505
4505
  }
4506
4506
  _emitEntry(entry) {
@@ -4608,19 +4608,19 @@ var require_sync3 = __commonJS({
4608
4608
  this._handleQueue();
4609
4609
  return this._storage;
4610
4610
  }
4611
- _pushToQueue(directory, base2) {
4612
- this._queue.add({ directory, base: base2 });
4611
+ _pushToQueue(directory, base) {
4612
+ this._queue.add({ directory, base });
4613
4613
  }
4614
4614
  _handleQueue() {
4615
4615
  for (const item of this._queue.values()) {
4616
4616
  this._handleDirectory(item.directory, item.base);
4617
4617
  }
4618
4618
  }
4619
- _handleDirectory(directory, base2) {
4619
+ _handleDirectory(directory, base) {
4620
4620
  try {
4621
4621
  const entries = this._scandir(directory, this._settings.fsScandirSettings);
4622
4622
  for (const entry of entries) {
4623
- this._handleEntry(entry, base2);
4623
+ this._handleEntry(entry, base);
4624
4624
  }
4625
4625
  } catch (error) {
4626
4626
  this._handleError(error);
@@ -4632,16 +4632,16 @@ var require_sync3 = __commonJS({
4632
4632
  }
4633
4633
  throw error;
4634
4634
  }
4635
- _handleEntry(entry, base2) {
4635
+ _handleEntry(entry, base) {
4636
4636
  const fullpath = entry.path;
4637
- if (base2 !== void 0) {
4638
- entry.path = common.joinPathSegments(base2, entry.name, this._settings.pathSegmentSeparator);
4637
+ if (base !== void 0) {
4638
+ entry.path = common.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
4639
4639
  }
4640
4640
  if (common.isAppliedFilter(this._settings.entryFilter, entry)) {
4641
4641
  this._pushToStorage(entry);
4642
4642
  }
4643
4643
  if (entry.dirent.isDirectory() && common.isAppliedFilter(this._settings.deepFilter, entry)) {
4644
- this._pushToQueue(fullpath, base2 === void 0 ? void 0 : entry.path);
4644
+ this._pushToQueue(fullpath, base === void 0 ? void 0 : entry.path);
4645
4645
  }
4646
4646
  }
4647
4647
  _pushToStorage(entry) {
@@ -5537,14 +5537,14 @@ __export(index_exports, {
5537
5537
  });
5538
5538
  module.exports = __toCommonJS(index_exports);
5539
5539
  var import_node_fs3 = require("fs");
5540
- var import_node_process4 = __toESM(require("process"), 1);
5540
+ var import_node_process5 = __toESM(require("process"), 1);
5541
5541
  var import_unplugin = require("unplugin");
5542
5542
 
5543
5543
  // src/core/compress.ts
5544
5544
  var import_node_fs = __toESM(require("fs"), 1);
5545
5545
  var import_node_path = __toESM(require("path"), 1);
5546
5546
  var import_node_process2 = __toESM(require("process"), 1);
5547
- var import_archiver = __toESM(require("archiver"), 1);
5547
+ var import_zip_lib = require("zip-lib");
5548
5548
 
5549
5549
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
5550
5550
  var ANSI_BACKGROUND_OFFSET = 10;
@@ -6056,39 +6056,37 @@ var Log = class {
6056
6056
  };
6057
6057
 
6058
6058
  // src/core/compress.ts
6059
- async function createCompress(options) {
6060
- const { outDir, ignoreBase } = options;
6059
+ async function createCompress(options, outDir) {
6060
+ const { ignoreBase } = options;
6061
6061
  const zipFilePath = import_node_path.default.resolve(import_node_process2.default.cwd(), `${import_node_path.default.basename(outDir)}.zip`);
6062
- Log.log("Compressing the directory", outDir);
6063
- return new Promise((resolve, reject) => {
6064
- try {
6065
- const output = import_node_fs.default.createWriteStream(zipFilePath);
6066
- const archive = (0, import_archiver.default)("zip");
6067
- output.on("close", () => {
6068
- Log.success("Successfully compressed to", `${zipFilePath} (${(archive.pointer() / 1024 / 1024).toFixed(1)} MB)`);
6069
- resolve(null);
6070
- });
6071
- archive.on("warning", (err) => {
6072
- if (err.code === "ENOENT")
6073
- Log.log("File not found during compression:", err.message);
6074
- else
6075
- Log.log("Compression warning:", err.message);
6076
- });
6077
- archive.on("error", (err) => {
6078
- Log.error("Compression error:", err);
6079
- reject(err);
6080
- });
6081
- archive.pipe(output);
6082
- const absoluteOutDir = import_node_path.default.resolve(outDir);
6083
- archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path.default.basename(outDir));
6084
- archive.finalize();
6085
- } catch (error) {
6086
- Log.error("Error compressing the directory", error);
6087
- throw error;
6088
- }
6089
- });
6062
+ const absoluteOutDir = import_node_path.default.resolve(outDir);
6063
+ Log.log("Compressing directory:", absoluteOutDir);
6064
+ try {
6065
+ const zip = new import_zip_lib.Zip();
6066
+ if (ignoreBase) {
6067
+ zip.addFolder(absoluteOutDir);
6068
+ } else {
6069
+ zip.addFolder(absoluteOutDir, import_node_path.default.basename(outDir));
6070
+ }
6071
+ await zip.archive(zipFilePath);
6072
+ const stat = import_node_fs.default.statSync(zipFilePath);
6073
+ const size = stat.size;
6074
+ const readableSize = size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(1)} MB` : `${(size / 1024).toFixed(1)} KB`;
6075
+ Log.success(
6076
+ "Successfully compressed to:",
6077
+ `${zipFilePath} (${readableSize})`
6078
+ );
6079
+ return null;
6080
+ } catch (error) {
6081
+ Log.error("Compression error:", error);
6082
+ throw error;
6083
+ }
6090
6084
  }
6091
6085
 
6086
+ // src/core/options.ts
6087
+ var import_node_process4 = __toESM(require("process"), 1);
6088
+ var import_utils = require("@antfu/utils");
6089
+
6092
6090
  // src/core/generate.ts
6093
6091
  var import_node_fs2 = require("fs");
6094
6092
  var import_node_path2 = __toESM(require("path"), 1);
@@ -6109,14 +6107,15 @@ function mergeObjects(prodObj, devObj) {
6109
6107
  }
6110
6108
  }
6111
6109
  }
6112
- async function generateScript(options, mode, base2) {
6110
+ async function generateScript(options, context) {
6113
6111
  const { dir, fileName, globalName, serve, build } = options.env;
6114
6112
  const folder = await findFolder(import_node_process3.default.cwd(), dir);
6115
6113
  const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6116
6114
  absolute: true,
6117
6115
  cwd: folder
6118
6116
  });
6119
- const testReg = mode === "serve" ? serve : build;
6117
+ const { mode, base } = context;
6118
+ const testReg = mode === "dev" ? serve : build;
6120
6119
  let target = "";
6121
6120
  let source = "";
6122
6121
  let code = "";
@@ -6145,7 +6144,7 @@ ${versionInfo}`;
6145
6144
  const formatCode = code;
6146
6145
  return {
6147
6146
  code,
6148
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6147
+ script: ` <script type="text/javascript" src="${base}${fileName}"></script>
6149
6148
  </head>`,
6150
6149
  emit: {
6151
6150
  type: "asset",
@@ -6157,7 +6156,7 @@ ${versionInfo}`;
6157
6156
  }
6158
6157
  async function generateVersion(options, mode) {
6159
6158
  const pkg = await (0, import_local_pkg.getPackageInfo)(import_node_process3.default.cwd());
6160
- return `console.info("Version: %c${pkg?.version}%c - ${mode === "serve" ? "runtime" : "built"} on %c${options.date}%c", "color: green;", '', "color: blue;", '')`;
6159
+ return `console.info("Version: %c${pkg?.version}%c - ${mode === "dev" ? "runtime" : "built"} on %c${options.datetime}%c", "color: green;", '', "color: blue;", '')`;
6161
6160
  }
6162
6161
  async function findFolder(directoryPath, dir) {
6163
6162
  const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
@@ -6181,7 +6180,6 @@ async function findFolder(directoryPath, dir) {
6181
6180
  }
6182
6181
 
6183
6182
  // src/core/options.ts
6184
- var import_utils = require("@antfu/utils");
6185
6183
  function resolveOptions(options) {
6186
6184
  const defaults = {
6187
6185
  env: {
@@ -6192,8 +6190,7 @@ function resolveOptions(options) {
6192
6190
  build: /prod/i
6193
6191
  },
6194
6192
  compress: {
6195
- outDir: "dist",
6196
- ignoreBase: true
6193
+ ignoreBase: false
6197
6194
  }
6198
6195
  };
6199
6196
  const mergeOptions = (0, import_utils.deepMerge)(defaults, options);
@@ -6203,67 +6200,160 @@ function resolveOptions(options) {
6203
6200
  });
6204
6201
  return {
6205
6202
  ...mergeOptions,
6206
- date: formatter.format(/* @__PURE__ */ new Date())
6203
+ datetime: formatter.format(/* @__PURE__ */ new Date())
6207
6204
  };
6208
6205
  }
6206
+ function createUnifiedContext(resolved) {
6207
+ const ctx = {
6208
+ framework: "",
6209
+ base: "/",
6210
+ outDir: "dist",
6211
+ mode: "build",
6212
+ scriptInfo: {
6213
+ code: "",
6214
+ script: "",
6215
+ emit: {
6216
+ type: "asset",
6217
+ source: "",
6218
+ fileName: ""
6219
+ },
6220
+ watchFiles: []
6221
+ },
6222
+ get isDev() {
6223
+ return this.mode === "dev";
6224
+ },
6225
+ get isBuild() {
6226
+ return this.mode === "build";
6227
+ },
6228
+ // --- VITE ---
6229
+ async setVite(config) {
6230
+ this.framework = "vite";
6231
+ this.base = config.base || "/";
6232
+ this.outDir = config.build?.outDir || "dist";
6233
+ this.mode = config.command === "serve" ? "dev" : "build";
6234
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6235
+ },
6236
+ // --- ROLLUP ---
6237
+ async setRollup(options) {
6238
+ this.framework = "rollup";
6239
+ this.outDir = options.dir || "dist";
6240
+ this.mode = import_node_process4.default.env.ROLLUP_WATCH ? "dev" : "build";
6241
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6242
+ },
6243
+ // --- WEBPACK ---
6244
+ async setWebpack(compiler) {
6245
+ this.framework = "webpack";
6246
+ this.base = compiler.options.output?.publicPath || "/";
6247
+ this.outDir = compiler.options.output?.path || "dist";
6248
+ const raw = compiler.options.mode || "production";
6249
+ this.mode = raw === "development" ? "dev" : "build";
6250
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6251
+ },
6252
+ // --- RSPACK ---
6253
+ async setRspack(compiler) {
6254
+ this.framework = "rspack";
6255
+ this.base = compiler.options.output?.publicPath || "/";
6256
+ this.outDir = compiler.options.output?.path || "dist";
6257
+ const raw = compiler.options.mode || "production";
6258
+ this.mode = raw === "development" ? "dev" : "build";
6259
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6260
+ },
6261
+ // --- ESBUILD ---
6262
+ async setEsbuild(build) {
6263
+ this.framework = "esbuild";
6264
+ this.outDir = build.initialOptions.outdir || "dist";
6265
+ this.mode = build.initialOptions.watch ? "dev" : "build";
6266
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6267
+ },
6268
+ // --- FARM ---
6269
+ async setFarm(compiler) {
6270
+ this.framework = "farm";
6271
+ this.base = compiler.config.output?.publicPath || "/";
6272
+ this.outDir = compiler.config.output?.path || "dist";
6273
+ const raw = compiler.config.mode || "production";
6274
+ this.mode = raw === "development" ? "dev" : "build";
6275
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6276
+ },
6277
+ // --- ROLLDOWN ---
6278
+ async setRolldown(options) {
6279
+ this.framework = "rolldown";
6280
+ this.outDir = options.dir || "dist";
6281
+ this.mode = import_node_process4.default.env.ROLLDOWN_WATCH ? "dev" : "build";
6282
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6283
+ }
6284
+ };
6285
+ return ctx;
6286
+ }
6209
6287
 
6210
6288
  // src/index.ts
6211
6289
  var virtualEnvId = "virtual:env";
6212
6290
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
6213
- var base = "";
6214
6291
  var unpluginFactory = (options = {}) => {
6215
6292
  const resolved = resolveOptions(options);
6293
+ const ctx = createUnifiedContext(resolved);
6216
6294
  return [{
6217
- name: "plugin-env-serve",
6218
- apply: "serve",
6295
+ name: "plugin-env",
6219
6296
  enforce: "post",
6220
- configResolved(config) {
6221
- base = config.base;
6297
+ rollup: {
6298
+ outputOptions(outputOptions) {
6299
+ ctx.setRollup(outputOptions);
6300
+ return outputOptions;
6301
+ }
6222
6302
  },
6223
- async resolveId(id) {
6224
- if (id.startsWith(virtualEnvId))
6225
- return resolvedVirtualEnvId;
6303
+ vite: {
6304
+ async configResolved(config) {
6305
+ await ctx.setVite(config);
6306
+ }
6226
6307
  },
6227
- async load(id) {
6228
- if (id.startsWith(resolvedVirtualEnvId)) {
6229
- const { code, watchFiles } = await generateScript(resolved, "serve", base);
6230
- watchFiles.forEach((file) => {
6231
- this.addWatchFile(file);
6232
- });
6233
- return code;
6308
+ async webpack(compiler) {
6309
+ await ctx.setWebpack(compiler);
6310
+ },
6311
+ rspack: async (compiler) => {
6312
+ await ctx.setRspack(compiler);
6313
+ },
6314
+ esbuild: {
6315
+ async setup(build) {
6316
+ await ctx.setEsbuild(build);
6234
6317
  }
6235
- }
6236
- }, {
6237
- name: "unplugin-env-build",
6238
- apply: "build",
6239
- enforce: "post",
6240
- configResolved(config) {
6241
- base = config.base;
6242
6318
  },
6243
- resolveId(id) {
6319
+ async farm(compiler) {
6320
+ await ctx.setFarm(compiler);
6321
+ },
6322
+ rolldown: {
6323
+ async outputOptions(outputOptions) {
6324
+ await ctx.setRolldown(outputOptions);
6325
+ }
6326
+ },
6327
+ async resolveId(id) {
6244
6328
  if (id.startsWith(virtualEnvId))
6245
6329
  return resolvedVirtualEnvId;
6246
6330
  },
6247
6331
  async load(id) {
6248
- const { emit, script } = await generateScript(resolved, "build", base);
6249
- if (id.startsWith(resolvedVirtualEnvId)) {
6250
- this.emitFile(emit);
6251
- return "";
6252
- }
6253
- if (id.endsWith(".html")) {
6254
- let code = await import_node_fs3.promises.readFile(id, "utf8");
6255
- code = code.replace(/<\/head>/g, script);
6256
- return {
6257
- code
6258
- };
6332
+ const { code, watchFiles, emit, script } = ctx.scriptInfo;
6333
+ if (ctx.isDev) {
6334
+ if (id.startsWith(resolvedVirtualEnvId)) {
6335
+ watchFiles.forEach((file) => {
6336
+ this.addWatchFile(file);
6337
+ });
6338
+ return code;
6339
+ }
6340
+ } else {
6341
+ if (id.startsWith(resolvedVirtualEnvId)) {
6342
+ this.emitFile(emit);
6343
+ return "";
6344
+ }
6345
+ if (id.endsWith(".html")) {
6346
+ let code2 = await import_node_fs3.promises.readFile(id, "utf-8");
6347
+ code2 = code2.replace(/<\/head>/g, script);
6348
+ return { code: code2 };
6349
+ }
6259
6350
  }
6260
- return null;
6261
6351
  },
6262
6352
  buildEnd: () => {
6263
- import_node_process4.default.on("beforeExit", async () => {
6353
+ import_node_process5.default.on("beforeExit", async () => {
6264
6354
  const { compress } = resolved;
6265
- await createCompress(compress);
6266
- import_node_process4.default.exit(0);
6355
+ await createCompress(compress, ctx.outDir);
6356
+ import_node_process5.default.exit(0);
6267
6357
  });
6268
6358
  }
6269
6359
  }];
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  index_default,
8
8
  unplugin,
9
9
  unpluginFactory
10
- } from "./chunk-QWCQIGEO.js";
10
+ } from "./chunk-IQADAXMX.js";
11
11
  export {
12
12
  index_default as default,
13
13
  unplugin,