-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
webpack.common.mjs
156 lines (153 loc) · 4 KB
/
webpack.common.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import path from 'path';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import webpack from 'webpack';
import h from 'hastscript';
import remarkResponsiveTable from './src/remark-plugins/remark-responsive-table/remark-responsive-table.mjs';
import gfm from 'remark-gfm';
import slug from './src/remark-plugins/remark-slug/index.mjs';
import cleanup from './src/remark-plugins/remark-cleanup-readme/index.mjs';
import aside from './src/remark-plugins/remark-custom-asides/index.mjs';
import autolink from 'remark-autolink-headings';
import refractor from 'remark-refractor';
import frontmatter from 'remark-frontmatter';
import { createRequire } from 'module';
import remarkEmoji from 'remark-emoji';
import { fileURLToPath } from 'url';
import remarkRemoveHeadingId from './src/remark-plugins/remark-remove-heading-id/index.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
const mdPlugins = [
gfm,
slug,
remarkResponsiveTable,
remarkEmoji,
cleanup,
[
aside,
{
mapping: {
'T>': 'tip',
'W>': 'warning',
'?>': 'todo',
},
},
],
[
autolink,
{
behavior: 'append',
content() {
return [h('span.header-link')];
},
},
],
refractor,
remarkRemoveHeadingId,
];
export default ({ ssg = false }) => ({
context: path.resolve(__dirname, './src'),
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename],
},
cacheDirectory: path.resolve(__dirname, 'node_modules/.cache/webpack'),
},
resolve: {
extensions: ['.js', '.jsx', '.scss'],
fallback: {
path: require.resolve('path-browserify'),
},
},
module: {
rules: [
{
test: /\.mdx$/,
use: [
'babel-loader',
{
loader: '@mdx-js/loader',
/** @type {import('@mdx-js/loader').Options} */
options: {
remarkPlugins: [...mdPlugins, [frontmatter]],
providerImportSource: path.resolve('./src/mdx-components.js'),
},
},
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
api: 'legacy',
sassOptions: {
includePaths: [path.join('./src/styles/partials')],
},
},
},
],
},
{
test: /\.woff2?$/,
type: 'asset/resource',
generator: {
filename: 'font/[name].[hash][ext][query]',
emit: ssg !== true,
},
},
{
test: /\.(jpg|jpeg|png|ico)$/i,
type: 'asset/resource',
generator: {
filename: '[name].[hash][ext][query]',
emit: ssg !== true,
},
},
{
test: /\.svg$/i,
type: 'asset/resource',
exclude: [path.resolve(__dirname, 'src/styles/icons')],
generator: {
filename: '[name].[hash][ext][query]',
emit: ssg !== true,
},
},
{
test: /\.svg$/i,
use: ['@svgr/webpack'],
include: [path.resolve(__dirname, 'src/styles/icons')],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
experimentalUseImportModule: true,
}),
new webpack.DefinePlugin({
// https://github.com/algolia/algoliasearch-client-javascript/issues/764
'process.env.RESET_APP_DATA_TIMER': JSON.stringify(''), // fix for algoliasearch
}),
],
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/',
filename: '[name].bundle.js',
hashFunction: 'xxhash64',
},
});