@open-xchange/vite-plugin-ox-manifests 0.5.2 → 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.
Files changed (111) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/index.d.ts +56 -0
  3. package/dist/index.js +175 -0
  4. package/dist/plugins/gettext.d.ts +2 -0
  5. package/dist/plugins/gettext.js +63 -0
  6. package/dist/plugins/manifests.d.ts +2 -0
  7. package/dist/plugins/manifests.js +132 -0
  8. package/dist/plugins/meta.d.ts +2 -0
  9. package/dist/plugins/meta.js +21 -0
  10. package/dist/plugins/plugin.d.ts +24 -0
  11. package/dist/plugins/plugin.js +6 -0
  12. package/dist/plugins/relative-paths.d.ts +2 -0
  13. package/dist/plugins/relative-paths.js +9 -0
  14. package/dist/plugins/serve.d.ts +2 -0
  15. package/dist/plugins/serve.js +66 -0
  16. package/dist/plugins/settings.d.ts +2 -0
  17. package/dist/plugins/settings.js +141 -0
  18. package/dist/util.d.ts +11 -0
  19. package/dist/util.js +77 -0
  20. package/package.json +24 -22
  21. package/.eslintrc.cjs +0 -10
  22. package/.gitlab-ci.yml +0 -26
  23. package/index.js +0 -172
  24. package/src/constants.js +0 -1
  25. package/src/plugins/gettext.js +0 -69
  26. package/src/plugins/manifests.js +0 -136
  27. package/src/plugins/meta.js +0 -13
  28. package/src/plugins/relative-paths.js +0 -7
  29. package/src/plugins/serve.js +0 -64
  30. package/src/plugins/settings.js +0 -147
  31. package/src/util.js +0 -61
  32. package/test/.eslintrc.cjs +0 -5
  33. package/test/deps/main.test.js +0 -32
  34. package/test/gettext-chunked/another_chunk.js +0 -4
  35. package/test/gettext-chunked/i18n/de_DE.po +0 -16
  36. package/test/gettext-chunked/i18n/en_US.po +0 -16
  37. package/test/gettext-chunked/main.test.js +0 -67
  38. package/test/gettext-chunked/manifest.json +0 -3
  39. package/test/gettext-chunked/register.js +0 -6
  40. package/test/gettext-multiple/i18n/de_DE.po +0 -22
  41. package/test/gettext-multiple/i18n/en_US.po +0 -22
  42. package/test/gettext-multiple/main.test.js +0 -71
  43. package/test/gettext-multiple/manifest.json +0 -3
  44. package/test/gettext-multiple/register.js +0 -4
  45. package/test/gettext-simple/i18n/de_DE.po +0 -18
  46. package/test/gettext-simple/i18n/en_US.po +0 -18
  47. package/test/gettext-simple/main.test.js +0 -61
  48. package/test/gettext-simple/manifest.json +0 -3
  49. package/test/gettext-simple/register.js +0 -3
  50. package/test/hmr/main.test.js +0 -121
  51. package/test/hmr/manifest.json +0 -3
  52. package/test/hmr/register.js +0 -1
  53. package/test/hmr/settings.js +0 -3
  54. package/test/html-relative-paths/index.html +0 -7
  55. package/test/html-relative-paths/index.js +0 -3
  56. package/test/html-relative-paths/main.test.js +0 -39
  57. package/test/html-relative-paths/other.html +0 -7
  58. package/test/html-relative-paths/style.css +0 -1
  59. package/test/manifest-dependencies/main.test.js +0 -26
  60. package/test/manifest-dependencies/manifest.json +0 -3
  61. package/test/manifest-dependencies/register.js +0 -3
  62. package/test/manifest-dependencies/style.less +0 -1
  63. package/test/manifest-dynamic-deps/main.test.js +0 -28
  64. package/test/manifest-dynamic-deps/manifest.json +0 -3
  65. package/test/manifest-dynamic-deps/register.js +0 -7
  66. package/test/manifest-dynamic-deps/some_dep.js +0 -5
  67. package/test/manifest-dynamic-deps/style.less +0 -1
  68. package/test/manifest-multiple/entry1.js +0 -1
  69. package/test/manifest-multiple/main.test.js +0 -83
  70. package/test/manifest-multiple/manifest.json +0 -7
  71. package/test/manifest-multiple/sub/entry2.js +0 -3
  72. package/test/manifest-multiple/sub/manifest.json +0 -4
  73. package/test/manifest-simple/main.test.js +0 -105
  74. package/test/manifest-simple/manifest.json +0 -3
  75. package/test/manifest-simple/register.js +0 -1
  76. package/test/meta/main.test.js +0 -25
  77. package/test/meta/manifest.json +0 -3
  78. package/test/meta/register.js +0 -1
  79. package/test/preload-helper-bundled/async.js +0 -1
  80. package/test/preload-helper-bundled/index.js +0 -1
  81. package/test/preload-helper-bundled/main.test.js +0 -32
  82. package/test/preload-helper-bundled/manifest.json +0 -4
  83. package/test/preload-helper-separate/bar.js +0 -1
  84. package/test/preload-helper-separate/foo.js +0 -3
  85. package/test/preload-helper-separate/index.js +0 -1
  86. package/test/preload-helper-separate/main.test.js +0 -34
  87. package/test/preload-helper-separate/manifest.json +0 -4
  88. package/test/settings-core/index.js +0 -4
  89. package/test/settings-core/main.test.js +0 -56
  90. package/test/settings-core/manifest.json +0 -4
  91. package/test/settings-core/no-settings.js +0 -3
  92. package/test/settings-core/settings.js +0 -3
  93. package/test/settings-external/index.js +0 -3
  94. package/test/settings-external/main.test.js +0 -61
  95. package/test/settings-external/manifest.json +0 -4
  96. package/test/settings-external/settings.js +0 -3
  97. package/test/settings-ts/index.js +0 -3
  98. package/test/settings-ts/main.test.js +0 -30
  99. package/test/settings-ts/manifest.json +0 -4
  100. package/test/settings-ts/settings.ts +0 -4
  101. package/test/settings-with-requirements/foo.js +0 -3
  102. package/test/settings-with-requirements/forth-settings.js +0 -3
  103. package/test/settings-with-requirements/index.js +0 -5
  104. package/test/settings-with-requirements/main.test.js +0 -95
  105. package/test/settings-with-requirements/manifest.json +0 -12
  106. package/test/settings-with-requirements/other-settings.js +0 -3
  107. package/test/settings-with-requirements/other.js +0 -4
  108. package/test/settings-with-requirements/settings.js +0 -3
  109. package/test/settings-with-requirements/third-settings.js +0 -3
  110. package/test/util/deep-merge.test.js +0 -59
  111. package/test/util.js +0 -16
