@vocab/webpack 0.0.0-cjs-exports-20230421064630
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/LICENSE +21 -0
- package/chunk-name/dist/vocab-webpack-chunk-name.cjs.d.ts +1 -0
- package/chunk-name/dist/vocab-webpack-chunk-name.cjs.dev.js +9 -0
- package/chunk-name/dist/vocab-webpack-chunk-name.cjs.js +7 -0
- package/chunk-name/dist/vocab-webpack-chunk-name.cjs.prod.js +9 -0
- package/chunk-name/dist/vocab-webpack-chunk-name.esm.js +5 -0
- package/chunk-name/package.json +4 -0
- package/dist/declarations/src/chunk-name.d.ts +1 -0
- package/dist/declarations/src/index.d.ts +12 -0
- package/dist/declarations/src/loader.d.ts +5 -0
- package/dist/declarations/src/logger.d.ts +3 -0
- package/dist/declarations/src/web.d.ts +3 -0
- package/dist/logger-65149486.cjs.dev.js +12 -0
- package/dist/logger-e1862ea1.esm.js +6 -0
- package/dist/logger-f79e34a8.cjs.prod.js +12 -0
- package/dist/vocab-webpack.cjs.d.ts +1 -0
- package/dist/vocab-webpack.cjs.dev.js +43 -0
- package/dist/vocab-webpack.cjs.js +7 -0
- package/dist/vocab-webpack.cjs.prod.js +43 -0
- package/dist/vocab-webpack.esm.js +38 -0
- package/loader/dist/vocab-webpack-loader.cjs.d.ts +2 -0
- package/loader/dist/vocab-webpack-loader.cjs.dev.js +136 -0
- package/loader/dist/vocab-webpack-loader.cjs.js +7 -0
- package/loader/dist/vocab-webpack-loader.cjs.prod.js +136 -0
- package/loader/dist/vocab-webpack-loader.esm.js +108 -0
- package/loader/package.json +4 -0
- package/package.json +56 -0
- package/web/dist/vocab-webpack-web.cjs.d.ts +1 -0
- package/web/dist/vocab-webpack-web.cjs.dev.js +34 -0
- package/web/dist/vocab-webpack-web.cjs.js +7 -0
- package/web/dist/vocab-webpack-web.cjs.prod.js +34 -0
- package/web/dist/vocab-webpack-web.esm.js +26 -0
- package/web/package.json +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
### MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 SEEK
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/chunk-name";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getChunkName(lang: string): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { UserConfig } from '@vocab/core';
|
|
2
|
+
import type { Compiler } from 'webpack';
|
|
3
|
+
interface UserOptions extends Partial<UserConfig> {
|
|
4
|
+
configFile?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const compiledVocabFileFilter: RegExp;
|
|
7
|
+
export declare class VocabWebpackPlugin {
|
|
8
|
+
options: UserConfig;
|
|
9
|
+
constructor({ configFile, ...rest }?: UserOptions);
|
|
10
|
+
apply(compiler: Compiler): void;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { UserConfig } from '@vocab/core';
|
|
2
|
+
import type { LoaderContext as WebpackLoaderContext } from 'webpack';
|
|
3
|
+
type LoaderContext = WebpackLoaderContext<UserConfig>;
|
|
4
|
+
export default function vocabLoader(this: LoaderContext, source: string): Promise<void>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('chalk');
|
|
4
|
+
var debug = require('debug');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var debug__default = /*#__PURE__*/_interopDefault(debug);
|
|
9
|
+
|
|
10
|
+
const trace = debug__default["default"](`vocab:webpack`);
|
|
11
|
+
|
|
12
|
+
exports.trace = trace;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('chalk');
|
|
4
|
+
var debug = require('debug');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var debug__default = /*#__PURE__*/_interopDefault(debug);
|
|
9
|
+
|
|
10
|
+
const trace = debug__default["default"](`vocab:webpack`);
|
|
11
|
+
|
|
12
|
+
exports.trace = trace;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./declarations/src/index";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@vocab/core');
|
|
6
|
+
var logger = require('./logger-65149486.cjs.dev.js');
|
|
7
|
+
require('chalk');
|
|
8
|
+
require('debug');
|
|
9
|
+
|
|
10
|
+
const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
|
|
11
|
+
class VocabWebpackPlugin {
|
|
12
|
+
constructor({
|
|
13
|
+
configFile,
|
|
14
|
+
...rest
|
|
15
|
+
} = {}) {
|
|
16
|
+
logger.trace(`Creating Vocab plugin${configFile ? ` with config file ${configFile}` : ''}`);
|
|
17
|
+
this.options = {
|
|
18
|
+
...core.resolveConfigSync(configFile),
|
|
19
|
+
...rest
|
|
20
|
+
};
|
|
21
|
+
core.validateConfig(this.options);
|
|
22
|
+
}
|
|
23
|
+
apply(compiler) {
|
|
24
|
+
logger.trace(`Applying plugin: ${compiler.options.name} (${compiler.options.target})`);
|
|
25
|
+
if (!compiler.options.module) {
|
|
26
|
+
// @ts-expect-error Support for older versions of webpack that may not have module defined at this stage
|
|
27
|
+
compiler.options.module = {
|
|
28
|
+
rules: []
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (!compiler.options.module.rules) {
|
|
32
|
+
compiler.options.module.rules = [];
|
|
33
|
+
}
|
|
34
|
+
compiler.options.module.rules.splice(0, 0, {
|
|
35
|
+
test: compiledVocabFileFilter,
|
|
36
|
+
loader: require.resolve('@vocab/webpack/loader'),
|
|
37
|
+
options: this.options
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.VocabWebpackPlugin = VocabWebpackPlugin;
|
|
43
|
+
exports.compiledVocabFileFilter = compiledVocabFileFilter;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@vocab/core');
|
|
6
|
+
var logger = require('./logger-f79e34a8.cjs.prod.js');
|
|
7
|
+
require('chalk');
|
|
8
|
+
require('debug');
|
|
9
|
+
|
|
10
|
+
const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
|
|
11
|
+
class VocabWebpackPlugin {
|
|
12
|
+
constructor({
|
|
13
|
+
configFile,
|
|
14
|
+
...rest
|
|
15
|
+
} = {}) {
|
|
16
|
+
logger.trace(`Creating Vocab plugin${configFile ? ` with config file ${configFile}` : ''}`);
|
|
17
|
+
this.options = {
|
|
18
|
+
...core.resolveConfigSync(configFile),
|
|
19
|
+
...rest
|
|
20
|
+
};
|
|
21
|
+
core.validateConfig(this.options);
|
|
22
|
+
}
|
|
23
|
+
apply(compiler) {
|
|
24
|
+
logger.trace(`Applying plugin: ${compiler.options.name} (${compiler.options.target})`);
|
|
25
|
+
if (!compiler.options.module) {
|
|
26
|
+
// @ts-expect-error Support for older versions of webpack that may not have module defined at this stage
|
|
27
|
+
compiler.options.module = {
|
|
28
|
+
rules: []
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (!compiler.options.module.rules) {
|
|
32
|
+
compiler.options.module.rules = [];
|
|
33
|
+
}
|
|
34
|
+
compiler.options.module.rules.splice(0, 0, {
|
|
35
|
+
test: compiledVocabFileFilter,
|
|
36
|
+
loader: require.resolve('@vocab/webpack/loader'),
|
|
37
|
+
options: this.options
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.VocabWebpackPlugin = VocabWebpackPlugin;
|
|
43
|
+
exports.compiledVocabFileFilter = compiledVocabFileFilter;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { resolveConfigSync, validateConfig } from '@vocab/core';
|
|
2
|
+
import { t as trace } from './logger-e1862ea1.esm.js';
|
|
3
|
+
import 'chalk';
|
|
4
|
+
import 'debug';
|
|
5
|
+
|
|
6
|
+
const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
|
|
7
|
+
class VocabWebpackPlugin {
|
|
8
|
+
constructor({
|
|
9
|
+
configFile,
|
|
10
|
+
...rest
|
|
11
|
+
} = {}) {
|
|
12
|
+
trace(`Creating Vocab plugin${configFile ? ` with config file ${configFile}` : ''}`);
|
|
13
|
+
this.options = {
|
|
14
|
+
...resolveConfigSync(configFile),
|
|
15
|
+
...rest
|
|
16
|
+
};
|
|
17
|
+
validateConfig(this.options);
|
|
18
|
+
}
|
|
19
|
+
apply(compiler) {
|
|
20
|
+
trace(`Applying plugin: ${compiler.options.name} (${compiler.options.target})`);
|
|
21
|
+
if (!compiler.options.module) {
|
|
22
|
+
// @ts-expect-error Support for older versions of webpack that may not have module defined at this stage
|
|
23
|
+
compiler.options.module = {
|
|
24
|
+
rules: []
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (!compiler.options.module.rules) {
|
|
28
|
+
compiler.options.module.rules = [];
|
|
29
|
+
}
|
|
30
|
+
compiler.options.module.rules.splice(0, 0, {
|
|
31
|
+
test: compiledVocabFileFilter,
|
|
32
|
+
loader: require.resolve('@vocab/webpack/loader'),
|
|
33
|
+
options: this.options
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { VocabWebpackPlugin, compiledVocabFileFilter };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var cjsModuleLexer = require('cjs-module-lexer');
|
|
7
|
+
var esModuleLexer = require('es-module-lexer');
|
|
8
|
+
var core = require('@vocab/core');
|
|
9
|
+
var chunkName_dist_vocabWebpackChunkName = require('../../chunk-name/dist/vocab-webpack-chunk-name.cjs.dev.js');
|
|
10
|
+
var logger = require('../../dist/logger-65149486.cjs.dev.js');
|
|
11
|
+
require('chalk');
|
|
12
|
+
require('debug');
|
|
13
|
+
|
|
14
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
function _interopNamespace(e) {
|
|
17
|
+
if (e && e.__esModule) return e;
|
|
18
|
+
var n = Object.create(null);
|
|
19
|
+
if (e) {
|
|
20
|
+
Object.keys(e).forEach(function (k) {
|
|
21
|
+
if (k !== 'default') {
|
|
22
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return e[k]; }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
n["default"] = e;
|
|
31
|
+
return Object.freeze(n);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
35
|
+
var cjsModuleLexer__namespace = /*#__PURE__*/_interopNamespace(cjsModuleLexer);
|
|
36
|
+
var esModuleLexer__namespace = /*#__PURE__*/_interopNamespace(esModuleLexer);
|
|
37
|
+
|
|
38
|
+
const trace = logger.trace.extend('loader');
|
|
39
|
+
|
|
40
|
+
// Resolve virtual-resource-loader dependency from current package
|
|
41
|
+
const virtualResourceLoader = require.resolve('virtual-resource-loader');
|
|
42
|
+
function createIdentifier(lang, resourcePath, loadedTranslation) {
|
|
43
|
+
var _loadedTranslation$la;
|
|
44
|
+
trace('Creating identifier for language', lang);
|
|
45
|
+
const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
46
|
+
const langJson = {};
|
|
47
|
+
for (const key of loadedTranslation.keys) {
|
|
48
|
+
langJson[key] = languageTranslations[key].message;
|
|
49
|
+
}
|
|
50
|
+
const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
|
|
51
|
+
const unloader = `${virtualResourceLoader}?source=${base64}`;
|
|
52
|
+
const fileIdent = path__default["default"].basename(resourcePath, 'translations.json');
|
|
53
|
+
return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// reimplement `stringifyRequest` from loader-utils 2.x
|
|
57
|
+
// https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
|
|
58
|
+
function stringifyRequest(request) {
|
|
59
|
+
return JSON.stringify(this.utils.contextify(this.context, request));
|
|
60
|
+
}
|
|
61
|
+
function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
|
|
62
|
+
return lang => {
|
|
63
|
+
const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
|
|
64
|
+
return (/* ts */`
|
|
65
|
+
createLanguage(
|
|
66
|
+
require.resolveWeak(${identifier}),
|
|
67
|
+
() => import(
|
|
68
|
+
/* webpackChunkName: ${JSON.stringify(chunkName_dist_vocabWebpackChunkName.getChunkName(lang))} */
|
|
69
|
+
${identifier}
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
`.trim()
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function findExportNames(source, mode) {
|
|
77
|
+
if (mode === 'esm') {
|
|
78
|
+
const [, exports] = esModuleLexer__namespace.parse(source);
|
|
79
|
+
return exports;
|
|
80
|
+
}
|
|
81
|
+
const {
|
|
82
|
+
exports
|
|
83
|
+
} = cjsModuleLexer__namespace.parse(source);
|
|
84
|
+
return exports;
|
|
85
|
+
}
|
|
86
|
+
async function vocabLoader(source) {
|
|
87
|
+
trace(`Using vocab loader for ${this.resourcePath}`);
|
|
88
|
+
const callback = this.async();
|
|
89
|
+
if (!callback) {
|
|
90
|
+
throw new Error(`Webpack didn't provide an async callback`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// this is necessary for the Web Assembly boot
|
|
94
|
+
await esModuleLexer__namespace.init;
|
|
95
|
+
const config = this.getOptions();
|
|
96
|
+
const devJsonFilePath = core.getDevLanguageFileFromTsFile(this.resourcePath);
|
|
97
|
+
const loadedTranslation = core.loadTranslation({
|
|
98
|
+
filePath: devJsonFilePath,
|
|
99
|
+
fallbacks: 'all'
|
|
100
|
+
}, config);
|
|
101
|
+
const target = this.target;
|
|
102
|
+
if (target && target !== 'web') {
|
|
103
|
+
trace(`Why are you using the loader on ${target}?`);
|
|
104
|
+
callback(new Error('Called Vocab Loader with non-web target'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
|
|
108
|
+
const translations = /* ts */`
|
|
109
|
+
const translations = createTranslationFile({
|
|
110
|
+
${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
|
|
111
|
+
});
|
|
112
|
+
`;
|
|
113
|
+
let result;
|
|
114
|
+
const esmExports = findExportNames(source, 'esm');
|
|
115
|
+
if (esmExports.length > 0) {
|
|
116
|
+
const exportName = esmExports[0];
|
|
117
|
+
result = /* ts */`
|
|
118
|
+
import { createLanguage, createTranslationFile } from '@vocab/webpack/${target};';
|
|
119
|
+
${translations}
|
|
120
|
+
export { translations as ${exportName} };
|
|
121
|
+
`;
|
|
122
|
+
} else {
|
|
123
|
+
// init needs to be called and waited upon
|
|
124
|
+
await cjsModuleLexer__namespace.init();
|
|
125
|
+
const exportName = findExportNames(source, 'cjs')[0];
|
|
126
|
+
result = /* ts */`
|
|
127
|
+
const { createLanguage, createTranslationFile } = require('@vocab/webpack/${target}');
|
|
128
|
+
${translations}
|
|
129
|
+
exports.${exportName} = translations;
|
|
130
|
+
`;
|
|
131
|
+
}
|
|
132
|
+
trace('Created translation file', result);
|
|
133
|
+
callback(null, result);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
exports["default"] = vocabLoader;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var cjsModuleLexer = require('cjs-module-lexer');
|
|
7
|
+
var esModuleLexer = require('es-module-lexer');
|
|
8
|
+
var core = require('@vocab/core');
|
|
9
|
+
var chunkName_dist_vocabWebpackChunkName = require('../../chunk-name/dist/vocab-webpack-chunk-name.cjs.prod.js');
|
|
10
|
+
var logger = require('../../dist/logger-f79e34a8.cjs.prod.js');
|
|
11
|
+
require('chalk');
|
|
12
|
+
require('debug');
|
|
13
|
+
|
|
14
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
function _interopNamespace(e) {
|
|
17
|
+
if (e && e.__esModule) return e;
|
|
18
|
+
var n = Object.create(null);
|
|
19
|
+
if (e) {
|
|
20
|
+
Object.keys(e).forEach(function (k) {
|
|
21
|
+
if (k !== 'default') {
|
|
22
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return e[k]; }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
n["default"] = e;
|
|
31
|
+
return Object.freeze(n);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
35
|
+
var cjsModuleLexer__namespace = /*#__PURE__*/_interopNamespace(cjsModuleLexer);
|
|
36
|
+
var esModuleLexer__namespace = /*#__PURE__*/_interopNamespace(esModuleLexer);
|
|
37
|
+
|
|
38
|
+
const trace = logger.trace.extend('loader');
|
|
39
|
+
|
|
40
|
+
// Resolve virtual-resource-loader dependency from current package
|
|
41
|
+
const virtualResourceLoader = require.resolve('virtual-resource-loader');
|
|
42
|
+
function createIdentifier(lang, resourcePath, loadedTranslation) {
|
|
43
|
+
var _loadedTranslation$la;
|
|
44
|
+
trace('Creating identifier for language', lang);
|
|
45
|
+
const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
46
|
+
const langJson = {};
|
|
47
|
+
for (const key of loadedTranslation.keys) {
|
|
48
|
+
langJson[key] = languageTranslations[key].message;
|
|
49
|
+
}
|
|
50
|
+
const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
|
|
51
|
+
const unloader = `${virtualResourceLoader}?source=${base64}`;
|
|
52
|
+
const fileIdent = path__default["default"].basename(resourcePath, 'translations.json');
|
|
53
|
+
return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// reimplement `stringifyRequest` from loader-utils 2.x
|
|
57
|
+
// https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
|
|
58
|
+
function stringifyRequest(request) {
|
|
59
|
+
return JSON.stringify(this.utils.contextify(this.context, request));
|
|
60
|
+
}
|
|
61
|
+
function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
|
|
62
|
+
return lang => {
|
|
63
|
+
const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
|
|
64
|
+
return (/* ts */`
|
|
65
|
+
createLanguage(
|
|
66
|
+
require.resolveWeak(${identifier}),
|
|
67
|
+
() => import(
|
|
68
|
+
/* webpackChunkName: ${JSON.stringify(chunkName_dist_vocabWebpackChunkName.getChunkName(lang))} */
|
|
69
|
+
${identifier}
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
`.trim()
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function findExportNames(source, mode) {
|
|
77
|
+
if (mode === 'esm') {
|
|
78
|
+
const [, exports] = esModuleLexer__namespace.parse(source);
|
|
79
|
+
return exports;
|
|
80
|
+
}
|
|
81
|
+
const {
|
|
82
|
+
exports
|
|
83
|
+
} = cjsModuleLexer__namespace.parse(source);
|
|
84
|
+
return exports;
|
|
85
|
+
}
|
|
86
|
+
async function vocabLoader(source) {
|
|
87
|
+
trace(`Using vocab loader for ${this.resourcePath}`);
|
|
88
|
+
const callback = this.async();
|
|
89
|
+
if (!callback) {
|
|
90
|
+
throw new Error(`Webpack didn't provide an async callback`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// this is necessary for the Web Assembly boot
|
|
94
|
+
await esModuleLexer__namespace.init;
|
|
95
|
+
const config = this.getOptions();
|
|
96
|
+
const devJsonFilePath = core.getDevLanguageFileFromTsFile(this.resourcePath);
|
|
97
|
+
const loadedTranslation = core.loadTranslation({
|
|
98
|
+
filePath: devJsonFilePath,
|
|
99
|
+
fallbacks: 'all'
|
|
100
|
+
}, config);
|
|
101
|
+
const target = this.target;
|
|
102
|
+
if (target && target !== 'web') {
|
|
103
|
+
trace(`Why are you using the loader on ${target}?`);
|
|
104
|
+
callback(new Error('Called Vocab Loader with non-web target'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
|
|
108
|
+
const translations = /* ts */`
|
|
109
|
+
const translations = createTranslationFile({
|
|
110
|
+
${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
|
|
111
|
+
});
|
|
112
|
+
`;
|
|
113
|
+
let result;
|
|
114
|
+
const esmExports = findExportNames(source, 'esm');
|
|
115
|
+
if (esmExports.length > 0) {
|
|
116
|
+
const exportName = esmExports[0];
|
|
117
|
+
result = /* ts */`
|
|
118
|
+
import { createLanguage, createTranslationFile } from '@vocab/webpack/${target};';
|
|
119
|
+
${translations}
|
|
120
|
+
export { translations as ${exportName} };
|
|
121
|
+
`;
|
|
122
|
+
} else {
|
|
123
|
+
// init needs to be called and waited upon
|
|
124
|
+
await cjsModuleLexer__namespace.init();
|
|
125
|
+
const exportName = findExportNames(source, 'cjs')[0];
|
|
126
|
+
result = /* ts */`
|
|
127
|
+
const { createLanguage, createTranslationFile } = require('@vocab/webpack/${target}');
|
|
128
|
+
${translations}
|
|
129
|
+
exports.${exportName} = translations;
|
|
130
|
+
`;
|
|
131
|
+
}
|
|
132
|
+
trace('Created translation file', result);
|
|
133
|
+
callback(null, result);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
exports["default"] = vocabLoader;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import * as cjsModuleLexer from 'cjs-module-lexer';
|
|
3
|
+
import * as esModuleLexer from 'es-module-lexer';
|
|
4
|
+
import { getDevLanguageFileFromTsFile, loadTranslation } from '@vocab/core';
|
|
5
|
+
import { getChunkName } from '../../chunk-name/dist/vocab-webpack-chunk-name.esm.js';
|
|
6
|
+
import { t as trace$1 } from '../../dist/logger-e1862ea1.esm.js';
|
|
7
|
+
import 'chalk';
|
|
8
|
+
import 'debug';
|
|
9
|
+
|
|
10
|
+
const trace = trace$1.extend('loader');
|
|
11
|
+
|
|
12
|
+
// Resolve virtual-resource-loader dependency from current package
|
|
13
|
+
const virtualResourceLoader = require.resolve('virtual-resource-loader');
|
|
14
|
+
function createIdentifier(lang, resourcePath, loadedTranslation) {
|
|
15
|
+
var _loadedTranslation$la;
|
|
16
|
+
trace('Creating identifier for language', lang);
|
|
17
|
+
const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
18
|
+
const langJson = {};
|
|
19
|
+
for (const key of loadedTranslation.keys) {
|
|
20
|
+
langJson[key] = languageTranslations[key].message;
|
|
21
|
+
}
|
|
22
|
+
const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
|
|
23
|
+
const unloader = `${virtualResourceLoader}?source=${base64}`;
|
|
24
|
+
const fileIdent = path.basename(resourcePath, 'translations.json');
|
|
25
|
+
return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// reimplement `stringifyRequest` from loader-utils 2.x
|
|
29
|
+
// https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
|
|
30
|
+
function stringifyRequest(request) {
|
|
31
|
+
return JSON.stringify(this.utils.contextify(this.context, request));
|
|
32
|
+
}
|
|
33
|
+
function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
|
|
34
|
+
return lang => {
|
|
35
|
+
const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
|
|
36
|
+
return (/* ts */`
|
|
37
|
+
createLanguage(
|
|
38
|
+
require.resolveWeak(${identifier}),
|
|
39
|
+
() => import(
|
|
40
|
+
/* webpackChunkName: ${JSON.stringify(getChunkName(lang))} */
|
|
41
|
+
${identifier}
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
`.trim()
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function findExportNames(source, mode) {
|
|
49
|
+
if (mode === 'esm') {
|
|
50
|
+
const [, exports] = esModuleLexer.parse(source);
|
|
51
|
+
return exports;
|
|
52
|
+
}
|
|
53
|
+
const {
|
|
54
|
+
exports
|
|
55
|
+
} = cjsModuleLexer.parse(source);
|
|
56
|
+
return exports;
|
|
57
|
+
}
|
|
58
|
+
async function vocabLoader(source) {
|
|
59
|
+
trace(`Using vocab loader for ${this.resourcePath}`);
|
|
60
|
+
const callback = this.async();
|
|
61
|
+
if (!callback) {
|
|
62
|
+
throw new Error(`Webpack didn't provide an async callback`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// this is necessary for the Web Assembly boot
|
|
66
|
+
await esModuleLexer.init;
|
|
67
|
+
const config = this.getOptions();
|
|
68
|
+
const devJsonFilePath = getDevLanguageFileFromTsFile(this.resourcePath);
|
|
69
|
+
const loadedTranslation = loadTranslation({
|
|
70
|
+
filePath: devJsonFilePath,
|
|
71
|
+
fallbacks: 'all'
|
|
72
|
+
}, config);
|
|
73
|
+
const target = this.target;
|
|
74
|
+
if (target && target !== 'web') {
|
|
75
|
+
trace(`Why are you using the loader on ${target}?`);
|
|
76
|
+
callback(new Error('Called Vocab Loader with non-web target'));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
|
|
80
|
+
const translations = /* ts */`
|
|
81
|
+
const translations = createTranslationFile({
|
|
82
|
+
${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
|
|
83
|
+
});
|
|
84
|
+
`;
|
|
85
|
+
let result;
|
|
86
|
+
const esmExports = findExportNames(source, 'esm');
|
|
87
|
+
if (esmExports.length > 0) {
|
|
88
|
+
const exportName = esmExports[0];
|
|
89
|
+
result = /* ts */`
|
|
90
|
+
import { createLanguage, createTranslationFile } from '@vocab/webpack/${target};';
|
|
91
|
+
${translations}
|
|
92
|
+
export { translations as ${exportName} };
|
|
93
|
+
`;
|
|
94
|
+
} else {
|
|
95
|
+
// init needs to be called and waited upon
|
|
96
|
+
await cjsModuleLexer.init();
|
|
97
|
+
const exportName = findExportNames(source, 'cjs')[0];
|
|
98
|
+
result = /* ts */`
|
|
99
|
+
const { createLanguage, createTranslationFile } = require('@vocab/webpack/${target}');
|
|
100
|
+
${translations}
|
|
101
|
+
exports.${exportName} = translations;
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
trace('Created translation file', result);
|
|
105
|
+
callback(null, result);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { vocabLoader as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vocab/webpack",
|
|
3
|
+
"version": "0.0.0-cjs-exports-20230421064630",
|
|
4
|
+
"main": "dist/vocab-webpack.cjs.js",
|
|
5
|
+
"module": "dist/vocab-webpack.esm.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package.json": "./package.json",
|
|
8
|
+
".": {
|
|
9
|
+
"module": "./dist/vocab-webpack.esm.js",
|
|
10
|
+
"default": "./dist/vocab-webpack.cjs.js"
|
|
11
|
+
},
|
|
12
|
+
"./chunk-name": {
|
|
13
|
+
"module": "./chunk-name/dist/vocab-webpack-chunk-name.esm.js",
|
|
14
|
+
"default": "./chunk-name/dist/vocab-webpack-chunk-name.cjs.js"
|
|
15
|
+
},
|
|
16
|
+
"./loader": {
|
|
17
|
+
"module": "./loader/dist/vocab-webpack-loader.esm.js",
|
|
18
|
+
"default": "./loader/dist/vocab-webpack-loader.cjs.js"
|
|
19
|
+
},
|
|
20
|
+
"./web": {
|
|
21
|
+
"module": "./web/dist/vocab-webpack-web.esm.js",
|
|
22
|
+
"default": "./web/dist/vocab-webpack-web.cjs.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"author": "SEEK",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"preconstruct": {
|
|
28
|
+
"entrypoints": [
|
|
29
|
+
"index.ts",
|
|
30
|
+
"loader.ts",
|
|
31
|
+
"web.ts",
|
|
32
|
+
"chunk-name.ts"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"chunk-name",
|
|
37
|
+
"dist",
|
|
38
|
+
"loader",
|
|
39
|
+
"web"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@vocab/core": "^1.3.0",
|
|
43
|
+
"chalk": "^4.1.0",
|
|
44
|
+
"cjs-module-lexer": "^1.2.2",
|
|
45
|
+
"debug": "^4.3.1",
|
|
46
|
+
"es-module-lexer": "^0.9.3",
|
|
47
|
+
"virtual-resource-loader": "^1.0.1"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/debug": "^4.1.5",
|
|
51
|
+
"webpack": "^5.37.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"webpack": "^5.37.0"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/web";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var icuHandler = require('@vocab/core/icu-handler');
|
|
6
|
+
var translationFile = require('@vocab/core/translation-file');
|
|
7
|
+
|
|
8
|
+
const createLanguage = (moduleId, loadImport) => {
|
|
9
|
+
let promiseValue;
|
|
10
|
+
return {
|
|
11
|
+
getValue: locale => {
|
|
12
|
+
// @ts-expect-error Missing webpack types
|
|
13
|
+
if (!__webpack_modules__[moduleId]) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// @ts-expect-error Missing webpack types
|
|
18
|
+
const m = __webpack_require__(moduleId);
|
|
19
|
+
return icuHandler.getParsedICUMessages(m, locale);
|
|
20
|
+
},
|
|
21
|
+
load: () => {
|
|
22
|
+
if (!promiseValue) {
|
|
23
|
+
promiseValue = loadImport();
|
|
24
|
+
}
|
|
25
|
+
return promiseValue;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(exports, 'createTranslationFile', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return translationFile.createTranslationFile; }
|
|
33
|
+
});
|
|
34
|
+
exports.createLanguage = createLanguage;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var icuHandler = require('@vocab/core/icu-handler');
|
|
6
|
+
var translationFile = require('@vocab/core/translation-file');
|
|
7
|
+
|
|
8
|
+
const createLanguage = (moduleId, loadImport) => {
|
|
9
|
+
let promiseValue;
|
|
10
|
+
return {
|
|
11
|
+
getValue: locale => {
|
|
12
|
+
// @ts-expect-error Missing webpack types
|
|
13
|
+
if (!__webpack_modules__[moduleId]) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// @ts-expect-error Missing webpack types
|
|
18
|
+
const m = __webpack_require__(moduleId);
|
|
19
|
+
return icuHandler.getParsedICUMessages(m, locale);
|
|
20
|
+
},
|
|
21
|
+
load: () => {
|
|
22
|
+
if (!promiseValue) {
|
|
23
|
+
promiseValue = loadImport();
|
|
24
|
+
}
|
|
25
|
+
return promiseValue;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(exports, 'createTranslationFile', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return translationFile.createTranslationFile; }
|
|
33
|
+
});
|
|
34
|
+
exports.createLanguage = createLanguage;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getParsedICUMessages } from '@vocab/core/icu-handler';
|
|
2
|
+
export { createTranslationFile } from '@vocab/core/translation-file';
|
|
3
|
+
|
|
4
|
+
const createLanguage = (moduleId, loadImport) => {
|
|
5
|
+
let promiseValue;
|
|
6
|
+
return {
|
|
7
|
+
getValue: locale => {
|
|
8
|
+
// @ts-expect-error Missing webpack types
|
|
9
|
+
if (!__webpack_modules__[moduleId]) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error Missing webpack types
|
|
14
|
+
const m = __webpack_require__(moduleId);
|
|
15
|
+
return getParsedICUMessages(m, locale);
|
|
16
|
+
},
|
|
17
|
+
load: () => {
|
|
18
|
+
if (!promiseValue) {
|
|
19
|
+
promiseValue = loadImport();
|
|
20
|
+
}
|
|
21
|
+
return promiseValue;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { createLanguage };
|
package/web/package.json
ADDED