1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const ORIGINAL_TEST = /\.(png|jpe?g|gif|svg|webp)$/i;
|
7 | const REPLACEMENT_TEST = /\.(png|jpe?g|gif|webp)$/i;
|
8 |
|
9 | module.exports = function (options) {
|
10 | this.extendBuild(setup);
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function setup(config) {
|
20 | const rules = config.module.rules;
|
21 |
|
22 |
|
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 |
|
52 |
|
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);
|
85 | }
|
86 |
|
87 | module.exports.meta = require("../package.json");
|