@pollora/vite-config 1.0.0 → 1.1.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/README.md CHANGED
@@ -24,6 +24,30 @@ It is a dev dependency of each theme, plugin or module, like `laravel-vite-plugi
24
24
 
25
25
  `<name>` is the directory name (`Modules/BlocksDemo` → `blocks-demo` for a module).
26
26
 
27
+ ## A classic theme, a plugin or a Bedrock site
28
+
29
+ Outside a Pollora project there is no shared `public/` directory to build into. `layout: 'standalone'` builds the package inside itself, where [`pollora/assets`](https://github.com/Pollora/assets) looks for it:
30
+
31
+ ```js
32
+ // wp-content/themes/my-theme/vite.config.js, or web/app/themes/my-theme/vite.config.js
33
+ import { defineConfig } from 'vite';
34
+ import pollora from '@pollora/vite-config';
35
+
36
+ export default defineConfig({
37
+ plugins: [pollora({ type: 'theme', layout: 'standalone' })],
38
+ });
39
+ ```
40
+
41
+ | | `layout: 'pollora'` (default) | `layout: 'standalone'` |
42
+ | --- | --- | --- |
43
+ | Build | `public/build/<type>/<name>/` | `build/` in the package |
44
+ | Hot file | `public/<name>.hot` | `hot` at the root of the package |
45
+ | `base` (URLs in the built CSS and chunks) | `/build/<type>/<name>/` | `./`: relative, so the build works wherever WordPress serves the folder from |
46
+ | Dev server | serves under the build path | serves from its root: the hot file holds the origin (`http://localhost:5173`) |
47
+ | Entry points | `resources/assets/app.js`, blocks | the same |
48
+
49
+ `buildDirectory` (`build`) and `hotFile` (`hot`) rename the two, and `publicDirectory` (`.`) moves the folder they sit in; give the same values to the container on the PHP side (`build_directory`, `hot_file`, `public_path`). The manifest keys do not change (`resources/assets/app.js`), so a file is looked up the same way in both layouts.
50
+
27
51
  ## Options
28
52
 
29
53
  | Option | Default | |
