-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
225 lines (196 loc) · 5.63 KB
/
gulpfile.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
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
219
220
221
222
223
224
225
var gulp = require("gulp");
var FwdRef = require("undertaker-forward-reference");
gulp.registry(FwdRef());
var path = require("path");
var fs = require("fs");
var del = require("del");
var browserify = require("browserify");
var watchify = require("watchify");
var hmr = require("browserify-hmr");
var { sucrasify } = require("./tools/transforms/sucrasify");
var discify = require("discify");
var gulp_typescript = require("gulp-typescript");
var livereload = require("gulp-livereload");
var concat = require("gulp-concat");
var less = require("./tools/gulp-plugins/gulp-less");
var rename = require("gulp-rename");
var rollup = require("./tools/gulp-plugins/gulp-rollup");
var typescript = gulp_typescript.createProject("tsconfig.json", {
module: "esnext",
target: "esnext",
importHelpers: true,
removeComments: true,
allowJs: true,
jsx: "react",
experimentalDecorators: true,
noResolve: true,
isolatedModules: true,
skipLibCheck: true,
});
gulp.task("clean", () => del(["dist"]));
gulp.task("html", () => {
return gulp
.src(["src/public/index.html"])
.pipe(rename("index.html"))
.pipe(gulp.dest("build/public"))
.pipe(livereload());
});
gulp.task("styles", async () => {
gulp
.src(["src/**/*.{css,less}"])
.pipe(concat("styles.less"))
.pipe(less())
//.pipe(postcss())
.pipe(gulp.dest("build/public"))
.pipe(livereload());
});
const b = watchify(
browserify({
//exposeAll: true,
entries: ["build/app/main.js"],
extensions: [".ts", ".tsx", ".js", ".jsx"],
cache: {},
packageCache: {},
debug: false,
sourceMaps: false,
fullPaths: true,
plugin:[
[discify, { outdir: "build/public/disc" }],
[hmr]
],
transform: [
sucrasify
]
//dedupe: true
})
)
// b.plugin(discify, { outdir: "build/public/disc" });
// b.plugin(hmr);
// b.transform(sucrasify);
// b.transform([
// envify({
// NODE_ENV: "development",
// }),
// { global: true },
// ]);
b.on("error", console.log);
b.on("syntax", console.log);
b.on("update", bundle);
async function bundle() {
b.bundle()
.on("error", console.error)
.pipe(fs.createWriteStream("build/public/main.js"));
}
gulp.task("roll:app", function () {
//let closure = require("@ampproject/rollup-plugin-closure-compiler");
//let { terser } = require("rollup-plugin-terser");
return gulp
.src(["./src/**/*{.js,.jsx,.ts,.tsx}"])
.pipe(typescript())
//.pipe(babel(BABEL_CONFIG))
.pipe(
rollup({
input: "src/app/main.js",
output: {
format: "esm",
},
external: Object.keys(require("./package.json").dependencies),
treeshake: {
moduleSideEffects: false,
},
inlineDynamicImports: true,
plugins: [
//terser(),
//closure()
],
onwarn: function (message) {
if (/external dependency/.test(message)) {
return;
}
if (message.code === "CIRCULAR_DEPENDENCY") {
return;
}
if (message.code === "INPUT_HOOK_IN_OUTPUT_PLUGIN") {
return;
} else console.error(message);
},
})
)
.pipe(gulp.dest("build"));
});
const serve = () => {
const { polka, sirv } = require("./tools/devserver");
const { PORT = 3002 } = process.env;
const PUBLIC_INDEX_HTML = fs.readFileSync(
path.resolve(__dirname, "build/public", "index.html")
);
const allowAMP = (res) =>
res.setHeader(
"AMP-Access-Control-Allow-Source-Origin",
`http://localhost:${PORT}`
);
polka()
.use(
sirv(path.resolve(__dirname, "build/public"), {
dev: true,
setHeaders: (res) => allowAMP(res),
})
)
.get("*", (req, res) => {
res.end(PUBLIC_INDEX_HTML);
})
.listen(PORT, () => {
console.log(`> Running on http://localhost:${PORT}`);
console.log(`> BundleAnalyzer on http://localhost:${PORT}/disc/map`);
});
};
gulp.task("watch", async () => {
livereload.listen();
gulp.watch("src/**/*.{css,less}", gulp.series("styles"));
gulp.watch("src/**/*.html", gulp.series("html"));
gulp.watch("src/**/*.{ts,tsx,js,jsx}", gulp.series("roll:app"));
});
gulp.task(
"start",
gulp.series(
"clean",
gulp.parallel("roll:app", "styles", "html"),
gulp.parallel(bundle, "watch", serve)
)
);
gulp.task(
"bundle",
gulp.series(
"clean",
gulp.parallel("roll:minify", "styles", "html"),
gulp.parallel([serve])
)
);
// .pipe(source('main.js'))
// .pipe(buffer())
// .pipe(sourcemaps.init({ loadMaps: true }))
// .pipe(minify())
// .pipe(sourcemaps.write('./'))
// .pipe(gulp.dest('build/js'))
//externalizes stuff so you can use unpkg
// .transform('browserify-shim', {
// "lodash": "_",
// "react": "React",
// "react-dom": "ReactDOM"
// }, {global: true});
//you can skip writing files and just use 'browserify-middleware' package if you want to do this
//.get('/main.js', browserifyMiddleware(__dirname+'/src/app.tsx'))
//windows users can use this
//const {watchify} = require("./tools/transforms/watchify");
// this is poor mans tsconfig paths / babel root import resolver, its a browserify transform
//var {tspathify} = require('./tools/transforms/tspathify')
//undertaker is ass, this makes it a little less so
//var FwdRef = require("undertaker-forward-reference");
/**
* this files tsconfig at the top doesnt check types, although a good strategy
* is to change the config to do type checks and to not emit on errors
* by setting noEmitOnError: true. The compiled javascript files won't hit the
* build dir so no pressure on the watcher and it won't run hmr until the code compiles
*/
// wtb pt 96 font
// VIVA LA BROWSERIFY