unplugin-env 1.0.2 → 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/nuxt.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) {
@@ -5541,14 +5541,14 @@ var import_unplugin2 = require("unplugin");
5541
5541
 
5542
5542
  // src/index.ts
5543
5543
  var import_node_fs3 = require("fs");
5544
- var import_node_process4 = __toESM(require("process"), 1);
5544
+ var import_node_process5 = __toESM(require("process"), 1);
5545
5545
  var import_unplugin = require("unplugin");
5546
5546
 
5547
5547
  // src/core/compress.ts
5548
5548
  var import_node_fs = __toESM(require("fs"), 1);
5549
5549
  var import_node_path = __toESM(require("path"), 1);
5550
5550
  var import_node_process2 = __toESM(require("process"), 1);
5551
- var import_archiver = __toESM(require("archiver"), 1);
5551
+ var import_zip_lib = require("zip-lib");
5552
5552
 
5553
5553
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
5554
5554
  var ANSI_BACKGROUND_OFFSET = 10;
@@ -6060,41 +6060,37 @@ var Log = class {
6060
6060
  };
6061
6061
 
6062
6062
  // src/core/compress.ts
6063
- async function createCompress(options, outDir2) {
6063
+ async function createCompress(options, outDir) {
6064
6064
  const { ignoreBase } = options;
6065
- const zipFilePath = import_node_path.default.resolve(import_node_process2.default.cwd(), `${import_node_path.default.basename(outDir2)}.zip`);
6066
- Log.log("Compressing the directory", outDir2);
6067
- return new Promise((resolve, reject) => {
6068
- try {
6069
- const output = import_node_fs.default.createWriteStream(zipFilePath);
6070
- const archive = (0, import_archiver.default)("zip");
6071
- output.on("close", () => {
6072
- const size = archive.pointer();
6073
- const sizeInMB = size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(1)} MB` : `${(size / 1024).toFixed(1)} KB`;
6074
- Log.success("Successfully compressed to", `${zipFilePath} (${sizeInMB})`);
6075
- resolve(null);
6076
- });
6077
- archive.on("warning", (err) => {
6078
- if (err.code === "ENOENT")
6079
- Log.log("File not found during compression:", err.message);
6080
- else
6081
- Log.log("Compression warning:", err.message);
6082
- });
6083
- archive.on("error", (err) => {
6084
- Log.error("Compression error:", err);
6085
- reject(err);
6086
- });
6087
- archive.pipe(output);
6088
- const absoluteOutDir = import_node_path.default.resolve(outDir2);
6089
- archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path.default.basename(outDir2));
6090
- archive.finalize();
6091
- } catch (error) {
6092
- Log.error("Error compressing the directory", error);
6093
- throw error;
6094
- }
6095
- });
6065
+ const zipFilePath = import_node_path.default.resolve(import_node_process2.default.cwd(), `${import_node_path.default.basename(outDir)}.zip`);
6066
+ const absoluteOutDir = import_node_path.default.resolve(outDir);
6067
+ Log.log("Compressing directory:", absoluteOutDir);
6068
+ try {
6069
+ const zip = new import_zip_lib.Zip();
6070
+ if (ignoreBase) {
6071
+ zip.addFolder(absoluteOutDir);
6072
+ } else {
6073
+ zip.addFolder(absoluteOutDir, import_node_path.default.basename(outDir));
6074
+ }
6075
+ await zip.archive(zipFilePath);
6076
+ const stat = import_node_fs.default.statSync(zipFilePath);
6077
+ const size = stat.size;
6078
+ const readableSize = size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(1)} MB` : `${(size / 1024).toFixed(1)} KB`;
6079
+ Log.success(
6080
+ "Successfully compressed to:",
6081
+ `${zipFilePath} (${readableSize})`
6082
+ );
6083
+ return null;
6084
+ } catch (error) {
6085
+ Log.error("Compression error:", error);
6086
+ throw error;
6087
+ }
6096
6088
  }
6097
6089
 
6090
+ // src/core/options.ts
6091
+ var import_node_process4 = __toESM(require("process"), 1);
6092
+ var import_utils = require("@antfu/utils");
6093
+
6098
6094
  // src/core/generate.ts
6099
6095
  var import_node_fs2 = require("fs");
6100
6096
  var import_node_path2 = __toESM(require("path"), 1);
@@ -6115,14 +6111,15 @@ function mergeObjects(prodObj, devObj) {
6115
6111
  }
6116
6112
  }
6117
6113
  }
6118
- async function generateScript(options, mode, base2) {
6114
+ async function generateScript(options, context) {
6119
6115
  const { dir, fileName, globalName, serve, build } = options.env;
6120
6116
  const folder = await findFolder(import_node_process3.default.cwd(), dir);
6121
6117
  const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6122
6118
  absolute: true,
6123
6119
  cwd: folder
6124
6120
  });
6125
- const testReg = mode === "serve" ? serve : build;
6121
+ const { mode, base } = context;
6122
+ const testReg = mode === "dev" ? serve : build;
6126
6123
  let target = "";
6127
6124
  let source = "";
6128
6125
  let code = "";
@@ -6151,7 +6148,7 @@ ${versionInfo}`;
6151
6148
  const formatCode = code;
6152
6149
  return {
6153
6150
  code,
6154
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6151
+ script: ` <script type="text/javascript" src="${base}${fileName}"></script>
6155
6152
  </head>`,
6156
6153
  emit: {
6157
6154
  type: "asset",
@@ -6163,7 +6160,7 @@ ${versionInfo}`;
6163
6160
  }
6164
6161
  async function generateVersion(options, mode) {
6165
6162
  const pkg = await (0, import_local_pkg.getPackageInfo)(import_node_process3.default.cwd());
6166
- return `console.info("Version: %c${pkg?.version}%c - ${mode === "serve" ? "runtime" : "built"} on %c${options.datetime}%c", "color: green;", '', "color: blue;", '')`;
6163
+ return `console.info("Version: %c${pkg?.version}%c - ${mode === "dev" ? "runtime" : "built"} on %c${options.datetime}%c", "color: green;", '', "color: blue;", '')`;
6167
6164
  }
6168
6165
  async function findFolder(directoryPath, dir) {
6169
6166
  const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
@@ -6187,7 +6184,6 @@ async function findFolder(directoryPath, dir) {
6187
6184
  }
6188
6185
 
6189
6186
  // src/core/options.ts
6190
- var import_utils = require("@antfu/utils");
6191
6187
  function resolveOptions(options) {
6192
6188
  const defaults = {
6193
6189
  env: {
@@ -6211,67 +6207,157 @@ function resolveOptions(options) {
6211
6207
  datetime: formatter.format(/* @__PURE__ */ new Date())
6212
6208
  };
6213
6209
  }
6210
+ function createUnifiedContext(resolved) {
6211
+ const ctx = {
6212
+ framework: "",
6213
+ base: "/",
6214
+ outDir: "dist",
6215
+ mode: "build",
6216
+ scriptInfo: {
6217
+ code: "",
6218
+ script: "",
6219
+ emit: {
6220
+ type: "asset",
6221
+ source: "",
6222
+ fileName: ""
6223
+ },
6224
+ watchFiles: []
6225
+ },
6226
+ get isDev() {
6227
+ return this.mode === "dev";
6228
+ },
6229
+ get isBuild() {
6230
+ return this.mode === "build";
6231
+ },
6232
+ // --- VITE ---
6233
+ async setVite(config) {
6234
+ this.framework = "vite";
6235
+ this.base = config.base || "/";
6236
+ this.outDir = config.build?.outDir || "dist";
6237
+ this.mode = config.command === "serve" ? "dev" : "build";
6238
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6239
+ },
6240
+ // --- ROLLUP ---
6241
+ async setRollup(options) {
6242
+ this.framework = "rollup";
6243
+ this.outDir = options.dir || "dist";
6244
+ this.mode = import_node_process4.default.env.ROLLUP_WATCH ? "dev" : "build";
6245
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6246
+ },
6247
+ // --- WEBPACK ---
6248
+ async setWebpack(compiler) {
6249
+ this.framework = "webpack";
6250
+ this.base = compiler.options.output?.publicPath || "/";
6251
+ this.outDir = compiler.options.output?.path || "dist";
6252
+ const raw = compiler.options.mode || "production";
6253
+ this.mode = raw === "development" ? "dev" : "build";
6254
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6255
+ },
6256
+ // --- RSPACK ---
6257
+ async setRspack(compiler) {
6258
+ this.framework = "rspack";
6259
+ this.base = compiler.options.output?.publicPath || "/";
6260
+ this.outDir = compiler.options.output?.path || "dist";
6261
+ const raw = compiler.options.mode || "production";
6262
+ this.mode = raw === "development" ? "dev" : "build";
6263
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6264
+ },
6265
+ // --- ESBUILD ---
6266
+ async setEsbuild(build) {
6267
+ this.framework = "esbuild";
6268
+ this.outDir = build.initialOptions.outdir || "dist";
6269
+ this.mode = build.initialOptions.watch ? "dev" : "build";
6270
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6271
+ },
6272
+ // --- FARM ---
6273
+ async setFarm(compiler) {
6274
+ this.framework = "farm";
6275
+ this.base = compiler.config.output?.publicPath || "/";
6276
+ this.outDir = compiler.config.output?.path || "dist";
6277
+ const raw = compiler.config.mode || "production";
6278
+ this.mode = raw === "development" ? "dev" : "build";
6279
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6280
+ },
6281
+ // --- ROLLDOWN ---
6282
+ async setRolldown(options) {
6283
+ this.framework = "rolldown";
6284
+ this.outDir = options.dir || "dist";
6285
+ this.mode = import_node_process4.default.env.ROLLDOWN_WATCH ? "dev" : "build";
6286
+ ctx.scriptInfo = await generateScript(resolved, ctx);
6287
+ }
6288
+ };
6289
+ return ctx;
6290
+ }
6214
6291
 
6215
6292
  // src/index.ts
6216
6293
  var virtualEnvId = "virtual:env";
6217
6294
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
6218
- var base = "";
6219
- var outDir = "";
6220
6295
  var unpluginFactory = (options = {}) => {
6221
6296
  const resolved = resolveOptions(options);
6297
+ const ctx = createUnifiedContext(resolved);
6222
6298
  return [{
6223
- name: "plugin-env-serve",
6224
- apply: "serve",
6299
+ name: "plugin-env",
6225
6300
  enforce: "post",
6226
- configResolved(config) {
6227
- outDir = config.build.outDir;
6228
- base = config.base;
6301
+ rollup: {
6302
+ outputOptions(outputOptions) {
6303
+ ctx.setRollup(outputOptions);
6304
+ return outputOptions;
6305
+ }
6229
6306
  },
6230
- async resolveId(id) {
6231
- if (id.startsWith(virtualEnvId))
6232
- return resolvedVirtualEnvId;
6307
+ vite: {
6308
+ async configResolved(config) {
6309
+ await ctx.setVite(config);
6310
+ }
6233
6311
  },
6234
- async load(id) {
6235
- if (id.startsWith(resolvedVirtualEnvId)) {
6236
- const { code, watchFiles } = await generateScript(resolved, "serve", base);
6237
- watchFiles.forEach((file) => {
6238
- this.addWatchFile(file);
6239
- });
6240
- return code;
6312
+ async webpack(compiler) {
6313
+ await ctx.setWebpack(compiler);
6314
+ },
6315
+ rspack: async (compiler) => {
6316
+ await ctx.setRspack(compiler);
6317
+ },
6318
+ esbuild: {
6319
+ async setup(build) {
6320
+ await ctx.setEsbuild(build);
6241
6321
  }
6242
- }
6243
- }, {
6244
- name: "unplugin-env-build",
6245
- apply: "build",
6246
- enforce: "post",
6247
- configResolved(config) {
6248
- base = config.base;
6249
- outDir = config.build.outDir;
6250
6322
  },
6251
- resolveId(id) {
6323
+ async farm(compiler) {
6324
+ await ctx.setFarm(compiler);
6325
+ },
6326
+ rolldown: {
6327
+ async outputOptions(outputOptions) {
6328
+ await ctx.setRolldown(outputOptions);
6329
+ }
6330
+ },
6331
+ async resolveId(id) {
6252
6332
  if (id.startsWith(virtualEnvId))
6253
6333
  return resolvedVirtualEnvId;
6254
6334
  },
6255
6335
  async load(id) {
6256
- const { emit, script } = await generateScript(resolved, "build", base);
6257
- if (id.startsWith(resolvedVirtualEnvId)) {
6258
- this.emitFile(emit);
6259
- return "";
6260
- }
6261
- if (id.endsWith(".html")) {
6262
- let code = await import_node_fs3.promises.readFile(id, "utf8");
6263
- code = code.replace(/<\/head>/g, script);
6264
- return {
6265
- code
6266
- };
6336
+ const { code, watchFiles, emit, script } = ctx.scriptInfo;
6337
+ if (ctx.isDev) {
6338
+ if (id.startsWith(resolvedVirtualEnvId)) {
6339
+ watchFiles.forEach((file) => {
6340
+ this.addWatchFile(file);
6341
+ });
6342
+ return code;
6343
+ }
6344
+ } else {
6345
+ if (id.startsWith(resolvedVirtualEnvId)) {
6346
+ this.emitFile(emit);
6347
+ return "";
6348
+ }
6349
+ if (id.endsWith(".html")) {
6350
+ let code2 = await import_node_fs3.promises.readFile(id, "utf-8");
6351
+ code2 = code2.replace(/<\/head>/g, script);
6352
+ return { code: code2 };
6353
+ }
6267
6354
  }
6268
- return null;
6269
6355
  },
6270
6356
  buildEnd: () => {
6271
- import_node_process4.default.on("beforeExit", async () => {
6357
+ import_node_process5.default.on("beforeExit", async () => {
6272
6358
  const { compress } = resolved;
6273
- await createCompress(compress, outDir);
6274
- import_node_process4.default.exit(0);
6359
+ await createCompress(compress, ctx.outDir);
6360
+ import_node_process5.default.exit(0);
6275
6361
  });
6276
6362
  }
6277
6363
  }];
package/dist/nuxt.js CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  import {
7
7
  vite_default
8
- } from "./chunk-6I6MSMXF.js";
8
+ } from "./chunk-7PNJWMAM.js";
9
9
  import {
10
10
  webpack_default
11
- } from "./chunk-664Z6SFJ.js";
12
- import "./chunk-V2T5VQQ7.js";
11
+ } from "./chunk-3MY5IADX.js";
12
+ import "./chunk-IQADAXMX.js";
13
13
 
14
14
  // src/nuxt.ts
15
15
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";