-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
150 lines (138 loc) · 3.28 KB
/
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
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
import { join, resolve, dirname } from 'path';
import fs from 'fs';
import CopyPlugin from "copy-webpack-plugin";
import ExtraWatchWebpackPlugin from 'extra-watch-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { fileURLToPath } from 'url';
import {
getComponents,
getComponentsFromNpmStart
} from './utils.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const args = getComponentsFromNpmStart();
export default function (config) {
const DIST_DIR = config.dist || 'dist';
const src = config.src || 'src';
const components = getComponents(src);
const inputs = [];
components.forEach(({ input, examples }) => {
inputs.push(resolve('./', input));
examples.forEach(({ exampleInput }) => {
inputs.push(resolve('./', exampleInput));
});
});
const entries = [];
const mode = config.mode || 'development';
if (config.before) {
config.before(components, args, mode);
}
function addEntries(input, name) {
entries.push({
mode: mode,
entry: input,
module: {
rules: [
{
test: /\.(css|html)$/i,
use: 'raw-loader',
},
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: `${name}.js`,
path: resolve('./', DIST_DIR),
},
performance: {
hints: false
},
stats: {
preset: 'errors-warnings',
},
infrastructureLogging: {
level: 'warn',
},
});
return entries[entries.length - 1];
}
// Individual *.js for each component (very slow)
if (mode === 'production') {
components.forEach(({ input, name }) => {
addEntries(`./${input}`, name);
});
}
// main.js Entry
const mainEntry = addEntries(inputs, 'main');
// Output Basic Runtime
mainEntry.plugins = [];
if (config.copy) {
mainEntry.plugins.push(
new CopyPlugin({
patterns: config.copy
})
);
};
const favicon = config.favicon || 'favicon.svg';
const favFile = fs.existsSync(join(src, favicon))
? join(src, favicon)
: join(__dirname, 'default', 'favicon.svg');
mainEntry.plugins.push(
new CopyPlugin({
patterns: [
{
from: favFile,
to: `favicon.svg`
}
]
})
);
mainEntry.plugins.push(
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
if (config.after) {
config.after(components, args, mode);
}
});
}
}
);
mainEntry.plugins.push(
new ExtraWatchWebpackPlugin({
files: config.watch || []
}),
);
if (config.index) {
mainEntry.plugins.push(
new HtmlWebpackPlugin({
templateContent: () => {
return config.index(components, args, mode);
},
})
);
} else {
mainEntry.plugins.push(
new HtmlWebpackPlugin({
template: `./${src}/index.html`,
})
);
}
mainEntry.devServer = {
static: {
directory: join('./', DIST_DIR)
},
compress: true,
port: config.port || 3000,
client: {
logging: 'warn',
},
};
return entries;
};