unplugin-env 1.0.0 → 1.0.2

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
@@ -4309,41 +4309,41 @@ var require_queue = __commonJS({
4309
4309
  queue.drained = drained;
4310
4310
  return queue;
4311
4311
  function push(value) {
4312
- var p = new Promise(function(resolve2, reject) {
4312
+ var p = new Promise(function(resolve, reject) {
4313
4313
  pushCb(value, function(err, result) {
4314
4314
  if (err) {
4315
4315
  reject(err);
4316
4316
  return;
4317
4317
  }
4318
- resolve2(result);
4318
+ resolve(result);
4319
4319
  });
4320
4320
  });
4321
4321
  p.catch(noop);
4322
4322
  return p;
4323
4323
  }
4324
4324
  function unshift(value) {
4325
- var p = new Promise(function(resolve2, reject) {
4325
+ var p = new Promise(function(resolve, reject) {
4326
4326
  unshiftCb(value, function(err, result) {
4327
4327
  if (err) {
4328
4328
  reject(err);
4329
4329
  return;
4330
4330
  }
4331
- resolve2(result);
4331
+ resolve(result);
4332
4332
  });
4333
4333
  });
4334
4334
  p.catch(noop);
4335
4335
  return p;
4336
4336
  }
4337
4337
  function drained() {
4338
- var p = new Promise(function(resolve2) {
4338
+ var p = new Promise(function(resolve) {
4339
4339
  process.nextTick(function() {
4340
4340
  if (queue.idle()) {
4341
- resolve2();
4341
+ resolve();
4342
4342
  } else {
4343
4343
  var previousDrain = queue.drain;
4344
4344
  queue.drain = function() {
4345
4345
  if (typeof previousDrain === "function") previousDrain();
4346
- resolve2();
4346
+ resolve();
4347
4347
  queue.drain = previousDrain;
4348
4348
  };
4349
4349
  }
@@ -4829,9 +4829,9 @@ var require_stream3 = __commonJS({
4829
4829
  });
4830
4830
  }
4831
4831
  _getStat(filepath) {
4832
- return new Promise((resolve2, reject) => {
4832
+ return new Promise((resolve, reject) => {
4833
4833
  this._stat(filepath, this._fsStatSettings, (error, stats) => {
4834
- return error === null ? resolve2(stats) : reject(error);
4834
+ return error === null ? resolve(stats) : reject(error);
4835
4835
  });
4836
4836
  });
4837
4837
  }
@@ -4855,10 +4855,10 @@ var require_async5 = __commonJS({
4855
4855
  this._readerStream = new stream_1.default(this._settings);
4856
4856
  }
4857
4857
  dynamic(root, options) {
4858
- return new Promise((resolve2, reject) => {
4858
+ return new Promise((resolve, reject) => {
4859
4859
  this._walkAsync(root, options, (error, entries) => {
4860
4860
  if (error === null) {
4861
- resolve2(entries);
4861
+ resolve(entries);
4862
4862
  } else {
4863
4863
  reject(error);
4864
4864
  }
@@ -4868,10 +4868,10 @@ var require_async5 = __commonJS({
4868
4868
  async static(patterns, options) {
4869
4869
  const entries = [];
4870
4870
  const stream = this._readerStream.static(patterns, options);
4871
- return new Promise((resolve2, reject) => {
4871
+ return new Promise((resolve, reject) => {
4872
4872
  stream.once("error", reject);
4873
4873
  stream.on("data", (entry) => entries.push(entry));
4874
- stream.once("end", () => resolve2(entries));
4874
+ stream.once("end", () => resolve(entries));
4875
4875
  });
4876
4876
  }
4877
4877
  };
@@ -5544,127 +5544,10 @@ var import_node_fs3 = require("fs");
5544
5544
  var import_node_process4 = __toESM(require("process"), 1);
5545
5545
  var import_unplugin = require("unplugin");
5546
5546
 
5547
- // src/core/generate.ts
5548
- var import_node_path = __toESM(require("path"), 1);
5549
- var import_node_fs = require("fs");
5550
- var import_node_process = __toESM(require("process"), 1);
5551
- var import_fast_glob = __toESM(require_out4(), 1);
5552
- var import_recast = __toESM(require("recast"), 1);
5553
- function mergeObjects(prodObj, devObj) {
5554
- const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
5555
- for (const prop of devObj.properties) {
5556
- const key = prop.key.name || prop.key.value;
5557
- if (prodProps.has(key)) {
5558
- const index = prodObj.properties.indexOf(prodProps.get(key));
5559
- if (index >= 0)
5560
- prodObj.properties[index] = prop;
5561
- } else {
5562
- prodObj.properties.push(prop);
5563
- }
5564
- }
5565
- }
5566
- async function generateScript(options, mode, base2) {
5567
- const { dir, fileName, globalName, serve, build } = options.env;
5568
- const folder = await findFolder(import_node_process.default.cwd(), dir);
5569
- const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
5570
- absolute: true,
5571
- cwd: folder
5572
- });
5573
- const testReg = mode === "serve" ? serve : build;
5574
- let target = "";
5575
- let source = "";
5576
- let code = "";
5577
- const name = fileName;
5578
- for (const file of files) {
5579
- try {
5580
- const mod = await import_node_fs.promises.readFile(file, "utf-8");
5581
- if (testReg == null ? void 0 : testReg.test(file))
5582
- target = mod;
5583
- else
5584
- source = mod;
5585
- } catch (error) {
5586
- console.error(`Error loading file ${file}:`, error);
5587
- }
5588
- }
5589
- const targetAst = import_recast.default.parse(target);
5590
- const sourceAst = import_recast.default.parse(source);
5591
- const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5592
- const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5593
- mergeObjects(sourceExport, targetExport);
5594
- const mergedCode = import_recast.default.print(sourceExport).code;
5595
- const returnedTarget = mergedCode;
5596
- const versionInfo = await generateVersion(options, mode);
5597
- code = `window.${globalName}=${returnedTarget};${versionInfo}`;
5598
- const formatCode = code;
5599
- return {
5600
- code,
5601
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
5602
- </head>`,
5603
- emit: {
5604
- type: "asset",
5605
- fileName: name,
5606
- source: formatCode
5607
- },
5608
- watchFiles: files
5609
- };
5610
- }
5611
- async function generateVersion(options, mode) {
5612
- const packageFile = await (0, import_fast_glob.default)("package.json", {
5613
- absolute: true,
5614
- cwd: (0, import_node_path.resolve)(import_node_process.default.cwd())
5615
- });
5616
- const packageString = await import_node_fs.promises.readFile(packageFile[0], "utf8");
5617
- const packageJson = JSON.parse(packageString);
5618
- return `console.info("Version: ${packageJson.version} - ${mode === "serve" ? "runtime" : "built"} on ${options.date}")`;
5619
- }
5620
- async function findFolder(directoryPath, dir) {
5621
- const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
5622
- const files = await import_node_fs.promises.readdir(directoryPath);
5623
- const filePaths = files.filter((item) => !ignore.has(item));
5624
- let nestedFolder = "";
5625
- for (const file of filePaths) {
5626
- const fullFilePath = import_node_path.default.join(directoryPath, file);
5627
- const stat = await import_node_fs.promises.stat(fullFilePath);
5628
- if (stat.isDirectory()) {
5629
- if (file.toLowerCase() === dir) {
5630
- return fullFilePath;
5631
- } else {
5632
- nestedFolder = await findFolder(fullFilePath, dir);
5633
- if (nestedFolder)
5634
- return nestedFolder;
5635
- }
5636
- }
5637
- }
5638
- return "";
5639
- }
5640
-
5641
- // src/core/options.ts
5642
- var import_utils = require("@antfu/utils");
5643
- function resolveOptions(options) {
5644
- const defaults = {
5645
- env: {
5646
- dir: "config",
5647
- fileName: "manifest.js",
5648
- globalName: "manifest",
5649
- serve: /dev|development/i,
5650
- build: /prod|production/i
5651
- },
5652
- compress: {
5653
- outDir: "dist",
5654
- ignoreBase: true
5655
- }
5656
- };
5657
- const mergeOptions = (0, import_utils.deepMerge)(defaults, options);
5658
- return {
5659
- ...mergeOptions,
5660
- date: (/* @__PURE__ */ new Date()).toLocaleString()
5661
- };
5662
- }
5663
-
5664
5547
  // src/core/compress.ts
5665
- var import_node_fs2 = __toESM(require("fs"), 1);
5666
- var import_node_path2 = __toESM(require("path"), 1);
5667
- var import_node_process3 = __toESM(require("process"), 1);
5548
+ var import_node_fs = __toESM(require("fs"), 1);
5549
+ var import_node_path = __toESM(require("path"), 1);
5550
+ var import_node_process2 = __toESM(require("process"), 1);
5668
5551
  var import_archiver = __toESM(require("archiver"), 1);
5669
5552
 
5670
5553
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
@@ -5854,16 +5737,16 @@ var ansiStyles = assembleStyles();
5854
5737
  var ansi_styles_default = ansiStyles;
5855
5738
 
5856
5739
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.js
5857
- var import_node_process2 = __toESM(require("process"), 1);
5740
+ var import_node_process = __toESM(require("process"), 1);
5858
5741
  var import_node_os = __toESM(require("os"), 1);
5859
5742
  var import_node_tty = __toESM(require("tty"), 1);
5860
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : import_node_process2.default.argv) {
5743
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : import_node_process.default.argv) {
5861
5744
  const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
5862
5745
  const position = argv.indexOf(prefix + flag);
5863
5746
  const terminatorPosition = argv.indexOf("--");
5864
5747
  return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
5865
5748
  }
5866
- var { env } = import_node_process2.default;
5749
+ var { env } = import_node_process.default;
5867
5750
  var flagForceColor;
5868
5751
  if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
5869
5752
  flagForceColor = 0;
@@ -5919,7 +5802,7 @@ function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
5919
5802
  if (env.TERM === "dumb") {
5920
5803
  return min;
5921
5804
  }
5922
- if (import_node_process2.default.platform === "win32") {
5805
+ if (import_node_process.default.platform === "win32") {
5923
5806
  const osRelease = import_node_os.default.release().split(".");
5924
5807
  if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
5925
5808
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
@@ -6177,17 +6060,19 @@ var Log = class {
6177
6060
  };
6178
6061
 
6179
6062
  // src/core/compress.ts
6180
- async function createCompress(options) {
6181
- const { outDir, ignoreBase } = options;
6182
- const zipFilePath = import_node_path2.default.resolve(import_node_process3.default.cwd(), `${import_node_path2.default.basename(outDir)}.zip`);
6183
- Log.log("Compressing the directory", outDir);
6184
- return new Promise((resolve2, reject) => {
6063
+ async function createCompress(options, outDir2) {
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) => {
6185
6068
  try {
6186
- const output = import_node_fs2.default.createWriteStream(zipFilePath);
6069
+ const output = import_node_fs.default.createWriteStream(zipFilePath);
6187
6070
  const archive = (0, import_archiver.default)("zip");
6188
6071
  output.on("close", () => {
6189
- Log.success("Successfully compressed to", `${zipFilePath} (${archive.pointer() / 1024 / 1024} MB)`);
6190
- resolve2(null);
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);
6191
6076
  });
6192
6077
  archive.on("warning", (err) => {
6193
6078
  if (err.code === "ENOENT")
@@ -6200,8 +6085,8 @@ async function createCompress(options) {
6200
6085
  reject(err);
6201
6086
  });
6202
6087
  archive.pipe(output);
6203
- const absoluteOutDir = import_node_path2.default.resolve(outDir);
6204
- archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path2.default.basename(outDir));
6088
+ const absoluteOutDir = import_node_path.default.resolve(outDir2);
6089
+ archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path.default.basename(outDir2));
6205
6090
  archive.finalize();
6206
6091
  } catch (error) {
6207
6092
  Log.error("Error compressing the directory", error);
@@ -6210,10 +6095,128 @@ async function createCompress(options) {
6210
6095
  });
6211
6096
  }
6212
6097
 
6098
+ // src/core/generate.ts
6099
+ var import_node_fs2 = require("fs");
6100
+ var import_node_path2 = __toESM(require("path"), 1);
6101
+ var import_node_process3 = __toESM(require("process"), 1);
6102
+ var import_fast_glob = __toESM(require_out4(), 1);
6103
+ var import_local_pkg = require("local-pkg");
6104
+ var import_recast = __toESM(require("recast"), 1);
6105
+ function mergeObjects(prodObj, devObj) {
6106
+ const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
6107
+ for (const prop of devObj.properties) {
6108
+ const key = prop.key.name || prop.key.value;
6109
+ if (prodProps.has(key)) {
6110
+ const index = prodObj.properties.indexOf(prodProps.get(key));
6111
+ if (index >= 0)
6112
+ prodObj.properties[index] = prop;
6113
+ } else {
6114
+ prodObj.properties.push(prop);
6115
+ }
6116
+ }
6117
+ }
6118
+ async function generateScript(options, mode, base2) {
6119
+ const { dir, fileName, globalName, serve, build } = options.env;
6120
+ const folder = await findFolder(import_node_process3.default.cwd(), dir);
6121
+ const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6122
+ absolute: true,
6123
+ cwd: folder
6124
+ });
6125
+ const testReg = mode === "serve" ? serve : build;
6126
+ let target = "";
6127
+ let source = "";
6128
+ let code = "";
6129
+ const name = fileName;
6130
+ for (const file of files) {
6131
+ try {
6132
+ const mod = await import_node_fs2.promises.readFile(file, "utf-8");
6133
+ if (testReg?.test(file))
6134
+ target = mod;
6135
+ else
6136
+ source = mod;
6137
+ } catch (error) {
6138
+ console.error(`Error loading file ${file}:`, error);
6139
+ }
6140
+ }
6141
+ const targetAst = import_recast.default.parse(target);
6142
+ const sourceAst = import_recast.default.parse(source);
6143
+ const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6144
+ const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6145
+ mergeObjects(sourceExport, targetExport);
6146
+ const mergedCode = import_recast.default.print(sourceExport).code;
6147
+ const returnedTarget = mergedCode;
6148
+ const versionInfo = await generateVersion(options, mode);
6149
+ code = `window.${globalName}=${returnedTarget};
6150
+ ${versionInfo}`;
6151
+ const formatCode = code;
6152
+ return {
6153
+ code,
6154
+ script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6155
+ </head>`,
6156
+ emit: {
6157
+ type: "asset",
6158
+ fileName: name,
6159
+ source: formatCode
6160
+ },
6161
+ watchFiles: files
6162
+ };
6163
+ }
6164
+ async function generateVersion(options, mode) {
6165
+ 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;", '')`;
6167
+ }
6168
+ async function findFolder(directoryPath, dir) {
6169
+ const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
6170
+ const files = await import_node_fs2.promises.readdir(directoryPath);
6171
+ const filePaths = files.filter((item) => !ignore.has(item));
6172
+ let nestedFolder = "";
6173
+ for (const file of filePaths) {
6174
+ const fullFilePath = import_node_path2.default.join(directoryPath, file);
6175
+ const stat = await import_node_fs2.promises.stat(fullFilePath);
6176
+ if (stat.isDirectory()) {
6177
+ if (file.toLowerCase() === dir) {
6178
+ return fullFilePath;
6179
+ } else {
6180
+ nestedFolder = await findFolder(fullFilePath, dir);
6181
+ if (nestedFolder)
6182
+ return nestedFolder;
6183
+ }
6184
+ }
6185
+ }
6186
+ return "";
6187
+ }
6188
+
6189
+ // src/core/options.ts
6190
+ var import_utils = require("@antfu/utils");
6191
+ function resolveOptions(options) {
6192
+ const defaults = {
6193
+ env: {
6194
+ dir: "config",
6195
+ fileName: "manifest.js",
6196
+ globalName: "manifest",
6197
+ serve: /dev/i,
6198
+ build: /prod/i
6199
+ },
6200
+ compress: {
6201
+ ignoreBase: false
6202
+ }
6203
+ };
6204
+ const mergeOptions = (0, import_utils.deepMerge)(defaults, options);
6205
+ const formatter = new Intl.DateTimeFormat("zh-CN", {
6206
+ dateStyle: "full",
6207
+ timeStyle: "medium"
6208
+ });
6209
+ return {
6210
+ ...mergeOptions,
6211
+ datetime: formatter.format(/* @__PURE__ */ new Date())
6212
+ };
6213
+ }
6214
+
6213
6215
  // src/index.ts
6214
6216
  var virtualEnvId = "virtual:env";
6215
6217
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
6216
6218
  var base = "";
6219
+ var outDir = "";
6217
6220
  var unpluginFactory = (options = {}) => {
6218
6221
  const resolved = resolveOptions(options);
6219
6222
  return [{
@@ -6221,6 +6224,7 @@ var unpluginFactory = (options = {}) => {
6221
6224
  apply: "serve",
6222
6225
  enforce: "post",
6223
6226
  configResolved(config) {
6227
+ outDir = config.build.outDir;
6224
6228
  base = config.base;
6225
6229
  },
6226
6230
  async resolveId(id) {
@@ -6242,6 +6246,7 @@ var unpluginFactory = (options = {}) => {
6242
6246
  enforce: "post",
6243
6247
  configResolved(config) {
6244
6248
  base = config.base;
6249
+ outDir = config.build.outDir;
6245
6250
  },
6246
6251
  resolveId(id) {
6247
6252
  if (id.startsWith(virtualEnvId))
@@ -6265,7 +6270,7 @@ var unpluginFactory = (options = {}) => {
6265
6270
  buildEnd: () => {
6266
6271
  import_node_process4.default.on("beforeExit", async () => {
6267
6272
  const { compress } = resolved;
6268
- await createCompress(compress);
6273
+ await createCompress(compress, outDir);
6269
6274
  import_node_process4.default.exit(0);
6270
6275
  });
6271
6276
  }
@@ -6289,7 +6294,7 @@ var nuxt_default = (0, import_kit.defineNuxtModule)({
6289
6294
  defaults: {
6290
6295
  // ...default options
6291
6296
  },
6292
- setup(options, nuxt) {
6297
+ setup(options, _nuxt) {
6293
6298
  (0, import_kit.addVitePlugin)(() => vite_default(options));
6294
6299
  (0, import_kit.addWebpackPlugin)(() => webpack_default(options));
6295
6300
  }
package/dist/nuxt.js CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  import {
7
7
  vite_default
8
- } from "./chunk-JDYYGKOJ.js";
8
+ } from "./chunk-6I6MSMXF.js";
9
9
  import {
10
10
  webpack_default
11
- } from "./chunk-OQXUTP45.js";
12
- import "./chunk-U7BMFDAG.js";
11
+ } from "./chunk-664Z6SFJ.js";
12
+ import "./chunk-V2T5VQQ7.js";
13
13
 
14
14
  // src/nuxt.ts
15
15
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
@@ -22,7 +22,7 @@ var nuxt_default = defineNuxtModule({
22
22
  defaults: {
23
23
  // ...default options
24
24
  },
25
- setup(options, nuxt) {
25
+ setup(options, _nuxt) {
26
26
  addVitePlugin(() => vite_default(options));
27
27
  addWebpackPlugin(() => webpack_default(options));
28
28
  }