WorkerPlugin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { pathToFileURL } = require("url");
  7. const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
  8. const CommentCompilationWarning = require("../CommentCompilationWarning");
  9. const {
  10. JAVASCRIPT_MODULE_TYPE_AUTO,
  11. JAVASCRIPT_MODULE_TYPE_ESM
  12. } = require("../ModuleTypeConstants");
  13. const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
  14. const EnableChunkLoadingPlugin = require("../javascript/EnableChunkLoadingPlugin");
  15. const { equals } = require("../util/ArrayHelpers");
  16. const createHash = require("../util/createHash");
  17. const { contextify } = require("../util/identifier");
  18. const EnableWasmLoadingPlugin = require("../wasm/EnableWasmLoadingPlugin");
  19. const ConstDependency = require("./ConstDependency");
  20. const CreateScriptUrlDependency = require("./CreateScriptUrlDependency");
  21. const {
  22. harmonySpecifierTag
  23. } = require("./HarmonyImportDependencyParserPlugin");
  24. const WorkerDependency = require("./WorkerDependency");
  25. /** @typedef {import("estree").CallExpression} CallExpression */
  26. /** @typedef {import("estree").Expression} Expression */
  27. /** @typedef {import("estree").MemberExpression} MemberExpression */
  28. /** @typedef {import("estree").ObjectExpression} ObjectExpression */
  29. /** @typedef {import("estree").Pattern} Pattern */
  30. /** @typedef {import("estree").Property} Property */
  31. /** @typedef {import("estree").SpreadElement} SpreadElement */
  32. /** @typedef {import("../../declarations/WebpackOptions").ChunkLoading} ChunkLoading */
  33. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  34. /** @typedef {import("../../declarations/WebpackOptions").OutputModule} OutputModule */
  35. /** @typedef {import("../../declarations/WebpackOptions").WasmLoading} WasmLoading */
  36. /** @typedef {import("../../declarations/WebpackOptions").WorkerPublicPath} WorkerPublicPath */
  37. /** @typedef {import("../Compiler")} Compiler */
  38. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  39. /** @typedef {import("../Entrypoint").EntryOptions} EntryOptions */
  40. /** @typedef {import("../NormalModule")} NormalModule */
  41. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  42. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  43. /** @typedef {import("../javascript/JavascriptParser").JavascriptParserState} JavascriptParserState */
  44. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  45. /** @typedef {import("./HarmonyImportDependencyParserPlugin").HarmonySettings} HarmonySettings */
  46. /**
  47. * Returns url.
  48. * @param {NormalModule} module module
  49. * @returns {string} url
  50. */
  51. const getUrl = (module) => pathToFileURL(module.resource).toString();
  52. const WorkerSpecifierTag = Symbol("worker specifier tag");
  53. const DEFAULT_SYNTAX = [
  54. "Worker",
  55. "SharedWorker",
  56. "navigator.serviceWorker.register()",
  57. "Worker from worker_threads"
  58. ];
  59. /** @type {WeakMap<JavascriptParserState, number>} */
  60. const workerIndexMap = new WeakMap();
  61. const PLUGIN_NAME = "WorkerPlugin";
  62. class WorkerPlugin {
  63. /**
  64. * Creates an instance of WorkerPlugin.
  65. * @param {ChunkLoading=} chunkLoading chunk loading
  66. * @param {WasmLoading=} wasmLoading wasm loading
  67. * @param {OutputModule=} module output module
  68. * @param {WorkerPublicPath=} workerPublicPath worker public path
  69. */
  70. constructor(chunkLoading, wasmLoading, module, workerPublicPath) {
  71. this._chunkLoading = chunkLoading;
  72. this._wasmLoading = wasmLoading;
  73. this._module = module;
  74. this._workerPublicPath = workerPublicPath;
  75. }
  76. /**
  77. * Applies the plugin by registering its hooks on the compiler.
  78. * @param {Compiler} compiler the compiler instance
  79. * @returns {void}
  80. */
  81. apply(compiler) {
  82. if (this._chunkLoading) {
  83. new EnableChunkLoadingPlugin(this._chunkLoading).apply(compiler);
  84. }
  85. if (this._wasmLoading) {
  86. new EnableWasmLoadingPlugin(this._wasmLoading).apply(compiler);
  87. }
  88. const cachedContextify = contextify.bindContextCache(
  89. compiler.context,
  90. compiler.root
  91. );
  92. compiler.hooks.thisCompilation.tap(
  93. PLUGIN_NAME,
  94. (compilation, { normalModuleFactory }) => {
  95. compilation.dependencyFactories.set(
  96. WorkerDependency,
  97. normalModuleFactory
  98. );
  99. compilation.dependencyTemplates.set(
  100. WorkerDependency,
  101. new WorkerDependency.Template()
  102. );
  103. compilation.dependencyTemplates.set(
  104. CreateScriptUrlDependency,
  105. new CreateScriptUrlDependency.Template()
  106. );
  107. /**
  108. * Returns parsed.
  109. * @param {JavascriptParser} parser the parser
  110. * @param {Expression} expr expression
  111. * @returns {[string, Range] | void} parsed
  112. */
  113. const parseModuleUrl = (parser, expr) => {
  114. if (expr.type !== "NewExpression" || expr.callee.type === "Super") {
  115. return;
  116. }
  117. if (
  118. expr.arguments.length === 1 &&
  119. expr.arguments[0].type === "MemberExpression" &&
  120. isMetaUrl(parser, expr.arguments[0])
  121. ) {
  122. const arg1 = expr.arguments[0];
  123. return [
  124. getUrl(parser.state.module),
  125. [
  126. /** @type {Range} */ (arg1.range)[0],
  127. /** @type {Range} */ (arg1.range)[1]
  128. ]
  129. ];
  130. } else if (expr.arguments.length === 2) {
  131. const [arg1, arg2] = expr.arguments;
  132. if (arg1.type === "SpreadElement") return;
  133. if (arg2.type === "SpreadElement") return;
  134. const callee = parser.evaluateExpression(expr.callee);
  135. if (!callee.isIdentifier() || callee.identifier !== "URL") return;
  136. const arg2Value = parser.evaluateExpression(arg2);
  137. if (
  138. !arg2Value.isString() ||
  139. !(
  140. /** @type {string} */ (arg2Value.string).startsWith("file://")
  141. ) ||
  142. arg2Value.string !== getUrl(parser.state.module)
  143. ) {
  144. return;
  145. }
  146. const arg1Value = parser.evaluateExpression(arg1);
  147. if (!arg1Value.isString()) return;
  148. return [
  149. /** @type {string} */ (arg1Value.string),
  150. [
  151. /** @type {Range} */ (arg1.range)[0],
  152. /** @type {Range} */ (arg2.range)[1]
  153. ]
  154. ];
  155. }
  156. };
  157. /**
  158. * Checks whether this worker plugin is meta url.
  159. * @param {JavascriptParser} parser the parser
  160. * @param {MemberExpression} expr expression
  161. * @returns {boolean} is `import.meta.url`
  162. */
  163. const isMetaUrl = (parser, expr) => {
  164. const chain = parser.extractMemberExpressionChain(expr);
  165. if (
  166. chain.members.length !== 1 ||
  167. chain.object.type !== "MetaProperty" ||
  168. chain.object.meta.name !== "import" ||
  169. chain.object.property.name !== "meta" ||
  170. chain.members[0] !== "url"
  171. ) {
  172. return false;
  173. }
  174. return true;
  175. };
  176. /** @typedef {Record<string, EXPECTED_ANY>} Values */
  177. /**
  178. * Parses object expression.
  179. * @param {JavascriptParser} parser the parser
  180. * @param {ObjectExpression} expr expression
  181. * @returns {{ expressions: Record<string, Expression | Pattern>, otherElements: (Property | SpreadElement)[], values: Values, spread: boolean, insertType: "comma" | "single", insertLocation: number }} parsed object
  182. */
  183. const parseObjectExpression = (parser, expr) => {
  184. /** @type {Values} */
  185. const values = {};
  186. /** @type {Record<string, Expression | Pattern>} */
  187. const expressions = {};
  188. /** @type {(Property | SpreadElement)[]} */
  189. const otherElements = [];
  190. let spread = false;
  191. for (const prop of expr.properties) {
  192. if (prop.type === "SpreadElement") {
  193. spread = true;
  194. } else if (
  195. prop.type === "Property" &&
  196. !prop.method &&
  197. !prop.computed &&
  198. prop.key.type === "Identifier"
  199. ) {
  200. expressions[prop.key.name] = prop.value;
  201. if (!prop.shorthand && !prop.value.type.endsWith("Pattern")) {
  202. const value = parser.evaluateExpression(
  203. /** @type {Expression} */
  204. (prop.value)
  205. );
  206. if (value.isCompileTimeValue()) {
  207. values[prop.key.name] = value.asCompileTimeValue();
  208. }
  209. }
  210. } else {
  211. otherElements.push(prop);
  212. }
  213. }
  214. const insertType = expr.properties.length > 0 ? "comma" : "single";
  215. const insertLocation = /** @type {Range} */ (
  216. expr.properties[expr.properties.length - 1].range
  217. )[1];
  218. return {
  219. expressions,
  220. otherElements,
  221. values,
  222. spread,
  223. insertType,
  224. insertLocation
  225. };
  226. };
  227. /**
  228. * Processes the provided parser.
  229. * @param {Parser} parser parser parser
  230. * @param {JavascriptParserOptions} parserOptions parserOptions
  231. * @returns {void}
  232. */
  233. const parserPlugin = (parser, parserOptions) => {
  234. if (parserOptions.worker === false) return;
  235. const options = !Array.isArray(parserOptions.worker)
  236. ? ["..."]
  237. : parserOptions.worker;
  238. /**
  239. * Returns true when handled.
  240. * @param {CallExpression} expr expression
  241. * @returns {boolean | void} true when handled
  242. */
  243. const handleNewWorker = (expr) => {
  244. if (expr.arguments.length === 0 || expr.arguments.length > 2) {
  245. return;
  246. }
  247. const [arg1, arg2] = expr.arguments;
  248. if (arg1.type === "SpreadElement") return;
  249. if (arg2 && arg2.type === "SpreadElement") return;
  250. /** @type {string} */
  251. let url;
  252. /** @type {Range} */
  253. let range;
  254. /** @type {boolean} */
  255. let needNewUrl = false;
  256. if (arg1.type === "MemberExpression" && isMetaUrl(parser, arg1)) {
  257. url = getUrl(parser.state.module);
  258. range = [
  259. /** @type {Range} */ (arg1.range)[0],
  260. /** @type {Range} */ (arg1.range)[1]
  261. ];
  262. needNewUrl = true;
  263. } else {
  264. const parsedUrl = parseModuleUrl(parser, arg1);
  265. if (!parsedUrl) return;
  266. [url, range] = parsedUrl;
  267. }
  268. const {
  269. expressions,
  270. otherElements,
  271. values: options,
  272. spread: hasSpreadInOptions,
  273. insertType,
  274. insertLocation
  275. } = arg2 && arg2.type === "ObjectExpression"
  276. ? parseObjectExpression(parser, arg2)
  277. : {
  278. expressions:
  279. /** @type {Record<string, Expression | Pattern>} */ ({}),
  280. otherElements: [],
  281. /** @type {Values} */
  282. values: {},
  283. spread: false,
  284. insertType: arg2 ? "spread" : "argument",
  285. insertLocation: arg2
  286. ? /** @type {Range} */ (arg2.range)
  287. : /** @type {Range} */ (arg1.range)[1]
  288. };
  289. const { options: importOptions, errors: commentErrors } =
  290. parser.parseCommentOptions(/** @type {Range} */ (expr.range));
  291. if (commentErrors) {
  292. for (const e of commentErrors) {
  293. const { comment } = e;
  294. parser.state.module.addWarning(
  295. new CommentCompilationWarning(
  296. `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
  297. /** @type {DependencyLocation} */ (comment.loc)
  298. )
  299. );
  300. }
  301. }
  302. /** @type {EntryOptions} */
  303. const entryOptions = {};
  304. if (importOptions) {
  305. if (importOptions.webpackIgnore !== undefined) {
  306. if (typeof importOptions.webpackIgnore !== "boolean") {
  307. parser.state.module.addWarning(
  308. new UnsupportedFeatureWarning(
  309. `\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
  310. /** @type {DependencyLocation} */ (expr.loc)
  311. )
  312. );
  313. } else if (importOptions.webpackIgnore) {
  314. return false;
  315. }
  316. }
  317. if (importOptions.webpackEntryOptions !== undefined) {
  318. if (
  319. typeof importOptions.webpackEntryOptions !== "object" ||
  320. importOptions.webpackEntryOptions === null
  321. ) {
  322. parser.state.module.addWarning(
  323. new UnsupportedFeatureWarning(
  324. `\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`,
  325. /** @type {DependencyLocation} */ (expr.loc)
  326. )
  327. );
  328. } else {
  329. Object.assign(
  330. entryOptions,
  331. importOptions.webpackEntryOptions
  332. );
  333. }
  334. }
  335. if (importOptions.webpackChunkName !== undefined) {
  336. if (typeof importOptions.webpackChunkName !== "string") {
  337. parser.state.module.addWarning(
  338. new UnsupportedFeatureWarning(
  339. `\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
  340. /** @type {DependencyLocation} */ (expr.loc)
  341. )
  342. );
  343. } else {
  344. entryOptions.name = importOptions.webpackChunkName;
  345. }
  346. }
  347. }
  348. if (
  349. !Object.prototype.hasOwnProperty.call(entryOptions, "name") &&
  350. options &&
  351. typeof options.name === "string"
  352. ) {
  353. entryOptions.name = options.name;
  354. }
  355. if (entryOptions.runtime === undefined) {
  356. const i = workerIndexMap.get(parser.state) || 0;
  357. workerIndexMap.set(parser.state, i + 1);
  358. const name = `${cachedContextify(
  359. parser.state.module.identifier()
  360. )}|${i}`;
  361. const hash = createHash(compilation.outputOptions.hashFunction);
  362. hash.update(name);
  363. const digest = hash.digest(compilation.outputOptions.hashDigest);
  364. entryOptions.runtime = digest.slice(
  365. 0,
  366. compilation.outputOptions.hashDigestLength
  367. );
  368. }
  369. const block = new AsyncDependenciesBlock({
  370. name: entryOptions.name,
  371. circular: false,
  372. entryOptions: {
  373. chunkLoading: this._chunkLoading,
  374. wasmLoading: this._wasmLoading,
  375. ...entryOptions
  376. }
  377. });
  378. block.loc = expr.loc;
  379. const dep = new WorkerDependency(url, range, {
  380. publicPath: this._workerPublicPath,
  381. needNewUrl
  382. });
  383. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  384. block.addDependency(dep);
  385. parser.state.module.addBlock(block);
  386. if (compilation.outputOptions.trustedTypes) {
  387. const dep = new CreateScriptUrlDependency(
  388. /** @type {Range} */ (expr.arguments[0].range)
  389. );
  390. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  391. parser.state.module.addDependency(dep);
  392. }
  393. if (expressions.type) {
  394. const expr = expressions.type;
  395. if (options.type !== false) {
  396. const dep = new ConstDependency(
  397. this._module ? '"module"' : "undefined",
  398. /** @type {Range} */ (expr.range)
  399. );
  400. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  401. parser.state.module.addPresentationalDependency(dep);
  402. /** @type {EXPECTED_ANY} */
  403. (expressions).type = undefined;
  404. }
  405. } else if (insertType === "comma") {
  406. if (this._module || hasSpreadInOptions) {
  407. const dep = new ConstDependency(
  408. `, type: ${this._module ? '"module"' : "undefined"}`,
  409. insertLocation
  410. );
  411. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  412. parser.state.module.addPresentationalDependency(dep);
  413. }
  414. } else if (insertType === "spread") {
  415. const dep1 = new ConstDependency(
  416. "Object.assign({}, ",
  417. /** @type {Range} */ (insertLocation)[0]
  418. );
  419. const dep2 = new ConstDependency(
  420. `, { type: ${this._module ? '"module"' : "undefined"} })`,
  421. /** @type {Range} */ (insertLocation)[1]
  422. );
  423. dep1.loc = /** @type {DependencyLocation} */ (expr.loc);
  424. dep2.loc = /** @type {DependencyLocation} */ (expr.loc);
  425. parser.state.module.addPresentationalDependency(dep1);
  426. parser.state.module.addPresentationalDependency(dep2);
  427. } else if (insertType === "argument" && this._module) {
  428. const dep = new ConstDependency(
  429. ', { type: "module" }',
  430. insertLocation
  431. );
  432. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  433. parser.state.module.addPresentationalDependency(dep);
  434. }
  435. parser.walkExpression(expr.callee);
  436. for (const key of Object.keys(expressions)) {
  437. if (expressions[key]) {
  438. if (expressions[key].type.endsWith("Pattern")) continue;
  439. parser.walkExpression(
  440. /** @type {Expression} */
  441. (expressions[key])
  442. );
  443. }
  444. }
  445. for (const prop of otherElements) {
  446. parser.walkProperty(prop);
  447. }
  448. if (insertType === "spread") {
  449. parser.walkExpression(arg2);
  450. }
  451. return true;
  452. };
  453. /**
  454. * Processes the provided item.
  455. * @param {string} item item
  456. */
  457. const processItem = (item) => {
  458. if (
  459. item.startsWith("*") &&
  460. item.includes(".") &&
  461. item.endsWith("()")
  462. ) {
  463. const firstDot = item.indexOf(".");
  464. const pattern = item.slice(1, firstDot);
  465. const itemMembers = item.slice(firstDot + 1, -2);
  466. parser.hooks.preDeclarator.tap(
  467. PLUGIN_NAME,
  468. (decl, _statement) => {
  469. if (
  470. decl.id.type === "Identifier" &&
  471. decl.id.name === pattern
  472. ) {
  473. parser.tagVariable(decl.id.name, WorkerSpecifierTag);
  474. return true;
  475. }
  476. }
  477. );
  478. parser.hooks.pattern.for(pattern).tap(PLUGIN_NAME, (pattern) => {
  479. parser.tagVariable(pattern.name, WorkerSpecifierTag);
  480. return true;
  481. });
  482. parser.hooks.callMemberChain
  483. .for(WorkerSpecifierTag)
  484. .tap(PLUGIN_NAME, (expression, members) => {
  485. if (itemMembers !== members.join(".")) {
  486. return;
  487. }
  488. return handleNewWorker(expression);
  489. });
  490. } else if (item.endsWith("()")) {
  491. parser.hooks.call
  492. .for(item.slice(0, -2))
  493. .tap(PLUGIN_NAME, handleNewWorker);
  494. } else {
  495. const match = /^(.+?)(\(\))?\s+from\s+(.+)$/.exec(item);
  496. if (match) {
  497. const ids = match[1].split(".");
  498. const call = match[2];
  499. const source = match[3];
  500. (call ? parser.hooks.call : parser.hooks.new)
  501. .for(harmonySpecifierTag)
  502. .tap(PLUGIN_NAME, (expr) => {
  503. const settings = /** @type {HarmonySettings} */ (
  504. parser.currentTagData
  505. );
  506. if (
  507. !settings ||
  508. settings.source !== source ||
  509. !equals(settings.ids, ids)
  510. ) {
  511. return;
  512. }
  513. return handleNewWorker(expr);
  514. });
  515. } else {
  516. parser.hooks.new.for(item).tap(PLUGIN_NAME, handleNewWorker);
  517. }
  518. }
  519. };
  520. for (const item of options) {
  521. if (item === "...") {
  522. for (const itemFromDefault of DEFAULT_SYNTAX) {
  523. processItem(itemFromDefault);
  524. }
  525. } else {
  526. processItem(item);
  527. }
  528. }
  529. };
  530. normalModuleFactory.hooks.parser
  531. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  532. .tap(PLUGIN_NAME, parserPlugin);
  533. normalModuleFactory.hooks.parser
  534. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  535. .tap(PLUGIN_NAME, parserPlugin);
  536. }
  537. );
  538. }
  539. }
  540. module.exports = WorkerPlugin;