@docusaurus/utils 2.0.0-beta.fc64c12e4 → 2.0.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 (113) hide show
  1. package/lib/constants.d.ts +73 -0
  2. package/lib/constants.d.ts.map +1 -0
  3. package/lib/constants.js +78 -0
  4. package/lib/constants.js.map +1 -0
  5. package/lib/dataFileUtils.d.ts +60 -0
  6. package/lib/dataFileUtils.d.ts.map +1 -0
  7. package/lib/dataFileUtils.js +91 -0
  8. package/lib/dataFileUtils.js.map +1 -0
  9. package/lib/emitUtils.d.ts +32 -0
  10. package/lib/emitUtils.d.ts.map +1 -0
  11. package/lib/emitUtils.js +80 -0
  12. package/lib/emitUtils.js.map +1 -0
  13. package/lib/gitUtils.d.ts +66 -0
  14. package/lib/gitUtils.d.ts.map +1 -0
  15. package/lib/gitUtils.js +63 -0
  16. package/lib/gitUtils.js.map +1 -0
  17. package/lib/globUtils.d.ts +29 -0
  18. package/lib/globUtils.d.ts.map +1 -0
  19. package/lib/globUtils.js +36 -12
  20. package/lib/globUtils.js.map +1 -0
  21. package/lib/hashUtils.d.ts +6 -4
  22. package/lib/hashUtils.d.ts.map +1 -0
  23. package/lib/hashUtils.js +13 -10
  24. package/lib/hashUtils.js.map +1 -0
  25. package/lib/i18nUtils.d.ts +53 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +70 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +15 -75
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/index.js +86 -395
  32. package/lib/index.js.map +1 -0
  33. package/lib/jsUtils.d.ts +28 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +57 -0
  36. package/lib/jsUtils.js.map +1 -0
  37. package/lib/markdownLinks.d.ts +49 -5
  38. package/lib/markdownLinks.d.ts.map +1 -0
  39. package/lib/markdownLinks.js +57 -13
  40. package/lib/markdownLinks.js.map +1 -0
  41. package/lib/markdownUtils.d.ts +112 -0
  42. package/lib/markdownUtils.d.ts.map +1 -0
  43. package/lib/markdownUtils.js +271 -0
  44. package/lib/markdownUtils.js.map +1 -0
  45. package/lib/pathUtils.d.ts +45 -1
  46. package/lib/pathUtils.d.ts.map +1 -0
  47. package/lib/pathUtils.js +92 -12
  48. package/lib/pathUtils.js.map +1 -0
  49. package/lib/shellUtils.d.ts +8 -0
  50. package/lib/shellUtils.d.ts.map +1 -0
  51. package/lib/shellUtils.js +21 -0
  52. package/lib/shellUtils.js.map +1 -0
  53. package/lib/slugger.d.ts +24 -0
  54. package/lib/slugger.d.ts.map +1 -0
  55. package/lib/slugger.js +23 -0
  56. package/lib/slugger.js.map +1 -0
  57. package/lib/tags.d.ts +59 -0
  58. package/lib/tags.d.ts.map +1 -0
  59. package/lib/tags.js +91 -0
  60. package/lib/tags.js.map +1 -0
  61. package/lib/urlUtils.d.ts +66 -0
  62. package/lib/urlUtils.d.ts.map +1 -0
  63. package/lib/urlUtils.js +207 -0
  64. package/lib/urlUtils.js.map +1 -0
  65. package/lib/webpackUtils.d.ts +35 -0
  66. package/lib/webpackUtils.d.ts.map +1 -0
  67. package/lib/webpackUtils.js +115 -0
  68. package/lib/webpackUtils.js.map +1 -0
  69. package/package.json +27 -12
  70. package/src/constants.ts +98 -0
  71. package/src/dataFileUtils.ts +122 -0
  72. package/src/deps.d.ts +10 -0
  73. package/src/emitUtils.ts +99 -0
  74. package/src/gitUtils.ts +146 -0
  75. package/src/globUtils.ts +37 -15
  76. package/src/hashUtils.ts +9 -8
  77. package/src/i18nUtils.ts +114 -0
  78. package/src/index.ts +91 -502
  79. package/src/jsUtils.ts +59 -0
  80. package/src/markdownLinks.ts +101 -30
  81. package/src/markdownUtils.ts +357 -0
  82. package/src/pathUtils.ts +93 -12
  83. package/src/shellUtils.ts +18 -0
  84. package/src/slugger.ts +36 -0
  85. package/src/tags.ts +130 -0
  86. package/src/urlUtils.ts +234 -0
  87. package/src/webpackUtils.ts +153 -0
  88. package/lib/.tsbuildinfo +0 -1
  89. package/lib/codeTranslationsUtils.d.ts +0 -11
  90. package/lib/codeTranslationsUtils.js +0 -50
  91. package/lib/escapePath.d.ts +0 -17
  92. package/lib/escapePath.js +0 -25
  93. package/lib/markdownParser.d.ts +0 -30
  94. package/lib/markdownParser.js +0 -140
  95. package/lib/posixPath.d.ts +0 -14
  96. package/lib/posixPath.js +0 -28
  97. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  98. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  99. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  100. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  101. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  102. package/src/__tests__/escapePath.test.ts +0 -25
  103. package/src/__tests__/globUtils.test.ts +0 -109
  104. package/src/__tests__/hashUtils.test.ts +0 -51
  105. package/src/__tests__/index.test.ts +0 -631
  106. package/src/__tests__/markdownParser.test.ts +0 -817
  107. package/src/__tests__/pathUtils.test.ts +0 -63
  108. package/src/__tests__/posixPath.test.ts +0 -25
  109. package/src/codeTranslationsUtils.ts +0 -56
  110. package/src/escapePath.ts +0 -23
  111. package/src/markdownParser.ts +0 -182
  112. package/src/posixPath.ts +0 -27
  113. package/tsconfig.json +0 -9
