-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
131 lines (116 loc) · 3.57 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
require("dotenv").config();
const run = require("gulp-run");
const { task, src, dest, series, parallel, watch } = require("gulp");
const terser = require("gulp-terser");
const rename = require("gulp-rename");
const stripComments = require("gulp-strip-comments");
const sourcemaps = require("gulp-sourcemaps");
const gulpif = require("gulp-if");
const cssnano = require("gulp-cssnano");
const prettier = require("gulp-prettier");
const postcss = require("gulp-postcss");
const size = require("gulp-size");
const imagemin = require("gulp-imagemin");
//rollup required plugins
const rollup = require("gulp-better-rollup");
const { nodeResolve } = require("@rollup/plugin-node-resolve"); //allow rollup to parse npm_modules
const commonjs = require("@rollup/plugin-commonjs"); //allow rollup to use npm_modules by converting to es6 exports
const rollupJson = require("@rollup/plugin-json"); //also used to use node modules
//=============================
// Configuration
//=============================
const isProduction = process.env.NODE_ENV === "production" ? true : false;
//main path ways
const config = {
srcImg: "src/images/**/*.{jpg,jpeg,png,gif,svg}",
srcJS: "src/js/*.{js, jsx, ts, tsx}",
srcStyles: "src/styles/*.css",
rootDist: "dist/**/*.{liquid, json}",
dest: "./dist/assets",
};
//=============================
// CHANNELS - pipeline wrappers
//=============================
//image build path
function imageBuildChannel(srcPath) {
src(srcPath)
.pipe(imagemin({ verbose: true }))
.pipe(size({ showFiles: true }))
.pipe(dest(config.dest));
}
//js channel
function jsBuildChannel(srcPath) {
src(srcPath)
.pipe(sourcemaps.init())
.pipe(
rollup(
{
plugins: [
commonjs(),
nodeResolve({ preferBuiltins: true, browser: true }),
],
},
"iife"
)
)
.pipe(stripComments())
.pipe(
gulpif(
isProduction,
terser({
compress: {
drop_console: true, //removes console logs, set to false to keep them
},
})
)
)
.pipe(rename({ extname: ".min.js" }))
.pipe(size({ showFiles: true }))
.pipe(dest(config.dest));
}
//css channel
function cssBuildChannel(srcPath) {
src(srcPath)
.pipe(gulpif(!isProduction, prettier()))
.pipe(gulpif(isProduction, cssnano()))
.pipe(postcss()) // configured in src/styles/postcss.config.js
.pipe(rename({ extname: ".min.css" }))
.pipe(size({ showFiles: true }))
.pipe(dest(config.dest));
}
//=============================
// TASKS
//=============================
//build js files
task("build", async () => {
jsBuildChannel(config.srcJS);
cssBuildChannel(config.srcStyles);
imageBuildChannel([config.srcImg]);
});
//compress images
task("build:img", async () => {
imageBuildChannel([config.srcImg]);
});
//build/bundle js
task("build:js", async () => {
jsBuildChannel("src/js/*.{js, jsx, ts, tsx}");
});
//build/compile tailwind css
task("build:css", async () => {
cssBuildChannel(config.srcStyles);
});
//watch /src files for changes then build
task("watch", async () => {
watch(config.srcJS, series("build:js"));
watch(config.srcStyles, series("build:css"));
watch(config.srcImg, series("build:img"));
watch(config.rootDist, parallel("build:css"));
});
task("deploy:staging", async () => {
const cmd = new run.Command(`shopify theme push --theme ${process.env.STAGING_ID} --store ${process.env.STORE_URL}`);
cmd.exec();
});
task("store:login", async () => {
const cmd = new run.Command(`shopify login --store ${process.env.STORE_URL}`);
cmd.exec();
});