/
packer.go
218 lines (181 loc) · 5.66 KB
/
packer.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2021 Guy A. Ross
// This source code is licensed under the GNU GPLv3 found in the
// license file in the root directory of this source tree.
package srcpack
import (
"context"
"fmt"
"os"
"sync"
"github.com/GuyARoss/orbit/pkg/jsparse"
"github.com/GuyARoss/orbit/pkg/log"
"github.com/GuyARoss/orbit/pkg/webwrap"
)
type Packer interface {
PackMany(pages []string) (PackedComponentList, error)
PackSingle(logger log.Logger, file string) (PackComponent, error)
ReattachLogger(logger log.Logger) Packer
}
// CachedEnvKeys represents a map where the key is the filepath
// for the env setting and where the value is a bundler key
type CachedEnvKeys map[string]string
// packer is the primary struct used for packing a directory of javascript files into
// valid web components.
type JSPacker struct {
JsParser jsparse.JSParser
ValidWebWrappers webwrap.JSWebWrapperList
Logger log.Logger
SkipFirstPassBundle bool
AssetDir string
WebDir string
cachedBundleKeys CachedEnvKeys
}
// concpack is a private packing mechanism embedding the packer to pack a set of files concurrently.
type concPack struct {
*JSPacker
m sync.Mutex
packedPages []PackComponent
packMap map[string]bool
cachedBundleKeys CachedEnvKeys
}
// PackMany packs the provided file paths into the orbit root directory
func (s *JSPacker) PackMany(pages []string) (PackedComponentList, error) {
cp := &concPack{
JSPacker: s,
packedPages: make([]PackComponent, 0),
packMap: make(map[string]bool),
cachedBundleKeys: s.cachedBundleKeys,
}
wg := &sync.WaitGroup{}
wg.Add(len(pages))
var errOnce sync.Once
var packErr error
sh := NewSyncHook(s.Logger)
for _, dir := range pages {
// we copy dir here to avoid the pointer of dir being passed to our wrap func.
t := dir
// go routine to pack every page found in the pages directory
// we wrap this routine with the sync hook to measure & log time deltas.
go sh.WrapFunc(dir, func() *webwrap.WrapStats {
page, err := cp.PackSingle(wg, t)
if err != nil {
errOnce.Do(func() {
packErr = err
})
return nil
}
return page.WebWrapper().Stats()
})
}
wg.Wait()
return cp.packedPages, packErr
}
func (p *JSPacker) PackSingle(logger log.Logger, file string) (PackComponent, error) {
return NewComponent(context.TODO(), &NewComponentOpts{
DefaultKey: p.cachedBundleKeys[file],
FilePath: file,
WebDir: p.WebDir,
JSWebWrappers: p.ValidWebWrappers,
JSParser: p.JsParser,
})
}
func (p *JSPacker) ReattachLogger(logger log.Logger) Packer {
p.Logger = logger
return p
}
// DefaultPackerOpts options for creating a new default packer
type DefaultPackerOpts struct {
WebDir string
BundlerMode string
NodeModuleDir string
CachedBundleKeys CachedEnvKeys
SkipFirstPassBundle bool
}
// pack single packs a single file path into a usable web component
// this process includes the following:
// 1. wrapping the component with the specified front-end web framework.
// 2. bundling the component with the specified javascript bundler.
func (p *concPack) PackSingle(wg *sync.WaitGroup, path string) (PackComponent, error) {
// this page has already been packed before and does not need to be repacked.
if p.packMap[path] {
wg.Done()
return nil, nil
}
// @@todo: we should validate if these components exist on our source map yet, if so we should
// inherit the metadata, rather than generate new metadata.
page, err := NewComponent(context.TODO(), &NewComponentOpts{
DefaultKey: p.cachedBundleKeys[path],
FilePath: path,
WebDir: p.WebDir,
JSWebWrappers: p.ValidWebWrappers,
JSParser: p.JsParser,
SkipFirstPassBundle: p.SkipFirstPassBundle,
})
if err != nil {
wg.Done()
return nil, err
}
p.m.Lock()
p.packedPages = append(p.packedPages, page)
p.packMap[path] = true
p.m.Unlock()
wg.Done()
return page, nil
}
type PackedComponentList []PackComponent
func (l *PackedComponentList) RepackMany(logger log.Logger) error {
wg := &sync.WaitGroup{}
wg.Add(len(*l))
var errOnce sync.Once
var packErr error
sh := NewSyncHook(logger)
for _, comp := range *l {
// we copy dir here to avoid the pointer of dir being passed to our wrap func.
t := comp
// go routine to pack every page found in the pages directory
// we wrap this routine with the sync hook to measure & log time deltas.
go sh.WrapFunc(t.OriginalFilePath(), func() *webwrap.WrapStats {
err := comp.RepackForWaitGroup(wg)
if err != nil {
errOnce.Do(func() {
packErr = err
})
}
return comp.WebWrapper().Stats()
})
}
wg.Wait()
return packErr
}
// Write creates an audit file of all the current components to the specified file
func (l *PackedComponentList) Write(path string) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
f.WriteString("audit: components\n")
for _, c := range *l {
f.WriteString(fmt.Sprintf("%s %s", c.Name(), c.BundleKey()) + "\n")
}
if err := f.Close(); err != nil {
return err
}
return nil
}
func NewDefaultPacker(logger log.Logger, opts *DefaultPackerOpts) Packer {
packer := &JSPacker{
WebDir: opts.WebDir,
JsParser: &jsparse.JSFileParser{},
ValidWebWrappers: webwrap.NewActiveMap(&webwrap.BaseBundler{
Mode: webwrap.BundlerMode(opts.BundlerMode),
WebDir: opts.WebDir,
PageOutputDir: ".orbit/base/pages",
NodeModulesDir: opts.NodeModuleDir,
Logger: logger,
}),
Logger: logger,
cachedBundleKeys: opts.CachedBundleKeys,
SkipFirstPassBundle: opts.SkipFirstPassBundle,
}
return packer
}