UNPKG

2.21 kBJavaScriptView Raw
1/**
2 * This is the original RegExp cloned from the original Nuxt.js configuration
3 * files, with only the search for ".svg" files removed. Keep tabs on this in
4 * case the core decides to add additional qualifiers to the pattern.
5 */
6const ORIGINAL_TEST = /\.(png|jpe?g|gif|svg|webp)$/i;
7const REPLACEMENT_TEST = /\.(png|jpe?g|gif|webp)$/i;
8
9module.exports = function (options) {
10 this.extendBuild(setup);
11};
12
13/**
14 * Perform the primary setup for the nuxt-svg module by removing and replacing
15 * all of the rules that match ".svg" with the new one.
16 *
17 * @param config The webpack configuration object to extend
18 */
19function setup(config) {
20 const rules = config.module.rules;
21
22 // Remove any original svg rules.
23 const svgRules = rules.filter((rule) => rule.test.test(".svg"));
24
25 for (const rule of svgRules) {
26 if (
27 rule.test.source !== ORIGINAL_TEST.source &&
28 rule.test.source !== REPLACEMENT_TEST.source
29 ) {
30 throw new Error(
31 "nuxt-svg: Unexpected '.svg' rule in the webpack configuration"
32 );
33 }
34 rule.test = REPLACEMENT_TEST;
35 }
36
37 const vueSvgLoader = [
38 {
39 loader: "vue-svg-loader",
40 options: { svgo: false },
41 },
42 ];
43
44 if (config.name !== "server") {
45 const jsxRule = config.module.rules.find((r) => r.test.test(".jsx"));
46 const babelLoader = jsxRule.use[jsxRule.use.length - 1];
47 vueSvgLoader.unshift(babelLoader);
48 }
49
50 /**
51 * Create the custom rule that supports multiple resource queries. By default,
52 * use file-loader (no resource query supplied).
53 */
54 const rule = {
55 test: /\.svg$/i,
56 oneOf: [
57 {
58 resourceQuery: /inline/,
59 use: vueSvgLoader,
60 },
61 {
62 resourceQuery: /data/,
63 use: {
64 loader: "url-loader",
65 options: { esModule: false },
66 },
67 },
68 {
69 resourceQuery: /raw/,
70 use: {
71 loader: "raw-loader",
72 options: { esModule: false },
73 },
74 },
75 {
76 use: {
77 loader: "file-loader",
78 options: { esModule: false },
79 },
80 },
81 ],
82 };
83
84 rules.push(rule); // Add the rule to the configuration.
85}
86
87module.exports.meta = require("../package.json");