@jahia/vite-plugin 0.5.1

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.
@@ -0,0 +1,351 @@
1
+ 'use strict';
2
+
3
+ const Module = require('module');
4
+ const crypto = require('crypto');
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const vm = require('vm');
8
+ const os = require('os');
9
+
10
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
11
+
12
+ //------------------------------------------------------------------------------
13
+ // FileSystemBlobStore
14
+ //------------------------------------------------------------------------------
15
+
16
+ class FileSystemBlobStore {
17
+ constructor(directory, prefix) {
18
+ const name = prefix ? slashEscape(prefix + '.') : '';
19
+ this._blobFilename = path.join(directory, name + 'BLOB');
20
+ this._mapFilename = path.join(directory, name + 'MAP');
21
+ this._lockFilename = path.join(directory, name + 'LOCK');
22
+ this._directory = directory;
23
+ this._load();
24
+ }
25
+
26
+ has(key, invalidationKey) {
27
+ if (hasOwnProperty.call(this._memoryBlobs, key)) {
28
+ return this._invalidationKeys[key] === invalidationKey;
29
+ } else if (hasOwnProperty.call(this._storedMap, key)) {
30
+ return this._storedMap[key][0] === invalidationKey;
31
+ }
32
+ return false;
33
+ }
34
+
35
+ get(key, invalidationKey) {
36
+ if (hasOwnProperty.call(this._memoryBlobs, key)) {
37
+ if (this._invalidationKeys[key] === invalidationKey) {
38
+ return this._memoryBlobs[key];
39
+ }
40
+ } else if (hasOwnProperty.call(this._storedMap, key)) {
41
+ const mapping = this._storedMap[key];
42
+ if (mapping[0] === invalidationKey) {
43
+ return this._storedBlob.slice(mapping[1], mapping[2]);
44
+ }
45
+ }
46
+ }
47
+
48
+ set(key, invalidationKey, buffer) {
49
+ this._invalidationKeys[key] = invalidationKey;
50
+ this._memoryBlobs[key] = buffer;
51
+ this._dirty = true;
52
+ }
53
+
54
+ delete(key) {
55
+ if (hasOwnProperty.call(this._memoryBlobs, key)) {
56
+ this._dirty = true;
57
+ delete this._memoryBlobs[key];
58
+ }
59
+ if (hasOwnProperty.call(this._invalidationKeys, key)) {
60
+ this._dirty = true;
61
+ delete this._invalidationKeys[key];
62
+ }
63
+ if (hasOwnProperty.call(this._storedMap, key)) {
64
+ this._dirty = true;
65
+ delete this._storedMap[key];
66
+ }
67
+ }
68
+
69
+ isDirty() {
70
+ return this._dirty;
71
+ }
72
+
73
+ save() {
74
+ const dump = this._getDump();
75
+ const blobToStore = Buffer.concat(dump[0]);
76
+ const mapToStore = JSON.stringify(dump[1]);
77
+
78
+ try {
79
+ mkdirpSync(this._directory);
80
+ fs.writeFileSync(this._lockFilename, 'LOCK', {flag: 'wx'});
81
+ } catch (error) {
82
+ // Swallow the exception if we fail to acquire the lock.
83
+ return false;
84
+ }
85
+
86
+ try {
87
+ fs.writeFileSync(this._blobFilename, blobToStore);
88
+ fs.writeFileSync(this._mapFilename, mapToStore);
89
+ } catch (error) {
90
+ throw error;
91
+ } finally {
92
+ fs.unlinkSync(this._lockFilename);
93
+ }
94
+
95
+ return true;
96
+ }
97
+
98
+ _load() {
99
+ try {
100
+ this._storedBlob = fs.readFileSync(this._blobFilename);
101
+ this._storedMap = JSON.parse(fs.readFileSync(this._mapFilename));
102
+ } catch (e) {
103
+ this._storedBlob = Buffer.alloc(0);
104
+ this._storedMap = {};
105
+ }
106
+ this._dirty = false;
107
+ this._memoryBlobs = {};
108
+ this._invalidationKeys = {};
109
+ }
110
+
111
+ _getDump() {
112
+ const buffers = [];
113
+ const newMap = {};
114
+ let offset = 0;
115
+
116
+ function push(key, invalidationKey, buffer) {
117
+ buffers.push(buffer);
118
+ newMap[key] = [invalidationKey, offset, offset + buffer.length];
119
+ offset += buffer.length;
120
+ }
121
+
122
+ for (const key of Object.keys(this._memoryBlobs)) {
123
+ const buffer = this._memoryBlobs[key];
124
+ const invalidationKey = this._invalidationKeys[key];
125
+ push(key, invalidationKey, buffer);
126
+ }
127
+
128
+ for (const key of Object.keys(this._storedMap)) {
129
+ if (hasOwnProperty.call(newMap, key)) continue;
130
+ const mapping = this._storedMap[key];
131
+ const buffer = this._storedBlob.slice(mapping[1], mapping[2]);
132
+ push(key, mapping[0], buffer);
133
+ }
134
+
135
+ return [buffers, newMap];
136
+ }
137
+ }
138
+
139
+ //------------------------------------------------------------------------------
140
+ // NativeCompileCache
141
+ //------------------------------------------------------------------------------
142
+
143
+ class NativeCompileCache {
144
+ constructor() {
145
+ this._cacheStore = null;
146
+ this._previousModuleCompile = null;
147
+ }
148
+
149
+ setCacheStore(cacheStore) {
150
+ this._cacheStore = cacheStore;
151
+ }
152
+
153
+ install() {
154
+ const self = this;
155
+ this._previousModuleCompile = Module.prototype._compile;
156
+ Module.prototype._compile = function(content, filename) {
157
+ const mod = this;
158
+ function require(id) {
159
+ return mod.require(id);
160
+ }
161
+ require.resolve = function(request) {
162
+ return Module._resolveFilename(request, mod);
163
+ };
164
+ require.main = process.mainModule;
165
+
166
+ // Enable support to add extra extension types
167
+ require.extensions = Module._extensions;
168
+ require.cache = Module._cache;
169
+
170
+ const dirname = path.dirname(filename);
171
+
172
+ const compiledWrapper = self._moduleCompile(filename, content);
173
+
174
+ // We skip the debugger setup because by the time we run, node has already
175
+ // done that itself.
176
+
177
+ const args = [mod.exports, require, mod, filename, dirname, process, global];
178
+ return compiledWrapper.apply(mod.exports, args);
179
+ };
180
+ }
181
+
182
+ uninstall() {
183
+ Module.prototype._compile = this._previousModuleCompile;
184
+ }
185
+
186
+ _moduleCompile(filename, content) {
187
+ // https://github.com/nodejs/node/blob/v7.5.0/lib/module.js#L511
188
+
189
+ // Remove shebang
190
+ var contLen = content.length;
191
+ if (contLen >= 2) {
192
+ if (content.charCodeAt(0) === 35/*#*/ &&
193
+ content.charCodeAt(1) === 33/*!*/) {
194
+ if (contLen === 2) {
195
+ // Exact match
196
+ content = '';
197
+ } else {
198
+ // Find end of shebang line and slice it off
199
+ var i = 2;
200
+ for (; i < contLen; ++i) {
201
+ var code = content.charCodeAt(i);
202
+ if (code === 10/*\n*/ || code === 13/*\r*/) break;
203
+ }
204
+ if (i === contLen) {
205
+ content = '';
206
+ } else {
207
+ // Note that this actually includes the newline character(s) in the
208
+ // new output. This duplicates the behavior of the regular
209
+ // expression that was previously used to replace the shebang line
210
+ content = content.slice(i);
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ // create wrapper function
217
+ var wrapper = Module.wrap(content);
218
+
219
+ var invalidationKey = crypto
220
+ .createHash('sha1')
221
+ .update(content, 'utf8')
222
+ .digest('hex');
223
+
224
+ var buffer = this._cacheStore.get(filename, invalidationKey);
225
+
226
+ var script = new vm.Script(wrapper, {
227
+ filename: filename,
228
+ lineOffset: 0,
229
+ displayErrors: true,
230
+ cachedData: buffer,
231
+ produceCachedData: true,
232
+ });
233
+
234
+ if (script.cachedDataProduced) {
235
+ this._cacheStore.set(filename, invalidationKey, script.cachedData);
236
+ } else if (script.cachedDataRejected) {
237
+ this._cacheStore.delete(filename);
238
+ }
239
+
240
+ var compiledWrapper = script.runInThisContext({
241
+ filename: filename,
242
+ lineOffset: 0,
243
+ columnOffset: 0,
244
+ displayErrors: true,
245
+ });
246
+
247
+ return compiledWrapper;
248
+ }
249
+ }
250
+
251
+ //------------------------------------------------------------------------------
252
+ // utilities
253
+ //
254
+ // https://github.com/substack/node-mkdirp/blob/f2003bb/index.js#L55-L98
255
+ // https://github.com/zertosh/slash-escape/blob/e7ebb99/slash-escape.js
256
+ //------------------------------------------------------------------------------
257
+
258
+ function mkdirpSync(p_) {
259
+ _mkdirpSync(path.resolve(p_), parseInt('0777', 8) & ~process.umask());
260
+ }
261
+
262
+ function _mkdirpSync(p, mode) {
263
+ try {
264
+ fs.mkdirSync(p, mode);
265
+ } catch (err0) {
266
+ if (err0.code === 'ENOENT') {
267
+ _mkdirpSync(path.dirname(p));
268
+ _mkdirpSync(p);
269
+ } else {
270
+ try {
271
+ const stat = fs.statSync(p);
272
+ if (!stat.isDirectory()) { throw err0; }
273
+ } catch (err1) {
274
+ throw err0;
275
+ }
276
+ }
277
+ }
278
+ }
279
+
280
+ function slashEscape(str) {
281
+ const ESCAPE_LOOKUP = {
282
+ '\\': 'zB',
283
+ ':': 'zC',
284
+ '/': 'zS',
285
+ '\x00': 'z0',
286
+ 'z': 'zZ',
287
+ };
288
+ return str.replace(/[\\:\/\x00z]/g, match => (ESCAPE_LOOKUP[match]));
289
+ }
290
+
291
+ function supportsCachedData() {
292
+ const script = new vm.Script('""', {produceCachedData: true});
293
+ // chakracore, as of v1.7.1.0, returns `false`.
294
+ return script.cachedDataProduced === true;
295
+ }
296
+
297
+ function getCacheDir() {
298
+ // Avoid cache ownership issues on POSIX systems.
299
+ const dirname = typeof process.getuid === 'function'
300
+ ? 'v8-compile-cache-' + process.getuid()
301
+ : 'v8-compile-cache';
302
+ const version = typeof process.versions.v8 === 'string'
303
+ ? process.versions.v8
304
+ : typeof process.versions.chakracore === 'string'
305
+ ? 'chakracore-' + process.versions.chakracore
306
+ : 'node-' + process.version;
307
+ const cacheDir = path.join(os.tmpdir(), dirname, version);
308
+ return cacheDir;
309
+ }
310
+
311
+ function getParentName() {
312
+ // `module.parent.filename` is undefined or null when:
313
+ // * node -e 'require("v8-compile-cache")'
314
+ // * node -r 'v8-compile-cache'
315
+ // * Or, requiring from the REPL.
316
+ const parentName = module.parent && typeof module.parent.filename === 'string'
317
+ ? module.parent.filename
318
+ : process.cwd();
319
+ return parentName;
320
+ }
321
+
322
+ //------------------------------------------------------------------------------
323
+ // main
324
+ //------------------------------------------------------------------------------
325
+
326
+ if (!process.env.DISABLE_V8_COMPILE_CACHE && supportsCachedData()) {
327
+ const cacheDir = getCacheDir();
328
+ const prefix = getParentName();
329
+ const blobStore = new FileSystemBlobStore(cacheDir, prefix);
330
+
331
+ const nativeCompileCache = new NativeCompileCache();
332
+ nativeCompileCache.setCacheStore(blobStore);
333
+ nativeCompileCache.install();
334
+
335
+ process.once('exit', code => {
336
+ if (blobStore.isDirty()) {
337
+ blobStore.save();
338
+ }
339
+ nativeCompileCache.uninstall();
340
+ });
341
+ }
342
+
343
+ module.exports.__TEST__ = {
344
+ FileSystemBlobStore,
345
+ NativeCompileCache,
346
+ mkdirpSync,
347
+ slashEscape,
348
+ supportsCachedData,
349
+ getCacheDir,
350
+ getParentName,
351
+ };
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "yarn",
3
+ "installationMethod": "tar",
4
+ "version": "1.22.22",
5
+ "packageManager": "yarn@1.22.17",
6
+ "license": "BSD-2-Clause",
7
+ "preferGlobal": true,
8
+ "description": "📦🐈 Fast, reliable, and secure dependency management.",
9
+ "resolutions": {
10
+ "sshpk": "^1.14.2"
11
+ },
12
+ "engines": {
13
+ "node": ">=4.0.0"
14
+ },
15
+ "repository": "yarnpkg/yarn",
16
+ "bin": {
17
+ "yarn": "./bin/yarn.js",
18
+ "yarnpkg": "./bin/yarn.js"
19
+ },
20
+ "scripts": {
21
+ "preinstall": ":; (node ./preinstall.js > /dev/null 2>&1 || true)"
22
+ },
23
+ "config": {
24
+ "commitizen": {
25
+ "path": "./node_modules/cz-conventional-changelog"
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,60 @@
1
+ // This file is a bit weird, so let me explain with some context: we're working
2
+ // to implement a tool called "Corepack" in Node. This tool will allow us to
3
+ // provide a Yarn shim to everyone using Node, meaning that they won't need to
4
+ // run `npm install -g yarn`.
5
+ //
6
+ // Still, we don't want to break the experience of people that already use `npm
7
+ // install -g yarn`! And one annoying thing with npm is that they install their
8
+ // binaries directly inside the Node bin/ folder. And Because of this, they
9
+ // refuse to overwrite binaries when they detect they don't belong to npm. Which
10
+ // means that, since the "yarn" Corepack symlink belongs to Corepack and not npm,
11
+ // running `npm install -g yarn` would crash by refusing to override the binary :/
12
+ //
13
+ // And thus we have this preinstall script, which checks whether Yarn is being
14
+ // installed as a global binary, and remove the existing symlink if it detects
15
+ // it belongs to Corepack. Since preinstall scripts run, in npm, before the global
16
+ // symlink is created, we bypass this way the ownership check.
17
+ //
18
+ // More info:
19
+ // https://github.com/arcanis/pmm/issues/6
20
+
21
+ if (process.env.npm_config_global) {
22
+ var cp = require('child_process');
23
+ var fs = require('fs');
24
+ var path = require('path');
25
+
26
+ try {
27
+ var targetPath = cp.execFileSync(process.execPath, [process.env.npm_execpath, 'bin', '-g'], {
28
+ encoding: 'utf8',
29
+ stdio: ['ignore', undefined, 'ignore'],
30
+ }).replace(/\n/g, '');
31
+
32
+ var manifest = require('./package.json');
33
+ var binNames = typeof manifest.bin === 'string'
34
+ ? [manifest.name.replace(/^@[^\/]+\//, '')]
35
+ : typeof manifest.bin === 'object' && manifest.bin !== null
36
+ ? Object.keys(manifest.bin)
37
+ : [];
38
+
39
+ binNames.forEach(function (binName) {
40
+ var binPath = path.join(targetPath, binName);
41
+
42
+ var binTarget;
43
+ try {
44
+ binTarget = fs.readlinkSync(binPath);
45
+ } catch (err) {
46
+ return;
47
+ }
48
+
49
+ if (binTarget.startsWith('../lib/node_modules/corepack/')) {
50
+ try {
51
+ fs.unlinkSync(binPath);
52
+ } catch (err) {
53
+ return;
54
+ }
55
+ }
56
+ });
57
+ } catch (err) {
58
+ // ignore errors
59
+ }
60
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@jahia/vite-plugin",
3
+ "version": "0.5.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com:Jahia/javascript-modules.git",
7
+ "directory": "vite-plugin"
8
+ },
9
+ "license": "MIT",
10
+ "type": "module",
11
+ "exports": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ },
15
+ "scripts": {
16
+ "build": "pkgroll && publint"
17
+ },
18
+ "dependencies": {
19
+ "@rollup/plugin-multi-entry": "^6.0.1",
20
+ "@rollup/pluginutils": "^5.1.4",
21
+ "esrap": "^1.4.5",
22
+ "tinyglobby": "^0.2.12",
23
+ "zimmerframe": "^1.1.2"
24
+ },
25
+ "devDependencies": {
26
+ "@types/estree": "^1.0.6",
27
+ "@types/node": "^22.13.5",
28
+ "pkgroll": "^2.11.0",
29
+ "publint": "^0.3.6",
30
+ "rollup": "^4.34.8",
31
+ "vite": "^6.1.1"
32
+ },
33
+ "peerDependencies": {
34
+ "vite": ">=6.0.0"
35
+ }
36
+ }
package/pom.xml ADDED
@@ -0,0 +1,112 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
+ <modelVersion>4.0.0</modelVersion>
3
+ <parent>
4
+ <artifactId>javascript-modules</artifactId>
5
+ <groupId>org.jahia.modules</groupId>
6
+ <version>0.5.1</version>
7
+ </parent>
8
+ <artifactId>vite-plugin</artifactId>
9
+ <name>Vite plugin for Jahia</name>
10
+ <packaging>pom</packaging>
11
+ <description>Vite plugin to produce a Jahia-compatible JavaScript Module.</description>
12
+
13
+ <build>
14
+ <plugins>
15
+ <plugin>
16
+ <groupId>org.apache.maven.plugins</groupId>
17
+ <artifactId>maven-clean-plugin</artifactId>
18
+ <configuration>
19
+ <filesets>
20
+ <fileset>
21
+ <directory>${project.basedir}/dist/</directory>
22
+ </fileset>
23
+ </filesets>
24
+ </configuration>
25
+ </plugin>
26
+ <plugin>
27
+ <groupId>com.github.eirslett</groupId>
28
+ <artifactId>frontend-maven-plugin</artifactId>
29
+ <executions>
30
+ <!-- Executions bound on the "initialize" phase (executed in order of declaration): -->
31
+ <execution>
32
+ <id>install node, yarn and npm</id>
33
+ <phase>initialize</phase>
34
+ <goals>
35
+ <goal>install-node-and-yarn</goal>
36
+ <goal>install-node-and-npm</goal>
37
+ </goals>
38
+ </execution>
39
+ <execution>
40
+ <id>yarn install</id>
41
+ <phase>initialize</phase>
42
+ <goals>
43
+ <goal>yarn</goal>
44
+ </goals>
45
+ </execution>
46
+ <execution>
47
+ <id>yarn build</id>
48
+ <phase>compile</phase>
49
+ <goals>
50
+ <goal>yarn</goal>
51
+ </goals>
52
+ <configuration>
53
+ <arguments>build</arguments>
54
+ </configuration>
55
+ </execution>
56
+ </executions>
57
+ </plugin>
58
+ </plugins>
59
+ </build>
60
+ <profiles>
61
+ <profile>
62
+ <!-- Keep package.json version in sync -->
63
+ <id>release-prepare</id>
64
+ <build>
65
+ <plugins>
66
+ <plugin>
67
+ <groupId>com.github.eirslett</groupId>
68
+ <artifactId>frontend-maven-plugin</artifactId>
69
+ <executions>
70
+ <!-- Keep the package.json in sync with the maven version -->
71
+ <execution>
72
+ <id>bump-version</id>
73
+ <phase>process-resources</phase>
74
+ <goals>
75
+ <goal>yarn</goal>
76
+ </goals>
77
+ <configuration>
78
+ <arguments>node sync-version.js ${project.version}</arguments>
79
+ </configuration>
80
+ </execution>
81
+ </executions>
82
+ </plugin>
83
+ </plugins>
84
+ </build>
85
+ </profile>
86
+ <profile>
87
+ <id>release-perform</id>
88
+ <!-- publish the tgz during the release:perform task (i.e. when the profile "release-perform" is enabled) -->
89
+ <build>
90
+ <plugins>
91
+ <plugin>
92
+ <groupId>com.github.eirslett</groupId>
93
+ <artifactId>frontend-maven-plugin</artifactId>
94
+ <executions>
95
+ <!-- Execution bound on the "deploy" phase: -->
96
+ <execution>
97
+ <id>publish-package</id>
98
+ <phase>deploy</phase>
99
+ <goals>
100
+ <goal>yarn</goal>
101
+ </goals>
102
+ <configuration>
103
+ <arguments>npm publish --access public</arguments>
104
+ </configuration>
105
+ </execution>
106
+ </executions>
107
+ </plugin>
108
+ </plugins>
109
+ </build>
110
+ </profile>
111
+ </profiles>
112
+ </project>