-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse.js
executable file
·142 lines (125 loc) · 3.64 KB
/
fuse.js
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
const fs = require('fs');
const {
EnvPlugin,
FuseBox,
Sparky,
UglifyJSPlugin,
BabelPlugin,
CSSModules,
CSSPlugin,
SassPlugin,
ImageBase64Plugin,
CopyPlugin,
CSSResourcePlugin,
} = require('fuse-box');
const path = require('path');
const pkg = require('./package.json');
// load babel file
const babelrc = fs.readFileSync('./.babelrc');
/*
env settings
*/
const envVars = {
VERSION: pkg.version,
NODE_ENV: 'development',
YEAR: new Date().getFullYear(),
};
/*
dir settings
*/
const directory = {
homeDir: 'src',
outFolder: 'build',
js: 'js',
};
let serverBundle;
let clientBundle;
let fuse;
let options = [];
Sparky.task('default', ['clean', 'version-file', 'options', 'build', 'start', 'run'], () => {
//
});
Sparky.task('start-prod', ['set-prod', 'clean', 'version-file', 'options', 'build', 'start', 'run'], () => {
//
});
Sparky.task('prod-build', ['set-prod', 'clean', 'version-file', 'options', 'build', 'run'], () => {
//
});
Sparky.task('set-prod', () => {
envVars.NODE_ENV = 'production';
});
Sparky.task('clean', () => Sparky.src(`${directory.outFolder}/*`).clean(`${directory.outFolder}`));
Sparky.task('version-file', () => {
const outputDir = path.join(__dirname, directory.outFolder);
const pubDir = path.join(outputDir, 'public');
const versionFilePath = path.join(pubDir, 'version.json');
fs.mkdirSync(outputDir);
fs.mkdirSync(pubDir);
fs.writeFileSync(versionFilePath, JSON.stringify({ version: envVars.VERSION }, undefined, 4));
});
Sparky.task('options', () => {
options = {
homeDir: directory.homeDir,
output: `${directory.outFolder}/$name.js`,
cache: envVars.NODE_ENV !== 'production',
hash: false,
plugins: [
EnvPlugin(envVars),
[CSSResourcePlugin({ inline: true }), CSSPlugin()],
[SassPlugin(), CSSModules(), CSSPlugin()],
BabelPlugin({
config: Object.assign({}, { sourceMaps: true }, JSON.parse(babelrc)),
}),
ImageBase64Plugin(),
CopyPlugin({
files: ['pages/Home/test.css', 'styles/reset.css'],
}),
],
};
});
Sparky.task('build', () => {
if (envVars.NODE_ENV === 'production') {
options.plugins.push(
UglifyJSPlugin(),
);
}
fuse = FuseBox.init(options);
clientBundle = fuse.bundle(`public/${directory.js}/vendor`).instructions('~client.js');
// Server Bundle
serverBundle = fuse.bundle('server').splitConfig({
server: `build/public/${directory.js}`,
dest: `public/${directory.js}`,
});
// Client Bundle
clientBundle = fuse.bundle(`public/${directory.js}/bundle`).splitConfig({
browser: `/${directory.js}`,
dest: `public/${directory.js}`,
});
serverBundle.instructions(' > [server.js] + process + fuse-box-css')
.completed(() => {
console.log('\x1b[36m%s\x1b[0m', 'server bundled');
});
clientBundle.instructions('> client.js')
.completed(() => {
console.log('\x1b[36m%s\x1b[0m', 'client bundled');
});
});
Sparky.task('start', () => {
if (envVars.NODE_ENV === 'development') {
fuse.dev({ hmr: true, httpServer: false });
serverBundle.watch('server/**');
clientBundle.hmr().watch();
}
serverBundle.completed(proc => proc.start());
});
Sparky.task('run', async () => {
const producer = await fuse.run();
const bundle = producer.bundles.get(`public/${directory.js}/bundle`);
const vendor = producer.bundles.get(`public/${directory.js}/vendor`);
const bundles = {
bundle: bundle.context.output.lastGeneratedFileName,
vendor: vendor.context.output.lastGeneratedFileName,
};
const outputDir = path.join(__dirname, directory.outFolder);
fs.writeFileSync(path.join(outputDir, 'bundles.json'), JSON.stringify(bundles));
});