-
Notifications
You must be signed in to change notification settings - Fork 38
/
webpack.config.js
206 lines (188 loc) · 8.1 KB
/
webpack.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* global __dirname, require, module*/
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const CopyPlugin = require("copy-webpack-plugin");
const pkg = require('./package.json');
const env = yargs.argv.env; // use --env with webpack 2
const mode = (env === 'build') ? 'production' : 'development';
const { filterSchemaNames } = require('./scripts/lib/utils');
const JSON5 = require('json5');
function getFiles(dir, prefix = '') {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = dirents
.filter(dirent => dirent.name !== '.gitkeep')
.map((dirent) => dirent.isDirectory() ? getFiles(path.resolve(dir, dirent.name), prefix + dirent.name + '/') : prefix + dirent.name);
return Array.prototype.concat(...files);
}
// наполняем объект entry записями о jsx/tsx файлах из директорий типа ds_xxx
// для development:
// 'srv/resources/ds_xxx/File': './src/ds_xxx/File.tsx',
// Для production (build)
// 'ds_xxx/File': './src/ds_xxx/File.tsx',
const entry = {}; // __dirname + '/src/index.js',
const SRC = path.resolve(__dirname, 'src');
const SCHEMA_NAMES = filterSchemaNames(fs.readdirSync(SRC).filter(fileName => fs.statSync(path.resolve(SRC, fileName)).isDirectory()));
SCHEMA_NAMES.forEach(schema_name => {
const files = getFiles(path.resolve(SRC, schema_name)).filter(fileName => fileName.endsWith('.tsx') || fileName.endsWith('.jsx'));
files.forEach(fileName => {
const fileNameWithoutExtension = fileName.replace(/\.[^/.]+$/, '');
console.log(`Register file: /${schema_name}/${fileNameWithoutExtension}`);
if (mode === 'production') {
entry[`${schema_name}/${fileNameWithoutExtension}`] = `./src/${schema_name}/${fileName}`;
} else {
entry[`srv/resources/${schema_name}/${fileNameWithoutExtension}`] = `./src/${schema_name}/${fileName}`;
}
});
});
function json5ToJson(buffer) {
const result = JSON.stringify(JSON5.parse(buffer.toString()), null, 2);
return Buffer.from(result);
}
module.exports = {
mode,
entry,
devtool: 'source-map',
output: {
publicPath: '',
path: path.resolve(__dirname, 'dist'),
hashFunction: 'xxhash64',
filename: '[name].js',
library: pkg.name,
libraryTarget: 'umd',
libraryExport: 'default',
assetModuleFilename: function() {
console.log('___xxx___images/[hash][ext][query]');
return null;
}
},
externals: {
'react': 'react',
'react-dom': 'react-dom',
'classnames': 'classnames',
//'jquery': 'jquery',
'axios': 'axios',
'three': 'three',
'@react-three/fiber': '@react-three/fiber',
'@react-three/drei': '@react-three/drei',
'echarts': 'echarts',
'bi-internal': 'bi-internal',
'bi-internal/font': 'bi-internal/font',
'bi-internal/core': 'bi-internal/core',
'bi-internal/face': 'bi-internal/face',
'bi-internal/root': 'bi-internal/root',
'bi-internal/types': 'bi-internal/types',
'bi-internal/shell': 'bi-internal/shell',
'bi-internal/services': 'bi-internal/services',
'bi-internal/utils': 'bi-internal/utils',
'bi-internal/ds-helpers': 'bi-internal/ds-helpers',
'bi-internal/ui': 'bi-internal/ui',
'bi-internal/_internal': 'bi-internal/_internal',
},
optimization: {
minimize: false, // disables uglify
},
module: {
rules: [
{
test: /(\.jsx|\.js|\.ts|\.tsx)$/,
use: {
loader: 'babel-loader',
},
exclude: /(node_modules|bower_components)/,
},
{
test: /\.s[ac]ss$/i,
use: [
{
loader: 'style-loader', // Creates `style` nodes from JS strings
options: {
insert: function insertToHead(element, options) {
document.head.appendChild(element);
var internalModule = require('bi-internal/_internal');
if (internalModule._registerStyleElement) internalModule._registerStyleElement(element);
},
},
},
'css-loader', // Translates CSS into CommonJS
'sass-loader', // Compiles Sass to CSS
],
},
{
test: /\.css$/i,
use: [
'style-loader', // Creates `style` nodes from JS strings
'css-loader', // Translates CSS into CommonJS
],
},
{
test: /\.(woff(2)?|ttf|eot)$/,
type: 'asset/resource',
generator: {
publicPath: 'srv/resources/',
filename: function(data, assetInfo) {
let resourcePath = data.filename.split(path.sep);
if (resourcePath[0] !== 'src') throw new Error('Cannot get image outside ot src', resourcePath);
resourcePath = resourcePath.slice(1);
const schema_name = resourcePath[0];
if (!schema_name.startsWith('ds_')) throw new Error('Cannot get image outside ot schema', resourcePath);
resourcePath = resourcePath.slice(1);
return path.join(schema_name, ...resourcePath);
},
},
},
{
test: /\.(jpe?g|gif|png|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
esModule: false,
limit: 8192,
name: '[name].[ext]',
publicPath: (url, resourcePath, context) => {
resourcePath = resourcePath.slice(context.length + path.sep.length);
resourcePath = resourcePath.split(path.sep);
if (resourcePath[0] !== 'src') throw new Error('Cannot get image outside ot src', resourcePath);
resourcePath = resourcePath.slice(1);
const schema_name = resourcePath[0];
if (!schema_name.startsWith('ds_')) throw new Error('Cannot get image outside ot schema', resourcePath);
resourcePath = resourcePath.slice(1);
return path.join('srv', 'resources', schema_name, ...resourcePath);
},
outputPath: (url, resourcePath, context) => {
resourcePath = resourcePath.slice(context.length + path.sep.length);
resourcePath = resourcePath.split(path.sep);
if (resourcePath[0] !== 'src') throw new Error('Cannot get image outside ot src', resourcePath);
resourcePath = resourcePath.slice(1);
const schema_name = resourcePath[0];
if (!schema_name.startsWith('ds_')) throw new Error('Cannot get image outside ot schema', resourcePath);
resourcePath = resourcePath.slice(1);
return path.join(schema_name, ...resourcePath);
},
}
}
],
}
],
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./src')],
extensions: ['.json', '.js', '.ts', '.jsx', '.tsx', '.css', '.scss', '.sass'],
},
plugins: [
new CopyPlugin({
// для каждой схемы из зарегистрированных копируем файлы в свою директорию (кроме файлов scss и react)
patterns: SCHEMA_NAMES.map(schema_name => ({
from: path.join('src', schema_name),
to: (mode === 'production') ? schema_name : `srv/resources/${schema_name}`,
filter: f => !(f.endsWith('.tsx') || f.endsWith('.jsx') || f.endsWith('.scss') || f.endsWith('.gitkeep')),
transform: (content, path) => {
if (path.match(/topic\./g) && path.endsWith('.json')) return json5ToJson(content);
return content;
},
noErrorOnMissing: true,
})),
}),
],
};