-
Notifications
You must be signed in to change notification settings - Fork 34
/
base.ts
214 lines (201 loc) · 7.68 KB
/
base.ts
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
206
207
208
209
210
211
212
213
214
import path from 'path';
import {existsSync} from 'fs';
import * as crypto from 'crypto';
import fs from 'fs/promises';
import {map} from 'ramda';
import {compact, dirFromImportMeta, resolveSync, findGitRoot, pMap} from '@reskript/core';
import {paramCase} from 'change-case';
import webpack, {EntryObject} from 'webpack';
import ResolveTypeScriptPlugin from 'resolve-typescript-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import ESLintPlugin from 'eslint-webpack-plugin';
import StyleLintPlugin from 'stylelint-webpack-plugin';
import InterpolateHTMLPlugin from '@reskript/webpack-plugin-interpolate-html';
import {getScriptLintBaseConfig, getStyleLintBaseConfig} from '@reskript/config-lint';
import {ConfigurationFactory, BuildContext} from '../interface.js';
import {createHTMLPluginInstances} from '../utils/html.js';
import {convertToWebpackEntry} from '../utils/entry.js';
import * as rules from '../rules/index.js';
const {DefinePlugin, ContextReplacementPlugin} = webpack;
const toDefines = (context: Record<string, any>, prefix: string): Record<string, string> => {
const entries = Object.entries(context);
const defines = entries.map(([key, value]) => [prefix + '.' + key, JSON.stringify(value)]);
return defines.reduce((output, [key, value]) => Object.assign(output, {[key]: value}), {});
};
const readFileIfExists = async (filename: string) => {
if (existsSync(filename)) {
const buffer = await fs.readFile(filename);
return buffer;
}
return Buffer.from('');
};
const updateHashFromFile = async (hash: crypto.Hash, filename: string): Promise<boolean> => {
if (existsSync(filename)) {
const buffer = await fs.readFile(filename);
hash.update(buffer);
return true;
}
return false;
};
const computeCacheKey = async (entry: BuildContext): Promise<string> => {
const hash = crypto.createHash('sha1');
hash.update(entry.usage);
hash.update(entry.mode);
hash.update(entry.hostPackageName);
hash.update(entry.cwd);
// `reSKRipt`自己的版本信息等
await updateHashFromFile(hash, path.join(dirFromImportMeta(import.meta.url), '..', '..', 'package.json'));
const hasTypeScriptSetting = await updateHashFromFile(hash, path.join(entry.cwd, 'reskript.config.ts'));
const hasJavaScriptSetting = await updateHashFromFile(hash, path.join(entry.cwd, 'reskript.config.mjs'));
if (!hasTypeScriptSetting && !hasJavaScriptSetting) {
hash.update(JSON.stringify(entry.projectSettings));
hash.update(entry.projectSettings.build.script.finalize.toString());
hash.update(entry.projectSettings.build.finalize.toString());
if (entry.usage === 'devServer') {
hash.update(entry.projectSettings.devServer.finalize.toString());
}
}
const gitRoot = await findGitRoot(entry.cwd) ?? entry.cwd;
const hashIncludes = [
path.join(gitRoot, 'node_modules', '.yarn-integrity'),
path.join(gitRoot, 'package-lock.json'),
path.join(gitRoot, 'pnpm-lock.yaml'),
// `package.json`里可能会有`browsers`之类的配置,所以不能只认lock文件
path.join(gitRoot, 'package.json'),
path.join(gitRoot, '.browserslistrc'),
];
const hashParts = await pMap(hashIncludes, readFileIfExists);
for (const part of hashParts) {
hash.update(part);
}
const key = hash.digest('hex');
return key;
};
const toDynamicDefines = (context: Record<string, any>, prefix: string): Record<string, any> => {
const staticDefines = toDefines(context, prefix);
return map(v => DefinePlugin.runtimeValue(() => v, true), staticDefines);
};
// eslint-disable-next-line complexity
const factory: ConfigurationFactory = async entry => {
const {
usage,
cwd,
srcDirectory,
hostPackageName,
mode,
features,
buildTarget,
buildVersion,
buildTime,
entries,
cache = 'persist',
cacheDirectory,
projectSettings: {
build: {
publicPath,
thirdParty,
reportLintErrors,
style: {
extract,
},
},
},
} = entry;
const buildInfo = {
mode,
version: buildVersion,
target: buildTarget,
time: buildTime,
};
const defines = {
...toDynamicDefines(process.env, 'process.env'),
...toDynamicDefines(features, '$features'),
...toDynamicDefines(buildInfo, '$build'),
};
const eslintOptions = {
eslintPath: resolveSync('eslint'),
baseConfig: getScriptLintBaseConfig({cwd}),
exclude: ['node_modules', 'externals'],
extensions: ['js', 'cjs', 'mjs', 'jsx', 'ts', 'tsx'],
emitError: true,
emitWarning: usage === 'devServer',
};
const styleLintOptions = {
config: getStyleLintBaseConfig({cwd}),
emitErrors: true,
allowEmptyInput: true,
failOnError: mode === 'production',
files: `${srcDirectory}/**/*.{css,less}`,
};
const htmlPlugins = thirdParty ? [] : createHTMLPluginInstances(entry);
const cssOutput = thirdParty ? 'index.css' : '[name].[contenthash].css';
const plugins = [
...htmlPlugins,
(usage === 'build' && extract) && new MiniCssExtractPlugin({filename: cssOutput}),
new ContextReplacementPlugin(/moment[\\/]locale$/, /^\.\/(en|zh-cn)$/),
new DefinePlugin(defines),
new InterpolateHTMLPlugin(process.env),
// @ts-expect-error
reportLintErrors && usage === 'build' && new ESLintPlugin(eslintOptions),
// @ts-expect-error
reportLintErrors && usage === 'build' && new StyleLintPlugin(styleLintOptions),
];
const cacheKey = await computeCacheKey(entry);
return {
mode,
context: cwd,
entry: entries.reduce(
(webpackEntry, appEntry) => {
const currentWebpackEntry = convertToWebpackEntry(appEntry);
webpackEntry[appEntry.name] = currentWebpackEntry;
return webpackEntry;
},
{} as EntryObject
),
output: {
path: path.join(cwd, 'dist', 'assets'),
filename: '[name].[chunkhash].js',
publicPath: publicPath || '/assets/',
},
module: {
rules: Object.values(rules).map(rule => rule(entry)),
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'],
mainFields: ['browser', 'module', 'main'],
alias: {
'@': path.join(cwd, srcDirectory),
...hostPackageName ? {[hostPackageName]: path.join(cwd, 'src')} : {},
'regenerator-runtime': path.dirname(resolveSync('regenerator-runtime')),
},
plugins: [
new ResolveTypeScriptPlugin(),
],
},
cache: cache === 'off'
? false
: (
cache === 'persist'
? {
type: 'filesystem',
version: cacheKey,
cacheDirectory: cacheDirectory ? path.join(cwd, cacheDirectory) : undefined,
name: `${paramCase(entry.usage)}-${paramCase(entry.mode)}`,
}
: {type: 'memory'}
),
snapshot: {
module: {
timestamp: usage !== 'build',
hash: usage === 'build',
},
resolve: {
timestamp: usage !== 'build',
hash: usage === 'build',
},
},
plugins: compact(plugins),
optimization: {},
};
};
export default factory;