NormalModule.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 loaderContext = {
  127. version: 2,
  128. emitWarning: warning => {
  129. if (!(warning instanceof Error)) {
  130. warning = new NonErrorEmittedError(warning);
  131. }
  132. const currentLoader = this.getCurrentLoader(loaderContext);
  133. this.warnings.push(
  134. new ModuleWarning(this, warning, {
  135. from: requestShortener.shorten(currentLoader.loader)
  136. })
  137. );
  138. },
  139. emitError: error => {
  140. if (!(error instanceof Error)) {
  141. error = new NonErrorEmittedError(error);
  142. }
  143. const currentLoader = this.getCurrentLoader(loaderContext);
  144. this.errors.push(
  145. new ModuleError(this, error, {
  146. from: requestShortener.shorten(currentLoader.loader)
  147. })
  148. );
  149. },
  150. // TODO remove in webpack 5
  151. exec: (code, filename) => {
  152. // @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.
  153. const module = new NativeModule(filename, this);
  154. // @ts-ignore _nodeModulePaths is deprecated and undocumented Node.js API
  155. module.paths = NativeModule._nodeModulePaths(this.context);
  156. module.filename = filename;
  157. module._compile(code, filename);
  158. return module.exports;
  159. },
  160. resolve(context, request, callback) {
  161. resolver.resolve({}, context, request, {}, callback);
  162. },
  163. getResolve(options) {
  164. const child = options ? resolver.withOptions(options) : resolver;
  165. return (context, request, callback) => {
  166. if (callback) {
  167. child.resolve({}, context, request, {}, callback);
  168. } else {
  169. return new Promise((resolve, reject) => {
  170. child.resolve({}, context, request, {}, (err, result) => {
  171. if (err) reject(err);
  172. else resolve(result);
  173. });
  174. });
  175. }
  176. };
  177. },
  178. emitFile: (name, content, sourceMap) => {
  179. if (!this.buildInfo.assets) {
  180. this.buildInfo.assets = Object.create(null);
  181. }
  182. this.buildInfo.assets[name] = this.createSourceForAsset(
  183. name,
  184. content,
  185. sourceMap
  186. );
  187. },
  188. rootContext: options.context,
  189. webpack: true,
  190. sourceMap: !!this.useSourceMap,
  191. _module: this,
  192. _compilation: compilation,
  193. _compiler: compilation.compiler,
  194. fs: fs
  195. };
  196. compilation.hooks.normalModuleLoader.call(loaderContext, this);
  197. if (options.loader) {
  198. Object.assign(loaderContext, options.loader);
  199. }
  200. return loaderContext;
  201. }
  202. getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
  203. if (
  204. this.loaders &&
  205. this.loaders.length &&
  206. index < this.loaders.length &&
  207. index >= 0 &&
  208. this.loaders[index]
  209. ) {
  210. return this.loaders[index];
  211. }
  212. return null;
  213. }
  214. createSource(source, resourceBuffer, sourceMap) {
  215. // if there is no identifier return raw source
  216. if (!this.identifier) {
  217. return new RawSource(source);
  218. }
  219. // from here on we assume we have an identifier
  220. const identifier = this.identifier();
  221. if (this.lineToLine && resourceBuffer) {
  222. return new LineToLineMappedSource(
  223. source,
  224. identifier,
  225. asString(resourceBuffer)
  226. );
  227. }
  228. if (this.useSourceMap && sourceMap) {
  229. return new SourceMapSource(source, identifier, sourceMap);
  230. }
  231. if (Buffer.isBuffer(source)) {
  232. // @ts-ignore
  233. // TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
  234. return new RawSource(source);
  235. }
  236. return new OriginalSource(source, identifier);
  237. }
  238. doBuild(options, compilation, resolver, fs, callback) {
  239. const loaderContext = this.createLoaderContext(
  240. resolver,
  241. options,
  242. compilation,
  243. fs
  244. );
  245. runLoaders(
  246. {
  247. resource: this.resource,
  248. loaders: this.loaders,
  249. context: loaderContext,
  250. readResource: fs.readFile.bind(fs)
  251. },
  252. (err, result) => {
  253. if (result) {
  254. this.buildInfo.cacheable = result.cacheable;
  255. this.buildInfo.fileDependencies = new Set(result.fileDependencies);
  256. this.buildInfo.contextDependencies = new Set(
  257. result.contextDependencies
  258. );
  259. }
  260. if (err) {
  261. if (!(err instanceof Error)) {
  262. err = new NonErrorEmittedError(err);
  263. }
  264. const currentLoader = this.getCurrentLoader(loaderContext);
  265. const error = new ModuleBuildError(this, err, {
  266. from:
  267. currentLoader &&
  268. compilation.runtimeTemplate.requestShortener.shorten(
  269. currentLoader.loader
  270. )
  271. });
  272. return callback(error);
  273. }
  274. const resourceBuffer = result.resourceBuffer;
  275. const source = result.result[0];
  276. const sourceMap = result.result.length >= 1 ? result.result[1] : null;
  277. const extraInfo = result.result.length >= 2 ? result.result[2] : null;
  278. if (!Buffer.isBuffer(source) && typeof source !== "string") {
  279. const currentLoader = this.getCurrentLoader(loaderContext, 0);
  280. const err = new Error(
  281. `Final loader (${
  282. currentLoader
  283. ? compilation.runtimeTemplate.requestShortener.shorten(
  284. currentLoader.loader
  285. )
  286. : "unknown"
  287. }) didn't return a Buffer or String`
  288. );
  289. const error = new ModuleBuildError(this, err);
  290. return callback(error);
  291. }
  292. this._source = this.createSource(
  293. this.binary ? asBuffer(source) : asString(source),
  294. resourceBuffer,
  295. sourceMap
  296. );
  297. this._ast =
  298. typeof extraInfo === "object" &&
  299. extraInfo !== null &&
  300. extraInfo.webpackAST !== undefined
  301. ? extraInfo.webpackAST
  302. : null;
  303. return callback();
  304. }
  305. );
  306. }
  307. markModuleAsErrored(error) {
  308. // Restore build meta from successful build to keep importing state
  309. this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta);
  310. this.error = error;
  311. this.errors.push(this.error);
  312. this._source = new RawSource(
  313. "throw new Error(" + JSON.stringify(this.error.message) + ");"
  314. );
  315. this._ast = null;
  316. }
  317. applyNoParseRule(rule, content) {
  318. // must start with "rule" if rule is a string
  319. if (typeof rule === "string") {
  320. return content.indexOf(rule) === 0;
  321. }
  322. if (typeof rule === "function") {
  323. return rule(content);
  324. }
  325. // we assume rule is a regexp
  326. return rule.test(content);
  327. }
  328. // check if module should not be parsed
  329. // returns "true" if the module should !not! be parsed
  330. // returns "false" if the module !must! be parsed
  331. shouldPreventParsing(noParseRule, request) {
  332. // if no noParseRule exists, return false
  333. // the module !must! be parsed.
  334. if (!noParseRule) {
  335. return false;
  336. }
  337. // we only have one rule to check
  338. if (!Array.isArray(noParseRule)) {
  339. // returns "true" if the module is !not! to be parsed
  340. return this.applyNoParseRule(noParseRule, request);
  341. }
  342. for (let i = 0; i < noParseRule.length; i++) {
  343. const rule = noParseRule[i];
  344. // early exit on first truthy match
  345. // this module is !not! to be parsed
  346. if (this.applyNoParseRule(rule, request)) {
  347. return true;
  348. }
  349. }
  350. // no match found, so this module !should! be parsed
  351. return false;
  352. }
  353. _initBuildHash(compilation) {
  354. const hash = createHash(compilation.outputOptions.hashFunction);
  355. if (this._source) {
  356. hash.update("source");
  357. this._source.updateHash(hash);
  358. }
  359. hash.update("meta");
  360. hash.update(JSON.stringify(this.buildMeta));
  361. this._buildHash = hash.digest("hex");
  362. }
  363. build(options, compilation, resolver, fs, callback) {
  364. this.buildTimestamp = Date.now();
  365. this.built = true;
  366. this._source = null;
  367. this._ast = null;
  368. this._buildHash = "";
  369. this.error = null;
  370. this.errors.length = 0;
  371. this.warnings.length = 0;
  372. this.buildMeta = {};
  373. this.buildInfo = {
  374. cacheable: false,
  375. fileDependencies: new Set(),
  376. contextDependencies: new Set()
  377. };
  378. return this.doBuild(options, compilation, resolver, fs, err => {
  379. this._cachedSources.clear();
  380. // if we have an error mark module as failed and exit
  381. if (err) {
  382. this.markModuleAsErrored(err);
  383. this._initBuildHash(compilation);
  384. return callback();
  385. }
  386. // check if this module should !not! be parsed.
  387. // if so, exit here;
  388. const noParseRule = options.module && options.module.noParse;
  389. if (this.shouldPreventParsing(noParseRule, this.request)) {
  390. this._initBuildHash(compilation);
  391. return callback();
  392. }
  393. const handleParseError = e => {
  394. const source = this._source.source();
  395. const error = new ModuleParseError(this, source, e);
  396. this.markModuleAsErrored(error);
  397. this._initBuildHash(compilation);
  398. return callback();
  399. };
  400. const handleParseResult = result => {
  401. this._lastSuccessfulBuildMeta = this.buildMeta;
  402. this._initBuildHash(compilation);
  403. return callback();
  404. };
  405. try {
  406. const result = this.parser.parse(
  407. this._ast || this._source.source(),
  408. {
  409. current: this,
  410. module: this,
  411. compilation: compilation,
  412. options: options
  413. },
  414. (err, result) => {
  415. if (err) {
  416. handleParseError(err);
  417. } else {
  418. handleParseResult(result);
  419. }
  420. }
  421. );
  422. if (result !== undefined) {
  423. // parse is sync
  424. handleParseResult(result);
  425. }
  426. } catch (e) {
  427. handleParseError(e);
  428. }
  429. });
  430. }
  431. getHashDigest(dependencyTemplates) {
  432. // TODO webpack 5 refactor
  433. let dtHash = dependencyTemplates.get("hash");
  434. return `${this.hash}-${dtHash}`;
  435. }
  436. source(dependencyTemplates, runtimeTemplate, type = "javascript") {
  437. const hashDigest = this.getHashDigest(dependencyTemplates);
  438. const cacheEntry = this._cachedSources.get(type);
  439. if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
  440. // We can reuse the cached source
  441. return cacheEntry.source;
  442. }
  443. const source = this.generator.generate(
  444. this,
  445. dependencyTemplates,
  446. runtimeTemplate,
  447. type
  448. );
  449. const cachedSource = new CachedSource(source);
  450. this._cachedSources.set(type, {
  451. source: cachedSource,
  452. hash: hashDigest
  453. });
  454. return cachedSource;
  455. }
  456. originalSource() {
  457. return this._source;
  458. }
  459. needRebuild(fileTimestamps, contextTimestamps) {
  460. // always try to rebuild in case of an error
  461. if (this.error) return true;
  462. // always rebuild when module is not cacheable
  463. if (!this.buildInfo.cacheable) return true;
  464. // Check timestamps of all dependencies
  465. // Missing timestamp -> need rebuild
  466. // Timestamp bigger than buildTimestamp -> need rebuild
  467. for (const file of this.buildInfo.fileDependencies) {
  468. const timestamp = fileTimestamps.get(file);
  469. if (!timestamp) return true;
  470. if (timestamp >= this.buildTimestamp) return true;
  471. }
  472. for (const file of this.buildInfo.contextDependencies) {
  473. const timestamp = contextTimestamps.get(file);
  474. if (!timestamp) return true;
  475. if (timestamp >= this.buildTimestamp) return true;
  476. }
  477. // elsewise -> no rebuild needed
  478. return false;
  479. }
  480. size() {
  481. return this._source ? this._source.size() : -1;
  482. }
  483. /**
  484. * @param {Hash} hash the hash used to track dependencies
  485. * @returns {void}
  486. */
  487. updateHash(hash) {
  488. hash.update(this._buildHash);
  489. super.updateHash(hash);
  490. }
  491. }
  492. module.exports = NormalModule;