-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 2.11 KB
/
index.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
const path = require("path");
const { addBeforeLoader, loaderByName, getLoader, throwUnexpectedConfigError } = require("@craco/craco");
// Detect watch
// <https://github.com/purescript/spago#get-started-from-scratch-with-webpack-frontend-projects>
const isWatch = process.argv.some((a) => a === "--watch");
const isWebpackDevServer = process.argv.some(
(a) => path.basename(a) === "webpack-dev-server"
);
const throwError = (message, githubIssueQuery) =>
throwUnexpectedConfigError({
packageName: "craco-purescript-loader",
githubRepo: "andys8/craco-purescript-loader",
message,
githubIssueQuery,
});
module.exports = {
overrideWebpackConfig: ({ webpackConfig, pluginOptions }) => {
// Resolve purescript extension
if (
!webpackConfig ||
!webpackConfig.resolve ||
!webpackConfig.resolve.extensions ||
typeof webpackConfig.resolve.extensions !== "object"
) {
throwError("No valid webpackConfig.resolve.extensions");
}
webpackConfig.resolve.extensions.push(".purs");
// Allow imports outside of `src` folder for purescript dependencies
if (
!webpackConfig ||
!webpackConfig.resolve ||
!webpackConfig.resolve.plugins ||
typeof webpackConfig.resolve.plugins !== "object"
) {
throwError("No valid webpackConfig.resolve.plugins");
}
webpackConfig.resolve.plugins = webpackConfig.resolve.plugins.filter(
({ constructor }) =>
!constructor || constructor.name !== "ModuleScopePlugin"
);
// PureScript loader
const defaultOptions = {
spago: true,
watch: isWebpackDevServer || isWatch,
};
const pursLoader = {
loader: "purs-loader",
test: /\.purs$/,
exclude: /node_modules/,
query: Object.assign({}, defaultOptions, pluginOptions),
};
// Append purs-loader before file-loader
const fileLoader = loaderByName("file-loader");
const { isFound } = getLoader(webpackConfig, fileLoader);
if (!isFound) {
throwError("Didn't find expected 'file-loader'");
}
addBeforeLoader(webpackConfig, fileLoader, pursLoader);
return webpackConfig;
},
};