-
-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathwebpackLoader.ts
More file actions
85 lines (75 loc) · 2.8 KB
/
Copy pathwebpackLoader.ts
File metadata and controls
85 lines (75 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import path from "path"
import * as R from "ramda"
import { getConfig } from "@lingui/conf"
import {
createCompiledCatalog,
getCatalogs,
getCatalogForFile,
} from "@lingui/cli/api"
import loaderUtils from "loader-utils"
// Check if webpack 5
const isWebpack5 = parseInt(require("webpack").version) === 5;
// Check if JavascriptParser and JavascriptGenerator exists -> Webpack 4
let JavascriptParser
let JavascriptGenerator
try {
JavascriptParser = require("webpack/lib/Parser")
JavascriptGenerator = require("webpack/lib/JavascriptGenerator")
} catch (error) {
if (error.code !== "MODULE_NOT_FOUND") {
throw error
}
}
const requiredType = "javascript/auto";
export default function (source) {
const options = loaderUtils.getOptions(this) || {}
if (!isWebpack5 && JavascriptParser && JavascriptGenerator) {
// Webpack 4 uses json-loader automatically, which breaks this loader because it
// doesn't return JSON, but JS module. This is a temporary workaround before
// official API is added (https://github.com/webpack/webpack/issues/7057#issuecomment-381883220)
// See https://github.com/webpack/webpack/issues/7057
this._module.type = requiredType
this._module.parser = new JavascriptParser()
this._module.generator = new JavascriptGenerator()
}
const config = getConfig({
configPath: options.config,
cwd: path.dirname(this.resourcePath),
})
const EMPTY_EXT = /\.[0-9a-z]+$/.test(this.resourcePath)
const JS_EXT = /\.js+$/.test(this.resourcePath)
const catalogRelativePath = path.relative(config.rootDir, this.resourcePath)
if (!EMPTY_EXT || JS_EXT) {
const formats = {
minimal: ".json",
po: ".po",
lingui: ".json"
}
// we replace the .js, because webpack appends automatically the .js on imports without extension
throw new Error(`File extension is mandatory, for ex: import("@lingui/loader!./${catalogRelativePath.replace(".js", formats[config.format])}")`)
}
const { locale, catalog } = getCatalogForFile(
catalogRelativePath,
getCatalogs(config)
)
const catalogs = catalog.readAll()
const messages = R.mapObjIndexed(
(_, key) =>
catalog.getTranslation(catalogs, locale, key, {
fallbackLocales: config.fallbackLocales,
sourceLocale: config.sourceLocale,
}),
catalogs[locale]
)
// In production we don't want untranslated strings. It's better to use message
// keys as a last resort.
// In development, however, we want to catch missing strings with `missing` parameter
// of I18nProvider (React) or setupI18n (core) and therefore we need to get
// empty translations if missing.
const strict = process.env.NODE_ENV !== "production"
return createCompiledCatalog(locale, messages, {
strict,
namespace: config.compileNamespace,
pseudoLocale: config.pseudoLocale,
})
}