unplugin-env 1.0.0 → 1.0.1

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.
@@ -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
  };
@@ -5533,127 +5533,10 @@ import { promises as fs3 } from "fs";
5533
5533
  import process5 from "process";
5534
5534
  import { createUnplugin } from "unplugin";
5535
5535
 
5536
- // src/core/generate.ts
5537
- var import_fast_glob = __toESM(require_out4(), 1);
5538
- import path, { resolve } from "path";
5539
- import { promises as fs } from "fs";
5540
- import process2 from "process";
5541
- import recast from "recast";
5542
- function mergeObjects(prodObj, devObj) {
5543
- const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
5544
- for (const prop of devObj.properties) {
5545
- const key = prop.key.name || prop.key.value;
5546
- if (prodProps.has(key)) {
5547
- const index = prodObj.properties.indexOf(prodProps.get(key));
5548
- if (index >= 0)
5549
- prodObj.properties[index] = prop;
5550
- } else {
5551
- prodObj.properties.push(prop);
5552
- }
5553
- }
5554
- }
5555
- async function generateScript(options, mode, base2) {
5556
- const { dir, fileName, globalName, serve, build } = options.env;
5557
- const folder = await findFolder(process2.cwd(), dir);
5558
- const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
5559
- absolute: true,
5560
- cwd: folder
5561
- });
5562
- const testReg = mode === "serve" ? serve : build;
5563
- let target = "";
5564
- let source = "";
5565
- let code = "";
5566
- const name = fileName;
5567
- for (const file of files) {
5568
- try {
5569
- const mod = await fs.readFile(file, "utf-8");
5570
- if (testReg == null ? void 0 : testReg.test(file))
5571
- target = mod;
5572
- else
5573
- source = mod;
5574
- } catch (error) {
5575
- console.error(`Error loading file ${file}:`, error);
5576
- }
5577
- }
5578
- const targetAst = recast.parse(target);
5579
- const sourceAst = recast.parse(source);
5580
- const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5581
- const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5582
- mergeObjects(sourceExport, targetExport);
5583
- const mergedCode = recast.print(sourceExport).code;
5584
- const returnedTarget = mergedCode;
5585
- const versionInfo = await generateVersion(options, mode);
5586
- code = `window.${globalName}=${returnedTarget};${versionInfo}`;
5587
- const formatCode = code;
5588
- return {
5589
- code,
5590
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
5591
- </head>`,
5592
- emit: {
5593
- type: "asset",
5594
- fileName: name,
5595
- source: formatCode
5596
- },
5597
- watchFiles: files
5598
- };
5599
- }
5600
- async function generateVersion(options, mode) {
5601
- const packageFile = await (0, import_fast_glob.default)("package.json", {
5602
- absolute: true,
5603
- cwd: resolve(process2.cwd())
5604
- });
5605
- const packageString = await fs.readFile(packageFile[0], "utf8");
5606
- const packageJson = JSON.parse(packageString);
5607
- return `console.info("Version: ${packageJson.version} - ${mode === "serve" ? "runtime" : "built"} on ${options.date}")`;
5608
- }
5609
- async function findFolder(directoryPath, dir) {
5610
- const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
5611
- const files = await fs.readdir(directoryPath);
5612
- const filePaths = files.filter((item) => !ignore.has(item));
5613
- let nestedFolder = "";
5614
- for (const file of filePaths) {
5615
- const fullFilePath = path.join(directoryPath, file);
5616
- const stat = await fs.stat(fullFilePath);
5617
- if (stat.isDirectory()) {
5618
- if (file.toLowerCase() === dir) {
5619
- return fullFilePath;
5620
- } else {
5621
- nestedFolder = await findFolder(fullFilePath, dir);
5622
- if (nestedFolder)
5623
- return nestedFolder;
5624
- }
5625
- }
5626
- }
5627
- return "";
5628
- }
5629
-
5630
- // src/core/options.ts
5631
- import { deepMerge } from "@antfu/utils";
5632
- function resolveOptions(options) {
5633
- const defaults = {
5634
- env: {
5635
- dir: "config",
5636
- fileName: "manifest.js",
5637
- globalName: "manifest",
5638
- serve: /dev|development/i,
5639
- build: /prod|production/i
5640
- },
5641
- compress: {
5642
- outDir: "dist",
5643
- ignoreBase: true
5644
- }
5645
- };
5646
- const mergeOptions = deepMerge(defaults, options);
5647
- return {
5648
- ...mergeOptions,
5649
- date: (/* @__PURE__ */ new Date()).toLocaleString()
5650
- };
5651
- }
5652
-
5653
5536
  // src/core/compress.ts
5654
- import fs2 from "fs";
5655
- import path2 from "path";
5656
- import process4 from "process";
5537
+ import fs from "fs";
5538
+ import path from "path";
5539
+ import process3 from "process";
5657
5540
  import archiver from "archiver";
5658
5541
 
5659
5542
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
@@ -5843,16 +5726,16 @@ var ansiStyles = assembleStyles();
5843
5726
  var ansi_styles_default = ansiStyles;
5844
5727
 
5845
5728
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.js
5846
- import process3 from "process";
5729
+ import process2 from "process";
5847
5730
  import os from "os";
5848
5731
  import tty from "tty";
5849
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process3.argv) {
5732
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process2.argv) {
5850
5733
  const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
5851
5734
  const position = argv.indexOf(prefix + flag);
5852
5735
  const terminatorPosition = argv.indexOf("--");
5853
5736
  return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
5854
5737
  }
5855
- var { env } = process3;
5738
+ var { env } = process2;
5856
5739
  var flagForceColor;
5857
5740
  if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
5858
5741
  flagForceColor = 0;
@@ -5908,7 +5791,7 @@ function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
5908
5791
  if (env.TERM === "dumb") {
5909
5792
  return min;
5910
5793
  }
5911
- if (process3.platform === "win32") {
5794
+ if (process2.platform === "win32") {
5912
5795
  const osRelease = os.release().split(".");
5913
5796
  if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
5914
5797
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
@@ -6168,15 +6051,15 @@ var Log = class {
6168
6051
  // src/core/compress.ts
6169
6052
  async function createCompress(options) {
6170
6053
  const { outDir, ignoreBase } = options;
6171
- const zipFilePath = path2.resolve(process4.cwd(), `${path2.basename(outDir)}.zip`);
6054
+ const zipFilePath = path.resolve(process3.cwd(), `${path.basename(outDir)}.zip`);
6172
6055
  Log.log("Compressing the directory", outDir);
6173
- return new Promise((resolve2, reject) => {
6056
+ return new Promise((resolve, reject) => {
6174
6057
  try {
6175
- const output = fs2.createWriteStream(zipFilePath);
6058
+ const output = fs.createWriteStream(zipFilePath);
6176
6059
  const archive = archiver("zip");
6177
6060
  output.on("close", () => {
6178
- Log.success("Successfully compressed to", `${zipFilePath} (${archive.pointer() / 1024 / 1024} MB)`);
6179
- resolve2(null);
6061
+ Log.success("Successfully compressed to", `${zipFilePath} (${(archive.pointer() / 1024 / 1024).toFixed(1)} MB)`);
6062
+ resolve(null);
6180
6063
  });
6181
6064
  archive.on("warning", (err) => {
6182
6065
  if (err.code === "ENOENT")
@@ -6189,8 +6072,8 @@ async function createCompress(options) {
6189
6072
  reject(err);
6190
6073
  });
6191
6074
  archive.pipe(output);
6192
- const absoluteOutDir = path2.resolve(outDir);
6193
- archive.directory(absoluteOutDir, ignoreBase ? false : path2.basename(outDir));
6075
+ const absoluteOutDir = path.resolve(outDir);
6076
+ archive.directory(absoluteOutDir, ignoreBase ? false : path.basename(outDir));
6194
6077
  archive.finalize();
6195
6078
  } catch (error) {
6196
6079
  Log.error("Error compressing the directory", error);
@@ -6199,6 +6082,124 @@ async function createCompress(options) {
6199
6082
  });
6200
6083
  }
6201
6084
 
6085
+ // src/core/generate.ts
6086
+ var import_fast_glob = __toESM(require_out4(), 1);
6087
+ import { promises as fs2 } from "fs";
6088
+ import path2 from "path";
6089
+ import process4 from "process";
6090
+ import { getPackageInfo } from "local-pkg";
6091
+ import recast from "recast";
6092
+ function mergeObjects(prodObj, devObj) {
6093
+ const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
6094
+ for (const prop of devObj.properties) {
6095
+ const key = prop.key.name || prop.key.value;
6096
+ if (prodProps.has(key)) {
6097
+ const index = prodObj.properties.indexOf(prodProps.get(key));
6098
+ if (index >= 0)
6099
+ prodObj.properties[index] = prop;
6100
+ } else {
6101
+ prodObj.properties.push(prop);
6102
+ }
6103
+ }
6104
+ }
6105
+ async function generateScript(options, mode, base2) {
6106
+ const { dir, fileName, globalName, serve, build } = options.env;
6107
+ const folder = await findFolder(process4.cwd(), dir);
6108
+ const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6109
+ absolute: true,
6110
+ cwd: folder
6111
+ });
6112
+ const testReg = mode === "serve" ? serve : build;
6113
+ let target = "";
6114
+ let source = "";
6115
+ let code = "";
6116
+ const name = fileName;
6117
+ for (const file of files) {
6118
+ try {
6119
+ const mod = await fs2.readFile(file, "utf-8");
6120
+ if (testReg?.test(file))
6121
+ target = mod;
6122
+ else
6123
+ source = mod;
6124
+ } catch (error) {
6125
+ console.error(`Error loading file ${file}:`, error);
6126
+ }
6127
+ }
6128
+ const targetAst = recast.parse(target);
6129
+ const sourceAst = recast.parse(source);
6130
+ const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6131
+ const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6132
+ mergeObjects(sourceExport, targetExport);
6133
+ const mergedCode = recast.print(sourceExport).code;
6134
+ const returnedTarget = mergedCode;
6135
+ const versionInfo = await generateVersion(options, mode);
6136
+ code = `window.${globalName}=${returnedTarget};
6137
+ ${versionInfo}`;
6138
+ const formatCode = code;
6139
+ return {
6140
+ code,
6141
+ script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6142
+ </head>`,
6143
+ emit: {
6144
+ type: "asset",
6145
+ fileName: name,
6146
+ source: formatCode
6147
+ },
6148
+ watchFiles: files
6149
+ };
6150
+ }
6151
+ async function generateVersion(options, mode) {
6152
+ const pkg = await getPackageInfo(process4.cwd());
6153
+ return `console.info("Version: %c${pkg?.version}%c - ${mode === "serve" ? "runtime" : "built"} on %c${options.date}%c", "color: green;", '', "color: blue;", '')`;
6154
+ }
6155
+ async function findFolder(directoryPath, dir) {
6156
+ const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
6157
+ const files = await fs2.readdir(directoryPath);
6158
+ const filePaths = files.filter((item) => !ignore.has(item));
6159
+ let nestedFolder = "";
6160
+ for (const file of filePaths) {
6161
+ const fullFilePath = path2.join(directoryPath, file);
6162
+ const stat = await fs2.stat(fullFilePath);
6163
+ if (stat.isDirectory()) {
6164
+ if (file.toLowerCase() === dir) {
6165
+ return fullFilePath;
6166
+ } else {
6167
+ nestedFolder = await findFolder(fullFilePath, dir);
6168
+ if (nestedFolder)
6169
+ return nestedFolder;
6170
+ }
6171
+ }
6172
+ }
6173
+ return "";
6174
+ }
6175
+
6176
+ // src/core/options.ts
6177
+ import { deepMerge } from "@antfu/utils";
6178
+ function resolveOptions(options) {
6179
+ const defaults = {
6180
+ env: {
6181
+ dir: "config",
6182
+ fileName: "manifest.js",
6183
+ globalName: "manifest",
6184
+ serve: /dev/i,
6185
+ build: /prod/i
6186
+ },
6187
+ compress: {
6188
+ outDir: "dist",
6189
+ ignoreBase: true
6190
+ }
6191
+ };
6192
+ const mergeOptions = deepMerge(defaults, options);
6193
+ const formatter = new Intl.DateTimeFormat("zh-CN", {
6194
+ dateStyle: "full",
6195
+ timeStyle: "medium"
6196
+ });
6197
+ return {
6198
+ ...mergeOptions,
6199
+ date: formatter.format(/* @__PURE__ */ new Date())
6200
+ };
6201
+ }
6202
+
6202
6203
  // src/index.ts
6203
6204
  var virtualEnvId = "virtual:env";
6204
6205
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
@@ -5,7 +5,7 @@
5
5
 
6
6
  import {
7
7
  unpluginFactory
8
- } from "./chunk-U7BMFDAG.js";
8
+ } from "./chunk-QWCQIGEO.js";
9
9
 
10
10
  // src/vite.ts
11
11
  import { createVitePlugin } from "unplugin";