UNPKG

3.96 kBJavaScriptView Raw
1'use strict'
2
3const MiniCssExtractPlugin = require('mini-css-extract-plugin')
4const autoprefixer = require('autoprefixer')
5
6/**
7 * Returns the set of loaders for the passed opts including:
8 * babel, sass, svgSprite, svgComponent, file, raw
9 */
10module.exports = ({ flags, paths, sassOptions }) => {
11 const cssLoader = {
12 loader: 'css-loader',
13 options: {
14 sourceMap: true,
15 modules: {
16 mode: 'global',
17 localIdentName: '[name]-[local]--[hash:5]',
18 },
19 },
20 }
21
22 const sassLoader = {
23 loader: 'sass-loader',
24 options: {
25 sourceMap: true,
26 sassOptions,
27 },
28 }
29
30 const postCSSLoader = {
31 loader: 'postcss-loader',
32 options: {
33 sourceMap: true,
34 plugins: [autoprefixer()],
35 },
36 }
37
38 return {
39 // --- 🎉 JS Loader
40 jsLoader: (overrides) => ({
41 test: /\.(jsx?|tsx?)$/,
42 include: paths.jsLoaderIncludes,
43 /**
44 * ## Using Eslint Loader
45 * The `eslint-loader` will run imported modules through eslint first and
46 * surface errors/warnings in the webpack build (These are also picked up by
47 * the webpack-dev-server).
48 *
49 * **DEPENDENCIES**: This package only includes the eslint-loader package,
50 * `eslint` and any packages required to run the eslint rules for a project
51 * must be included by that project. This allows projects to handle
52 * specifying and configuring eslint explicitly as required.
53 */
54 use: [
55 { loader: 'babel-loader', options: { cacheDirectory: true } },
56 { loader: 'eslint-loader' },
57 ],
58 ...overrides,
59 }),
60
61 // --- 📝 MDX Loader
62 mdxLoader: (overrides) => ({
63 test: /\.mdx$/,
64 use: [
65 { loader: 'babel-loader' },
66 {
67 loader: '@mdx-js/loader',
68 options: {
69 rehypePlugins: [],
70 remarkPlugins: [],
71 },
72 },
73 ],
74 ...overrides,
75 }),
76
77 // --- 😍 Styles Loader
78 sassLoader: (overrides) => ({
79 test: /\.scss$/,
80 use: flags.production
81 ? // ℹī¸ Prod styles are run through Autoprefixer for browser compatability
82 // and extracted into a single separate stylesheet
83 [MiniCssExtractPlugin.loader, cssLoader, postCSSLoader, sassLoader]
84 : // ℹī¸ Dev styles are injected into the DOM
85 [{ loader: 'style-loader' }, cssLoader, sassLoader],
86 ...overrides,
87 }),
88
89 // --- đŸ“Ļ SVG icon sprite loader
90 // Create an svg sprite with any icons imported into app
91 svgSpriteLoader: (overrides) => ({
92 test: /\.svg$/,
93 include: paths.iconSpriteIncludes,
94 use: [{ loader: 'svg-symbol-sprite-loader' }],
95 ...overrides,
96 }),
97
98 // --- 👾 SVG to React Loader
99 // Imported SVGs are converted to React components
100 svgComponentLoader: (overrides) => ({
101 test: /\.svg$/,
102 // Make sure that we don't try to use with icons for svg sprite
103 exclude: paths.iconSpriteIncludes,
104 use: [
105 {
106 loader: '@svgr/webpack',
107 // For some reason svgr configures svgo to strip out `viewbox` attrs
108 // which makes it impossible to scale svgs... so fix that
109 options: {
110 svgo: true,
111 svgoConfig: {
112 plugins: [{ removeViewBox: false }],
113 },
114 },
115 },
116 ],
117 ...overrides,
118 }),
119
120 // --- đŸ–ŧ Images Loader
121 // Basic image loader setup with file name hashing
122 fileLoader: (overrides) => ({
123 test: /\.(jpe?g|png|gif)$/i,
124 use: [
125 {
126 loader: 'file-loader',
127 options: {
128 name: 'static/media/[name].[hash:8].[ext]',
129 },
130 },
131 ],
132 ...overrides,
133 }),
134
135 // --- 📝 Text files Loader
136 // If you want to import a text file you can ¯\_(ツ)_/¯
137 rawLoader: (overrides) => ({
138 test: /\.txt$/,
139 use: [{ loader: 'raw-loader' }],
140 ...overrides,
141 }),
142 }
143}