Compiler.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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, source, info } of compilation.getAssets()) {
  287. this.parentCompilation.emitAsset(name, source, info);
  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.getAssets(),
  309. 15,
  310. ({ name: file, source }, 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. compilation.updateAsset(file, cacheEntry.sizeOnlySource, {
  346. size: cacheEntry.sizeOnlySource.size()
  347. });
  348. return callback();
  349. }
  350. }
  351. // TODO webpack 5: if info.immutable check if file already exists in output
  352. // skip emitting if it's already there
  353. // get the binary (Buffer) content from the Source
  354. /** @type {Buffer} */
  355. let content;
  356. if (typeof source.buffer === "function") {
  357. content = source.buffer();
  358. } else {
  359. const bufferOrString = source.source();
  360. if (Buffer.isBuffer(bufferOrString)) {
  361. content = bufferOrString;
  362. } else {
  363. content = Buffer.from(bufferOrString, "utf8");
  364. }
  365. }
  366. // Create a replacement resource which only allows to ask for size
  367. // This allows to GC all memory allocated by the Source
  368. // (expect when the Source is stored in any other cache)
  369. cacheEntry.sizeOnlySource = new SizeOnlySource(content.length);
  370. compilation.updateAsset(file, cacheEntry.sizeOnlySource, {
  371. size: content.length
  372. });
  373. // Write the file to output file system
  374. this.outputFileSystem.writeFile(targetPath, content, err => {
  375. if (err) return callback(err);
  376. // information marker that the asset has been emitted
  377. compilation.emittedAssets.add(file);
  378. // cache the information that the Source has been written to that location
  379. const newGeneration =
  380. targetFileGeneration === undefined
  381. ? 1
  382. : targetFileGeneration + 1;
  383. cacheEntry.writtenTo.set(targetPath, newGeneration);
  384. this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
  385. this.hooks.assetEmitted.callAsync(file, content, callback);
  386. });
  387. } else {
  388. if (source.existsAt === targetPath) {
  389. source.emitted = false;
  390. return callback();
  391. }
  392. let content = source.source();
  393. if (!Buffer.isBuffer(content)) {
  394. content = Buffer.from(content, "utf8");
  395. }
  396. source.existsAt = targetPath;
  397. source.emitted = true;
  398. this.outputFileSystem.writeFile(targetPath, content, err => {
  399. if (err) return callback(err);
  400. this.hooks.assetEmitted.callAsync(file, content, callback);
  401. });
  402. }
  403. };
  404. if (targetFile.match(/\/|\\/)) {
  405. const dir = path.dirname(targetFile);
  406. this.outputFileSystem.mkdirp(
  407. this.outputFileSystem.join(outputPath, dir),
  408. writeOut
  409. );
  410. } else {
  411. writeOut();
  412. }
  413. },
  414. err => {
  415. if (err) return callback(err);
  416. this.hooks.afterEmit.callAsync(compilation, err => {
  417. if (err) return callback(err);
  418. return callback();
  419. });
  420. }
  421. );
  422. };
  423. this.hooks.emit.callAsync(compilation, err => {
  424. if (err) return callback(err);
  425. outputPath = compilation.getPath(this.outputPath);
  426. this.outputFileSystem.mkdirp(outputPath, emitFiles);
  427. });
  428. }
  429. emitRecords(callback) {
  430. if (!this.recordsOutputPath) return callback();
  431. const idx1 = this.recordsOutputPath.lastIndexOf("/");
  432. const idx2 = this.recordsOutputPath.lastIndexOf("\\");
  433. let recordsOutputPathDirectory = null;
  434. if (idx1 > idx2) {
  435. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1);
  436. } else if (idx1 < idx2) {
  437. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2);
  438. }
  439. const writeFile = () => {
  440. this.outputFileSystem.writeFile(
  441. this.recordsOutputPath,
  442. JSON.stringify(this.records, undefined, 2),
  443. callback
  444. );
  445. };
  446. if (!recordsOutputPathDirectory) {
  447. return writeFile();
  448. }
  449. this.outputFileSystem.mkdirp(recordsOutputPathDirectory, err => {
  450. if (err) return callback(err);
  451. writeFile();
  452. });
  453. }
  454. readRecords(callback) {
  455. if (!this.recordsInputPath) {
  456. this.records = {};
  457. return callback();
  458. }
  459. this.inputFileSystem.stat(this.recordsInputPath, err => {
  460. // It doesn't exist
  461. // We can ignore this.
  462. if (err) return callback();
  463. this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => {
  464. if (err) return callback(err);
  465. try {
  466. this.records = parseJson(content.toString("utf-8"));
  467. } catch (e) {
  468. e.message = "Cannot parse records: " + e.message;
  469. return callback(e);
  470. }
  471. return callback();
  472. });
  473. });
  474. }
  475. createChildCompiler(
  476. compilation,
  477. compilerName,
  478. compilerIndex,
  479. outputOptions,
  480. plugins
  481. ) {
  482. const childCompiler = new Compiler(this.context);
  483. if (Array.isArray(plugins)) {
  484. for (const plugin of plugins) {
  485. plugin.apply(childCompiler);
  486. }
  487. }
  488. for (const name in this.hooks) {
  489. if (
  490. ![
  491. "make",
  492. "compile",
  493. "emit",
  494. "afterEmit",
  495. "invalid",
  496. "done",
  497. "thisCompilation"
  498. ].includes(name)
  499. ) {
  500. if (childCompiler.hooks[name]) {
  501. childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
  502. }
  503. }
  504. }
  505. childCompiler.name = compilerName;
  506. childCompiler.outputPath = this.outputPath;
  507. childCompiler.inputFileSystem = this.inputFileSystem;
  508. childCompiler.outputFileSystem = null;
  509. childCompiler.resolverFactory = this.resolverFactory;
  510. childCompiler.fileTimestamps = this.fileTimestamps;
  511. childCompiler.contextTimestamps = this.contextTimestamps;
  512. const relativeCompilerName = makePathsRelative(this.context, compilerName);
  513. if (!this.records[relativeCompilerName]) {
  514. this.records[relativeCompilerName] = [];
  515. }
  516. if (this.records[relativeCompilerName][compilerIndex]) {
  517. childCompiler.records = this.records[relativeCompilerName][compilerIndex];
  518. } else {
  519. this.records[relativeCompilerName].push((childCompiler.records = {}));
  520. }
  521. childCompiler.options = Object.create(this.options);
  522. childCompiler.options.output = Object.create(childCompiler.options.output);
  523. for (const name in outputOptions) {
  524. childCompiler.options.output[name] = outputOptions[name];
  525. }
  526. childCompiler.parentCompilation = compilation;
  527. compilation.hooks.childCompiler.call(
  528. childCompiler,
  529. compilerName,
  530. compilerIndex
  531. );
  532. return childCompiler;
  533. }
  534. isChild() {
  535. return !!this.parentCompilation;
  536. }
  537. createCompilation() {
  538. return new Compilation(this);
  539. }
  540. newCompilation(params) {
  541. const compilation = this.createCompilation();
  542. compilation.fileTimestamps = this.fileTimestamps;
  543. compilation.contextTimestamps = this.contextTimestamps;
  544. compilation.name = this.name;
  545. compilation.records = this.records;
  546. compilation.compilationDependencies = params.compilationDependencies;
  547. this.hooks.thisCompilation.call(compilation, params);
  548. this.hooks.compilation.call(compilation, params);
  549. return compilation;
  550. }
  551. createNormalModuleFactory() {
  552. const normalModuleFactory = new NormalModuleFactory(
  553. this.options.context,
  554. this.resolverFactory,
  555. this.options.module || {}
  556. );
  557. this.hooks.normalModuleFactory.call(normalModuleFactory);
  558. return normalModuleFactory;
  559. }
  560. createContextModuleFactory() {
  561. const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
  562. this.hooks.contextModuleFactory.call(contextModuleFactory);
  563. return contextModuleFactory;
  564. }
  565. newCompilationParams() {
  566. const params = {
  567. normalModuleFactory: this.createNormalModuleFactory(),
  568. contextModuleFactory: this.createContextModuleFactory(),
  569. compilationDependencies: new Set()
  570. };
  571. return params;
  572. }
  573. compile(callback) {
  574. const params = this.newCompilationParams();
  575. this.hooks.beforeCompile.callAsync(params, err => {
  576. if (err) return callback(err);
  577. this.hooks.compile.call(params);
  578. const compilation = this.newCompilation(params);
  579. this.hooks.make.callAsync(compilation, err => {
  580. if (err) return callback(err);
  581. compilation.finish(err => {
  582. if (err) return callback(err);
  583. compilation.seal(err => {
  584. if (err) return callback(err);
  585. this.hooks.afterCompile.callAsync(compilation, err => {
  586. if (err) return callback(err);
  587. return callback(null, compilation);
  588. });
  589. });
  590. });
  591. });
  592. });
  593. }
  594. }
  595. module.exports = Compiler;
  596. class SizeOnlySource extends Source {
  597. constructor(size) {
  598. super();
  599. this._size = size;
  600. }
  601. _error() {
  602. return new Error(
  603. "Content and Map of this Source is no longer available (only size() is supported)"
  604. );
  605. }
  606. size() {
  607. return this._size;
  608. }
  609. /**
  610. * @param {any} options options
  611. * @returns {string} the source
  612. */
  613. source(options) {
  614. throw this._error();
  615. }
  616. node() {
  617. throw this._error();
  618. }
  619. listMap() {
  620. throw this._error();
  621. }
  622. map() {
  623. throw this._error();
  624. }
  625. listNode() {
  626. throw this._error();
  627. }
  628. updateHash() {
  629. throw this._error();
  630. }
  631. }