-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathivy-extract-loader.ts
94 lines (85 loc) · 2.99 KB
/
ivy-extract-loader.ts
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
86
87
88
89
90
91
92
93
94
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { MessageExtractor } from '@angular/localize/src/tools/src/extract/extraction';
import { getOptions } from 'loader-utils';
import * as nodePath from 'path';
interface LocalizeExtractLoaderOptions {
messageHandler: (messages: import('@angular/localize').ɵParsedMessage[]) => void;
}
export default function localizeExtractLoader(
this: import('webpack').loader.LoaderContext,
content: string,
// Source map types are broken in the webpack type definitions
// tslint:disable-next-line: no-any
map: any,
) {
const loaderContext = this;
// Casts are needed to workaround the loader-utils typings limited support for option values
const options = (getOptions(this) as unknown) as LocalizeExtractLoaderOptions | undefined;
// Setup a Webpack-based logger instance
const logger = {
// level 2 is warnings
level: 2,
debug(...args: string[]): void {
// tslint:disable-next-line: no-console
console.debug(...args);
},
info(...args: string[]): void {
loaderContext.emitWarning(args.join(''));
},
warn(...args: string[]): void {
loaderContext.emitWarning(args.join(''));
},
error(...args: string[]): void {
loaderContext.emitError(args.join(''));
},
};
let filename = loaderContext.resourcePath;
if (map?.file) {
// The extractor's internal sourcemap handling expects the filenames to match
filename = nodePath.join(loaderContext.context, map.file);
}
// Setup a virtual file system instance for the extractor
// * MessageExtractor itself uses readFile, relative and resolve
// * Internal SourceFileLoader (sourcemap support) uses dirname, exists, readFile, and resolve
const filesystem = {
readFile(path: string): string {
if (path === filename) {
return content;
} else if (path === filename + '.map') {
return typeof map === 'string' ? map : JSON.stringify(map);
} else {
throw new Error('Unknown file requested: ' + path);
}
},
relative(from: string, to: string): string {
return nodePath.relative(from, to);
},
resolve(...paths: string[]): string {
return nodePath.resolve(...paths);
},
exists(path: string): boolean {
return path === filename || path === filename + '.map';
},
dirname(path: string): string {
return nodePath.dirname(path);
},
};
// tslint:disable-next-line: no-any
const extractor = new MessageExtractor(filesystem as any, logger, {
// tslint:disable-next-line: no-any
basePath: this.rootContext as any,
useSourceMaps: !!map,
});
const messages = extractor.extractMessages(filename);
if (messages.length > 0) {
options?.messageHandler(messages);
}
// Pass through the original content now that messages have been extracted
this.callback(undefined, content, map);
}