Getting Ready for v2027 (II)

Part 1: Escaping Global Scope Chaos with <script type="module">

In Getting Ready for v2027 (I), we covered system requirements and environment configuration. Now, let's dive into the technical improvements in the generated code.

If you're upgrading from v2026 or earlier, one of the biggest changes in PHPMaker 2027 is how client-side scripts work. Previous versions split your custom JavaScript into two sections, Client Script and Startup Script, as a workaround for two limitations of classic <script> tags: DOM readiness and execution order.

In legacy pages, scripts were split like this:

<!DOCTYPE html>
<html>
<head>
  <!-- 1. Client scripts in <head>: libraries & helper functions -->
  <script src="./lib.js"></script>
  <script src="./clientscript.js"></script>
</head>
<body>
  <button id="save-btn">Save</button>

  <!-- 2. Startup scripts before </body>: DOM access & initialization -->
  <script src="./startupscript.js"></script>
</body>
</html>

You had to maintain this split for two reasons:

  1. The HTML wasn't parsed yet: If startupscript.js ran inside <head>, document.getElementById('save-btn') returned null because the <button> further down the page didn't exist yet. Placing startup scripts right before </body> guaranteed the DOM was ready.
  2. Manual load order: Classic scripts execute immediately in the exact order they appear. Placing helper scripts at the top ensured their global functions existed before startupscript.js called them at the bottom.

In v2027, Module Script (<script type="module">) solves both problems natively, letting you keep your scripts cleanly in <head> or load a single entry script.

How Module Scripts Eliminate the Split

1. Automatic defer makes the DOM ready even in <head>

Every <script type="module"> is deferred by default. When the browser encounters a module script in <head>, it:

  • Downloads the file in the background without blocking HTML rendering.
  • Waits until the entire HTML document has finished parsing before executing the code.

Because the DOM is guaranteed to exist when the module runs, you no longer need to push startup scripts to the bottom of <body> (or wrap initialization code in ew.ready / $(function () { ... }) just to wait for elements).

2. import handles dependency order automatically

Instead of relying on <script> tag order—and dumping every helper function onto the global window object—your module explicitly imports what it needs:

modulescript.js

import { formatDate } from './lib.js';
import { saveData } from './clientscript.js';

// Safe to access the DOM even though this script is loaded in <head>
const btn = document.getElementById('save-btn');
btn.addEventListener('click', () => {
  saveData(formatDate(new Date()));
});

*.php

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>Module Startup</title>

  <!-- One clean entry point in <head> -->
  <script type="module" src="./modulescript.js"></script>
</head>
<body>
  <button id="save-btn">Save</button>
</body>
</html>

When the browser loads modulescript.js, it reads the import statements, fetches lib.js and clientscript.js in parallel, executes those dependencies first, and only runs modulescript.js once both its imports and the page's HTML are ready.

Key Differences from Classic <script> Tags

Switching to <script type="module"> also changes scoping and execution rules:

  • Private module scope: Variables and functions declared at the top level of a module stay scoped to that file instead of polluting window.
  • Strict mode by default: "use strict" is always active inside modules. Silent bugs (such as accidentally creating a global variable by forgetting let or const) throw clear errors immediately.
  • Executed only once: Even if you reference the same module in multiple <script type="module"> tags or import it across several files, the browser fetches and runs it only once and shares the exported bindings.
  • Inline modules work the same way: A <script type="module"> ... </script> block without a src attribute still gets its own private scope, strict mode, and automatic defer.

Further Reading

(Next up: Part 2: Connecting Files with export and import)

Part 2: Connecting Files with export and import

In Part 1, we saw how <script type="module"> gives every file its own private scope instead of dumping variables onto window. To share functions, constants, or classes across files (such as moving reusable helpers out of your PHPMaker Module Script into dedicated .js files), you connect them explicitly using export and import.

JavaScript modules support two kinds of exports: named exports (for sharing multiple items) and default exports (for sharing one primary item).

math.js (exporting code):

// Named exports: export as many items as you need
export const PI = 3.14159;

export function add(a, b) {
  return a + b;
}

// Default export: maximum of one per file
export default function multiply(a, b) {
  return a * b;
}

app.js (importing code):

// Default export comes first (can use any local name); named exports go inside { }
import multiply, { PI, add } from './math.js';

console.log(add(2, 3));      // 5
console.log(multiply(2, 3)); // 6
console.log(PI);             // 3.14159

Tip: If two modules export a function with the same name, rename one on import using as:
import { format as formatDate } from './dateutils.js';

layout.php (single entry point):

<script type="module" src="./app.js"></script>

Unlike classic scripts, where you had to list every .js file in the exact right order in HTML, you only load your top level entry module (app.js). The browser reads its import statements and fetches math.js automatically.

Browser Path Rules to Remember

If you have used bundlers like Webpack or Vite before, native browser import paths are stricter because the browser fetches files directly over HTTP:

  • Start with a path prefix: Relative or root relative paths must begin with ./ (same folder), ../ (parent folder), or / (site root). Bare names like 'math.js' fail unless mapped via <script type="importmap">.

    • import { add } from './math.js'; :white_check_mark:
    • import { add } from 'math.js'; :cross_mark:
  • Include the file extension: The browser requests the exact URL you write and will not guess .js for you.

    • import { add } from './math.js'; :white_check_mark:
    • import { add } from './math'; :cross_mark:

Further Reading

(Next up: Part 3: Importing External Packages with importmap)