@vocab/webpack 1.1.4 → 1.1.6

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.
@@ -1,8 +1,5 @@
1
- interface LoaderContext {
2
- addDependency: (filePath: string) => void;
3
- target: string;
4
- resourcePath: string;
5
- async: () => (err: unknown, result?: string) => void;
6
- }
1
+ import { UserConfig } from '@vocab/types';
2
+ import type { LoaderContext as WebpackLoaderContext } from 'webpack';
3
+ type LoaderContext = WebpackLoaderContext<UserConfig>;
7
4
  export default function vocabLoader(this: LoaderContext): Promise<void>;
8
5
  export {};
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var core = require('@vocab/core');
7
- var loaderUtils = require('loader-utils');
8
7
  var chunkName_dist_vocabWebpackChunkName = require('../../chunk-name/dist/vocab-webpack-chunk-name.cjs.dev.js');
9
8
  var logger = require('../../dist/logger-65149486.cjs.dev.js');
10
9
  require('chalk');
@@ -15,9 +14,9 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e };
15
14
  var path__default = /*#__PURE__*/_interopDefault(path);
16
15
 
17
16
  const trace = logger.trace.extend('loader');
17
+
18
18
  // Resolve virtual-resource-loader dependency from current package
19
19
  const virtualResourceLoader = require.resolve('virtual-resource-loader');
