-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack-helpers.js
107 lines (90 loc) · 2.71 KB
/
webpack-helpers.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
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
const path = require('path');
const fs = require('fs');
const getDirNames = (_path) =>
!fs.existsSync(_path)
? []
: fs
.readdirSync(_path, { withFileTypes: true })
.filter(Boolean)
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const getFilesNames = (_path) =>
!fs.existsSync(_path)
? []
: fs
.readdirSync(_path, { withFileTypes: true })
.filter(Boolean)
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name);
const makeEntryPointsBySource = (source, extList = [], filterCb = null) => {
if (!fs.existsSync(source)) {
return {};
}
return getFilesNames(source).reduce((res, name) => {
const isRelatedFile = extList.includes(path.parse(name).ext);
const isFilteredFile = filterCb ? filterCb(name) : true;
if (!isRelatedFile || !isFilteredFile) {
return res;
}
const entryKey = path.parse(name).name;
const entryFile = path.resolve(__dirname, source, name);
if (Array.isArray(res[entryKey])) {
res[entryKey].push(entryFile);
} else {
res[entryKey] = [entryFile];
}
return res;
}, {});
};
const makeJsEntryPoints = (source) => makeEntryPointsBySource(source, ['.js']);
const makeTemplatesEntryPoints = (templatesSource) => {
if (!fs.existsSync(templatesSource)) {
return {};
}
return getDirNames(templatesSource)
.filter((name) => name !== 'common')
.reduce((res, dirName) => {
const templateEntries =
makeEntryPointsBySource(
path.resolve(templatesSource, dirName),
['.js', '.scss'],
(name) => name.includes(dirName)
) || {};
if (Object.keys(templateEntries).length) {
Object.assign(res, templateEntries);
}
return res;
}, {});
};
const makeTemplateCopyPluginPattern = (templatePath, nestedDestPath = '/') => ({
from: `${templatePath}/*/*.{liquid,json}`,
to: path.resolve(__dirname, `dist/templates${nestedDestPath}[name][ext]`),
noErrorOnMissing: true,
globOptions: {
ignore: ['.gitkeep'],
},
});
const makeSnippetCopyPluginPattern = (templatePath) => ({
from: `${templatePath}/*/*.liquid`,
to: path.resolve(__dirname, `dist/snippets/[name][ext]`),
noErrorOnMissing: true,
globOptions: {
ignore: ['.gitkeep'],
},
});
const makeSectionCopyPluginPattern = (templatePath) => ({
from: `${templatePath}/*/*/*.liquid`,
to: path.resolve(__dirname, `dist/sections/[name][ext]`),
noErrorOnMissing: true,
globOptions: {
ignore: ['.gitkeep'],
},
});
module.exports = {
makeTemplatesEntryPoints,
makeSnippetCopyPluginPattern,
makeJsEntryPoints,
makeTemplateCopyPluginPattern,
makeSectionCopyPluginPattern,
getDirNames,
};