Compiler.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 util = require("util");
  10. const {
  11. Tapable,
  12. SyncHook,
  13. SyncBailHook,
  14. AsyncParallelHook,
  15. AsyncSeriesHook
  16. } = require("tapable");
  17. const Compilation = require("./Compilation");
  18. const Stats = require("./Stats");
  19. const Watching = require("./Watching");
  20. const NormalModuleFactory = require("./NormalModuleFactory");
  21. const ContextModuleFactory = require("./ContextModuleFactory");
  22. const ResolverFactory = require("./ResolverFactory");
  23. const RequestShortener = require("./RequestShortener");
  24. const { makePathsRelative } = require("./util/identifier");
  25. const ConcurrentCompilationError = require("./ConcurrentCompilationError");
  26. /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
  27. /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
  28. /**
  29. * @typedef {Object} CompilationParams
  30. * @property {NormalModuleFactory} normalModuleFactory
  31. * @property {ContextModuleFactory} contextModuleFactory
  32. * @property {Set<string>} compilationDependencies
  33. */
  34. class Compiler extends Tapable {
  35. constructor(context) {
  36. super();
  37. this.hooks = {
  38. /** @type {SyncBailHook<Compilation>} */
  39. shouldEmit: new SyncBailHook(["compilation"]),
  40. /** @type {AsyncSeriesHook<Stats>} */
  41. done: new AsyncSeriesHook(["stats"]),
  42. /** @type {AsyncSeriesHook<>} */
  43. additionalPass: new AsyncSeriesHook([]),
  44. /** @type {AsyncSeriesHook<Compiler>} */
  45. beforeRun: new AsyncSeriesHook(["compiler"]),
  46. /** @type {AsyncSeriesHook<Compiler>} */
  47. run: new AsyncSeriesHook(["compiler"]),
  48. /** @type {AsyncSeriesHook<Compilation>} */
  49. emit: new AsyncSeriesHook(["compilation"]),
  50. /** @type {AsyncSeriesHook<Compilation>} */
  51. afterEmit: new AsyncSeriesHook(["compilation"]),
  52. /** @type {SyncHook<Compilation, CompilationParams>} */
  53. thisCompilation: new SyncHook(["compilation", "params"]),
  54. /** @type {SyncHook<Compilation, CompilationParams>} */
  55. compilation: new SyncHook(["compilation", "params"]),
  56. /** @type {SyncHook<NormalModuleFactory>} */
  57. normalModuleFactory: new SyncHook(["normalModuleFactory"]),
  58. /** @type {SyncHook<ContextModuleFactory>} */
  59. contextModuleFactory: new SyncHook(["contextModulefactory"]),
  60. /** @type {AsyncSeriesHook<CompilationParams>} */
  61. beforeCompile: new AsyncSeriesHook(["params"]),
  62. /** @type {SyncHook<CompilationParams>} */
  63. compile: new SyncHook(["params"]),
  64. /** @type {AsyncParallelHook<Compilation>} */
  65. make: new AsyncParallelHook(["compilation"]),
  66. /** @type {AsyncSeriesHook<Compilation>} */
  67. afterCompile: new AsyncSeriesHook(["compilation"]),
  68. /** @type {AsyncSeriesHook<Compiler>} */
  69. watchRun: new AsyncSeriesHook(["compiler"]),
  70. /** @type {SyncHook<Error>} */
  71. failed: new SyncHook(["error"]),
  72. /** @type {SyncHook<string, string>} */
  73. invalid: new SyncHook(["filename", "changeTime"]),
  74. /** @type {SyncHook} */
  75. watchClose: new SyncHook([]),
  76. // TODO the following hooks are weirdly located here
  77. // TODO move them for webpack 5
  78. /** @type {SyncHook} */
  79. environment: new SyncHook([]),
  80. /** @type {SyncHook} */
  81. afterEnvironment: new SyncHook([]),
  82. /** @type {SyncHook<Compiler>} */
  83. afterPlugins: new SyncHook(["compiler"]),
  84. /** @type {SyncHook<Compiler>} */
  85. afterResolvers: new SyncHook(["compiler"]),
  86. /** @type {SyncBailHook<string, Entry>} */
  87. entryOption: new SyncBailHook(["context", "entry"])
  88. };
  89. this._pluginCompat.tap("Compiler", options => {
  90. switch (options.name) {
  91. case "additional-pass":
  92. case "before-run":
  93. case "run":
  94. case "emit":
  95. case "after-emit":
  96. case "before-compile":
  97. case "make":
  98. case "after-compile":
  99. case "watch-run":
  100. options.async = true;
  101. break;
  102. }
  103. });
  104. /** @type {string=} */
  105. this.name = undefined;
  106. /** @type {Compilation=} */
  107. this.parentCompilation = undefined;
  108. /** @type {string} */
  109. this.outputPath = "";
  110. this.outputFileSystem = null;
  111. this.inputFileSystem = null;
  112. /** @type {string|null} */
  113. this.recordsInputPath = null;
  114. /** @type {string|null} */
  115. this.recordsOutputPath = null;
  116. this.records = {};
  117. this.removedFiles = new Set();
  118. /** @type {Map<string, number>} */
  119. this.fileTimestamps = new Map();
  120. /** @type {Map<string, number>} */
  121. this.contextTimestamps = new Map();
  122. /** @type {ResolverFactory} */
  123. this.resolverFactory = new ResolverFactory();
  124. // TODO remove in webpack 5
  125. this.resolvers = {
  126. normal: {
  127. plugins: util.deprecate((hook, fn) => {
  128. this.resolverFactory.plugin("resolver normal", resolver => {
  129. resolver.plugin(hook, fn);
  130. });
  131. }, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  132. apply: util.deprecate((...args) => {
  133. this.resolverFactory.plugin("resolver normal", resolver => {
  134. resolver.apply(...args);
  135. });
  136. }, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  137. },
  138. loader: {
  139. plugins: util.deprecate((hook, fn) => {
  140. this.resolverFactory.plugin("resolver loader", resolver => {
  141. resolver.plugin(hook, fn);
  142. });
  143. }, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  144. apply: util.deprecate((...args) => {
  145. this.resolverFactory.plugin("resolver loader", resolver => {
  146. resolver.apply(...args);
  147. });
  148. }, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  149. },
  150. context: {
  151. plugins: util.deprecate((hook, fn) => {
  152. this.resolverFactory.plugin("resolver context", resolver => {
  153. resolver.plugin(hook, fn);
  154. });
  155. }, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.plugin(/* … */);\n}); instead.'),
  156. apply: util.deprecate((...args) => {
  157. this.resolverFactory.plugin("resolver context", resolver => {
  158. resolver.apply(...args);
  159. });
  160. }, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.apply(/* … */);\n}); instead.')
  161. }
  162. };
  163. /** @type {WebpackOptions} */
  164. this.options = /** @type {WebpackOptions} */ ({});
  165. this.context = context;
  166. this.requestShortener = new RequestShortener(context);
  167. /** @type {boolean} */
  168. this.running = false;
  169. /** @type {boolean} */
  170. this.watchMode = false;
  171. }
  172. watch(watchOptions, handler) {
  173. if (this.running) return handler(new ConcurrentCompilationError());
  174. this.running = true;
  175. this.watchMode = true;
  176. this.fileTimestamps = new Map();
  177. this.contextTimestamps = new Map();
  178. this.removedFiles = new Set();
  179. return new Watching(this, watchOptions, handler);
  180. }
  181. run(callback) {
  182. if (this.running) return callback(new ConcurrentCompilationError());
  183. const finalCallback = (err, stats) => {
  184. this.running = false;
  185. if (err) {
  186. this.hooks.failed.call(err);
  187. }
  188. if (callback !== undefined) return callback(err, stats);
  189. };
  190. const startTime = Date.now();
  191. this.running = true;
  192. const onCompiled = (err, compilation) => {
  193. if (err) return finalCallback(err);
  194. if (this.hooks.shouldEmit.call(compilation) === false) {
  195. const stats = new Stats(compilation);
  196. stats.startTime = startTime;
  197. stats.endTime = Date.now();
  198. this.hooks.done.callAsync(stats, err => {
  199. if (err) return finalCallback(err);
  200. return finalCallback(null, stats);
  201. });
  202. return;
  203. }
  204. this.emitAssets(compilation, err => {
  205. if (err) return finalCallback(err);
  206. if (compilation.hooks.needAdditionalPass.call()) {
  207. compilation.needAdditionalPass = true;
  208. const stats = new Stats(compilation);
  209. stats.startTime = startTime;
  210. stats.endTime = Date.now();
  211. this.hooks.done.callAsync(stats, err => {
  212. if (err) return finalCallback(err);
  213. this.hooks.additionalPass.callAsync(err => {
  214. if (err) return finalCallback(err);
  215. this.compile(onCompiled);
  216. });
  217. });
  218. return;
  219. }
  220. this.emitRecords(err => {
  221. if (err) return finalCallback(err);
  222. const stats = new Stats(compilation);
  223. stats.startTime = startTime;
  224. stats.endTime = Date.now();
  225. this.hooks.done.callAsync(stats, err => {
  226. if (err) return finalCallback(err);
  227. return finalCallback(null, stats);
  228. });
  229. });
  230. });
  231. };
  232. this.hooks.beforeRun.callAsync(this, err => {
  233. if (err) return finalCallback(err);
  234. this.hooks.run.callAsync(this, err => {
  235. if (err) return finalCallback(err);
  236. this.readRecords(err => {
  237. if (err) return finalCallback(err);
  238. this.compile(onCompiled);
  239. });
  240. });
  241. });
  242. }
  243. runAsChild(callback) {
  244. this.compile((err, compilation) => {
  245. if (err) return callback(err);
  246. this.parentCompilation.children.push(compilation);
  247. for (const name of Object.keys(compilation.assets)) {
  248. this.parentCompilation.assets[name] = compilation.assets[name];
  249. }
  250. const entries = Array.from(
  251. compilation.entrypoints.values(),
  252. ep => ep.chunks
  253. ).reduce((array, chunks) => {
  254. return array.concat(chunks);
  255. }, []);
  256. return callback(null, entries, compilation);
  257. });
  258. }
  259. purgeInputFileSystem() {
  260. if (this.inputFileSystem && this.inputFileSystem.purge) {
  261. this.inputFileSystem.purge();
  262. }
  263. }
  264. emitAssets(compilation, callback) {
  265. let outputPath;
  266. const emitFiles = err => {
  267. if (err) return callback(err);
  268. asyncLib.forEach(
  269. compilation.assets,
  270. (source, file, callback) => {
  271. let targetFile = file;
  272. const queryStringIdx = targetFile.indexOf("?");
  273. if (queryStringIdx >= 0) {
  274. targetFile = targetFile.substr(0, queryStringIdx);
  275. }
  276. const writeOut = err => {
  277. if (err) return callback(err);
  278. const targetPath = this.outputFileSystem.join(
  279. outputPath,
  280. targetFile
  281. );
  282. if (source.existsAt === targetPath) {
  283. source.emitted = false;
  284. return callback();
  285. }
  286. let content = source.source();
  287. if (!Buffer.isBuffer(content)) {
  288. content = Buffer.from(content, "utf8");
  289. }
  290. source.existsAt = targetPath;
  291. source.emitted = true;
  292. this.outputFileSystem.writeFile(targetPath, content, callback);
  293. };
  294. if (targetFile.match(/\/|\\/)) {
  295. const dir = path.dirname(targetFile);
  296. this.outputFileSystem.mkdirp(
  297. this.outputFileSystem.join(outputPath, dir),
  298. writeOut
  299. );
  300. } else {
  301. writeOut();
  302. }
  303. },
  304. err => {
  305. if (err) return callback(err);
  306. this.hooks.afterEmit.callAsync(compilation, err => {
  307. if (err) return callback(err);
  308. return callback();
  309. });
  310. }
  311. );
  312. };
  313. this.hooks.emit.callAsync(compilation, err => {
  314. if (err) return callback(err);
  315. outputPath = compilation.getPath(this.outputPath);
  316. this.outputFileSystem.mkdirp(outputPath, emitFiles);
  317. });
  318. }
  319. emitRecords(callback) {
  320. if (!this.recordsOutputPath) return callback();
  321. const idx1 = this.recordsOutputPath.lastIndexOf("/");
  322. const idx2 = this.recordsOutputPath.lastIndexOf("\\");
  323. let recordsOutputPathDirectory = null;
  324. if (idx1 > idx2) {
  325. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1);
  326. } else if (idx1 < idx2) {
  327. recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2);
  328. }
  329. const writeFile = () => {
  330. this.outputFileSystem.writeFile(
  331. this.recordsOutputPath,
  332. JSON.stringify(this.records, undefined, 2),
  333. callback
  334. );
  335. };
  336. if (!recordsOutputPathDirectory) {
  337. return writeFile();
  338. }
  339. this.outputFileSystem.mkdirp(recordsOutputPathDirectory, err => {
  340. if (err) return callback(err);
  341. writeFile();
  342. });
  343. }
  344. readRecords(callback) {
  345. if (!this.recordsInputPath) {
  346. this.records = {};
  347. return callback();
  348. }
  349. this.inputFileSystem.stat(this.recordsInputPath, err => {
  350. // It doesn't exist
  351. // We can ignore this.
  352. if (err) return callback();
  353. this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => {
  354. if (err) return callback(err);
  355. try {
  356. this.records = parseJson(content.toString("utf-8"));
  357. } catch (e) {
  358. e.message = "Cannot parse records: " + e.message;
  359. return callback(e);
  360. }
  361. return callback();
  362. });
  363. });
  364. }
  365. createChildCompiler(
  366. compilation,
  367. compilerName,
  368. compilerIndex,
  369. outputOptions,
  370. plugins
  371. ) {
  372. const childCompiler = new Compiler(this.context);
  373. if (Array.isArray(plugins)) {
  374. for (const plugin of plugins) {
  375. plugin.apply(childCompiler);
  376. }
  377. }
  378. for (const name in this.hooks) {
  379. if (
  380. ![
  381. "make",
  382. "compile",
  383. "emit",
  384. "afterEmit",
  385. "invalid",
  386. "done",
  387. "thisCompilation"
  388. ].includes(name)
  389. ) {
  390. if (childCompiler.hooks[name]) {
  391. childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
  392. }
  393. }
  394. }
  395. childCompiler.name = compilerName;
  396. childCompiler.outputPath = this.outputPath;
  397. childCompiler.inputFileSystem = this.inputFileSystem;
  398. childCompiler.outputFileSystem = null;
  399. childCompiler.resolverFactory = this.resolverFactory;
  400. childCompiler.fileTimestamps = this.fileTimestamps;
  401. childCompiler.contextTimestamps = this.contextTimestamps;
  402. const relativeCompilerName = makePathsRelative(this.context, compilerName);
  403. if (!this.records[relativeCompilerName]) {
  404. this.records[relativeCompilerName] = [];
  405. }
  406. if (this.records[relativeCompilerName][compilerIndex]) {
  407. childCompiler.records = this.records[relativeCompilerName][compilerIndex];
  408. } else {
  409. this.records[relativeCompilerName].push((childCompiler.records = {}));
  410. }
  411. childCompiler.options = Object.create(this.options);
  412. childCompiler.options.output = Object.create(childCompiler.options.output);
  413. for (const name in outputOptions) {
  414. childCompiler.options.output[name] = outputOptions[name];
  415. }
  416. childCompiler.parentCompilation = compilation;
  417. compilation.hooks.childCompiler.call(
  418. childCompiler,
  419. compilerName,
  420. compilerIndex
  421. );
  422. return childCompiler;
  423. }
  424. isChild() {
  425. return !!this.parentCompilation;
  426. }
  427. createCompilation() {
  428. return new Compilation(this);
  429. }
  430. newCompilation(params) {
  431. const compilation = this.createCompilation();
  432. compilation.fileTimestamps = this.fileTimestamps;
  433. compilation.contextTimestamps = this.contextTimestamps;
  434. compilation.name = this.name;
  435. compilation.records = this.records;
  436. compilation.compilationDependencies = params.compilationDependencies;
  437. this.hooks.thisCompilation.call(compilation, params);
  438. this.hooks.compilation.call(compilation, params);
  439. return compilation;
  440. }
  441. createNormalModuleFactory() {
  442. const normalModuleFactory = new NormalModuleFactory(
  443. this.options.context,
  444. this.resolverFactory,
  445. this.options.module || {}
  446. );
  447. this.hooks.normalModuleFactory.call(normalModuleFactory);
  448. return normalModuleFactory;
  449. }
  450. createContextModuleFactory() {
  451. const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
  452. this.hooks.contextModuleFactory.call(contextModuleFactory);
  453. return contextModuleFactory;
  454. }
  455. newCompilationParams() {
  456. const params = {
  457. normalModuleFactory: this.createNormalModuleFactory(),
  458. contextModuleFactory: this.createContextModuleFactory(),
  459. compilationDependencies: new Set()
  460. };
  461. return params;
  462. }
  463. compile(callback) {
  464. const params = this.newCompilationParams();
  465. this.hooks.beforeCompile.callAsync(params, err => {
  466. if (err) return callback(err);
  467. this.hooks.compile.call(params);
  468. const compilation = this.newCompilation(params);
  469. this.hooks.make.callAsync(compilation, err => {
  470. if (err) return callback(err);
  471. compilation.finish();
  472. compilation.seal(err => {
  473. if (err) return callback(err);
  474. this.hooks.afterCompile.callAsync(compilation, err => {
  475. if (err) return callback(err);
  476. return callback(null, compilation);
  477. });
  478. });
  479. });
  480. });
  481. }
  482. }
  483. module.exports = Compiler;