pluginWebpack5.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const qs = require("querystring");
  4. const id = 'vue-loader-plugin';
  5. const NS = 'vue-loader';
  6. const NormalModule = require('webpack/lib/NormalModule');
  7. const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin');
  8. const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin');
  9. const UseEffectRulePlugin = require('webpack/lib/rules/UseEffectRulePlugin');
  10. const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler');
  11. let objectMatcherRulePlugins = [];
  12. try {
  13. const ObjectMatcherRulePlugin = require('webpack/lib/rules/ObjectMatcherRulePlugin');
  14. objectMatcherRulePlugins.push(new ObjectMatcherRulePlugin('assert', 'assertions'), new ObjectMatcherRulePlugin('descriptionData'));
  15. }
  16. catch (e) {
  17. const DescriptionDataMatcherRulePlugin = require('webpack/lib/rules/DescriptionDataMatcherRulePlugin');
  18. objectMatcherRulePlugins.push(new DescriptionDataMatcherRulePlugin());
  19. }
  20. const ruleSetCompiler = new RuleSetCompiler([
  21. new BasicMatcherRulePlugin('test', 'resource'),
  22. new BasicMatcherRulePlugin('mimetype'),
  23. new BasicMatcherRulePlugin('dependency'),
  24. new BasicMatcherRulePlugin('include', 'resource'),
  25. new BasicMatcherRulePlugin('exclude', 'resource', true),
  26. new BasicMatcherRulePlugin('conditions'),
  27. new BasicMatcherRulePlugin('resource'),
  28. new BasicMatcherRulePlugin('resourceQuery'),
  29. new BasicMatcherRulePlugin('resourceFragment'),
  30. new BasicMatcherRulePlugin('realResource'),
  31. new BasicMatcherRulePlugin('issuer'),
  32. new BasicMatcherRulePlugin('compiler'),
  33. ...objectMatcherRulePlugins,
  34. new BasicEffectRulePlugin('type'),
  35. new BasicEffectRulePlugin('sideEffects'),
  36. new BasicEffectRulePlugin('parser'),
  37. new BasicEffectRulePlugin('resolve'),
  38. new BasicEffectRulePlugin('generator'),
  39. new UseEffectRulePlugin(),
  40. ]);
  41. class VueLoaderPlugin {
  42. apply(compiler) {
  43. // @ts-ignore
  44. const normalModule = compiler.webpack.NormalModule || NormalModule;
  45. // add NS marker so that the loader can detect and report missing plugin
  46. compiler.hooks.compilation.tap(id, (compilation) => {
  47. normalModule
  48. .getCompilationHooks(compilation)
  49. .loader.tap(id, (loaderContext) => {
  50. loaderContext[NS] = true;
  51. });
  52. });
  53. const rules = compiler.options.module.rules;
  54. let rawVueRule;
  55. let vueRules = [];
  56. for (const rawRule of rules) {
  57. // skip rules with 'enforce'. eg. rule for eslint-loader
  58. if (rawRule.enforce) {
  59. continue;
  60. }
  61. vueRules = match(rawRule, 'foo.vue');
  62. if (!vueRules.length) {
  63. vueRules = match(rawRule, 'foo.vue.html');
  64. }
  65. if (vueRules.length > 0) {
  66. if (rawRule.oneOf) {
  67. throw new Error(`[VueLoaderPlugin Error] vue-loader currently does not support vue rules with oneOf.`);
  68. }
  69. rawVueRule = rawRule;
  70. break;
  71. }
  72. }
  73. if (!vueRules.length) {
  74. throw new Error(`[VueLoaderPlugin Error] No matching rule for .vue files found.\n` +
  75. `Make sure there is at least one root-level rule that matches .vue or .vue.html files.`);
  76. }
  77. // get the normlized "use" for vue files
  78. const vueUse = vueRules
  79. .filter((rule) => rule.type === 'use')
  80. .map((rule) => rule.value);
  81. // get vue-loader options
  82. const vueLoaderUseIndex = vueUse.findIndex((u) => {
  83. // FIXME: this code logic is incorrect when project paths starts with `vue-loader-something`
  84. return /^vue-loader|(\/|\\|@)vue-loader/.test(u.loader);
  85. });
  86. if (vueLoaderUseIndex < 0) {
  87. throw new Error(`[VueLoaderPlugin Error] No matching use for vue-loader is found.\n` +
  88. `Make sure the rule matching .vue files include vue-loader in its use.`);
  89. }
  90. // make sure vue-loader options has a known ident so that we can share
  91. // options by reference in the template-loader by using a ref query like
  92. // template-loader??vue-loader-options
  93. const vueLoaderUse = vueUse[vueLoaderUseIndex];
  94. const vueLoaderOptions = (vueLoaderUse.options =
  95. vueLoaderUse.options || {});
  96. // for each user rule (expect the vue rule), create a cloned rule
  97. // that targets the corresponding language blocks in *.vue files.
  98. const refs = new Map();
  99. const clonedRules = rules
  100. .filter((r) => r !== rawVueRule)
  101. .map((rawRule) => cloneRule(rawRule, refs, langBlockRuleCheck, langBlockRuleResource));
  102. // fix conflict with config.loader and config.options when using config.use
  103. delete rawVueRule.loader;
  104. delete rawVueRule.options;
  105. rawVueRule.use = vueUse;
  106. // rule for template compiler
  107. const templateCompilerRule = {
  108. loader: require.resolve('./templateLoader'),
  109. resourceQuery: (query) => {
  110. if (!query) {
  111. return false;
  112. }
  113. const parsed = qs.parse(query.slice(1));
  114. return parsed.vue != null && parsed.type === 'template';
  115. },
  116. options: vueLoaderOptions,
  117. };
  118. // for each rule that matches plain .js files, also create a clone and
  119. // match it against the compiled template code inside *.vue files, so that
  120. // compiled vue render functions receive the same treatment as user code
  121. // (mostly babel)
  122. const jsRulesForRenderFn = rules
  123. .filter((r) => r !== rawVueRule &&
  124. (match(r, 'test.js').length > 0 || match(r, 'test.ts').length > 0))
  125. .map((rawRule) => cloneRule(rawRule, refs, jsRuleCheck, jsRuleResource));
  126. // global pitcher (responsible for injecting template compiler loader & CSS
  127. // post loader)
  128. const pitcher = {
  129. loader: require.resolve('./pitcher'),
  130. resourceQuery: (query) => {
  131. if (!query) {
  132. return false;
  133. }
  134. const parsed = qs.parse(query.slice(1));
  135. return parsed.vue != null;
  136. },
  137. };
  138. // replace original rules
  139. compiler.options.module.rules = [
  140. pitcher,
  141. ...jsRulesForRenderFn,
  142. templateCompilerRule,
  143. ...clonedRules,
  144. ...rules,
  145. ];
  146. }
  147. }
  148. VueLoaderPlugin.NS = NS;
  149. const matcherCache = new WeakMap();
  150. function match(rule, fakeFile) {
  151. let ruleSet = matcherCache.get(rule);
  152. if (!ruleSet) {
  153. // skip the `include` check when locating the vue rule
  154. const clonedRawRule = Object.assign({}, rule);
  155. delete clonedRawRule.include;
  156. ruleSet = ruleSetCompiler.compile([clonedRawRule]);
  157. matcherCache.set(rule, ruleSet);
  158. }
  159. return ruleSet.exec({
  160. resource: fakeFile,
  161. });
  162. }
  163. const langBlockRuleCheck = (query, rule) => {
  164. return (query.type === 'custom' || !rule.conditions.length || query.lang != null);
  165. };
  166. const langBlockRuleResource = (query, resource) => `${resource}.${query.lang}`;
  167. const jsRuleCheck = (query) => {
  168. return query.type === 'template';
  169. };
  170. const jsRuleResource = (query, resource) => `${resource}.${query.ts ? `ts` : `js`}`;
  171. let uid = 0;
  172. function cloneRule(rawRule, refs, ruleCheck, ruleResource) {
  173. const compiledRule = ruleSetCompiler.compileRule(`clonedRuleSet-${++uid}`, rawRule, refs);
  174. // do not process rule with enforce
  175. if (!rawRule.enforce) {
  176. const ruleUse = compiledRule.effects
  177. .filter((effect) => effect.type === 'use')
  178. .map((effect) => effect.value);
  179. // fix conflict with config.loader and config.options when using config.use
  180. delete rawRule.loader;
  181. delete rawRule.options;
  182. rawRule.use = ruleUse;
  183. }
  184. let currentResource;
  185. const res = Object.assign(Object.assign({}, rawRule), { resource: (resources) => {
  186. currentResource = resources;
  187. return true;
  188. }, resourceQuery: (query) => {
  189. if (!query) {
  190. return false;
  191. }
  192. const parsed = qs.parse(query.slice(1));
  193. if (parsed.vue == null) {
  194. return false;
  195. }
  196. if (!ruleCheck(parsed, compiledRule)) {
  197. return false;
  198. }
  199. const fakeResourcePath = ruleResource(parsed, currentResource);
  200. for (const condition of compiledRule.conditions) {
  201. // add support for resourceQuery
  202. const request = condition.property === 'resourceQuery' ? query : fakeResourcePath;
  203. if (condition && !condition.fn(request)) {
  204. return false;
  205. }
  206. }
  207. return true;
  208. } });
  209. delete res.test;
  210. if (rawRule.rules) {
  211. res.rules = rawRule.rules.map((rule) => cloneRule(rule, refs, ruleCheck, ruleResource));
  212. }
  213. if (rawRule.oneOf) {
  214. res.oneOf = rawRule.oneOf.map((rule) => cloneRule(rule, refs, ruleCheck, ruleResource));
  215. }
  216. return res;
  217. }
  218. exports.default = VueLoaderPlugin;