@arponascension/express-inertia 1.5.1 → 1.6.0

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.mjs CHANGED
@@ -724,93 +724,44 @@ function requestIdMiddleware() {
724
724
  function generateRequestId() {
725
725
  return randomUUID();
726
726
  }
727
-
728
- // src/directives.ts
729
- var customDirectives = {};
730
- function escapeJsString(str) {
731
- return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
732
- }
733
- function registerDirective(name, handler) {
734
- customDirectives[name] = handler;
735
- }
736
- function clearCustomDirectives() {
737
- for (const name of Object.keys(customDirectives)) {
738
- delete customDirectives[name];
739
- }
740
- }
741
- function compileBladeDirectives(templateSource, localDirectives) {
742
- let compiled = templateSource;
743
- compiled = compiled.replace(
744
- /<x-inertia(\s[^>]*)?\s*(\/|><\/x-inertia>)/gi,
745
- (_match, attrs) => {
746
- const idMatch = attrs?.match(/id=(?:"([^"]*)"|'([^']*)')/);
747
- const id = idMatch ? idMatch[1] || idMatch[2] : void 0;
748
- return id ? `<%- inertia('${escapeJsString(id)}') %>` : `<%- inertia() %>`;
749
- }
750
- );
751
- compiled = compiled.replace(
752
- /<x-inertia-(?:script|data)(\s[^>]*)?\s*(\/|><\/x-inertia-(?:script|data)>)/gi,
753
- (_match, attrs) => {
754
- const idMatch = attrs?.match(/id=(?:"([^"]*)"|'([^']*)')/);
755
- const id = idMatch ? idMatch[1] || idMatch[2] : void 0;
756
- return id ? `<%- inertiaScript('${escapeJsString(id)}') %>` : `<%- inertiaScript() %>`;
757
- }
758
- );
759
- compiled = compiled.replace(
760
- /<x-inertia-head\s*(?:\/>|><\/x-inertia-head>)|<x-inertiaHead\s*(?:\/>|><\/x-inertiaHead>)/gi,
761
- "<%- inertiaHead() %>"
762
- );
763
- compiled = compiled.replace(
764
- /<x-vite(\s[^>]*)?\s*(?:\/>|><\/x-vite>)/gi,
765
- (_match, attrs) => {
766
- const srcMatch = attrs?.match(/(?:src|entries)=(?:"([^"]*)"|'([^']*)')/);
767
- const val = srcMatch ? srcMatch[1] || srcMatch[2] : "";
768
- if (val.startsWith("[") && val.endsWith("]")) {
769
- return `<%- vite(${val}) %>`;
727
+ function globToRegExp(glob) {
728
+ const normalized = glob.replace(/\\/g, "/");
729
+ let source = "";
730
+ let i = 0;
731
+ const escape = (ch) => ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
732
+ while (i < normalized.length) {
733
+ const ch = normalized[i];
734
+ if (ch === "*") {
735
+ if (normalized[i + 1] === "*") {
736
+ if (normalized[i + 2] === "/") {
737
+ source += "(?:.*/)?";
738
+ i += 3;
739
+ } else {
740
+ source += ".*";
741
+ i += 2;
742
+ }
743
+ } else {
744
+ source += "[^/]*";
745
+ i += 1;
770
746
  }
771
- return `<%- vite('${escapeJsString(val)}') %>`;
772
- }
773
- );
774
- compiled = compiled.replace(
775
- /<x-vite-react-refresh\s*(?:\/>|><\/x-vite-react-refresh>)|<x-viteReactRefresh\s*(?:\/>|><\/x-viteReactRefresh>)/gi,
776
- "<%- viteReactRefresh() %>"
777
- );
778
- compiled = compiled.replace(/<x-csrf\s*(?:\/>|><\/x-csrf>)/gi, "<%- csrf() %>");
779
- compiled = compiled.replace(
780
- /<x-routes(\s[^>]*)?\s*(?:\/>|><\/x-routes>)/gi,
781
- (_match, attrs) => {
782
- const groupMatch = attrs?.match(/group=(?:"([^"]*)"|'([^']*)')/);
783
- const group = groupMatch ? groupMatch[1] || groupMatch[2] : void 0;
784
- return group ? `<%- routes('${escapeJsString(group)}') %>` : `<%- routes() %>`;
747
+ } else if (ch === "?") {
748
+ source += "[^/]";
749
+ i += 1;
750
+ } else {
751
+ source += escape(ch);
752
+ i += 1;
785
753
  }
786
- );
787
- compiled = compiled.replace(/@inertiaHead\b/g, "<%- inertiaHead() %>");
788
- compiled = compiled.replace(/@(inertiaScript|inertiaData)(\([^)]*\))?(?!\w)/g, (_match, _d, args) => {
789
- return args ? `<%- inertiaScript${args} %>` : `<%- inertiaScript() %>`;
790
- });
791
- compiled = compiled.replace(/@inertia(\([^)]*\))?(?!\w)/g, (_match, args) => {
792
- return args ? `<%- inertia${args} %>` : `<%- inertia() %>`;
793
- });
794
- compiled = compiled.replace(/@viteReactRefresh\b/g, "<%- viteReactRefresh() %>");
795
- compiled = compiled.replace(/@vite\(([^)]*(?:\([^)]*\)[^)]*)*)\)/g, (_match, args) => {
796
- return `<%- vite(${args.trim()}) %>`;
797
- });
798
- compiled = compiled.replace(/@csrf\b/g, "<%- csrf() %>");
799
- compiled = compiled.replace(/@routes(\([^)]*\))?(?!\w)/g, (_match, args) => {
800
- return args ? `<%- routes${args} %>` : `<%- routes() %>`;
801
- });
802
- compiled = compiled.replace(/@json\(([^)]*(?:\([^)]*\)[^)]*)*)\)/g, (_match, args) => {
803
- return `<%- json(${args.trim()}) %>`;
804
- });
805
- const allDirectives = { ...customDirectives, ...localDirectives || {} };
806
- for (const [name, handler] of Object.entries(allDirectives)) {
807
- const escapedName = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
808
- const pattern = new RegExp(`@${escapedName}(\\([^)]*(?:\\([^)]*\\)[^)]*)*\\))?(?!\\w)`, "g");
809
- compiled = compiled.replace(pattern, (_match, args) => {
810
- return handler(args ? args.slice(1, -1).trim() : "", {});
811
- });
812
754
  }
