NormalModule.js 14 KB

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