@jahia/vite-plugin 0.5.5 → 0.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/src/index.ts DELETED
@@ -1,234 +0,0 @@
1
- import multiEntry from "@rollup/plugin-multi-entry";
2
- import sharedLibs from "javascript-modules-engine/shared-libs.mjs";
3
- import path from "node:path";
4
- import { styleText } from "node:util";
5
- import type { Plugin } from "rollup";
6
- import { globSync } from "tinyglobby";
7
- import type { PluginOption } from "vite";
8
- import { insertFilename } from "./insert-filename.js";
9
-
10
- // These libraries are provided by Jahia and should not be bundled
11
- const external = Object.keys(sharedLibs);
12
-
13
- /** Plugin to execute a callback when a build succeeds. */
14
- function buildSuccessPlugin(callback: () => void | Promise<void>): Plugin {
15
- let succeeded = true;
16
- return {
17
- name: "build-success-callback",
18
- buildEnd(error) {
19
- succeeded = !error;
20
- },
21
- async closeBundle() {
22
- if (succeeded) await callback();
23
- },
24
- };
25
- }
26
-
27
- export default function jahia(
28
- options: {
29
- /** Options for the client-side loader. */
30
- client?: {
31
- /** Entrypoint for the client-side bundle. */
32
- input?: {
33
- /**
34
- * Parent directory of the client-side code.
35
- *
36
- * @default "./src/client/"
37
- */
38
- dir?: string;
39
- /**
40
- * Glob pattern(s) used to find all client-side code in `dir`.
41
- *
42
- * See [tinyglobby](https://www.npmjs.com/package/tinyglobby) for supported patterns.
43
- *
44
- * @default "**‍/*.jsx"
45
- */
46
- glob?: string | string[];
47
- };
48
- /**
49
- * Where to put the client-side bundle. It is a directory that will have the same structure as
50
- * the source directory.
51
- *
52
- * @default "./javascript/client/"
53
- */
54
- output?: string;
55
- };
56
-
57
- /** Options for the server-side bundle. */
58
- server?: {
59
- /**
60
- * Entrypoint for the server-side bundle.
61
- *
62
- * [Glob patterns are
63
- * supported.](https://www.npmjs.com/package/@rollup/plugin-multi-entry#supported-input-types)
64
- *
65
- * @default "./src/index.{js,ts}"
66
- */
67
- input?: string;
68
- /** Where to put the built server-side bundle. */
69
- output?: {
70
- /**
71
- * Directory where to put the built server-side bundle.
72
- *
73
- * @default "./javascript/server/"
74
- */
75
- dir?: string;
76
- /**
77
- * Base name for the built server-side bundle.
78
- *
79
- * Will be appended with '.js' for the JavaScript output and '.css' for the CSS output.
80
- *
81
- * @default "index"
82
- */
83
- fileName?: string;
84
- };
85
- };
86
-
87
- /**
88
- * Function to execute when the build is complete in watch mode. Can be used to automatically
89
- * deploy your module to a local Jahia instance.
90
- *
91
- * @default undefined
92
- */
93
- watchCallback?: () => void | Promise<void>;
94
- } = {},
95
- ): PluginOption {
96
- const clientBaseDir = options.client?.input?.dir ?? "./src/client/";
97
- const clientEntries = globSync(options.client?.input?.glob ?? "**/*.jsx", { cwd: clientBaseDir });
98
-
99
- if (clientEntries.length === 0) {
100
- console.warn(
101
- `${styleText("yellowBright", "[@jahia/vite-plugin] Skipping client build because there are no entry files...")}
102
- • If this is the intended behavior, you can safely ignore this message
103
- • Otherwise, ensure that your client files are properly configured in the plugin options
104
- Client base directory: ${styleText("cyanBright", options.client?.input?.dir ?? "./src/client/ (default value)")}
105
- Client glob pattern: ${styleText("cyanBright", String(options.client?.input?.glob ?? "**/*.jsx (default value)"))}`,
106
- );
107
- }
108
-
109
- return {
110
- name: "@jahia/vite-plugin",
111
-
112
- /**
113
- * Configuration hook.
114
- *
115
- * Updating the configuration can be done both by mutation or by merging. We use both methods to
116
- * offer the best experience for the user.
117
- *
118
- * @see https://vite.dev/guide/api-plugin.html#config
119
- */
120
- config(config) {
121
- // Mutate the configuration to set base settings if they are not already set
122
- // Enable the modern JSX runtime
123
- config.esbuild ??= { jsx: "automatic" };
124
-
125
- return {
126
- // Build all environments https://vite.dev/guide/api-environment-frameworks.html#environments-during-build
127
- builder: { sharedConfigBuild: true },
128
- // Enforce bundling of all dependencies
129
- ssr: { noExternal: true },
130
- // Define the environments (client and ssr)
131
- environments: {
132
- client: {
133
- build: {
134
- lib: {
135
- entry: Object.fromEntries(
136
- clientEntries.map((file) => [file, path.join(clientBaseDir, file)]),
137
- ),
138
- formats: ["es"],
139
- },
140
- rollupOptions: {
141
- output: {
142
- dir: options.client?.output ?? "./javascript/client/",
143
- },
144
- external,
145
- plugins: [
146
- {
147
- name: "forbid-library",
148
- resolveId(id) {
149
- this.debug(id);
150
- console.log(id);
151
- if (id === "@jahia/javascript-modules-library") {
152
- throw new Error(
153
- `You cannot import '@jahia/javascript-modules-library' in the client bundle`,
154
- );
155
- }
156
- },
157
- },
158
- ],
159
- },
160
- },
161
- },
162
- ssr: {
163
- build: {
164
- lib: {
165
- /**
166
- * Necessary for IIFE format but not used; it's the name given to the global
167
- * variable that will be created by the IIFE.
168
- */
169
- name: "serverBundle",
170
- entry: options.server?.input ?? "./src/index.{js,ts}",
171
- fileName: options.server?.output?.fileName ?? "index",
172
- // Bundle the old way, as an IIFE, to replace libs with globals
173
- formats: ["iife"],
174
- },
175
- rollupOptions: {
176
- output: {
177
- dir: options.server?.output?.dir ?? "./javascript/server/",
178
- // Replace the imports of external libraries with the globals
179
- globals: Object.fromEntries(
180
- [
181
- ...external,
182
- // This is only available on the server, attempting to import it
183
- // on the client will throw an error
184
- "@jahia/javascript-modules-library",
185
- ].map((lib) => [
186
- lib,
187
- // This is how a shared library is imported in the server bundle
188
- `javascriptModulesLibraryBuilder.getSharedLibrary(${JSON.stringify(lib)})`,
189
- ]),
190
- ),
191
- },
192
- external: [...external, "@jahia/javascript-modules-library"],
193
- treeshake: {
194
- // Manually mark useEffect as pure to have it removed from the SSR bundle
195
- manualPureFunctions: ["useEffect"],
196
- },
197
- plugins: [
198
- multiEntry({
199
- exports: false,
200
- entryFileName: `${options.server?.output?.fileName ?? "index"}.js`,
201
- }),
202
- // Only add the callback plugin in watch mode
203
- config.build?.watch &&
204
- options.watchCallback &&
205
- buildSuccessPlugin(options.watchCallback),
206
- // Insert filenames in client-side components
207
- insertFilename(
208
- options.client?.input?.dir ?? "./src/client/",
209
- options.client?.output ?? "./javascript/client/",
210
- ),
211
- ],
212
- },
213
- },
214
- },
215
- },
216
- };
217
- },
218
-
219
- configResolved(config) {
220
- // If there are no client entries, remove the client environment to prevent a build error
221
- if (clientEntries.length === 0) delete config.environments.client;
222
- },
223
-
224
- // Needed to run before Vite's default resolver
225
- enforce: "pre",
226
- resolveId(id, importer) {
227
- if (this.environment.name === "client" && id === "@jahia/javascript-modules-library") {
228
- this.error(
229
- `\n\tCannot import @jahia/javascript-modules-library in the client bundle\n\tin ${importer}\n\t${styleText("bgRedBright", "This module is only available on the server.")}`,
230
- );
231
- }
232
- },
233
- };
234
- }
@@ -1,105 +0,0 @@
1
- import { print } from "esrap";
2
- import type { Node } from "estree";
3
- import { Plugin } from "rollup";
4
- import { walk } from "zimmerframe";
5
- import { createFilter } from "@rollup/pluginutils";
6
- import path from "path";
7
-
8
- /**
9
- * This plugin adds a `__filename` property to all default exports.
10
- *
11
- * ```js
12
- * export default function myFunction() {
13
- * console.log(myFunction.__filename);
14
- * }
15
- * ```
16
- *
17
- * Becomes
18
- *
19
- * ```js
20
- * export default Object.defineProperty(
21
- * function myFunction() {
22
- * console.log(myFunction.__filename);
23
- * },
24
- * "__filename",
25
- * {
26
- * value: "path/to/file.js",
27
- * enumerable: false,
28
- * },
29
- * );
30
- * ```
31
- *
32
- * @param root The root of the transformation. Files outside this directory will not be transformed,
33
- * files inside will have their inserted path relative to this directory.
34
- */
35
- export const insertFilename = (root: string, prefix: string): Plugin => {
36
- const filter = createFilter(null, null, {
37
- resolve: root,
38
- });
39
- return {
40
- name: "insert-path",
41
-
42
- transform(code, id) {
43
- if (!filter(id)) return;
44
- const ast = walk(this.parse(code) as Node, null, {
45
- // Only target `export default function`
46
- ExportDefaultDeclaration(node) {
47
- if (node.declaration.type !== "FunctionDeclaration") return;
48
- return {
49
- // export default
50
- type: "ExportDefaultDeclaration",
51
- leadingComments: node.leadingComments,
52
- trailingComments: node.trailingComments,
53
- range: node.range,
54
- loc: node.loc,
55
- declaration: {
56
- // Object.defineProperty(...)
57
- type: "CallExpression",
58
- optional: false,
59
- callee: {
60
- type: "MemberExpression",
61
- computed: false,
62
- optional: false,
63
- object: { type: "Identifier", name: "Object" },
64
- property: { type: "Identifier", name: "defineProperty" },
65
- },
66
- arguments: [
67
- {
68
- // Original function
69
- ...node.declaration,
70
- type: "FunctionExpression",
71
- },
72
- { type: "Literal", value: "__filename" },
73
- {
74
- // { value: id, enumerable: false }
75
- type: "ObjectExpression",
76
- properties: [
77
- {
78
- type: "Property",
79
- computed: false,
80
- kind: "init",
81
- method: false,
82
- shorthand: false,
83
- key: { type: "Identifier", name: "value" },
84
- value: { type: "Literal", value: prefix + path.relative(root, id) },
85
- },
86
- {
87
- type: "Property",
88
- computed: false,
89
- kind: "init",
90
- method: false,
91
- shorthand: false,
92
- key: { type: "Identifier", name: "enumerable" },
93
- value: { type: "Literal", value: false },
94
- },
95
- ],
96
- },
97
- ],
98
- },
99
- };
100
- },
101
- });
102
- return print(ast);
103
- },
104
- };
105
- };