You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NormalModule.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NativeModule = require("module");
  7. const {
  8. CachedSource,
  9. LineToLineMappedSource,
  10. OriginalSource,
  11. RawSource,
  12. SourceMapSource
  13. } = require("webpack-sources");
  14. const { getContext, runLoaders } = require("loader-runner");
  15. const WebpackError = require("./WebpackError");
  16. const Module = require("./Module");
  17. const ModuleParseError = require("./ModuleParseError");
  18. const ModuleBuildError = require("./ModuleBuildError");
  19. const ModuleError = require("./ModuleError");
  20. const ModuleWarning = require("./ModuleWarning");
  21. const createHash = require("./util/createHash");
  22. const contextify = require("./util/identifier").contextify;
  23. /** @typedef {import("./util/createHash").Hash} Hash */
  24. const asString = buf => {
  25. if (Buffer.isBuffer(buf)) {
  26. return buf.toString("utf-8");
  27. }
  28. return buf;
  29. };
  30. const asBuffer = str => {
  31. if (!Buffer.isBuffer(str)) {
  32. return Buffer.from(str, "utf-8");
  33. }
  34. return str;
  35. };
  36. class NonErrorEmittedError extends WebpackError {
  37. constructor(error) {
  38. super();
  39. this.name = "NonErrorEmittedError";
  40. this.message = "(Emitted value instead of an instance of Error) " + error;
  41. Error.captureStackTrace(this, this.constructor);
  42. }
  43. }
  44. /**
  45. * @typedef {Object} CachedSourceEntry
  46. * @property {TODO} source the generated source
  47. * @property {string} hash the hash value
  48. */
  49. class NormalModule extends Module {
  50. constructor({
  51. type,
  52. request,
  53. userRequest,
  54. rawRequest,
  55. loaders,
  56. resource,
  57. matchResource,
  58. parser,
  59. generator,
  60. resolveOptions
  61. }) {
  62. super(type, getContext(resource));
  63. // Info from Factory
  64. this.request = request;
  65. this.userRequest = userRequest;
  66. this.rawRequest = rawRequest;
  67. this.binary = type.startsWith("webassembly");
  68. this.parser = parser;
  69. this.generator = generator;
  70. this.resource = resource;
  71. this.matchResource = matchResource;
  72. this.loaders = loaders;
  73. if (resolveOptions !== undefined) this.resolveOptions = resolveOptions;
  74. // Info from Build
  75. this.error = null;
  76. this._source = null;
  77. this._buildHash = "";
  78. this.buildTimestamp = undefined;
  79. /** @private @type {Map<string, CachedSourceEntry>} */
  80. this._cachedSources = new Map();
  81. // Options for the NormalModule set by plugins
  82. // TODO refactor this -> options object filled from Factory
  83. this.useSourceMap = false;
  84. this.lineToLine = false;
  85. // Cache
  86. this._lastSuccessfulBuildMeta = {};
  87. }
  88. identifier() {
  89. return this.request;
  90. }
  91. readableIdentifier(requestShortener) {
  92. return requestShortener.shorten(this.userRequest);
  93. }
  94. libIdent(options) {
  95. return contextify(options.context, this.userRequest);
  96. }
  97. nameForCondition() {
  98. const resource = this.matchResource || this.resource;
  99. const idx = resource.indexOf("?");
  100. if (idx >= 0) return resource.substr(0, idx);
  101. return resource;
  102. }
  103. updateCacheModule(module) {
  104. this.type = module.type;
  105. this.request = module.request;
  106. this.userRequest = module.userRequest;
  107. this.rawRequest = module.rawRequest;
  108. this.parser = module.parser;
  109. this.generator = module.generator;
  110. this.resource = module.resource;
  111. this.matchResource = module.matchResource;
  112. this.loaders = module.loaders;
  113. this.resolveOptions = module.resolveOptions;
  114. }
  115. createSourceForAsset(name, content, sourceMap) {
  116. if (!sourceMap) {
  117. return new RawSource(content);
  118. }
  119. if (typeof sourceMap === "string") {
  120. return new OriginalSource(content, sourceMap);
  121. }
  122. return new SourceMapSource(content, name, sourceMap);
  123. }
  124. createLoaderContext(resolver, options, compilation, fs) {
  125. const requestShortener = compilation.runtimeTemplate.requestShortener;
  126. const getCurrentLoaderName = () => {
  127. const currentLoader = this.getCurrentLoader(loaderContext);
  128. if (!currentLoader) return "(not in loader scope)";
  129. return requestShortener.shorten(currentLoader.loader);
  130. };
  131. const loaderContext = {
  132. version: 2,
  133. emitWarning: warning => {
  134. if (!(warning instanceof Error)) {
  135. warning = new NonErrorEmittedError(warning);
  136. }
  137. this.warnings.push(
  138. new ModuleWarning(this, warning, {
  139. from: getCurrentLoaderName()
  140. })
  141. );
  142. },
  143. emitError: error => {
  144. if (!(error instanceof Error)) {
  145. error = new NonErrorEmittedError(error);
  146. }
  147. this.errors.push(
  148. new ModuleError(this, error, {
  149. from: getCurrentLoaderName()
  150. })
  151. );
  152. },
  153. getLogger: name => {
  154. const currentLoader = this.getCurrentLoader(loaderContext);
  155. return compilation.getLogger(() =>
  156. [currentLoader && currentLoader.loader, name, this.identifier()]
  157. .filter(Boolean)
  158. .join("|")
  159. );
  160. },
  161. // TODO remove in webpack 5
  162. exec: (code, filename) => {
  163. // @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.
  164. const module = new NativeModule(filename, this);
  165. // @ts-ignore _nodeModulePaths is deprecated and undocumented Node.js API
  166. module.paths = NativeModule._nodeModulePaths(this.context);
  167. module.filename = filename;
  168. module._compile(code, filename);
  169. return module.exports;
  170. },
  171. resolve(context, request, callback) {
  172. resolver.resolve({}, context, request, {}, callback);
  173. },
  174. getResolve(options) {
  175. const child = options ? resolver.withOptions(options) : resolver;
  176. return (context, request, callback) => {
  177. if (callback) {
  178. child.resolve({}, context, request, {}, callback);
  179. } else {
  180. return new Promise((resolve, reject) => {
  181. child.resolve({}, context, request, {}, (err, result) => {
  182. if (err) reject(err);
  183. else resolve(result);
  184. });
  185. });
  186. }
  187. };
  188. },
  189. emitFile: (name, content, sourceMap, assetInfo) => {
  190. if (!this.buildInfo.assets) {
  191. this.buildInfo.assets = Object.create(null);
  192. this.buildInfo.assetsInfo = new Map();
  193. }
  194. this.buildInfo.assets[name] = this.createSourceForAsset(
  195. name,
  196. content,
  197. sourceMap
  198. );
  199. this.buildInfo.assetsInfo.set(name, assetInfo);
  200. },
  201. rootContext: options.context,
  202. webpack: true,
  203. sourceMap: !!this.useSourceMap,
  204. mode: options.mode || "production",
  205. _module: this,
  206. _compilation: compilation,
  207. _compiler: compilation.compiler,
  208. fs: fs
  209. };
  210. compilation.hooks.normalModuleLoader.call(loaderContext, this);
  211. if (options.loader) {
  212. Object.assign(loaderContext, options.loader);
  213. }
  214. return loaderContext;
  215. }
  216. getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
  217. if (
  218. this.loaders &&
  219. this.loaders.length &&
  220. index < this.loaders.length &&
  221. index >= 0 &&
  222. this.loaders[index]
  223. ) {
  224. return this.loaders[index];
  225. }
  226. return null;
  227. }
  228. createSource(source, resourceBuffer, sourceMap) {
  229. // if there is no identifier return raw source
  230. if (!this.identifier) {
  231. return new RawSource(source);
  232. }
  233. // from here on we assume we have an identifier
  234. const identifier = this.identifier();
  235. if (this.lineToLine && resourceBuffer) {
  236. return new LineToLineMappedSource(
  237. source,
  238. identifier,
  239. asString(resourceBuffer)
  240. );
  241. }
  242. if (this.useSourceMap && sourceMap) {
  243. return new SourceMapSource(source, identifier, sourceMap);
  244. }
  245. if (Buffer.isBuffer(source)) {
  246. // @ts-ignore
  247. // TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
  248. return new RawSource(source);
  249. }
  250. return new OriginalSource(source, identifier);
  251. }
  252. doBuild(options, compilation, resolver, fs, callback) {
  253. const loaderContext = this.createLoaderContext(
  254. resolver,
  255. options,
  256. compilation,
  257. fs
  258. );
  259. runLoaders(
  260. {
  261. resource: this.resource,
  262. loaders: this.loaders,
  263. context: loaderContext,
  264. readResource: fs.readFile.bind(fs)
  265. },
  266. (err, result) => {
  267. if (result) {
  268. this.buildInfo.cacheable = result.cacheable;
  269. this.buildInfo.fileDependencies = new Set(result.fileDependencies);
  270. this.buildInfo.contextDependencies = new Set(
  271. result.contextDependencies
  272. );
  273. }
  274. if (err) {
  275. if (!(err instanceof Error)) {
  276. err = new NonErrorEmittedError(err);
  277. }
  278. const currentLoader = this.getCurrentLoader(loaderContext);
  279. const error = new ModuleBuildError(this, err, {
  280. from:
  281. currentLoader &&
  282. compilation.runtimeTemplate.requestShortener.shorten(
  283. currentLoader.loader
  284. )
  285. });
  286. return callback(error);
  287. }
  288. const resourceBuffer = result.resourceBuffer;
  289. const source = result.result[0];
  290. const sourceMap = result.result.length >= 1 ? result.result[1] : null;
  291. const extraInfo = result.result.length >= 2 ? result.result[2] : null;
  292. if (!Buffer.isBuffer(source) && typeof source !== "string") {
  293. const currentLoader = this.getCurrentLoader(loaderContext, 0);
  294. const err = new Error(
  295. `Final loader (${
  296. currentLoader
  297. ? compilation.runtimeTemplate.requestShortener.shorten(
  298. currentLoader.loader
  299. )
  300. : "unknown"
  301. }) didn't return a Buffer or String`
  302. );
  303. const error = new ModuleBuildError(this, err);
  304. return callback(error);
  305. }
  306. this._source = this.createSource(
  307. this.binary ? asBuffer(source) : asString(source),
  308. resourceBuffer,
  309. sourceMap
  310. );
  311. this._ast =
  312. typeof extraInfo === "object" &&
  313. extraInfo !== null &&
  314. extraInfo.webpackAST !== undefined
  315. ? extraInfo.webpackAST
  316. : null;
  317. return callback();
  318. }
  319. );
  320. }
  321. markModuleAsErrored(error) {
  322. // Restore build meta from successful build to keep importing state
  323. this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta);
  324. this.error = error;
  325. this.errors.push(this.error);
  326. this._source = new RawSource(
  327. "throw new Error(" + JSON.stringify(this.error.message) + ");"
  328. );
  329. this._ast = null;
  330. }
  331. applyNoParseRule(rule, content) {
  332. // must start with "rule" if rule is a string
  333. if (typeof rule === "string") {
  334. return content.indexOf(rule) === 0;
  335. }
  336. if (typeof rule === "function") {
  337. return rule(content);
  338. }
  339. // we assume rule is a regexp
  340. return rule.test(content);
  341. }
  342. // check if module should not be parsed
  343. // returns "true" if the module should !not! be parsed
  344. // returns "false" if the module !must! be parsed
  345. shouldPreventParsing(noParseRule, request) {
  346. // if no noParseRule exists, return false
  347. // the module !must! be parsed.
  348. if (!noParseRule) {
  349. return false;
  350. }
  351. // we only have one rule to check
  352. if (!Array.isArray(noParseRule)) {
  353. // returns "true" if the module is !not! to be parsed
  354. return this.applyNoParseRule(noParseRule, request);
  355. }
  356. for (let i = 0; i < noParseRule.length; i++) {
  357. const rule = noParseRule[i];
  358. // early exit on first truthy match
  359. // this module is !not! to be parsed
  360. if (this.applyNoParseRule(rule, request)) {
  361. return true;
  362. }
  363. }
  364. // no match found, so this module !should! be parsed
  365. return false;
  366. }
  367. _initBuildHash(compilation) {
  368. const hash = createHash(compilation.outputOptions.hashFunction);
  369. if (this._source) {
  370. hash.update("source");
  371. this._source.updateHash(hash);
  372. }
  373. hash.update("meta");
  374. hash.update(JSON.stringify(this.buildMeta));
  375. this._buildHash = /** @type {string} */ (hash.digest("hex"));
  376. }
  377. build(options, compilation, resolver, fs, callback) {
  378. this.buildTimestamp = Date.now();
  379. this.built = true;
  380. this._source = null;
  381. this._ast = null;
  382. this._buildHash = "";
  383. this.error = null;
  384. this.errors.length = 0;
  385. this.warnings.length = 0;
  386. this.buildMeta = {};
  387. this.buildInfo = {
  388. cacheable: false,
  389. fileDependencies: new Set(),
  390. contextDependencies: new Set(),
  391. assets: undefined,
  392. assetsInfo: undefined
  393. };
  394. return this.doBuild(options, compilation, resolver, fs, err => {
  395. this._cachedSources.clear();
  396. // if we have an error mark module as failed and exit
  397. if (err) {
  398. this.markModuleAsErrored(err);
  399. this._initBuildHash(compilation);
  400. return callback();
  401. }
  402. // check if this module should !not! be parsed.
  403. // if so, exit here;
  404. const noParseRule = options.module && options.module.noParse;
  405. if (this.shouldPreventParsing(noParseRule, this.request)) {
  406. this._initBuildHash(compilation);
  407. return callback();
  408. }
  409. const handleParseError = e => {
  410. const source = this._source.source();
  411. const loaders = this.loaders.map(item =>
  412. contextify(options.context, item.loader)
  413. );
  414. const error = new ModuleParseError(this, source, e, loaders);
  415. this.markModuleAsErrored(error);
  416. this._initBuildHash(compilation);
  417. return callback();
  418. };
  419. const handleParseResult = result => {
  420. this._lastSuccessfulBuildMeta = this.buildMeta;
  421. this._initBuildHash(compilation);
  422. return callback();
  423. };
  424. try {
  425. const result = this.parser.parse(
  426. this._ast || this._source.source(),
  427. {
  428. current: this,
  429. module: this,
  430. compilation: compilation,
  431. options: options
  432. },
  433. (err, result) => {
  434. if (err) {
  435. handleParseError(err);
  436. } else {
  437. handleParseResult(result);
  438. }
  439. }
  440. );
  441. if (result !== undefined) {
  442. // parse is sync
  443. handleParseResult(result);
  444. }
  445. } catch (e) {
  446. handleParseError(e);
  447. }
  448. });
  449. }
  450. getHashDigest(dependencyTemplates) {
  451. // TODO webpack 5 refactor
  452. let dtHash = dependencyTemplates.get("hash");
  453. return `${this.hash}-${dtHash}`;
  454. }
  455. source(dependencyTemplates, runtimeTemplate, type = "javascript") {
  456. const hashDigest = this.getHashDigest(dependencyTemplates);
  457. const cacheEntry = this._cachedSources.get(type);
  458. if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
  459. // We can reuse the cached source
  460. return cacheEntry.source;
  461. }
  462. const source = this.generator.generate(
  463. this,
  464. dependencyTemplates,
  465. runtimeTemplate,
  466. type
  467. );
  468. const cachedSource = new CachedSource(source);
  469. this._cachedSources.set(type, {
  470. source: cachedSource,
  471. hash: hashDigest
  472. });
  473. return cachedSource;
  474. }
  475. originalSource() {
  476. return this._source;
  477. }
  478. needRebuild(fileTimestamps, contextTimestamps) {
  479. // always try to rebuild in case of an error
  480. if (this.error) return true;
  481. // always rebuild when module is not cacheable
  482. if (!this.buildInfo.cacheable) return true;
  483. // Check timestamps of all dependencies
  484. // Missing timestamp -> need rebuild
  485. // Timestamp bigger than buildTimestamp -> need rebuild
  486. for (const file of this.buildInfo.fileDependencies) {
  487. const timestamp = fileTimestamps.get(file);
  488. if (!timestamp) return true;
  489. if (timestamp >= this.buildTimestamp) return true;
  490. }
  491. for (const file of this.buildInfo.contextDependencies) {
  492. const timestamp = contextTimestamps.get(file);
  493. if (!timestamp) return true;
  494. if (timestamp >= this.buildTimestamp) return true;
  495. }
  496. // elsewise -> no rebuild needed
  497. return false;
  498. }
  499. size() {
  500. return this._source ? this._source.size() : -1;
  501. }
  502. /**
  503. * @param {Hash} hash the hash used to track dependencies
  504. * @returns {void}
  505. */
  506. updateHash(hash) {
  507. hash.update(this._buildHash);
  508. super.updateHash(hash);
  509. }
  510. }
  511. module.exports = NormalModule;