-
Notifications
You must be signed in to change notification settings - Fork 789
/
Copy pathvalidate-dist.ts
154 lines (131 loc) · 4.54 KB
/
validate-dist.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
import * as d from '../../../declarations';
import { getAbsolutePath } from '../config-utils';
import {
COPY,
DIST_COLLECTION,
DIST_GLOBAL_STYLES,
DIST_LAZY,
DIST_LAZY_LOADER,
DIST_TYPES,
getComponentsDtsTypesFilePath,
isOutputTargetDist,
} from '../../output-targets/output-utils';
import { isAbsolute, join, resolve } from 'path';
import { isBoolean, isString } from '@utils';
import { validateCopy } from '../validate-copy';
export const validateDist = (config: d.Config, userOutputs: d.OutputTarget[]) => {
const distOutputTargets = userOutputs.filter(isOutputTargetDist);
return distOutputTargets.reduce((outputs, o) => {
const distOutputTarget = validateOutputTargetDist(config, o);
outputs.push(distOutputTarget);
const namespace = config.fsNamespace || 'app';
const lazyDir = join(distOutputTarget.buildDir, namespace);
// Lazy build for CDN in dist
outputs.push({
type: DIST_LAZY,
esmDir: lazyDir,
systemDir: config.buildEs5 ? lazyDir : undefined,
systemLoaderFile: config.buildEs5 ? join(lazyDir, namespace + '.js') : undefined,
legacyLoaderFile: join(distOutputTarget.buildDir, namespace + '.js'),
polyfills: distOutputTarget.polyfills !== undefined ? !!distOutputTarget.polyfills : true,
isBrowserBuild: true,
empty: distOutputTarget.empty,
});
outputs.push({
type: COPY,
dir: lazyDir,
copyAssets: 'dist',
});
outputs.push({
type: DIST_GLOBAL_STYLES,
file: join(lazyDir, `${config.fsNamespace}.css`),
});
if (config.buildDist) {
if (distOutputTarget.collectionDir) {
outputs.push({
type: DIST_COLLECTION,
dir: distOutputTarget.dir,
collectionDir: distOutputTarget.collectionDir,
empty: distOutputTarget.empty,
});
outputs.push({
type: COPY,
dir: distOutputTarget.collectionDir,
copyAssets: 'collection',
copy: [...distOutputTarget.copy, { src: '**/*.svg' }, { src: '**/*.js' }],
});
}
outputs.push({
type: DIST_TYPES,
dir: distOutputTarget.dir,
typesDir: distOutputTarget.typesDir,
empty: distOutputTarget.empty,
});
const esmDir = join(distOutputTarget.dir, 'esm');
const esmEs5Dir = config.buildEs5 ? join(distOutputTarget.dir, 'esm-es5') : undefined;
const cjsDir = join(distOutputTarget.dir, 'cjs');
// Create lazy output-target
outputs.push({
type: DIST_LAZY,
esmDir,
esmEs5Dir,
cjsDir,
cjsIndexFile: join(distOutputTarget.dir, 'index.js'),
esmIndexFile: join(distOutputTarget.dir, 'index.mjs'),
polyfills: true,
empty: distOutputTarget.empty,
});
// Create output target that will generate the /loader entry-point
outputs.push({
type: DIST_LAZY_LOADER,
dir: distOutputTarget.esmLoaderPath,
esmDir,
esmEs5Dir,
cjsDir,
componentDts: getComponentsDtsTypesFilePath(distOutputTarget),
empty: distOutputTarget.empty,
});
}
return outputs;
}, []);
};
const validateOutputTargetDist = (config: d.Config, o: d.OutputTargetDist) => {
const outputTarget = {
...o,
dir: getAbsolutePath(config, o.dir || DEFAULT_DIR),
};
if (!isString(outputTarget.buildDir)) {
outputTarget.buildDir = DEFAULT_BUILD_DIR;
}
if (!isAbsolute(outputTarget.buildDir)) {
outputTarget.buildDir = join(outputTarget.dir, outputTarget.buildDir);
}
if (outputTarget.collectionDir === undefined) {
outputTarget.collectionDir = DEFAULT_COLLECTION_DIR;
}
if (outputTarget.collectionDir && !isAbsolute(outputTarget.collectionDir)) {
outputTarget.collectionDir = join(outputTarget.dir, outputTarget.collectionDir);
}
if (!outputTarget.esmLoaderPath) {
outputTarget.esmLoaderPath = DEFAULT_ESM_LOADER_DIR;
}
if (!isAbsolute(outputTarget.esmLoaderPath)) {
outputTarget.esmLoaderPath = resolve(outputTarget.dir, outputTarget.esmLoaderPath);
}
if (!outputTarget.typesDir) {
outputTarget.typesDir = DEFAULT_TYPES_DIR;
}
if (!isAbsolute(outputTarget.typesDir)) {
outputTarget.typesDir = join(outputTarget.dir, outputTarget.typesDir);
}
if (!isBoolean(outputTarget.empty)) {
outputTarget.empty = true;
}
outputTarget.copy = validateCopy(outputTarget.copy, config.copy);
return outputTarget;
};
const DEFAULT_DIR = 'dist';
const DEFAULT_BUILD_DIR = '';
const DEFAULT_COLLECTION_DIR = 'collection';
const DEFAULT_TYPES_DIR = 'types';
const DEFAULT_ESM_LOADER_DIR = 'loader';