-
Notifications
You must be signed in to change notification settings - Fork 34
/
webpack.ts
63 lines (61 loc) · 2.73 KB
/
webpack.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
import path from 'path';
import webpack from 'webpack';
import {reject, isNil} from 'ramda';
import {dirFromImportMeta} from '@reskript/core';
import {createWebpackConfig as createBaseWebpackConfig, BuildContext} from '@reskript/config-webpack';
import * as loaders from '@reskript/config-webpack/loaders';
import {createWebpackDevServerPartial} from '@reskript/config-webpack-dev-server';
import {resolveComponentName} from './utils/path.js';
import {PlayCommandLineArgs} from './interface.js';
import {resolveHost} from './utils/host.js';
const currentDirectory = dirFromImportMeta(import.meta.url);
export const createWebpackConfig = async (target: string, cmd: PlayCommandLineArgs, buildContext: BuildContext) => {
const hostType = await resolveHost(cmd.host);
const extra = await createWebpackDevServerPartial(buildContext, hostType);
const baseConfig = await createBaseWebpackConfig(buildContext, {extras: [extra]});
const enableConcurrentMode = cmd.concurrentMode ?? buildContext.projectSettings.play.defaultEnableConcurrentMode;
const playEntryPath = enableConcurrentMode
? path.join(currentDirectory, 'assets', 'playground-entry-cm.js.tpl')
: path.join(currentDirectory, 'assets', 'playground-entry.js.tpl');
const componentTypeName = resolveComponentName(target);
const entryLoaders = [
await loaders.babel(buildContext),
{
loader: path.join(currentDirectory, 'loader'),
options: {
...buildContext.projectSettings.play,
componentTypeName,
cwd: buildContext.cwd,
componentModulePath: path.resolve(buildContext.cwd, target),
globalSetupModulePath: cmd.setup
? path.resolve(cmd.cwd, cmd.setup)
: buildContext.projectSettings.play.defaultGlobalSetup,
},
},
];
const config: webpack.Configuration = {
...baseConfig,
entry: {
index: playEntryPath,
},
module: {
rules: [
{
test: playEntryPath,
use: reject(isNil, entryLoaders),
},
...(baseConfig.module?.rules ?? []),
],
},
resolve: {
...baseConfig.resolve,
fallback: {
...baseConfig.resolve?.fallback,
// React要从`17.0.3`开始才会有`exports`配置,这之前的版本在ESM下是必须加`.js`后续的。
// 利用这个`resolve.fallback`,可以在找不到`react/jsx-runtime`时跳到`react/jsx-runtime.js`上去,兼容旧版本。
'react/jsx-runtime': 'react/jsx-runtime.js',
},
},
};
return config;
};