UNPKG

5.38 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 var desc = Object.getOwnPropertyDescriptor(m, k);
12 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13 desc = { enumerable: true, get: function() { return m[k]; } };
14 }
15 Object.defineProperty(o, k2, desc);
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33const nodePath = __importStar(require("path"));
34const load_esm_1 = require("../../utils/load-esm");
35function localizeExtractLoader(content, map) {
36 // This loader is not cacheable due to how message extraction works.
37 // Extracted messages are not part of webpack pipeline and hence they cannot be retrieved from cache.
38 // TODO: We should investigate in the future on making this deterministic and more cacheable.
39 this.cacheable(false);
40 const options = this.getOptions();
41 const callback = this.async();
42 extract(this, content, map, options).then(() => {
43 // Pass through the original content now that messages have been extracted
44 callback(undefined, content, map);
45 }, (error) => {
46 callback(error);
47 });
48}
49exports.default = localizeExtractLoader;
50async function extract(loaderContext, content, map, options) {
51 // Try to load the `@angular/localize` message extractor.
52 // All the localize usages are setup to first try the ESM entry point then fallback to the deep imports.
53 // This provides interim compatibility while the framework is transitioned to bundled ESM packages.
54 let MessageExtractor;
55 try {
56 // Load ESM `@angular/localize/tools` using the TypeScript dynamic import workaround.
57 // Once TypeScript provides support for keeping the dynamic import this workaround can be
58 // changed to a direct dynamic import.
59 const localizeToolsModule = await (0, load_esm_1.loadEsmModule)('@angular/localize/tools');
60 MessageExtractor = localizeToolsModule.MessageExtractor;
61 }
62 catch {
63 throw new Error(`Unable to load message extractor. Please ensure '@angular/localize' is installed.`);
64 }
65 // Setup a Webpack-based logger instance
66 const logger = {
67 // level 2 is warnings
68 level: 2,
69 debug(...args) {
70 // eslint-disable-next-line no-console
71 console.debug(...args);
72 },
73 info(...args) {
74 loaderContext.emitWarning(new Error(args.join('')));
75 },
76 warn(...args) {
77 loaderContext.emitWarning(new Error(args.join('')));
78 },
79 error(...args) {
80 loaderContext.emitError(new Error(args.join('')));
81 },
82 };
83 let filename = loaderContext.resourcePath;
84 const mapObject = typeof map === 'string' ? JSON.parse(map) : map;
85 if (mapObject === null || mapObject === void 0 ? void 0 : mapObject.file) {
86 // The extractor's internal sourcemap handling expects the filenames to match
87 filename = nodePath.join(loaderContext.context, mapObject.file);
88 }
89 // Setup a virtual file system instance for the extractor
90 // * MessageExtractor itself uses readFile, relative and resolve
91 // * Internal SourceFileLoader (sourcemap support) uses dirname, exists, readFile, and resolve
92 const filesystem = {
93 readFile(path) {
94 if (path === filename) {
95 return content;
96 }
97 else if (path === filename + '.map') {
98 return typeof map === 'string' ? map : JSON.stringify(map);
99 }
100 else {
101 throw new Error('Unknown file requested: ' + path);
102 }
103 },
104 relative(from, to) {
105 return nodePath.relative(from, to);
106 },
107 resolve(...paths) {
108 return nodePath.resolve(...paths);
109 },
110 exists(path) {
111 return path === filename || path === filename + '.map';
112 },
113 dirname(path) {
114 return nodePath.dirname(path);
115 },
116 };
117 // eslint-disable-next-line @typescript-eslint/no-explicit-any
118 const extractor = new MessageExtractor(filesystem, logger, {
119 // eslint-disable-next-line @typescript-eslint/no-explicit-any
120 basePath: loaderContext.rootContext,
121 useSourceMaps: !!map,
122 });
123 const messages = extractor.extractMessages(filename);
124 if (messages.length > 0) {
125 options === null || options === void 0 ? void 0 : options.messageHandler(messages);
126 }
127}