|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
| 5 | +import {__NGTOOLS_PRIVATE_API_2} from '@angular/compiler-cli'; |
| 6 | + |
| 7 | +import {Tapable} from './webpack'; |
| 8 | +import {WebpackResourceLoader} from './resource_loader'; |
| 9 | + |
| 10 | +export interface ExtractI18nPluginOptions { |
| 11 | + tsConfigPath: string; |
| 12 | + basePath?: string; |
| 13 | + genDir?: string; |
| 14 | + i18nFormat?: string; |
| 15 | + exclude?: string[]; |
| 16 | +} |
| 17 | + |
| 18 | +export class ExtractI18nPlugin implements Tapable { |
| 19 | + private _resourceLoader: WebpackResourceLoader; |
| 20 | + |
| 21 | + private _donePromise: Promise<void>; |
| 22 | + private _compiler: any = null; |
| 23 | + private _compilation: any = null; |
| 24 | + |
| 25 | + private _tsConfigPath: string; |
| 26 | + private _basePath: string; |
| 27 | + private _genDir: string; |
| 28 | + private _rootFilePath: string[]; |
| 29 | + private _compilerOptions: any = null; |
| 30 | + private _angularCompilerOptions: any = null; |
| 31 | + // private _compilerHost: WebpackCompilerHost; |
| 32 | + private _compilerHost: ts.CompilerHost; |
| 33 | + private _program: ts.Program; |
| 34 | + |
| 35 | + private _i18nFormat: string; |
| 36 | + |
| 37 | + constructor(options: ExtractI18nPluginOptions) { |
| 38 | + this._setupOptions(options); |
| 39 | + } |
| 40 | + |
| 41 | + private _setupOptions(options: ExtractI18nPluginOptions) { |
| 42 | + if (!options.hasOwnProperty('tsConfigPath')) { |
| 43 | + throw new Error('Must specify "tsConfigPath" in the configuration of @ngtools/webpack.'); |
| 44 | + } |
| 45 | + this._tsConfigPath = options.tsConfigPath; |
| 46 | + |
| 47 | + // Check the base path. |
| 48 | + const maybeBasePath = path.resolve(process.cwd(), this._tsConfigPath); |
| 49 | + let basePath = maybeBasePath; |
| 50 | + if (fs.statSync(maybeBasePath).isFile()) { |
| 51 | + basePath = path.dirname(basePath); |
| 52 | + } |
| 53 | + if (options.hasOwnProperty('basePath')) { |
| 54 | + basePath = path.resolve(process.cwd(), options.basePath); |
| 55 | + } |
| 56 | + |
| 57 | + let tsConfigJson: any = null; |
| 58 | + try { |
| 59 | + tsConfigJson = JSON.parse(fs.readFileSync(this._tsConfigPath, 'utf8')); |
| 60 | + } catch (err) { |
| 61 | + throw new Error(`An error happened while parsing ${this._tsConfigPath} JSON: ${err}.`); |
| 62 | + } |
| 63 | + const tsConfig = ts.parseJsonConfigFileContent( |
| 64 | + tsConfigJson, ts.sys, basePath, null, this._tsConfigPath); |
| 65 | + |
| 66 | + let fileNames = tsConfig.fileNames; |
| 67 | + if (options.hasOwnProperty('exclude')) { |
| 68 | + let exclude: string[] = typeof options.exclude == 'string' |
| 69 | + ? [options.exclude as string] : (options.exclude as string[]); |
| 70 | + |
| 71 | + exclude.forEach((pattern: string) => { |
| 72 | + const basePathPattern = '(' + basePath.replace(/\\/g, '/') |
| 73 | + .replace(/[\-\[\]\/{}()+?.\\^$|*]/g, '\\$&') + ')?'; |
| 74 | + pattern = pattern |
| 75 | + // Replace windows path separators with forward slashes. |
| 76 | + .replace(/\\/g, '/') |
| 77 | + // Escape characters that are used normally in regexes, except stars. |
| 78 | + .replace(/[\-\[\]{}()+?.\\^$|]/g, '\\$&') |
| 79 | + // Two stars replacement. |
| 80 | + .replace(/\*\*/g, '(?:.*)') |
| 81 | + // One star replacement. |
| 82 | + .replace(/\*/g, '(?:[^/]*)') |
| 83 | + // Escape characters from the basePath and make sure it's forward slashes. |
| 84 | + .replace(/^/, basePathPattern); |
| 85 | + |
| 86 | + const re = new RegExp('^' + pattern + '$'); |
| 87 | + fileNames = fileNames.filter(x => !x.replace(/\\/g, '/').match(re)); |
| 88 | + }); |
| 89 | + } else { |
| 90 | + fileNames = fileNames.filter(fileName => !/\.spec\.ts$/.test(fileName)); |
| 91 | + } |
| 92 | + this._rootFilePath = fileNames; |
| 93 | + |
| 94 | + // By default messages will be generated in basePath |
| 95 | + let genDir = basePath; |
| 96 | + |
| 97 | + if (options.hasOwnProperty('genDir')) { |
| 98 | + genDir = path.resolve(process.cwd(), options.genDir); |
| 99 | + } |
| 100 | + |
| 101 | + this._compilerOptions = tsConfig.options; |
| 102 | + this._angularCompilerOptions = Object.assign( |
| 103 | + { genDir }, |
| 104 | + this._compilerOptions, |
| 105 | + tsConfig.raw['angularCompilerOptions'], |
| 106 | + { basePath } |
| 107 | + ); |
| 108 | + |
| 109 | + this._basePath = basePath; |
| 110 | + this._genDir = genDir; |
| 111 | + |
| 112 | + // this._compilerHost = new WebpackCompilerHost(this._compilerOptions, this._basePath); |
| 113 | + this._compilerHost = ts.createCompilerHost(this._compilerOptions, true); |
| 114 | + this._program = ts.createProgram( |
| 115 | + this._rootFilePath, this._compilerOptions, this._compilerHost); |
| 116 | + |
| 117 | + if (options.hasOwnProperty('i18nFormat')) { |
| 118 | + this._i18nFormat = options.i18nFormat; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + apply(compiler: any) { |
| 123 | + this._compiler = compiler; |
| 124 | + |
| 125 | + compiler.plugin('make', (compilation: any, cb: any) => this._make(compilation, cb)); |
| 126 | + |
| 127 | + compiler.plugin('after-emit', (compilation: any, cb: any) => { |
| 128 | + this._donePromise = null; |
| 129 | + this._compilation = null; |
| 130 | + compilation._ngToolsWebpackXi18nPluginInstance = null; |
| 131 | + cb(); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private _make(compilation: any, cb: (err?: any, request?: any) => void) { |
| 136 | + this._compilation = compilation; |
| 137 | + if (this._compilation._ngToolsWebpackXi18nPluginInstance) { |
| 138 | + return cb(new Error('An @ngtools/webpack xi18n plugin already exist for ' + |
| 139 | + 'this compilation.')); |
| 140 | + } |
| 141 | + if (!this._compilation._ngToolsWebpackPluginInstance) { |
| 142 | + return cb(new Error('An @ngtools/webpack aot plugin does not exists ' + |
| 143 | + 'for this compilation')); |
| 144 | + } |
| 145 | + |
| 146 | + this._compilation._ngToolsWebpackXi18nPluginInstance = this; |
| 147 | + |
| 148 | + this._resourceLoader = new WebpackResourceLoader(compilation); |
| 149 | + |
| 150 | + this._donePromise = Promise.resolve() |
| 151 | + .then(() => { |
| 152 | + return __NGTOOLS_PRIVATE_API_2.extractI18n({ |
| 153 | + basePath: this._basePath, |
| 154 | + compilerOptions: this._compilerOptions, |
| 155 | + program: this._program, |
| 156 | + host: this._compilerHost, |
| 157 | + angularCompilerOptions: this._angularCompilerOptions, |
| 158 | + i18nFormat: this._i18nFormat, |
| 159 | + |
| 160 | + readResource: (path: string) => this._resourceLoader.get(path) |
| 161 | + }); |
| 162 | + }) |
| 163 | + .then(() => cb(), (err: any) => { |
| 164 | + compilation.errors.push(err); |
| 165 | + cb(); |
| 166 | + }); |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments