@shophost/rest-api 2.0.31 → 2.0.32

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/minify.mjs +15 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shophost/rest-api",
3
- "version": "2.0.31",
3
+ "version": "2.0.32",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "shophost-rest-api": "./scripts/shophost-rest-api.mjs"
@@ -5,6 +5,8 @@ import { minify } from "terser";
5
5
  const DIST_SRC = new URL("../../../dist/packages/rest-api/src", import.meta.url)
6
6
  .pathname;
7
7
 
8
+ const DIRECTIVE_RE = /^(?:["']use (?:client|server)["'];?\s*\n?)+/;
9
+
8
10
  const TERSER_OPTIONS = {
9
11
  compress: {
10
12
  passes: 3,
@@ -64,18 +66,29 @@ async function run() {
64
66
  const results = await Promise.all(
65
67
  jsFiles.map(async (filePath) => {
66
68
  const source = await readFile(filePath, "utf-8");
67
- const result = await minify(source, TERSER_OPTIONS);
69
+
70
+ // Extract "use client" / "use server" directives before minification
71
+ const directiveMatch = source.match(DIRECTIVE_RE);
72
+ const directive = directiveMatch ? directiveMatch[0].trim() + "\n" : "";
73
+ const sourceWithoutDirective = directive
74
+ ? source.slice(directiveMatch[0].length)
75
+ : source;
76
+
77
+ const result = await minify(sourceWithoutDirective, TERSER_OPTIONS);
68
78
 
69
79
  if (result.code == null) {
70
80
  throw new Error(`Terser returned no code for ${filePath}`);
71
81
  }
72
82
 
73
83
  // Strip trailing sourceMappingURL comment if present
74
- const code = result.code.replace(
84
+ const minified = result.code.replace(
75
85
  /\/\/# sourceMappingURL=.+\.js\.map\s*$/,
76
86
  ""
77
87
  );
78
88
 
89
+ // Re-prepend directive so Next.js can identify client/server components
90
+ const code = directive + minified;
91
+
79
92
  await writeFile(filePath, code, "utf-8");
80
93
 
81
94
  return {