@@ -0,0 +1,141 @@
1
+ import fs from 'node:fs/promises';
2
+ import path, { posix } from 'node:path';
3
+ import fastGlob from 'fast-glob';
4
+ import { PROJECT_NAME as EXTERNALS_PROJECT_NAME } from '@open-xchange/vite-plugin-ox-externals';
5
+ import { definePlugin } from './plugin';
6
+ import { applyInputToOptions, basepath } from '../util';
7
+ import { PROJECT_NAME } from '../index';
8
+ function shiftSet(set) {
9
+ const result = set.values().next();
10
+ if (result.done)
11
+ return;
12
+ set.delete(result.value);
13
+ return result.value;
14
+ }
15
+ function assignSet(set, values) {
16
+ for (const value of values)
17
+ set.add(value);
18
+ }
19
+ function getRequirements(context, entry) {
20
+ let moduleInfo = context.getModuleInfo(entry);
21
+ if (!moduleInfo)
22
+ return [];
23
+ const open = new Set([...moduleInfo.importers, ...moduleInfo.dynamicImporters]);
24
+ const closed = new Set(open);
25
+ const entryPoints = [];
26
+ for (;;) {
27
+ const first = shiftSet(open);
28
+ if (first === undefined)
29
+ break;
30
+ if (first.includes('node_modules'))
31
+ continue;
32
+ moduleInfo = context.getModuleInfo(first);
33
+ if (!moduleInfo)
34
+ continue;
35
+ if (moduleInfo.isEntry) {
36
+ const { manifests } = moduleInfo.meta;
37
+ if (manifests) {
38
+ entryPoints.push(...manifests.map(manifest => ({ requires: 'true', ...manifest })));
39
+ continue;
40
+ }
41
+ }
42
+ const nextOpen = [...moduleInfo.importers, ...moduleInfo.dynamicImporters].filter(o => !closed.has(o));
43
+ assignSet(closed, nextOpen);
44
+ assignSet(open, nextOpen);
45
+ }
46
+ return [...new Set(entryPoints.map(e => e.requires).filter(Boolean))];
47
+ }
48
+ function mergeRequirements(requirements) {
49
+ if (requirements.length === 0)
50
+ return;
51
+ if (requirements.includes('true'))
52
+ return;
53
+ // check if is just a simple string
54
+ return requirements.map(req => /^[-\w]*$/.test(req) ? req : `(${req})`).join('||');
55
+ }
56
+ export default definePlugin(({ autoloadSettings }) => {
57
+ const settingsModules = new Map();
58
+ let resolvedConfig;
59
+ let externalPrefix;
60
+ let moduleGraph;
61
+ async function isSettingsFile(entry) {
62
+ const containsSettingsRegex = new RegExp(`import.+[,\\s]Settings.+from ['"][@${externalPrefix}]\\/io.ox\\/core\\/settings(\\.js)?['"]`);
63
+ const containsExportRegex = /export const settings = new Settings\(/;
64
+ const content = await fs.readFile(entry, { encoding: 'utf-8' });
65
+ // special case
66
+ if (entry === `${resolvedConfig.root}/io.ox/core/settings.js`)
67
+ return true;
68
+ return containsSettingsRegex.test(content) && containsExportRegex.test(content);
69
+ }
70
+ return {
71
+ name: `${PROJECT_NAME}/settings-plugin`,
72
+ configResolved(config) {
73
+ resolvedConfig = config;
74
+ const externalPlugin = resolvedConfig.plugins.find(plugin => plugin.name === EXTERNALS_PROJECT_NAME);
75
+ externalPrefix = externalPlugin ? externalPlugin.meta.prefix : '';
76
+ },
77
+ async options(options) {
78
+ if (!autoloadSettings)
79
+ return;
80
+ const input = {};
81
+ const root = resolvedConfig.root;
82
+ const entries = await fastGlob(`${root.replace(/\\/g, '/')}/**/*.{js,ts,mjs}`, { absolute: true });
83
+ await Promise.all(entries.map(async (entry) => {
84
+ if (!await isSettingsFile(entry))
85
+ return;
86
+ settingsModules.set(entry, { namespace: 'settings', path: '', generator: 'settings' });
87
+ const baseLength = root.length + 1;
88
+ const extLength = path.extname(entry).length;
89
+ const entryPath = entry.substring(baseLength, entry.length - extLength);
90
+ input[entryPath] = entry;
91
+ }));
92
+ if (resolvedConfig.mode === 'production') {
93
+ applyInputToOptions(input, options);
94
+ }
95
+ return options;
96
+ },
97
+ configureServer(server) {
98
+ moduleGraph = server.moduleGraph;
99
+ },
100
+ // specific call to collect all manifests
101
+ getManifests() {
102
+ return Array.from(settingsModules, ([path, manifest]) => {
103
+ manifest.path = basepath(path.slice(resolvedConfig.root.length + 1));
104
+ const moduleNode = moduleGraph?.idToModuleMap.get(path);
105
+ if (moduleNode?.lastHMRTimestamp) {
106
+ manifest.raw = posix.join(resolvedConfig.base, `/${manifest.path}.js?t=${moduleNode.lastHMRTimestamp}`);
107
+ }
108
+ return manifest;
109
+ });
110
+ },
111
+ transform(_code, id) {
112
+ if (resolvedConfig.mode !== 'production')
113
+ return;
114
+ const manifest = settingsModules.get(id);
115
+ return manifest ? { meta: { manifests: [manifest] } } : undefined;
116
+ },
117
+ generateBundle(_options, bundle) {
118
+ if (!autoloadSettings)
119
+ return;
120
+ for (const file in bundle) {
121
+ const chunk = bundle[file];
122
+ if (chunk.type !== 'chunk' || !chunk.facadeModuleId)
123
+ continue;
124
+ const meta = this.getModuleInfo(chunk.facadeModuleId)?.meta;
125
+ if (!meta?.manifests)
126
+ continue;
127
+ for (const manifest of meta.manifests) {
128
+ if (manifest.generator !== 'settings')
129
+ continue;
130
+ delete manifest.generator;
131
+ const requirements = getRequirements(this, chunk.facadeModuleId);
132
+ if (!requirements.length)
133
+ continue;
134
+ const requires = mergeRequirements(requirements);
135
+ if (requires)
136
+ manifest.requires = requires;
137
+ }
138
+ }
139
+ }
140
+ };
141
+ });
package/dist/util.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ResolvedConfig, ViteDevServer, Rollup } from 'vite';
2
+ import type { OxManifest } from './index';
3
+ export type Dict<T = unknown> = Record<string, T>;
4
+ export declare function applyInputToOptions(input: Dict<string>, options: Rollup.InputOptions): void;
5
+ export declare function joinUrlPaths(...paths: string[]): string;
6
+ export declare function basepath(src: string): string;
7
+ export declare function deepMergeObject(a: Dict, b: Dict): Dict;
8
+ export declare function mergeManifests(manifests: OxManifest[]): OxManifest[];
9
+ export declare function stringifyJSON({ build }: ResolvedConfig, json: unknown): string;
10
+ export declare function waitForReload(server: ViteDevServer): Promise<void>;
11
+ export declare function wait(millis: number): Promise<void>;
package/dist/util.js ADDED
@@ -0,0 +1,77 @@
1
+ import path from 'node:path';
2
+ import { normalizePath } from 'vite';
3
+ export function applyInputToOptions(input, options) {
4
+ // apply changes to input
5
+ if (Object.keys(input).length > 0) {
6
+ if (options.input) {
7
+ if (typeof options.input === 'string') {
8
+ input.index = options.input;
9
+ options.input = input;
10
+ }
11
+ else if (Array.isArray(options.input)) {
12
+ options.input = [...options.input, ...Object.values(input)];
13
+ }
14
+ else {
15
+ Object.assign(input, options.input);
16
+ options.input = input;
17
+ }
18
+ }
19
+ }
20
+ }
21
+ export function joinUrlPaths(...paths) {
22
+ return normalizePath(path.join(...paths));
23
+ }
24
+ export function basepath(src) {
25
+ const { dir, name } = path.parse(src);
26
+ return joinUrlPaths(dir, name);
27
+ }
28
+ export function deepMergeObject(a, b) {
29
+ const keys = [...new Set([...Object.keys(a), ...Object.keys(b)])];
30
+ const result = {};
31
+ for (const key of keys) {
32
+ if (a[key] === undefined)
33
+ result[key] = b[key];
34
+ else if (b[key] === undefined)
35
+ result[key] = a[key];
36
+ else if (Array.isArray(a[key]) !== Array.isArray(b[key]))
37
+ throw new Error('Cannot merge different types');
38
+ else if (typeof a[key] !== typeof b[key])
39
+ throw new Error('Cannot merge different types');
40
+ else if (Array.isArray(a[key]))
41
+ result[key] = [...a[key], ...b[key]];
42
+ else if (typeof a[key] === 'object')
43
+ result[key] = deepMergeObject(a[key], b[key]);
44
+ else
45
+ result[key] = b[key];
46
+ }
47
+ return result;
48
+ }
49
+ export function mergeManifests(manifests) {
50
+ const manifestsByHash = new Map();
51
+ for (const manifest of manifests) {
52
+ const hash = `${manifest.namespace}--${manifest.path}`;
53
+ // overwrites existing ones. last wins
54
+ manifestsByHash.set(hash, manifest);
55
+ }
56
+ return Array.from(manifestsByHash.values());
57
+ }
58
+ export function stringifyJSON({ build }, json) {
59
+ return (build.minify === false) ? JSON.stringify(json, null, 2) : JSON.stringify(json);
60
+ }
61
+ export function waitForReload(server) {
62
+ return new Promise(resolve => {
63
+ const send = server.ws.send.bind(server.ws);
64
+ server.ws.send = function (arg1, ...args) {
65
+ try {
66
+ send(arg1, ...args);
67
+ }
68
+ finally {
69
+ if (typeof arg1 === 'object' && arg1.type === 'full-reload')
70
+ resolve();
71
+ }
72
+ };
73
+ });
74
+ }
75
+ export async function wait(millis) {
76
+ await new Promise(resolve => setTimeout(resolve, millis));
77
+ }
package/package.json CHANGED
@@ -1,42 +1,44 @@
1
1
  {
2
2
  "name": "@open-xchange/vite-plugin-ox-manifests",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "A vite plugin to concat and serve ox manifests",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "type": "module",
7
8
  "repository": "git@gitlab.open-xchange.com:frontend/vite-plugin-ox-manifests.git",
8
9
  "author": "Richard Petersen",
9
10
  "license": "MIT",
10
11
  "scripts": {
11
- "lint": "eslint *.js src/**/*.js test/**/**.js",
12
12
  "prepare": "husky install",
13
- "test": "NODE_OPTIONS=\"--experimental-vm-modules --max_old_space_size=1792\" jest test/**/*.test.js --testTimeout=15000 --runInBand"
13
+ "lint": "tsc --noEmit && eslint . --ext .js,.cjs,.mjs,.ts",
14
+ "build": "npx --yes rimraf dist && tsc",
15
+ "test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --max_old_space_size=1792\" jest --testTimeout=15000 --runInBand"
14
16
  },
15
17
  "dependencies": {
16
- "@open-xchange/rollup-plugin-po2json": "^0.5.0",
18
+ "@open-xchange/rollup-plugin-po2json": "^0.8.0",
17
19
  "chokidar": "^3.5.1",
18
- "fast-glob": "^3.2.5",
19
- "magic-string": "^0.26.2",
20
+ "fast-glob": "^3.2.12",
21
+ "magic-string": "^0.30.0",
20
22
  "parseurl": "^1.3.3"
21
23
  },
22
24
  "devDependencies": {
23
- "@open-xchange/vite-plugin-ox-externals": "^0.3.0",
24
- "@rollup/plugin-json": "^4.1.0",
25
- "eslint": "^8.21.0",
26
- "eslint-config-standard": "^17.0.0",
27
- "eslint-plugin-import": "^2.26.0",
28
- "eslint-plugin-jest": "^26.8.0",
29
- "eslint-plugin-node": "^11.1.0",
30
- "eslint-plugin-promise": "^6.0.0",
31
- "express": "^4.18.1",
32
- "husky": "^8.0.1",
33
- "jest": "^28.1.3",
25
+ "@open-xchange/lint": "^0.0.2",
26
+ "@open-xchange/vite-plugin-ox-externals": "^0.5.0",
27
+ "@types/node": "^20.1.5",
28
+ "@types/parseurl": "^1.3.1",
29
+ "@typescript-eslint/eslint-plugin": "^5.59.0",
30
+ "@typescript-eslint/parser": "^5.59.0",
31
+ "cross-env": "^7.0.3",
32
+ "eslint-plugin-jest": "^27.2.1",
33
+ "express": "^4.18.2",
34
+ "jest": "^29.5.0",
35
+ "jest-junit": "^16.0.0",
34
36
  "less": "^4.1.3",
35
- "rollup": "^2.77.2",
36
- "rollup-plugin-auto-external": "^2.0.0",
37
- "vite": "^4.0.2"
37
+ "ts-jest": "^29.1.0",
38
+ "typescript": "^5.0.4",
39
+ "vite": "^4.2.1"
38
40
  },
39
41
  "lint-staged": {
40
- "*.js": "eslint --cache --fix"
42
+ "*.{js,cjs,mjs,ts}": "eslint --cache --fix"
41
43
  }
42
44
  }
