-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreact.config.js
68 lines (60 loc) · 1.96 KB
/
preact.config.js
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
// @ts-ignore
const preactCliPostCSS = require("preact-cli-postcss")
const PurgecssPlugin = require("purgecss-webpack-plugin")
const glob = require("glob")
// Custom PurgeCSS extractor for special characters in Tailwind's classnames
class TailwindExtractor {
/**
* @param {string} content
*/
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g) || []
}
}
// PurgeCSS webpack plugin
const purgeCssPlugin = new PurgecssPlugin({
paths: glob.sync("./src/**/*.ts"),
extractors: [
{
extractor: TailwindExtractor,
extensions: ["ts", "tsx"],
},
],
})
export default function (config, env, helpers) {
/**
* Function that mutates the original webpack config.
* Supports asynchronous changes when a promise is returned (or it's an async function).
*
* @param {object} config - original webpack config.
* @param {object} env - options passed to the CLI.
* @param {WebpackConfigHelpers} helpers - object with useful helpers for working with the webpack config.
**/
// Use Preact CLI's helpers object to get the babel-loader
const babel = helpers.getLoadersByName(config, "babel-loader")[0].rule
// Update the loader config to include preact-i18nline
babel.loader = [
{
// create an entry for the old loader
loader: babel.loader,
options: babel.options,
},
{
// add the preact-i18nline webpack loader
loader: "preact-i18nline/webpack-loader",
},
]
// remove the old loader options
delete babel.options
// Use postcss.config.js instead of default postCSS config
preactCliPostCSS(config, helpers)
config.module.rules.push({
test: /\.tsx?$/,
loader: require.resolve("ts-loader"),
})
// Run styles through purgeCSS for production only
if (env.production) {
config.plugins.push(purgeCssPlugin)
}
return config
}