forked from swc-project/swc
/
spack.ts
104 lines (82 loc) · 2.27 KB
/
spack.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
import * as path from 'path';
import { Options } from "./types";
export type BundleInput = BundleOptions | BundleOptions[];
export const isLocalFile = /^\.{0,2}\//; // starts with '/' './' '../'
export async function compileBundleOptions(config: BundleInput | string | undefined): Promise<BundleInput> {
const f = config === undefined ? '.' : config;
try {
const filepath = typeof f === 'string' ? f : 'spack.config.js';
const fileModule = isLocalFile.test(filepath) ? path.resolve(filepath) : filepath;
let configFromFile: BundleInput = require(fileModule);
if ((configFromFile as any).default) {
configFromFile = (configFromFile as any).default;
}
if (Array.isArray(configFromFile)) {
if (Array.isArray(f)) {
return [...configFromFile, ...f]
}
if (typeof f !== 'string') {
configFromFile.push(f);
}
return configFromFile;
}
return {
...configFromFile,
...(typeof config === 'string') ? {} : config
};
} catch (e) {
if (typeof f === 'string') {
throw new Error(`Error occurred while loading config file at ${config}: ${e}`);
}
return f;
}
}
/**
* Usage: In `spack.config.js` / `spack.config.ts`, you can utilize type annotations (to get autocompletions) like
*
* ```ts
* import { config } from '@swc/core/spack';
*
* export default config({
* name: 'web',
* });
* ```
*
*
*
*/
export function config(c: BundleInput): BundleInput {
return c
}
export interface BundleOptions extends SpackConfig {
workingDir?: string
}
/**
* `spack.config,js`
*/
export interface SpackConfig {
/**
* @default process.env.NODE_ENV
*/
mode?: Mode
target?: Target
entry: EntryConfig,
output: OutputConfig
module: ModuleConfig
options?: Options
/**
* Modules to exclude from bundle.
*/
externalModules?: string[]
}
export interface OutputConfig {
name: string
path: string
}
export interface ModuleConfig {
}
export type Mode = 'production' | 'development' | 'none';
export type Target = 'browser' | 'node';
export type EntryConfig = string | string[] | {
[name: string]: string
}