package/src/slugger.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import GithubSlugger from 'github-slugger';
9
+
10
+ // We create our own abstraction on top of the lib:
11
+ // - unify usage everywhere in the codebase
12
+ // - ability to add extra options
13
+ export type SluggerOptions = {
14
+ /** Keep the headings' casing, otherwise make all lowercase. */
15
+ maintainCase?: boolean;
16
+ };
17
+
18
+ export type Slugger = {
19
+ /**
20
+ * Takes a Markdown heading like "Josh Cena" and sluggifies it according to
21
+ * GitHub semantics (in this case `josh-cena`). Stateful, because if you try
22
+ * to sluggify "Josh Cena" again it would return `josh-cena-1`.
23
+ */
24
+ slug: (value: string, options?: SluggerOptions) => string;
25
+ };
26
+
27
+ /**
28
+ * A thin wrapper around github-slugger. This is a factory function that returns
29
+ * a stateful Slugger object.
30
+ */
31
+ export function createSlugger(): Slugger {
32
+ const githubSlugger = new GithubSlugger();
33
+ return {
34
+ slug: (value, options) => githubSlugger.slug(value, options?.maintainCase),
35
+ };
36
+ }
package/src/tags.ts ADDED
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import _ from 'lodash';
9
+ import {normalizeUrl} from './urlUtils';
10
+
11
+ /** What the user configures. */
12
+ export type Tag = {
13
+ label: string;
14
+ /** Permalink to this tag's page, without the `/tags/` base path. */
15
+ permalink: string;
16
+ };
17
+
18
+ /** What the tags list page should know about each tag. */
19
+ export type TagsListItem = Tag & {
20
+ /** Number of posts/docs with this tag. */
21
+ count: number;
22
+ };
23
+
24
+ /** What the tag's own page should know about the tag. */
25
+ export type TagModule = TagsListItem & {
26
+ /** The tags list page's permalink. */
27
+ allTagsPath: string;
28
+ };
29
+
30
+ export type FrontMatterTag = string | Tag;
31
+
32
+ function normalizeFrontMatterTag(
33
+ tagsPath: string,
34
+ frontMatterTag: FrontMatterTag,
35
+ ): Tag {
36
+ function toTagObject(tagString: string): Tag {
37
+ return {
38
+ label: tagString,
39
+ permalink: _.kebabCase(tagString),
40
+ };
41
+ }
42
+
43
+ // TODO maybe make ensure the permalink is valid url path?
44
+ function normalizeTagPermalink(permalink: string): string {
45
+ // Note: we always apply tagsPath on purpose. For versioned docs, v1/doc.md
46
+ // and v2/doc.md tags with custom permalinks don't lead to the same created
47
+ // page. tagsPath is different for each doc version
48
+ return normalizeUrl([tagsPath, permalink]);
49
+ }
50
+
51
+ const tag: Tag =
52
+ typeof frontMatterTag === 'string'
53
+ ? toTagObject(frontMatterTag)
54
+ : frontMatterTag;
55
+
56
+ return {
57
+ label: tag.label,
58
+ permalink: normalizeTagPermalink(tag.permalink),
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Takes tag objects as they are defined in front matter, and normalizes each
64
+ * into a standard tag object. The permalink is created by appending the
65
+ * sluggified label to `tagsPath`. Front matter tags already containing
66
+ * permalinks would still have `tagsPath` prepended.
67
+ *
68
+ * The result will always be unique by permalinks. The behavior with colliding
69
+ * permalinks is undetermined.
70
+ */
71
+ export function normalizeFrontMatterTags(
72
+ /** Base path to append the tag permalinks to. */
73
+ tagsPath: string,
74
+ /** Can be `undefined`, so that we can directly pipe in `frontMatter.tags`. */
75
+ frontMatterTags: FrontMatterTag[] | undefined = [],
76
+ ): Tag[] {
77
+ const tags = frontMatterTags.map((tag) =>
78
+ normalizeFrontMatterTag(tagsPath, tag),
79
+ );
80
+
81
+ return _.uniqBy(tags, (tag) => tag.permalink);
82
+ }
83
+
84
+ type TaggedItemGroup<Item> = {
85
+ tag: Tag;
86
+ items: Item[];
87
+ };
88
+
89
+ /**
90
+ * Permits to group docs/blog posts by tag (provided by front matter).
91
+ *
92
+ * @returns a map from tag permalink to the items and other relevant tag data.
93
+ * The record is indexed by permalink, because routes must be unique in the end.
94
+ * Labels may vary on 2 MD files but they are normalized. Docs with
95
+ * label='some label' and label='some-label' should end up in the same page.
96
+ */
97
+ export function groupTaggedItems<Item>(
98
+ items: readonly Item[],
99
+ /**
100
+ * A callback telling me how to get the tags list of the current item. Usually
101
+ * simply getting it from some metadata of the current item.
102
+ */
103
+ getItemTags: (item: Item) => readonly Tag[],
104
+ ): {[permalink: string]: TaggedItemGroup<Item>} {
105
+ const result: {[permalink: string]: TaggedItemGroup<Item>} = {};
106
+
107
+ items.forEach((item) => {
108
+ getItemTags(item).forEach((tag) => {
109
+ // Init missing tag groups
110
+ // TODO: it's not really clear what should be the behavior if 2 tags have
111
+ // the same permalink but the label is different for each
112
+ // For now, the first tag found wins
113
+ result[tag.permalink] ??= {
114
+ tag,
115
+ items: [],
116
+ };
117
+
118
+ // Add item to group
119
+ result[tag.permalink]!.items.push(item);
120
+ });
121
+ });
122
+
123
+ // If user add twice the same tag to a md doc (weird but possible),
124
+ // we don't want the item to appear twice in the list...
125
+ Object.values(result).forEach((group) => {
126
+ group.items = _.uniq(group.items);
127
+ });
128
+
129
+ return result;
130
+ }
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import resolvePathnameUnsafe from 'resolve-pathname';
9
+ import {removeSuffix} from './jsUtils';
10
+
11
+ /**
12
+ * Much like `path.join`, but much better. Takes an array of URL segments, and
13
+ * joins them into a reasonable URL.
14
+ *
15
+ * - `["file:", "/home", "/user/", "website"]` => `file:///home/user/website`
16
+ * - `["file://", "home", "/user/", "website"]` => `file://home/user/website` (relative!)
17
+ * - Remove trailing slash before parameters or hash.
18
+ * - Replace `?` in query parameters with `&`.
19
+ * - Dedupe forward slashes in the entire path, avoiding protocol slashes.
20
+ *
21
+ * @throws {TypeError} If any of the URL segment is not a string, this throws.
22
+ */
23
+ export function normalizeUrl(rawUrls: string[]): string {
24
+ const urls = [...rawUrls];
25
+ const resultArray = [];
26
+
27
+ let hasStartingSlash = false;
28
+ let hasEndingSlash = false;
29
+
30
+ const isNonEmptyArray = (arr: string[]): arr is [string, ...string[]] =>
31
+ arr.length > 0;
32
+
33
+ if (!isNonEmptyArray(urls)) {
34
+ return '';
35
+ }
36
+
37
+ // If the first part is a plain protocol, we combine it with the next part.
38
+ if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
39
+ const first = urls.shift()!;
40
+ if (first.startsWith('file:') && urls[0].startsWith('/')) {
41
+ // Force a double slash here, else we lose the information that the next
42
+ // segment is an absolute path
43
+ urls[0] = `${first}//${urls[0]}`;
44
+ } else {
45
+ urls[0] = first + urls[0];
46
+ }
47
+ }
48
+
49
+ // There must be two or three slashes in the file protocol,
50
+ // two slashes in anything else.
51
+ const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
52
+ urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
53
+
54
+ for (let i = 0; i < urls.length; i += 1) {
55
+ let component = urls[i];
56
+
57
+ if (typeof component !== 'string') {
58
+ throw new TypeError(`Url must be a string. Received ${typeof component}`);
59
+ }
60
+
61
+ if (component === '') {
62
+ if (i === urls.length - 1 && hasEndingSlash) {
63
+ resultArray.push('/');
64
+ }
65
+ continue;
66
+ }
67
+
68
+ if (component !== '/') {
69
+ if (i > 0) {
70
+ // Removing the starting slashes for each component but the first.
71
+ component = component.replace(
72
+ /^\/+/,
73
+ // Special case where the first element of rawUrls is empty
74
+ // ["", "/hello"] => /hello
75
+ component.startsWith('/') && !hasStartingSlash ? '/' : '',
76
+ );
77
+ }
78
+
79
+ hasEndingSlash = component.endsWith('/');
80
+ // Removing the ending slashes for each component but the last. For the
81
+ // last component we will combine multiple slashes to a single one.
82
+ component = component.replace(/\/+$/, i < urls.length - 1 ? '' : '/');
83
+ }
84
+
85
+ hasStartingSlash = true;
86
+ resultArray.push(component);
87
+ }
88
+
89
+ let str = resultArray.join('/');
90
+ // Each input component is now separated by a single slash except the possible
91
+ // first plain protocol part.
92
+
93
+ // Remove trailing slash before parameters or hash.
94
+ str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
95
+
96
+ // Replace ? in parameters with &.
97
+ const parts = str.split('?');
98
+ str = parts.shift()! + (parts.length > 0 ? '?' : '') + parts.join('&');
99
+
100
+ // Dedupe forward slashes in the entire path, avoiding protocol slashes.
101
+ str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
102
+
103
+ // Dedupe forward slashes at the beginning of the path.
104
+ str = str.replace(/^\/+/g, '/');
105
+
106
+ return str;
107
+ }
108
+
109
+ /**
110
+ * Takes a file's path, relative to its content folder, and computes its edit
111
+ * URL. If `editUrl` is `undefined`, this returns `undefined`, as is the case
112
+ * when the user doesn't want an edit URL in her config.
113
+ */
114
+ export function getEditUrl(
115
+ fileRelativePath: string,
116
+ editUrl?: string,
117
+ ): string | undefined {
118
+ return editUrl
119
+ ? // Don't use posixPath for this: we need to force a forward slash path
120
+ normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
121
+ : undefined;
122
+ }
123
+
124
+ /**
125
+ * Converts file path to a reasonable URL path, e.g. `'index.md'` -> `'/'`,
126
+ * `'foo/bar.js'` -> `'/foo/bar'`
127
+ */
128
+ export function fileToPath(file: string): string {
129
+ const indexRE = /(?<dirname>^|.*\/)index\.(?:mdx?|jsx?|tsx?)$/i;
130
+ const extRE = /\.(?:mdx?|jsx?|tsx?)$/;
131
+
132
+ if (indexRE.test(file)) {
133
+ return file.replace(indexRE, '/$1');
134
+ }
135
+ return `/${file.replace(extRE, '').replace(/\\/g, '/')}`;
136
+ }
137
+
138
+ /**
139
+ * Similar to `encodeURI`, but uses `encodeURIComponent` and assumes there's no
140
+ * query.
141
+ *
142
+ * `encodeURI("/question?/answer")` => `"/question?/answer#section"`;
143
+ * `encodePath("/question?/answer#section")` => `"/question%3F/answer%23foo"`
144
+ */
145
+ export function encodePath(userPath: string): string {
146
+ return userPath
147
+ .split('/')
148
+ .map((item) => encodeURIComponent(item))
149
+ .join('/');
150
+ }
151
+
152
+ /**
153
+ * Whether `str` is a valid pathname. It must be absolute, and not contain
154
+ * special characters.
155
+ */
156
+ export function isValidPathname(str: string): boolean {
157
+ if (!str.startsWith('/')) {
158
+ return false;
159
+ }
160
+ try {
161
+ const parsedPathname = new URL(str, 'https://domain.com').pathname;
162
+ return parsedPathname === str || parsedPathname === encodeURI(str);
163
+ } catch {
164
+ return false;
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Resolve pathnames and fail-fast if resolution fails. Uses standard URL
170
+ * semantics (provided by `resolve-pathname` which is used internally by React
171
+ * router)
172
+ */
173
+ export function resolvePathname(to: string, from?: string): string {
174
+ return resolvePathnameUnsafe(to, from);
175
+ }
176
+ /** Appends a leading slash to `str`, if one doesn't exist. */
177
+ export function addLeadingSlash(str: string): string {
178
+ return str.startsWith('/') ? str : `/${str}`;
179
+ }
180
+
181
+ // TODO deduplicate: also present in @docusaurus/utils-common
182
+ /** Appends a trailing slash to `str`, if one doesn't exist. */
183
+ export function addTrailingSlash(str: string): string {
184
+ return str.endsWith('/') ? str : `${str}/`;
185
+ }
186
+
187
+ /** Removes the trailing slash from `str`. */
188
+ export function removeTrailingSlash(str: string): string {
189
+ return removeSuffix(str, '/');
190
+ }
191
+
192
+ /** Constructs an SSH URL that can be used to push to GitHub. */
193
+ export function buildSshUrl(
194
+ githubHost: string,
195
+ organizationName: string,
196
+ projectName: string,
197
+ githubPort?: string,
198
+ ): string {
199
+ if (githubPort) {
200
+ return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
201
+ }
202
+ return `git@${githubHost}:${organizationName}/${projectName}.git`;
203
+ }
204
+
205
+ /** Constructs an HTTP URL that can be used to push to GitHub. */
206
+ export function buildHttpsUrl(
207
+ gitCredentials: string,
208
+ githubHost: string,
209
+ organizationName: string,
210
+ projectName: string,
211
+ githubPort?: string,
212
+ ): string {
213
+ if (githubPort) {
214
+ return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
215
+ }
216
+ return `https://${gitCredentials}@${githubHost}/${organizationName}/${projectName}.git`;
217
+ }
218
+
219
+ /**
220
+ * Whether the current URL is an SSH protocol. In addition to looking for
221
+ * `ssh:`, it will also allow protocol-less URLs like
222
+ * `git@github.com:facebook/docusaurus.git`.
223
+ */
224
+ export function hasSSHProtocol(sourceRepoUrl: string): boolean {
225
+ try {
226
+ if (new URL(sourceRepoUrl).protocol === 'ssh:') {
227
+ return true;
228
+ }
229
+ return false;
230
+ } catch {
231
+ // Fails when there isn't a protocol
232
+ return /^(?:[\w-]+@)?[\w.-]+:[\w./-]+/.test(sourceRepoUrl);
233
+ }
234
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import path from 'path';
9
+ import {escapePath} from './pathUtils';
10
+ import {
11
+ WEBPACK_URL_LOADER_LIMIT,
12
+ OUTPUT_STATIC_ASSETS_DIR_NAME,
13
+ } from './constants';
14
+ import type {RuleSetRule} from 'webpack';
15
+
16
+ type AssetFolder = 'images' | 'files' | 'fonts' | 'medias';
17
+
18
+ type FileLoaderUtils = {
19
+ loaders: {
20
+ file: (options: {folder: AssetFolder}) => RuleSetRule;
21
+ url: (options: {folder: AssetFolder}) => RuleSetRule;
22
+ inlineMarkdownImageFileLoader: string;
23
+ inlineMarkdownLinkFileLoader: string;
24
+ };
25
+ rules: {
26
+ images: () => RuleSetRule;
27
+ fonts: () => RuleSetRule;
28
+ media: () => RuleSetRule;
29
+ svg: () => RuleSetRule;
30
+ otherAssets: () => RuleSetRule;
31
+ };
32
+ };
33
+
34
+ /**
35
+ * Returns unified loader configurations to be used for various file types.
36
+ *
37
+ * Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
38
+ */
39
+ export function getFileLoaderUtils(): FileLoaderUtils {
40
+ // Files/images < urlLoaderLimit will be inlined as base64 strings directly in
41
+ // the html
42
+ const urlLoaderLimit = WEBPACK_URL_LOADER_LIMIT;
43
+
44
+ const fileLoaderFileName = (folder: AssetFolder) =>
45
+ path.posix.join(
46
+ OUTPUT_STATIC_ASSETS_DIR_NAME,
47
+ folder,
48
+ '[name]-[contenthash].[ext]',
49
+ );
50
+
51
+ const loaders: FileLoaderUtils['loaders'] = {
52
+ file: (options: {folder: AssetFolder}) => ({
53
+ loader: require.resolve(`file-loader`),
54
+ options: {
55
+ name: fileLoaderFileName(options.folder),
56
+ },
57
+ }),
58
+ url: (options: {folder: AssetFolder}) => ({
59
+ loader: require.resolve('url-loader'),
60
+ options: {
61
+ limit: urlLoaderLimit,
62
+ name: fileLoaderFileName(options.folder),
63
+ fallback: require.resolve('file-loader'),
64
+ },
65
+ }),
66
+
67
+ // TODO avoid conflicts with the ideal-image plugin
68
+ // TODO this may require a little breaking change for ideal-image users?
69
+ // Maybe with the ideal image plugin, all md images should be "ideal"?
70
+ // This is used to force url-loader+file-loader on markdown images
71
+ // https://webpack.js.org/concepts/loaders/#inline
72
+ inlineMarkdownImageFileLoader: `!${escapePath(
73
+ require.resolve('url-loader'),
74
+ )}?limit=${urlLoaderLimit}&name=${fileLoaderFileName(
75
+ 'images',
76
+ )}&fallback=${escapePath(require.resolve('file-loader'))}!`,
77
+ inlineMarkdownLinkFileLoader: `!${escapePath(
78
+ require.resolve('file-loader'),
79
+ )}?name=${fileLoaderFileName('files')}!`,
80
+ };
81
+
82
+ const rules: FileLoaderUtils['rules'] = {
83
+ /**
84
+ * Loads image assets, inlines images via a data URI if they are below
85
+ * the size threshold
86
+ */
87
+ images: () => ({
88
+ use: [loaders.url({folder: 'images'})],
89
+ test: /\.(?:ico|jpe?g|png|gif|webp)(?:\?.*)?$/i,
90
+ }),
91
+
92
+ fonts: () => ({
93
+ use: [loaders.url({folder: 'fonts'})],
94
+ test: /\.(?:woff2?|eot|ttf|otf)$/i,
95
+ }),
96
+
97
+ /**
98
+ * Loads audio and video and inlines them via a data URI if they are below
99
+ * the size threshold
100
+ */
101
+ media: () => ({
102
+ use: [loaders.url({folder: 'medias'})],
103
+ test: /\.(?:mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/i,
104
+ }),
105
+
106
+ svg: () => ({
107
+ test: /\.svg$/i,
108
+ oneOf: [
109
+ {
110
+ use: [
111
+ {
112
+ loader: require.resolve('@svgr/webpack'),
113
+ options: {
114
+ prettier: false,
115
+ svgo: true,
116
+ svgoConfig: {
117
+ plugins: [
118
+ {
119
+ name: 'preset-default',
120
+ params: {
121
+ overrides: {
122
+ removeTitle: false,
123
+ removeViewBox: false,
124
+ },
125
+ },
126
+ },
127
+ ],
128
+ },
129
+ titleProp: true,
130
+ ref: ![path],
131
+ },
132
+ },
133
+ ],
134
+ // We don't want to use SVGR loader for non-React source code
135
+ // ie we don't want to use SVGR for CSS files...
136
+ issuer: {
137
+ and: [/\.(?:tsx?|jsx?|mdx?)$/i],
138
+ },
139
+ },
140
+ {
141
+ use: [loaders.url({folder: 'images'})],
142
+ },
143
+ ],
144
+ }),
145
+
146
+ otherAssets: () => ({
147
+ use: [loaders.file({folder: 'files'})],
148
+ test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
149
+ }),
150
+ };
151
+
152
+ return {loaders, rules};
153
+ }