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.

582 lines
15 KiB

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