813
- return compiled;
755
+ return new RegExp(`^${source}$`);
756
+ }
757
+ function normalizeRefreshGlobs(refresh) {
758
+ if (refresh === void 0 || refresh === false) {
759
+ return null;
760
+ }
761
+ if (refresh === true) {
762
+ return ["views/**/*.ejs"];
763
+ }
764
+ return Array.isArray(refresh) ? refresh : [refresh];
814
765
  }
815
766
  function resolveDevServerUrl(config, override) {
816
767
  if (override) return override.replace(/\/$/, "");
@@ -833,6 +784,7 @@ function inertiaVitePlugin(options = {}) {
833
784
  const logger2 = createLogger({ prefix: "express-inertia:vite" });
834
785
  let cleanupRegistered = false;
835
786
  let exitConfig = null;
787
+ const buildDirectory = options.buildDirectory || "build";
836
788
  const write = (config, server) => {
837
789
  try {
838
790
  const hotFile = path.resolve(config.root, options.hotFile || "public/hot");
@@ -856,8 +808,56 @@ function inertiaVitePlugin(options = {}) {
856
808
  remove(exitConfig);
857
809
  }
858
810
  };
811
+ const installRefresh = (server) => {
812
+ const refreshGlobs = normalizeRefreshGlobs(options.refresh);
813
+ if (!refreshGlobs) {
814
+ return;
815
+ }
816
+ const root = server.config.root;
817
+ const matchers = refreshGlobs.map(globToRegExp);
818
+ const matches = (file) => {
819
+ const rel = path.relative(root, file).split(path.sep).join("/");
820
+ return matchers.some((re) => re.test(rel));
821
+ };
822
+ const onFileEvent = (file) => {
823
+ if (file && matches(file)) {
824
+ server.ws.send({ type: "full-reload" });
825
+ }
826
+ };
827
+ try {
828
+ server.watcher.add(refreshGlobs);
829
+ } catch (err) {
830
+ logger2.warn("Failed to watch refresh paths", { error: err.message });
831
+ }
832
+ server.watcher.on("change", onFileEvent);
833
+ server.watcher.on("add", onFileEvent);
834
+ server.watcher.on("unlink", onFileEvent);
835
+ };
859
836
  return {
860
837
  name: "express-inertia-hot-file",
838
+ config(config, env) {
839
+ if (options.input) {
840
+ if (config.base === void 0) {
841
+ config.base = env.command === "build" ? `/${buildDirectory}/` : "/";
842
+ }
843
+ if (env.command === "build") {
844
+ if (config.publicDir === void 0 || config.publicDir === "public") {
845
+ config.publicDir = false;
846
+ }
847
+ if (config.build === void 0) {
848
+ config.build = {};
849
+ }
850
+ if (config.build.manifest !== false) {
851
+ config.build.manifest = true;
852
+ }
853
+ if (config.build.outDir === void 0) {
854
+ config.build.outDir = path.resolve(config.root || process.cwd(), "public", buildDirectory);
855
+ }
856
+ config.build.rollupOptions = config.build.rollupOptions || {};
857
+ config.build.rollupOptions.input = config.build.rollupOptions.input || options.input;
858
+ }
859
+ }
860
+ },
861
861
  configResolved(config) {
862
862
  if (config.command === "build") {
863
863
  remove(config);
@@ -874,6 +874,7 @@ function inertiaVitePlugin(options = {}) {
874
874
  process.once("exit", removeOnExit);
875
875
  cleanupRegistered = true;
876
876
  }
877
+ installRefresh(server);
877
878
  },
878
879
  buildStart() {
879
880
  }
@@ -1108,7 +1109,7 @@ function createInertiaEngine(options) {
1108
1109
  const templateTimestamps = /* @__PURE__ */ new Map();
1109
1110
  engineCaches.add(templateCache);
1110
1111
  engineTimestamps.add(templateTimestamps);
1111
- return function inertiaBladeEngine(filePath, rawOptions, callback) {
1112
+ return function inertiaEjsEngine(filePath, rawOptions, callback) {
1112
1113
  try {
1113
1114
  const template = options?.templateSource;
1114
1115
  const hasTemplateSource = template !== void 0;
@@ -1131,7 +1132,7 @@ function createInertiaEngine(options) {
1131
1132
  }
1132
1133
  if (!compiledTemplate) {
1133
1134
  const templateSource = template !== void 0 ? template : fs.readFileSync(filePath, "utf-8");
1134
- const compiledSource = options?.compileTemplate ? options.compileTemplate(templateSource) : compileBladeDirectives(templateSource, options?.directives);
1135
+ const compiledSource = options?.compileTemplate ? options.compileTemplate(templateSource) : templateSource;
1135
1136
  compiledTemplate = ejs.compile(compiledSource, {
1136
1137
  filename: filePath,
1137
1138
  cache: false,
@@ -1300,6 +1301,6 @@ function createPrefetchHelper(viteHelper) {
1300
1301
  // src/index.ts
1301
1302
  var src_default = createInertia;
1302
1303
 
1303
- export { CircuitBreaker, DEFAULT_COMPONENT_NAME_PATTERN, DEFAULT_SECURITY_OPTIONS, PrefetchHelper, ViteHelper, always, calculateBackoff, clearCustomDirectives, clearEngineCache, compileBladeDirectives, createInertia, createInertiaEngine, createLogger, createPrefetchHelper, createViteHelper, src_default as default, defaultViteHelper, defer, escapeHtmlAttr, generateSriHash, getGlobalLogger, createInertia as inertia, inertiaEngine, inertiaVitePlugin, isAlways, isDeferred, isLazy, isMerge, isOptional, lazy, merge, optional, registerDirective, renderSSR, requestIdMiddleware, resetAllCircuitBreakers, resolveProps, safeStringify, sanitizeViewData, serializePage, setGlobalLogger, shouldRetry, validateComponentName };
1304
+ export { CircuitBreaker, DEFAULT_COMPONENT_NAME_PATTERN, DEFAULT_SECURITY_OPTIONS, PrefetchHelper, ViteHelper, always, calculateBackoff, clearEngineCache, createInertia, createInertiaEngine, createLogger, createPrefetchHelper, createViteHelper, src_default as default, defaultViteHelper, defer, escapeHtmlAttr, generateSriHash, getGlobalLogger, createInertia as inertia, inertiaEngine, inertiaVitePlugin, isAlways, isDeferred, isLazy, isMerge, isOptional, lazy, merge, optional, renderSSR, requestIdMiddleware, resetAllCircuitBreakers, resolveProps, safeStringify, sanitizeViewData, serializePage, setGlobalLogger, shouldRetry, validateComponentName };
1304
1305
  //# sourceMappingURL=index.mjs.map
1305
1306
  //# sourceMappingURL=index.mjs.map