Compiler.js 16 KB

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