10
0

WorkerPlugin.js 17 KB

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