-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (90 loc) · 2.52 KB
/
Copy pathindex.js
File metadata and controls
106 lines (90 loc) · 2.52 KB
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
// @flow
/**
* Webpack block for copy-webpack-plugin
*
* @see https://github.com/kevlened/copy-webpack-plugin
*/
import CopyWebpackPlugin from 'copy-webpack-plugin'
type Config = Object;
type Helpers = { merge: Config => Config };
type Block = {
(context: Object, helpers: Helpers): Config => Config,
post: (context: Object, helpers: Helpers) => Config => Config
};
export type Pattern = {
from: string | Object,
to?: string,
toType?: "file" | "dir" | "template",
context?: string,
flatten?: boolean,
ignore?: Array<string>,
transform?: (content: string, path: string) => string,
force?: boolean
};
export type Options = {
ignore?: Array<string>,
copyUnmodified?: boolean,
debug?: "warning" | "info" | "debug"
};
/**
* Adds a simple copy pattern to the list of patterns for the plugin.
*/
function copy (from: string, to?: string): Block {
return Object.assign(
context => prevConfig => {
context.copyPlugin = context.copyPlugin || {patterns: []}
// Merge the provided simple pattern into the config
context.copyPlugin = {
...context.copyPlugin,
patterns: [...context.copyPlugin.patterns, {from, to}],
}
// Don't alter the config yet
return prevConfig
},
{post: postConfig}
)
}
/**
* Adds an advanced pattern to the list of patterns for the plugin.
*/
function copyPattern (pattern: Pattern): Block {
return Object.assign(
context => prevConfig => {
context.copyPlugin = context.copyPlugin || {patterns: []}
// Merge the provided advanced pattern into the config
context.copyPlugin = {
...context.copyPlugin,
patterns: [...context.copyPlugin.patterns, pattern],
}
// Don't alter the config yet
return prevConfig
},
{post: postConfig}
)
}
/**
* Sets options for the copy plugin.
*/
function copyOptions (options: Options): Block {
return Object.assign(
context => prevConfig => {
context.copyPlugin = context.copyPlugin || {}
// Merge the provided copy plugin config into the context
context.copyPlugin = {...context.copyPlugin, options}
// Don't alter the config yet
return prevConfig
},
{post: postConfig}
)
}
/**
* Instantiate the copy plugin.
*/
function postConfig (context: Object, {merge}: Helpers): Config => Config {
return prevConfig => {
const {patterns, options} = context.copyPlugin
const plugin = new CopyWebpackPlugin(patterns, options)
return merge({plugins: [plugin]})
}
}
export {copy, copyPattern, copyOptions}