package/.eslintrc.cjs DELETED
@@ -1,10 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- env: {
4
- node: true,
5
- es2021: true
6
- },
7
- extends: [
8
- 'standard'
9
- ]
10
- }
package/.gitlab-ci.yml DELETED
@@ -1,26 +0,0 @@
1
- image: node:18-alpine
2
-
3
- stages:
4
- - test
5
-
6
- unit tests:
7
- stage: test
8
- variables:
9
- KUBERNETES_CPU_REQUEST: 2
10
- KUBERNETES_CPU_LIMIT: 3
11
- KUBERNETES_MEMORY_REQUEST: 2Gi
12
- KUBERNETES_MEMORY_LIMIT: 2Gi
13
- script:
14
- - apk add git
15
- - yarn
16
- - yarn test --ci --coverage
17
- coverage: /^Lines\s*:\s\d+.\d+\%/
18
- artifacts:
19
- reports:
20
- junit: output/junit.xml
21
- coverage_report:
22
- coverage_format: cobertura
23
- path: output/coverage/cobertura-coverage.xml
24
- needs: []
25
- tags:
26
- - kubernetes
package/index.js DELETED
@@ -1,172 +0,0 @@
1
- import { PROJECT_NAME as name } from './src/constants.js'
2
- import manifestsPlugin from './src/plugins/manifests.js'
3
- import relativePathsPlugin from './src/plugins/relative-paths.js'
4
- import settingsPlugin from './src/plugins/settings.js'
5
- import servePlugin from './src/plugins/serve.js'
6
- import gettextPlugin from './src/plugins/gettext.js'
7
- import metaPlugin from './src/plugins/meta.js'
8
- import { deepMergeObject } from './src/util.js'
9
-
10
- /**
11
- * Creates a vite-plugin to include manifests in dev and production mode
12
- *
13
- * @param {object} [options]
14
- * @param {boolean} [options.watch=false] - If set to true, this plugin will watch manifest changes and reload the ui
15
- * @param {string} [options.entryPoints] - Convenience method to specify additional entry points for the production build. Can be specified as a glob-pattern.
16
- * @param {boolean} [options.manifestsAsEntryPoints=true] - Specifies, that every path in a manifest is used as an entry-point for the production build.
17
- * @param {boolean} [options.autoloadSettings=true] - If disabled, settings will not be automatically detected and therefore have to provide a manual manifest.json.
18
- * @param supportedEntryExtensions {string[]=['js', 'mjs', 'ts']} - An array of valid file-extensions for entry points.
19
- * @param {object} [options.meta] - An object that will be translated into a meta.json file in the root directory.
20
- */
21
- export default function ({
22
- watch = false,
23
- entryPoints,
24
- manifestsAsEntryPoints = true,
25
- transformAbsolutePaths,
26
- autoloadSettings = true,
27
- supportedEntryExtensions = ['js', 'mjs', 'ts'],
28
- meta
29
- } = {}) {
30
- const options = {
31
- watch,
32
- entryPoints,
33
- manifestsAsEntryPoints,
34
- transformAbsolutePaths,
35
- autoloadSettings,
36
- supportedEntryExtensions,
37
- meta
38
- }
39
-
40
- const plugins = [
41
- // add settings first
42
- settingsPlugin(options),
43
- relativePathsPlugin(options),
44
- servePlugin(options),
45
- gettextPlugin(options),
46
- metaPlugin(options),
47
- // manifest plugin last
48
- manifestsPlugin(options)
49
- ]
50
- let resolvedConfig
51
-
52
- const vitePluginOxManifests = {
53
-
54
- name,
55
-
56
- enforce: 'post',
57
-
58
- async config (config, env) {
59
- for (const plugin of plugins) {
60
- if (plugin.config) {
61
- config = (await plugin.config.call(this, config, env)) || config
62
- }
63
- }
64
- return config
65
- },
66
-
67
- async getManifests () {
68
- const manifestsByHash = {}
69
- for (const plugin of plugins) {
70
- if (plugin.getManifests) {
71
- const manifests = await plugin.getManifests.call(this)
72
- manifests.forEach(manifest => {
73
- const hash = `${manifest.namespace}--${manifest.path}`
74
- // overwrites existing ones. last wins
75
- manifestsByHash[hash] = manifest
76
- })
77
- }
78
- }
79
- return Object.values(manifestsByHash)
80
- },
81
-
82
- async configResolved (config) {
83
- resolvedConfig = config
84
- for (const plugin of plugins) {
85
- if (plugin.configResolved) {
86
- plugin._config = config
87
- await plugin.configResolved.call(this, config)
88
- }
89
- }
90
- },
91
-
92
- async options (options) {
93
- if (resolvedConfig.mode === 'production') {
94
- options.preserveEntrySignatures = 'strict'
95
- }
96
- for (const plugin of plugins) {
97
- if (plugin.options) {
98
- options = (await plugin.options.call(this, options)) || options
99
- }
100
- }
101
- return options
102
- },
103
-
104
- buildStart (options) {
105
- for (const plugin of plugins) {
106
- if (plugin.buildStart) {
107
- plugin.buildStart.call(this, options)
108
- }
109
- }
110
- },
111
-
112
- async configureServer (server) {
113
- for (const plugin of plugins) {
114
- if (plugin.configureServer) {
115
- await plugin.configureServer.call(vitePluginOxManifests, server)
116
- }
117
- }
118
- },
119
-
120
- async resolveId (source, importer, options) {
121
- for (const plugin of plugins) {
122
- if (plugin.resolveId) {
123
- const result = await plugin.resolveId.call(this, source, importer, options)
124
- if (result !== undefined) return result
125
- }
126
- }
127
- },
128
-
129
- async load (id) {
130
- for (const plugin of plugins) {
131
- if (plugin.load) {
132
- const result = await plugin.load.call(this, id)
133
- if (result !== undefined) return result
134
- }
135
- }
136
- },
137
-
138
- async transform (code, id) {
139
- let result = {}
140
- for (const plugin of plugins) {
141
- if (plugin.transform) {
142
- // TODO check what happens if there is already meta from other plugins
143
- const that = await plugin.transform.call(this, code, id)
144
- if (that !== undefined) {
145
- result = deepMergeObject(result, that)
146
- }
147
- }
148
- }
149
- if (Object.keys(result).length === 0) return
150
- return result
151
- },
152
-
153
- async generateBundle (options, bundle) {
154
- for (const plugin of plugins) {
155
- if (plugin.generateBundle) {
156
- await plugin.generateBundle.call(this, options, bundle)
157
- }
158
- }
159
- },
160
-
161
- async transformIndexHtml (src) {
162
- for (const plugin of plugins) {
163
- if (plugin.transformIndexHtml) {
164
- src = (await plugin.transformIndexHtml.call(this, src)) || src
165
- }
166
- }
167
- return src
168
- }
169
- }
170
-
171
- return vitePluginOxManifests
172
- }
package/src/constants.js DELETED
@@ -1 +0,0 @@
1
- export const PROJECT_NAME = '@open-xchange/vite-plugin-ox-manifests'
@@ -1,69 +0,0 @@
1
- import { applyInputToOptions } from '../util.js'
2
- import path, { posix } from 'path'
3
- import { readdir } from 'fs/promises'
4
- import { normalizePath } from 'vite'
5
- import { parsePoFile, namespacesFrom } from '@open-xchange/rollup-plugin-po2json/lib/po2json.js'
6
-
7
- export default function () {
8
- const manifests = []
9
- let resolvedConfig
10
- return {
11
- configResolved (config) {
12
- resolvedConfig = config
13
- },
14
-
15
- async options (options) {
16
- const gettextPlugin = resolvedConfig.plugins.find(plugin => plugin.name === 'gettext')
17
- if (!gettextPlugin) return
18
-
19
- const { meta: { poFiles, defaultDictionary, defaultLanguage } } = gettextPlugin
20
- const input = {}
21
-
22
- // add dictionaries as entrypoints
23
- if (typeof poFiles === 'string') {
24
- const poDir = path.dirname(poFiles)
25
- const files = (await readdir(poDir)).filter(f => f.indexOf(defaultLanguage) >= 0).map(f => normalizePath(`${poDir}/${f}`))
26
- await Promise.all(files.map(async (file) => {
27
- const po = await parsePoFile(file)
28
- for (const namespace of (namespacesFrom(po.items))) {
29
- if (namespace === defaultDictionary) continue
30
- input[namespace] = `gettext?dictionary=${namespace}`
31
- manifests.push({ namespace: 'i18n', path: `${namespace}` })
32
- }
33
- }))
34
- }
35
- input[defaultDictionary] = 'gettext'
36
- manifests.push({ namespace: 'i18n', path: `${defaultDictionary}` })
37
-
38
- if (resolvedConfig.mode === 'production') {
39
- applyInputToOptions(input, options)
40
- }
41
-
42
- return options
43
- },
44
-
45
- getManifests () {
46
- if (resolvedConfig.mode !== 'production') {
47
- return manifests.map(({ namespace, path }) => ({ namespace, raw: posix.join(resolvedConfig.base, `/@id/${path}.js`), path }))
48
- }
49
- return manifests
50
- },
51
-
52
- async generateBundle (options, bundle) {
53
- for (const file in bundle) {
54
- const chunk = bundle[file]
55
- if (!chunk.facadeModuleId) continue
56
-
57
- const modules = Object.keys(chunk.modules)
58
- if (modules.length === 0) modules.push(chunk.facadeModuleId)
59
- modules.forEach(id => {
60
- const meta = this.getModuleInfo(id)?.meta
61
- if (meta?.gettext?.dictionary !== true) return
62
-
63
- meta.manifests = meta.manifests || []
64
- meta.manifests.push({ namespace: 'i18n' })
65
- })
66
- }
67
- }
68
- }
69
- }