-
Notifications
You must be signed in to change notification settings - Fork 367
/
output.ts
98 lines (86 loc) · 3 KB
/
output.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
import { posix } from 'path';
import type {
BuilderContext,
SharedBuilderPluginAPI,
SharedNormalizedConfig,
} from '../types';
import { getDistPath, getFilename } from '../fs';
import { DEFAULT_PORT } from '../constants';
import { addTrailingSlash } from '../utils';
import { DEFAULT_DEV_HOST } from '@modern-js/utils';
export function applyBuilderOutputPlugin(api: SharedBuilderPluginAPI) {
api.modifyBundlerChain(
async (chain, { isProd, isServer, isServiceWorker }) => {
const config = api.getNormalizedConfig();
const jsPath = getDistPath(config.output, 'js');
const publicPath = getPublicPath({
config,
isProd,
context: api.context,
});
// js output
const jsFilename = getFilename(config.output, 'js', isProd);
chain.output
.path(api.context.distPath)
.filename(posix.join(jsPath, jsFilename))
.chunkFilename(posix.join(jsPath, `async/${jsFilename}`))
.publicPath(publicPath)
// disable pathinfo to improve compile performance
// the path info is useless in most cases
// see: https://webpack.js.org/guides/build-performance/#output-without-path-info
.pathinfo(false)
// since webpack v5.54.0+, hashFunction supports xxhash64 as a faster algorithm
// which will be used as default when experiments.futureDefaults is enabled.
.hashFunction('xxhash64');
if (isServer) {
const serverPath = getDistPath(config.output, 'server');
const filename = posix.join(serverPath, `[name].js`);
chain.output
.filename(filename)
.chunkFilename(filename)
.libraryTarget('commonjs2');
}
if (isServiceWorker) {
const workerPath = getDistPath(config.output, 'worker');
const filename = posix.join(workerPath, `[name].js`);
chain.output
.filename(filename)
.chunkFilename(filename)
.libraryTarget('commonjs2');
}
},
);
}
function getPublicPath({
config,
isProd,
context,
}: {
config: SharedNormalizedConfig;
isProd: boolean;
context: BuilderContext;
}) {
const { dev, output } = config;
let publicPath = '/';
if (isProd) {
if (output.assetPrefix) {
publicPath = output.assetPrefix;
}
} else if (typeof dev.assetPrefix === 'string') {
publicPath = dev.assetPrefix;
} else if (dev.assetPrefix === true) {
const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST;
const port = context.devServer?.port || DEFAULT_PORT;
if (hostname === DEFAULT_DEV_HOST) {
const localHostname = `127.0.0.1`;
// If user not specify the hostname, it would use 0.0.0.0
// The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`;
// Relative to docs:
// - https://github.com/quarkusio/quarkus/issues/12246
publicPath = `//${localHostname}:${port}/`;
} else {
publicPath = `//${hostname}:${port}/`;
}
}
return addTrailingSlash(publicPath);
}