@@ -35,6 +59,10 @@ It is a dev dependency of each theme, plugin or module, like `laravel-vite-plugi
35
59
  | `tailwind` | `true` | Add `@tailwindcss/vite` |
36
60
  | `themeJson` | `{ baseThemeJsonPath: './theme.json' }` | Options for `wordpressThemeJson` (themes), or `false` |
37
61
  | `wordpress` | `{}` | Options for `wordpressPlugin` |
62
+ | `layout` | `'pollora'` | `'pollora'` (a Pollora project) or `'standalone'` (a classic theme, a plugin, Bedrock) |
63
+ | `publicDirectory` | the type's directory in a Pollora project, `.` when standalone | The public directory the build and hot file sit in, relative to the package root |
64
+ | `buildDirectory` | `build` (standalone only) | Build directory, relative to the public directory |
65
+ | `hotFile` | `hot` (standalone only) | Hot file, relative to the public directory |
38
66
  | `port` | `VITE_PORT`, else the type's port | Dev server port |
39
67
  | `refresh` | `[]` | Extra paths that reload the page |
40
68
  | `reloadOn` | theme: `['.blade.php']`, plugin and module: `['.blade.php', '.php']` | File endings whose change reloads the page (a block theme adds `.html`) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pollora/vite-config",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Vite configuration shared by Pollora themes, plugins and modules",
5
5
  "license": "MIT",
6
6
  "author": "RuBee group (https://rubee.group)",
package/src/index.d.ts CHANGED
@@ -9,8 +9,17 @@ export interface PolloraOptions {
9
9
  root?: string;
10
10
  /** Entries besides blocks; defaults to resources/assets/app.js when it exists */
11
11
  input?: string[];
12
- /** The project's public directory, relative to the package root */
12
+ /**
13
+ * `pollora` (default): a Pollora project, built under its public directory as build/<type>/<name>.
14
+ * `standalone`: a classic theme, a plugin or a Bedrock site: built inside the package, into build/, with a hot file at its root.
15
+ */
16
+ layout?: 'pollora' | 'standalone';
17
+ /** The project's public directory, relative to the package root; the package root itself (`.`) for a standalone layout */
13
18
  publicDirectory?: string;
19
+ /** Standalone layout: the build directory, relative to the public directory (default `build`) */
20
+ buildDirectory?: string;
21
+ /** Standalone layout: the hot file, relative to the public directory (default `hot`) */
22
+ hotFile?: string;
14
23
  /** Dev server port; defaults to VITE_PORT, else 5173 (theme), 5174 (plugin), 5175 (module) */
15
24
  port?: number;
16
25
  /** Build the blocks in resources/views/blocks (default true) */
@@ -32,6 +41,16 @@ export interface PolloraOptions {
32
41
  export default function pollora(options: PolloraOptions): PluginOption[];
33
42
  export { pollora };
34
43
 
44
+ export function layoutOf(settings: {
45
+ layout?: 'pollora' | 'standalone';
46
+ type: 'theme' | 'plugin' | 'module';
47
+ name: string;
48
+ root: string;
49
+ publicDirectory?: string;
50
+ buildDirectory?: string;
51
+ hotFile?: string;
52
+ }): { publicDirectory: string; buildDirectory: string; hotFile: string; base: string | null };
53
+
35
54
  export function kebab(name: string): string;
36
55
  export function blockEntries(root: string): Record<string, string>;
37
56
  export function devServer(port: number): Record<string, unknown>;
package/src/index.js CHANGED
@@ -111,6 +111,39 @@ function resolvePort(option, fallback) {
111
111
  return port === undefined || port === '' ? fallback : Number(port);
112
112
  }
113
113
 
114
+ const LAYOUTS = ['pollora', 'standalone'];
115
+
116
+ /**
117
+ * Where a package builds, and where its dev server is announced.
118
+ *
119
+ * `pollora` is a Pollora project: every package builds under the project's
120
+ * public directory, as build/<type>/<name> with a <name>.hot file beside it.
121
+ * `standalone` is a classic theme, a plugin or a Bedrock site, which have no
122
+ * shared public directory: the package builds inside itself, into `build/`
123
+ * with a `hot` file at its root, and `base` is relative so the built CSS and
124
+ * chunks find each other wherever WordPress serves the folder from.
125
+ *
126
+ * @param {object} settings
127
+ * @returns {{ publicDirectory: string, buildDirectory: string, hotFile: string, base: string|null }}
128
+ */
129
+ export function layoutOf({ layout, type, name, root, publicDirectory, buildDirectory, hotFile }) {
130
+ if (layout === 'standalone') {
131
+ return {
132
+ publicDirectory: publicDirectory ?? '.',
133
+ buildDirectory: buildDirectory ?? 'build',
134
+ hotFile: path.resolve(root, publicDirectory ?? '.', hotFile ?? 'hot'),
135
+ base: null,
136
+ };
137
+ }
138
+
139
+ return {
140
+ publicDirectory: publicDirectory ?? TYPES[type].publicDirectory,
141
+ buildDirectory: path.join('build', type, name),
142
+ hotFile: path.resolve(root, publicDirectory ?? TYPES[type].publicDirectory, `${name}.hot`),
143
+ base: `/build/${type}/${name}`,
144
+ };
145
+ }
146
+
114
147
  /**
115
148
  * The Vite plugins a Pollora theme, plugin or module builds with.
116
149
  *
@@ -124,14 +157,23 @@ export default function pollora(options = {}) {
124
157
  throw new Error(`@pollora/vite-config: "type" must be one of ${Object.keys(TYPES).join(', ')}, got ${JSON.stringify(type)}.`);
125
158
  }
126
159
 
160
+ const layout = options.layout ?? 'pollora';
161
+
162
+ if (!LAYOUTS.includes(layout)) {
163
+ throw new Error(`@pollora/vite-config: "layout" must be one of ${LAYOUTS.join(', ')}, got ${JSON.stringify(layout)}.`);
164
+ }
165
+
127
166
  const defaults = TYPES[type];
128
167
  const root = path.resolve(options.root ?? process.cwd());
129
168
  const directoryName = path.basename(root);
130
169
  const name = options.name ?? (type === 'module' ? kebab(directoryName) : directoryName);
131
170
  // Relative to the package root: laravel-vite-plugin strips a leading slash
132
- const publicDirectory = options.publicDirectory ?? defaults.publicDirectory;
133
- const buildDirectory = path.join('build', type, name);
134
- const base = `/build/${type}/${name}`;
171
+ const { publicDirectory, buildDirectory, hotFile, base } = layoutOf({
172
+ layout, type, name, root,
173
+ publicDirectory: options.publicDirectory,
174
+ buildDirectory: options.buildDirectory,
175
+ hotFile: options.hotFile,
176
+ });
135
177
  const port = resolvePort(options.port, defaults.port);
136
178
 
137
179
  const blocks = options.blocks === false ? {} : blockEntries(root);
@@ -151,8 +193,9 @@ export default function pollora(options = {}) {
151
193
  const plugins = [
152
194
  {
153
195
  name: 'pollora:config',
154
- config: () => ({
155
- base,
196
+ // A standalone build is relative to wherever it is served from; its dev server serves from the root
197
+ config: (config, { command }) => ({
198
+ ...(base === null ? (command === 'build' ? { base: './' } : {}) : { base }),
156
199
  build: { emptyOutDir: false },
157
200
  server: devServer(port),
158
201
  }),
@@ -167,7 +210,7 @@ export default function pollora(options = {}) {
167
210
  input: [...input, ...Object.values(blocks)],
168
211
  publicDirectory,
169
212
  // Written from the working directory, unlike the build directory resolved from the root
170
- hotFile: path.resolve(root, publicDirectory, `${name}.hot`),
213
+ hotFile,
171
214
  buildDirectory,
172
215
  refresh,
173
216
  assets: options.assets ?? defaults.assets,