WorkerPlugin.js 18 KB

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