Compiler.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const parseJson = require("json-parse-better-errors");
  7. const asyncLib = require("neo-async");
  8. const path = require("path");
  9. const { Source } = require("webpack-sources");
  10. const util = require("util");
  11. const {
  12. Tapable,
  13. SyncHook,
  14. SyncBailHook,
  15. AsyncParallelHook,
  16. AsyncSeriesHook
  17. } = require("tapable");
  18. const Compilation = require("./Compilation");
  19. const Stats = require("./Stats");
  20. const Watching = require("./Watching");
  21. const NormalModuleFactory = require("./NormalModuleFactory");
  22. const ContextModuleFactory = require("./ContextModuleFactory");
  23. const ResolverFactory = require("./ResolverFactory");
  24. const RequestShortener = require("./RequestShortener");
  25. const { makePathsRelative } = require("./util/identifier");
  26. const ConcurrentCompilationError = require("./ConcurrentCompilationError");
  27. const { Logger } = require("./logging/Logger");
  28. /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
  29. /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
  30. /**
  31. * @typedef {Object} CompilationParams
  32. * @property {NormalModuleFactory} normalModuleFactory
  33. * @property {ContextModuleFactory} contextModuleFactory
  34. * @property {Set<string>} compilationDependencies
  35. */
  36. class Compiler extends Tapable {
  37. constructor(context) {
  38. super();
  39. this.hooks = {
  40. /** @type {SyncBailHook<Compilation>} */
  41. shouldEmit: new SyncBailHook(["compilation"]),
  42. /** @type {AsyncSeriesHook<Stats>} */
  43. done: new AsyncSeriesHook(["stats"]),
  44. /** @type {AsyncSeriesHook<>} */
  45. additionalPass: new AsyncSeriesHook([]),
  46. /** @type {AsyncSeriesHook<Compiler>} */
  47. beforeRun: new AsyncSeriesHook(["compiler"]),
  48. /** @type {AsyncSeriesHook<Compiler>} */
  49. run: new AsyncSeriesHook(["compiler"]),
  50. /** @type {AsyncSeriesHook<Compilation>} */
  51. emit: new AsyncSeriesHook(["compilation"]),
  52. /** @type {AsyncSeriesHook<string, Buffer>} */
  53. assetEmitted: new AsyncSeriesHook(["file", "content"]),
  54. /** @type {AsyncSeriesHook<Compilation>} */
  55. afterEmit: new AsyncSeriesHook(["compilation"]),
  56. /** @type {SyncHook<Compilation, CompilationParams>} */
  57. thisCompilation: new SyncHook(["compilation", "params"]),
  58. /** @type {SyncHook<Compilation, CompilationParams>} */
  59. compilation: new SyncHook(["compilation", "params"]),
  60. /** @type {SyncHook<NormalModuleFactory>} */
  61. normalModuleFactory: new SyncHook(["normalModuleFactory"]),
  62. /** @type {SyncHook<ContextModuleFactory>} */
  63. contextModuleFactory: new SyncHook(["contextModulefactory"]),
  64. /** @type {AsyncSeriesHook<CompilationParams>} */
  65. beforeCompile: new AsyncSeriesHook(["params"]),
  66. /** @type {SyncHook<CompilationParams>} */
  67. compile: new SyncHook(["params"]),
  68. /** @type {AsyncParallelHook<Compilation>} */
  69. make: new AsyncParallelHook(["compilation"]),
  70. /** @type {AsyncSeriesHook<Compilation>} */
  71. afterCompile: new AsyncSeriesHook(["compilation"]),
  72. /** @type {AsyncSeriesHook<Compiler>} */
  73. watchRun: new AsyncSeriesHook(["compiler"]),
  74. /** @type {SyncHook<Error>} */
  75. failed: new SyncHook(["error"]),
  76. /** @type {SyncHook<string, string>} */
  77. invalid: new SyncHook(["filename", "changeTime"]),
  78. /** @type {SyncHook} */
  79. watchClose: new SyncHook([]),
  80. /** @type {SyncBailHook<string, string, any[]>} */
  81. infrastructureLog: new SyncBailHook(["origin", "type", "args"]),
  82. // TODO the following hooks are weirdly located here
  83. // TODO move them for webpack 5
  84. /** @type {SyncHook} */
  85. environment: new SyncHook([]),
  86. /** @type {SyncHook} */
  87. afterEnvironment: new SyncHook([]),
  88. /** @type {SyncHook<Compiler>} */
  89. afterPlugins: new SyncHook(["compiler"]),
  90. /** @type {SyncHook<Compiler>} */
  91. afterResolvers: new SyncHook(["compiler"]),
  92. /** @type {SyncBailHook<string, Entry>} */
  93. entryOption: new SyncBailHook(["context", "entry"])
  94. };
  95. // TODO webpack 5 remove this
  96. this.hooks.infrastructurelog = this.hooks.infrastructureLog;
  97. this._pluginCompat.tap("Compiler", options => {
  98. switch (options.name) {
  99. case "additional-pass":
  100. case "before-run":
  101. case "run":
  102. case "emit":
  103. case "after-emit":
  104. case "before-compile":
  105. case "make":
  106. case "after-compile":
  107. case "watch-run":
  108. options.async = true;
  109. break;
  110. }
  111. });
  112. /** @type {string=} */
  113. this.name = undefined;
  114. /** @type {Compilation=} */
  115. this.parentCompilation = undefined;
  116. /** @type {string} */
  117. this.outputPath = "";
  118. this.outputFileSystem = null;
  119. this.inputFileSystem = null;
  120. /** @type {string|null} */
  121. this.recordsInputPath = null;
  122. /** @type {string|null} */
  123. this.recordsOutputPath = null;
  124. this.records = {};
  125. this.removedFiles = new Set();
  126. /** @type {Map<string, number>} */
  127. this.fileTimestamps = new Map();
  128. /** @type {Map<string, number>} */
  129. this.contextTimestamps = new Map();
  130. /** @type {ResolverFactory} */
  131. this.resolverFactory = new ResolverFactory();
  132. this.infrastructureLogger = undefined;
  133. // TODO remove in webpack 5
  134. this.resolvers = {
  135. normal: {
  136. plugins: util.deprecate((hook, fn) => {
  137. this.resolverFactory.plugin("resolver normal", resolver => {
  138. resolver.plugin(hook, fn);
  139. });
  140. }, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  141. apply: util.deprecate((...args) => {
  142. this.resolverFactory.plugin("resolver normal", resolver => {
  143. resolver.apply(...args);
  144. });
  145. }, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  146. },
  147. loader: {
  148. plugins: util.deprecate((hook, fn) => {
  149. this.resolverFactory.plugin("resolver loader", resolver => {
  150. resolver.plugin(hook, fn);
  151. });
  152. }, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  153. apply: util.deprecate((...args) => {
  154. this.resolverFactory.plugin("resolver loader", resolver => {
  155. resolver.apply(...args);
  156. });
  157. }, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  158. },
  159. context: {
  160. plugins: util.deprecate((hook, fn) => {
  161. this.resolverFactory.plugin("resolver context", resolver => {
  162. resolver.plugin(hook, fn);
  163. });
  164. }, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  165. apply: util.deprecate((...args) => {
  166. this.resolverFactory.plugin("resolver context", resolver => {
  167. resolver.apply(...args);
  168. });
  169. }, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  170. }
  171. };
  172. /** @type {WebpackOptions} */
  173. this.options = /** @type {WebpackOptions} */ ({});
  174. this.context = context;
  175. this.requestShortener = new RequestShortener(context);
  176. /** @type {boolean} */
  177. this.running = false;
  178. /** @type {boolean} */
  179. this.watchMode = false;
  180. /** @private @type {WeakMap<Source, { sizeOnlySource: SizeOnlySource, writtenTo: Map<string, number> }>} */
  181. this._assetEmittingSourceCache = new WeakMap();
  182. /** @private @type {Map<string, number>} */
  183. this._assetEmittingWrittenFiles = new Map();
  184. }
  185. /**
  186. * @param {string | (function(): string)} name name of the logger, or function called once to get the logger name
  187. * @returns {Logger} a logger with that name
  188. */
  189. getInfrastructureLogger(name) {
  190. if (!name) {
  191. throw new TypeError(
  192. "Compiler.getInfrastructureLogger(name) called without a name"
  193. );
  194. }
  195. return new Logger((type, args) => {
  196. if (typeof name === "function") {
  197. name = name();
  198. if (!name) {
  199. throw new TypeError(
  200. "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
  201. );
  202. }
  203. }
  204. if (this.hooks.infrastructureLog.call(name, type, args) === undefined) {
  205. if (this.infrastructureLogger !== undefined) {
  206. this.infrastructureLogger(name, type, args);
  207. }
  208. }
  209. });
  210. }
  211. watch(watchOptions, handler) {
  212. if (this.running) return handler(new ConcurrentCompilationError());
  213. this.running = true;
  214. this.watchMode = true;
  215. this.fileTimestamps = new Map();
  216. this.contextTimestamps = new Map();
  217. this.removedFiles = new Set();
  218. return new Watching(this, watchOptions, handler);
  219. }
  220. run(callback) {
  221. if (this.running) return callback(new ConcurrentCompilationError());
  222. const finalCallback = (err, stats) => {
  223. this.running = false;
  224. if (err) {
  225. this.hooks.failed.call(err);
  226. }
  227. if (callback !== undefined) return callback(err, stats);
  228. };
  229. const startTime = Date.now();
  230. this.running = true;
  231. const onCompiled = (err, compilation) => {
  232. if (err) return finalCallback(err);
  233. if (this.hooks.shouldEmit.call(compilation) === false) {
  234. const stats = new Stats(compilation);
  235. stats.startTime = startTime;
  236. stats.endTime = Date.now();
  237. this.hooks.done.callAsync(stats, err => {
  238. if (err) return finalCallback(err);
  239. return finalCallback(null, stats);
  240. });
  241. return;
  242. }
  243. this.emitAssets(compilation, err => {
  244. if (err) return finalCallback(err);
  245. if (compilation.hooks.needAdditionalPass.call()) {
  246. compilation.needAdditionalPass = true;
  247. const stats = new Stats(compilation);
  248. stats.startTime = startTime;
  249. stats.endTime = Date.now();
  250. this.hooks.done.callAsync(stats, err => {
  251. if (err) return finalCallback(err);
  252. this.hooks.additionalPass.callAsync(err => {
  253. if (err) return finalCallback(err);
  254. this.compile(onCompiled);
  255. });
  256. });
  257. return;
  258. }
  259. this.emitRecords(err => {
  260. if (err) return finalCallback(err);
  261. const stats = new Stats(compilation);
  262. stats.startTime = startTime;
  263. stats.endTime = Date.now();
  264. this.hooks.done.callAsync(stats, err => {
  265. if (err) return finalCallback(err);
  266. return finalCallback(null, stats);
  267. });
  268. });
  269. });
  270. };
  271. this.hooks.beforeRun.callAsync(this, err => {
  272. if (err) return finalCallback(err);
  273. this.hooks.run.callAsync(this, err => {
  274. if (err) return finalCallback(err);
  275. this.readRecords(err => {
  276. if (err) return finalCallback(err);
  277. this.compile(onCompiled);
  278. });
  279. });
  280. });
  281. }
  282. runAsChild(callback) {
  283. this.compile((err, compilation) => {
  284. if (err) return callback(err);
  285. this.parentCompilation.children.push(compilation);
  286. for (const name of Object.keys(compilation.assets)) {
  287. this.parentCompilation.assets[name] = compilation.assets[name];
  288. }
  289. const entries = Array.from(
  290. compilation.entrypoints.values(),
  291. ep => ep.chunks
  292. ).reduce((array, chunks) => {
  293. return array.concat(chunks);
  294. }, []);
  295. return callback(null, entries, compilation);
  296. });
  297. }
  298. purgeInputFileSystem() {
  299. if (this.inputFileSystem && this.inputFileSystem.purge) {
  300. this.inputFileSystem.purge();
  301. }
  302. }
  303. emitAssets(compilation, callback) {
  304. let outputPath;
  305. const emitFiles = err => {
  306. if (err) return callback(err);
  307. asyncLib.forEachLimit(
  308. compilation.assets,
  309. 15,
  310. (source, file, callback) => {
  311. let targetFile = file;
  312. const queryStringIdx = targetFile.indexOf("?");
  313. if (queryStringIdx >= 0) {
  314. targetFile = targetFile.substr(0, queryStringIdx);
  315. }
  316. const writeOut = err => {
  317. if (err) return callback(err);
  318. const targetPath = this.outputFileSystem.join(
  319. outputPath,
  320. targetFile
  321. );
  322. // TODO webpack 5 remove futureEmitAssets option and make it on by default
  323. if (this.options.output.futureEmitAssets) {
  324. // check if the target file has already been written by this Compiler
  325. const targetFileGeneration = this._assetEmittingWrittenFiles.get(
  326. targetPath
  327. );
  328. // create an cache entry for this Source if not already existing
  329. let cacheEntry = this._assetEmittingSourceCache.get(source);
  330. if (cacheEntry === undefined) {
  331. cacheEntry = {
  332. sizeOnlySource: undefined,
  333. writtenTo: new Map()
  334. };
  335. this._assetEmittingSourceCache.set(source, cacheEntry);
  336. }
  337. // if the target file has already been written
  338. if (targetFileGeneration !== undefined) {
  339. // check if the Source has been written to this target file
  340. const writtenGeneration = cacheEntry.writtenTo.get(targetPath);
  341. if (writtenGeneration === targetFileGeneration) {
  342. // if yes, we skip writing the file
  343. // as it's already there
  344. // (we assume one doesn't remove files while the Compiler is running)
  345. return callback();
  346. }
  347. }
  348. // get the binary (Buffer) content from the Source
  349. /** @type {Buffer} */
  350. let content;
  351. if (typeof source.buffer === "function") {
  352. content = source.buffer();
  353. } else {
  354. const bufferOrString = source.source();
  355. if (Buffer.isBuffer(bufferOrString)) {
  356. content = bufferOrString;
  357. } else {
  358. content = Buffer.from(bufferOrString, "utf8");
  359. }
  360. }
  361. // Create a replacement resource which only allows to ask for size
  362. // This allows to GC all memory allocated by the Source
  363. // (expect when the Source is stored in any other cache)
  364. cacheEntry.sizeOnlySource = new SizeOnlySource(content.length);
  365. compilation.assets[file] = cacheEntry.sizeOnlySource;
  366. // Write the file to output file system
  367. this.outputFileSystem.writeFile(targetPath, content, err => {
  368. if (err) return callback(err);
  369. // information marker that the asset has been emitted
  370. compilation.emittedAssets.add(file);
  371. // cache the information that the Source has been written to that location
  372. const newGeneration =
  373. targetFileGeneration === undefined
  374. ? 1
  375. : targetFileGeneration + 1;
  376. cacheEntry.writtenTo.set(targetPath, newGeneration);
  377. this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
  378. this.hooks.assetEmitted.callAsync(file, content, callback);
  379. });
  380. } else {
  381. if (source.existsAt === targetPath) {
  382. source.emitted = false;
  383. return callback();
  384. }
  385. let content = source.source();
  386. if (!Buffer.isBuffer(content)) {
  387. content = Buffer.from(content, "utf8");
  388. }
  389. source.existsAt = targetPath;
  390. source.emitted = true;
  391. this.outputFileSystem.writeFile(targetPath, content, err => {
  392. if (err) return callback(err);
  393. this.hooks.assetEmitted.callAsync(file, content, callback);
  394. });
  395. }
  396. };
  397. if (targetFile.match(/\/|\\/)) {
  398. const dir = path.dirname(targetFile);
  399. this.outputFileSystem.mkdirp(
  400. this.outputFileSystem.join(outputPath, dir),
  401. writeOut
  402. );
  403. } else {
  404. writeOut();
  405. }
  406. },
  407. err => {
  408. if (err) return callback(err);
  409. this.hooks.afterEmit.callAsync(compilation, err => {
  410. if (err) return callback(err);
  411. return callback();
  412. });
  413. }
  414. );
  415. };
  416. this.hooks.emit.callAsync(compilation, err => {
  417. if (err) return callback(err);
  418. outputPath = compilation.getPath(this.outputPath);
  419. this.outputFileSystem.mkdirp(outputPath, emitFiles);
  420. });
  421. }
  422. emitRecords(callback) {
  423. if (!this.recordsOutputPath) return callback();
  424. const idx1 = this.recordsOutputPath.lastIndexOf("/");
  425. const idx2 = this.recordsOutputPath.lastIndexOf("\\");
  426. let recordsOutputPathDirectory = null;
  427. if (idx1 > idx2) {
  428. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1);
  429. } else if (idx1 < idx2) {
  430. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2);
  431. }
  432. const writeFile = () => {
  433. this.outputFileSystem.writeFile(
  434. this.recordsOutputPath,
  435. JSON.stringify(this.records, undefined, 2),
  436. callback
  437. );
  438. };
  439. if (!recordsOutputPathDirectory) {
  440. return writeFile();
  441. }
  442. this.outputFileSystem.mkdirp(recordsOutputPathDirectory, err => {
  443. if (err) return callback(err);
  444. writeFile();
  445. });
  446. }
  447. readRecords(callback) {
  448. if (!this.recordsInputPath) {
  449. this.records = {};
  450. return callback();
  451. }
  452. this.inputFileSystem.stat(this.recordsInputPath, err => {
  453. // It doesn't exist
  454. // We can ignore this.
  455. if (err) return callback();
  456. this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => {
  457. if (err) return callback(err);
  458. try {
  459. this.records = parseJson(content.toString("utf-8"));
  460. } catch (e) {
  461. e.message = "Cannot parse records: " + e.message;
  462. return callback(e);
  463. }
  464. return callback();
  465. });
  466. });
  467. }
  468. createChildCompiler(
  469. compilation,
  470. compilerName,
  471. compilerIndex,
  472. outputOptions,
  473. plugins
  474. ) {
  475. const childCompiler = new Compiler(this.context);
  476. if (Array.isArray(plugins)) {
  477. for (const plugin of plugins) {
  478. plugin.apply(childCompiler);
  479. }
  480. }
  481. for (const name in this.hooks) {
  482. if (
  483. ![
  484. "make",
  485. "compile",
  486. "emit",
  487. "afterEmit",
  488. "invalid",
  489. "done",
  490. "thisCompilation"
  491. ].includes(name)
  492. ) {
  493. if (childCompiler.hooks[name]) {
  494. childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
  495. }
  496. }
  497. }
  498. childCompiler.name = compilerName;
  499. childCompiler.outputPath = this.outputPath;
  500. childCompiler.inputFileSystem = this.inputFileSystem;
  501. childCompiler.outputFileSystem = null;
  502. childCompiler.resolverFactory = this.resolverFactory;
  503. childCompiler.fileTimestamps = this.fileTimestamps;
  504. childCompiler.contextTimestamps = this.contextTimestamps;
  505. const relativeCompilerName = makePathsRelative(this.context, compilerName);
  506. if (!this.records[relativeCompilerName]) {
  507. this.records[relativeCompilerName] = [];
  508. }
  509. if (this.records[relativeCompilerName][compilerIndex]) {
  510. childCompiler.records = this.records[relativeCompilerName][compilerIndex];
  511. } else {
  512. this.records[relativeCompilerName].push((childCompiler.records = {}));
  513. }
  514. childCompiler.options = Object.create(this.options);
  515. childCompiler.options.output = Object.create(childCompiler.options.output);
  516. for (const name in outputOptions) {
  517. childCompiler.options.output[name] = outputOptions[name];
  518. }
  519. childCompiler.parentCompilation = compilation;
  520. compilation.hooks.childCompiler.call(
  521. childCompiler,
  522. compilerName,
  523. compilerIndex
  524. );
  525. return childCompiler;
  526. }
  527. isChild() {
  528. return !!this.parentCompilation;
  529. }
  530. createCompilation() {
  531. return new Compilation(this);
  532. }
  533. newCompilation(params) {
  534. const compilation = this.createCompilation();
  535. compilation.fileTimestamps = this.fileTimestamps;
  536. compilation.contextTimestamps = this.contextTimestamps;
  537. compilation.name = this.name;
  538. compilation.records = this.records;
  539. compilation.compilationDependencies = params.compilationDependencies;
  540. this.hooks.thisCompilation.call(compilation, params);
  541. this.hooks.compilation.call(compilation, params);
  542. return compilation;
  543. }
  544. createNormalModuleFactory() {
  545. const normalModuleFactory = new NormalModuleFactory(
  546. this.options.context,
  547. this.resolverFactory,
  548. this.options.module || {}
  549. );
  550. this.hooks.normalModuleFactory.call(normalModuleFactory);
  551. return normalModuleFactory;
  552. }
  553. createContextModuleFactory() {
  554. const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
  555. this.hooks.contextModuleFactory.call(contextModuleFactory);
  556. return contextModuleFactory;
  557. }
  558. newCompilationParams() {
  559. const params = {
  560. normalModuleFactory: this.createNormalModuleFactory(),
  561. contextModuleFactory: this.createContextModuleFactory(),
  562. compilationDependencies: new Set()
  563. };
  564. return params;
  565. }
  566. compile(callback) {
  567. const params = this.newCompilationParams();
  568. this.hooks.beforeCompile.callAsync(params, err => {
  569. if (err) return callback(err);
  570. this.hooks.compile.call(params);
  571. const compilation = this.newCompilation(params);
  572. this.hooks.make.callAsync(compilation, err => {
  573. if (err) return callback(err);
  574. compilation.finish(err => {
  575. if (err) return callback(err);
  576. compilation.seal(err => {
  577. if (err) return callback(err);
  578. this.hooks.afterCompile.callAsync(compilation, err => {
  579. if (err) return callback(err);
  580. return callback(null, compilation);
  581. });
  582. });
  583. });
  584. });
  585. });
  586. }
  587. }
  588. module.exports = Compiler;
  589. class SizeOnlySource extends Source {
  590. constructor(size) {
  591. super();
  592. this._size = size;
  593. }
  594. _error() {
  595. return new Error(
  596. "Content and Map of this Source is no longer available (only size() is supported)"
  597. );
  598. }
  599. size() {
  600. return this._size;
  601. }
  602. /**
  603. * @param {any} options options
  604. * @returns {string} the source
  605. */
  606. source(options) {
  607. throw this._error();
  608. }
  609. node() {
  610. throw this._error();
  611. }
  612. listMap() {
  613. throw this._error();
  614. }
  615. map() {
  616. throw this._error();
  617. }
  618. listNode() {
  619. throw this._error();
  620. }
  621. updateHash() {
  622. throw this._error();
  623. }
  624. }