20
- const encodeWithinSingleQuotes = v => v.replace(/'/g, "\\'");
21
20
  function createIdentifier(lang, resourcePath, loadedTranslation) {
22
21
  var _loadedTranslation$la;
23
22
  trace('Creating identifier for language', lang);
@@ -31,24 +30,34 @@ function createIdentifier(lang, resourcePath, loadedTranslation) {
31
30
  const fileIdent = path__default["default"].basename(resourcePath, 'translations.json');
32
31
  return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
33
32
  }
34
- const renderLanguageLoaderAsync = (resourcePath, loadedTranslation) => lang => {
35
- const identifier = createIdentifier(lang, resourcePath, loadedTranslation);
36
- return `'${encodeWithinSingleQuotes(lang)}': createLanguage(require.resolveWeak('${identifier}'), () => import(
37
- /* webpackChunkName: "${chunkName_dist_vocabWebpackChunkName.getChunkName(lang)}" */
38
- '${identifier}'
39
- ))`;
40
- };
33
+
34
+ // reimplement `stringifyRequest` from loader-utils 2.x
35
+ // https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
36
+ function stringifyRequest(request) {
37
+ return JSON.stringify(this.utils.contextify(this.context, request));
38
+ }
39
+ function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
40
+ return lang => {
41
+ const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
42
+ return (/* ts */`
43
+ createLanguage(
44
+ require.resolveWeak(${identifier}),
45
+ () => import(
46
+ /* webpackChunkName: ${JSON.stringify(chunkName_dist_vocabWebpackChunkName.getChunkName(lang))} */
47
+ ${identifier}
48
+ )
49
+ )
50
+ `.trim()
51
+ );
52
+ };
53
+ }
41
54
  async function vocabLoader() {
42
55
  trace(`Using vocab loader for ${this.resourcePath}`);
43
56
  const callback = this.async();
44
57
  if (!callback) {
45
58
  throw new Error(`Webpack didn't provide an async callback`);
46
59
  }
47
-
48
- // @ts-expect-error We define our own loader context type, getOptions expects
49
- // webpack's LoaderContext type, but it's missing the target field
50
- // https://github.com/webpack/webpack/issues/16753
51
- const config = loaderUtils.getOptions(this);
60
+ const config = this.getOptions();
52
61
  const devJsonFilePath = core.getDevLanguageFileFromTsFile(this.resourcePath);
53
62
  const loadedTranslation = core.loadTranslation({
54
63
  filePath: devJsonFilePath,
@@ -60,15 +69,15 @@ async function vocabLoader() {
60
69
  callback(new Error('Called Vocab Loader with non-web target'));
61
70
  return;
62
71
  }
63
- const renderLanguageLoader = renderLanguageLoaderAsync(devJsonFilePath, loadedTranslation);
72
+ const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
64
73
  const loadedLanguages = Object.keys(loadedTranslation.languages);
65
- const result = `
66
- import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
74
+ const result = /* ts */`
75
+ import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
67
76
 
68
- export default createTranslationFile({
69
- ${loadedLanguages.map(lang => renderLanguageLoader(lang)).join(',')}
70
- });
71
- `;
77
+ export default createTranslationFile({
78
+ ${loadedLanguages.map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
79
+ });
80
+ `;
72
81
  trace('Created translation file', result);
73
82
  callback(null, result);
74
83
  }
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var core = require('@vocab/core');
7
- var loaderUtils = require('loader-utils');
8
7
  var chunkName_dist_vocabWebpackChunkName = require('../../chunk-name/dist/vocab-webpack-chunk-name.cjs.prod.js');
9
8
  var logger = require('../../dist/logger-f79e34a8.cjs.prod.js');
10
9
  require('chalk');
@@ -15,9 +14,9 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e };
15
14
  var path__default = /*#__PURE__*/_interopDefault(path);
16
15
 
17
16
  const trace = logger.trace.extend('loader');
17
+
18
18
  // Resolve virtual-resource-loader dependency from current package
19
19
  const virtualResourceLoader = require.resolve('virtual-resource-loader');
20
- const encodeWithinSingleQuotes = v => v.replace(/'/g, "\\'");
21
20
  function createIdentifier(lang, resourcePath, loadedTranslation) {
22
21
  var _loadedTranslation$la;
23
22
  trace('Creating identifier for language', lang);
@@ -31,24 +30,34 @@ function createIdentifier(lang, resourcePath, loadedTranslation) {
31
30
  const fileIdent = path__default["default"].basename(resourcePath, 'translations.json');
32
31
  return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
33
32
  }
34
- const renderLanguageLoaderAsync = (resourcePath, loadedTranslation) => lang => {
35
- const identifier = createIdentifier(lang, resourcePath, loadedTranslation);
36
- return `'${encodeWithinSingleQuotes(lang)}': createLanguage(require.resolveWeak('${identifier}'), () => import(
37
- /* webpackChunkName: "${chunkName_dist_vocabWebpackChunkName.getChunkName(lang)}" */
38
- '${identifier}'
39
- ))`;
40
- };
33
+
34
+ // reimplement `stringifyRequest` from loader-utils 2.x
35
+ // https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
36
+ function stringifyRequest(request) {
37
+ return JSON.stringify(this.utils.contextify(this.context, request));
38
+ }
39
+ function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
40
+ return lang => {
41
+ const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
42
+ return (/* ts */`
43
+ createLanguage(
44
+ require.resolveWeak(${identifier}),
45
+ () => import(
46
+ /* webpackChunkName: ${JSON.stringify(chunkName_dist_vocabWebpackChunkName.getChunkName(lang))} */
47
+ ${identifier}
48
+ )
49
+ )
50
+ `.trim()
51
+ );
52
+ };
53
+ }
41
54
  async function vocabLoader() {
42
55
  trace(`Using vocab loader for ${this.resourcePath}`);
43
56
  const callback = this.async();
44
57
  if (!callback) {
45
58
  throw new Error(`Webpack didn't provide an async callback`);
46
59
  }
47
-
48
- // @ts-expect-error We define our own loader context type, getOptions expects
49
- // webpack's LoaderContext type, but it's missing the target field
50
- // https://github.com/webpack/webpack/issues/16753
51
- const config = loaderUtils.getOptions(this);
60
+ const config = this.getOptions();
52
61
  const devJsonFilePath = core.getDevLanguageFileFromTsFile(this.resourcePath);
53
62
  const loadedTranslation = core.loadTranslation({
54
63
  filePath: devJsonFilePath,
@@ -60,15 +69,15 @@ async function vocabLoader() {
60
69
  callback(new Error('Called Vocab Loader with non-web target'));
61
70
  return;
62
71
  }
63
- const renderLanguageLoader = renderLanguageLoaderAsync(devJsonFilePath, loadedTranslation);
72
+ const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
64
73
  const loadedLanguages = Object.keys(loadedTranslation.languages);
65
- const result = `
66
- import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
74
+ const result = /* ts */`
75
+ import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
67
76
 
68
- export default createTranslationFile({
69
- ${loadedLanguages.map(lang => renderLanguageLoader(lang)).join(',')}
70
- });
71
- `;
77
+ export default createTranslationFile({
78
+ ${loadedLanguages.map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
79
+ });
80
+ `;
72
81
  trace('Created translation file', result);
73
82
  callback(null, result);
74
83
  }
@@ -1,15 +1,14 @@
1
1
  import path from 'path';
2
2
  import { getDevLanguageFileFromTsFile, loadTranslation } from '@vocab/core';
3
- import { getOptions } from 'loader-utils';
4
3
  import { getChunkName } from '../../chunk-name/dist/vocab-webpack-chunk-name.esm.js';
5
4
  import { t as trace$1 } from '../../dist/logger-e1862ea1.esm.js';
6
5
  import 'chalk';
7
6
  import 'debug';
8
7
 
9
8
  const trace = trace$1.extend('loader');
9
+
10
10
  // Resolve virtual-resource-loader dependency from current package
11
11
  const virtualResourceLoader = require.resolve('virtual-resource-loader');
12
- const encodeWithinSingleQuotes = v => v.replace(/'/g, "\\'");
13
12
  function createIdentifier(lang, resourcePath, loadedTranslation) {
14
13
  var _loadedTranslation$la;
15
14
  trace('Creating identifier for language', lang);
@@ -23,24 +22,34 @@ function createIdentifier(lang, resourcePath, loadedTranslation) {
23
22
  const fileIdent = path.basename(resourcePath, 'translations.json');
24
23
  return `./${fileIdent}-${lang}-virtual.json!=!${unloader}!`;
25
24
  }
26
- const renderLanguageLoaderAsync = (resourcePath, loadedTranslation) => lang => {
27
- const identifier = createIdentifier(lang, resourcePath, loadedTranslation);
28
- return `'${encodeWithinSingleQuotes(lang)}': createLanguage(require.resolveWeak('${identifier}'), () => import(
29
- /* webpackChunkName: "${getChunkName(lang)}" */
30
- '${identifier}'
31
- ))`;
32
- };
25
+
26
+ // reimplement `stringifyRequest` from loader-utils 2.x
27
+ // https://github.com/webpack/loader-utils/blob/master/CHANGELOG.md#300-2021-10-20
28
+ function stringifyRequest(request) {
29
+ return JSON.stringify(this.utils.contextify(this.context, request));
30
+ }
31
+ function renderLanguageLoaderAsync(resourcePath, loadedTranslation) {
32
+ return lang => {
33
+ const identifier = stringifyRequest.call(this, createIdentifier(lang, resourcePath, loadedTranslation));
34
+ return (/* ts */`
35
+ createLanguage(
36
+ require.resolveWeak(${identifier}),
37
+ () => import(
38
+ /* webpackChunkName: ${JSON.stringify(getChunkName(lang))} */
39
+ ${identifier}
40
+ )
41
+ )
42
+ `.trim()
43
+ );
44
+ };
45
+ }
33
46
  async function vocabLoader() {
34
47
  trace(`Using vocab loader for ${this.resourcePath}`);
35
48
  const callback = this.async();
36
49
  if (!callback) {
37
50
  throw new Error(`Webpack didn't provide an async callback`);
38
51
  }
39
-
40
- // @ts-expect-error We define our own loader context type, getOptions expects
41
- // webpack's LoaderContext type, but it's missing the target field
42
- // https://github.com/webpack/webpack/issues/16753
43
- const config = getOptions(this);
52
+ const config = this.getOptions();
44
53
  const devJsonFilePath = getDevLanguageFileFromTsFile(this.resourcePath);
45
54
  const loadedTranslation = loadTranslation({
46
55
  filePath: devJsonFilePath,
@@ -52,15 +61,15 @@ async function vocabLoader() {
52
61
  callback(new Error('Called Vocab Loader with non-web target'));
53
62
  return;
54
63
  }
55
- const renderLanguageLoader = renderLanguageLoaderAsync(devJsonFilePath, loadedTranslation);
64
+ const renderLanguageLoader = renderLanguageLoaderAsync.call(this, devJsonFilePath, loadedTranslation);
56
65
  const loadedLanguages = Object.keys(loadedTranslation.languages);
57
- const result = `
58
- import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
66
+ const result = /* ts */`
67
+ import { createLanguage, createTranslationFile } from '@vocab/webpack/${target}';
59
68
 
60
- export default createTranslationFile({
61
- ${loadedLanguages.map(lang => renderLanguageLoader(lang)).join(',')}
62
- });
63
- `;
69
+ export default createTranslationFile({
70
+ ${loadedLanguages.map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
71
+ });
72
+ `;
64
73
  trace('Created translation file', result);
65
74
  callback(null, result);
66
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vocab/webpack",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "main": "dist/vocab-webpack.cjs.js",
5
5
  "module": "dist/vocab-webpack.esm.js",
6
6
  "exports": {
@@ -39,15 +39,17 @@
39
39
  "web"
40
40
  ],
41
41
  "dependencies": {
42
- "@vocab/core": "^1.2.4",
42
+ "@vocab/core": "^1.2.5",
43
43
  "@vocab/types": "^1.1.2",
44
44
  "chalk": "^4.1.0",
45
45
  "debug": "^4.3.1",
46
- "loader-utils": "^2.0.0",
47
46
  "virtual-resource-loader": "^1.0.1"
48
47
  },
49
48
  "devDependencies": {
50
49
  "@types/debug": "^4.1.5",
51
- "@types/loader-utils": "^2.0.1"
50
+ "webpack": "^5.37.0"
51
+ },
52
+ "peerDependencies": {
53
+ "webpack": "^5.37.0"
52
54
  }
53
55
  }