gulpfile.js 8.0 KB
Newer Older
C
codecalm 已提交
1
const gulp = require('gulp'),
C
codecalm 已提交
2
	debug = require('gulp-debug'),
C
codecalm 已提交
3
	clean = require('gulp-clean'),
C
codecalm 已提交
4 5
	sass = require('gulp-sass'),
	postcss = require('gulp-postcss'),
C
codecalm 已提交
6
	header = require('gulp-header'),
C
codecalm 已提交
7
	cleanCSS = require('gulp-clean-css'),
C
codecalm 已提交
8
	minifyJS = require('gulp-minify'),
C
codecalm 已提交
9
	rename = require('gulp-rename'),
C
codecalm 已提交
10
	rollupStream = require('@rollup/stream'),
C
codecalm 已提交
11 12
	rollupBabel = require('rollup-plugin-babel'),
	rollupCleanup = require('rollup-plugin-cleanup'),
C
codecalm 已提交
13 14 15 16
	{nodeResolve} = require('@rollup/plugin-node-resolve'),
	rollupCommonjs = require('@rollup/plugin-commonjs'),
	vinylSource = require('vinyl-source-stream'),
	vinylBuffer = require('vinyl-buffer'),
C
codecalm 已提交
17
	browserSync = require('browser-sync'),
C
codecalm 已提交
18
	glob = require('glob'),
C
codecalm 已提交
19
	spawn = require('cross-spawn'),
C
codecalm 已提交
20 21 22
	fs = require('fs'),
	path = require('path'),
	YAML = require('yaml'),
C
codecalm 已提交
23 24
	cp = require('child_process'),
	pkg = require('./package.json'),
C
codecalm 已提交
25
	year = new Date().getFullYear();
C
codecalm 已提交
26

C
codecalm 已提交
27 28 29
let BUILD = false,
	distDir = './.tmp',
	demoDir = './.tmp',
C
codecalm 已提交
30 31
	srcDir = './src';

C
codecalm 已提交
32 33 34
/**
 * Enable BUILD mode and set directories
 */
C
codecalm 已提交
35
gulp.task('build-on', (cb) => {
C
codecalm 已提交
36 37 38
	BUILD = true;
	distDir = './dist';
	demoDir = './demo';
C
codecalm 已提交
39

C
codecalm 已提交
40 41 42 43 44 45
	cb();
});

/**
 * Return banner added to CSS and JS dist files
 */
C
codecalm 已提交
46 47 48 49 50 51 52 53 54 55 56 57
const getBanner = () => {
	return `/*!
* Tabler v${pkg.version} (${pkg.homepage})
* @version ${pkg.version}
* @link ${pkg.homepage}
* Copyright 2018-${year} The Tabler Authors
* Copyright 2018-${year} codecalm.net Paweł Kuna
* Licensed under MIT (https://github.com/tabler/tabler/blob/master/LICENSE)
*/
`;
};

C
codecalm 已提交
58 59 60
/**
 * Array.flat polyfill
 */
C
codecalm 已提交
61 62
if (!Array.prototype.flat) {
	Object.defineProperty(Array.prototype, 'flat', {
C
codecalm 已提交
63 64
		value: function (depth = 1) {
			return this.reduce(function (flat, toFlatten) {
C
codecalm 已提交
65 66 67 68 69 70
				return flat.concat((Array.isArray(toFlatten) && (depth > 1)) ? toFlatten.flat(depth - 1) : toFlatten);
			}, []);
		}
	});
}

C
codecalm 已提交
71 72 73
/**
 * Import tabler-icons form npm and other svg files from `/svg/brand/` directory and generate Jekyll `.yml` data files
 */
C
codecalm 已提交
74 75
gulp.task('svg-icons', (cb) => {
	const prepareSvgFile = (svg) => {
C
codecalm 已提交
76 77
		return svg.replace(/\n/g, '').replace(/>\s+</g, '><');
	};
C
codecalm 已提交
78

C
codecalm 已提交
79
	const generateIconsYml = (dir, filename) => {
C
codecalm 已提交
80 81
		const files = glob.sync(dir);
		let svgList = {};
C
codecalm 已提交
82

C
codecalm 已提交
83
		files.forEach((file) => {
C
codecalm 已提交
84 85 86
			const basename = path.basename(file, '.svg');
			svgList[basename] = prepareSvgFile(fs.readFileSync(file).toString());
		});
C
codecalm 已提交
87

C
codecalm 已提交
88 89 90
		fs.writeFileSync(filename, YAML.stringify(svgList));
	};

C
codecalm 已提交
91 92 93 94 95 96
	generateIconsYml("./node_modules/tabler-icons/icons/*.svg", `${srcDir}/pages/_data/icons-tabler.yml`);
	generateIconsYml(`${srcDir}/svg/brand/*.svg`, `${srcDir}/pages/_data/icons-brand.yml`);

	cb();
});

C
codecalm 已提交
97 98 99
/**
 * Check unused Jekyll partials
 */
C
codecalm 已提交
100
gulp.task('unused-files', (cb) => {
C
codecalm 已提交
101 102
	let foundFiles = [];

C
codecalm 已提交
103
	glob.sync(`${srcDir}/pages/**/*.{html,md}`).forEach((file) => {
C
codecalm 已提交
104 105
		let fileContent = fs.readFileSync(file);

C
codecalm 已提交
106
		fileContent.toString().replace(/\{% include(_cached)? ([a-z0-9\/_-]+\.html)/g, (f, c, filename) => {
C
codecalm 已提交
107 108 109 110 111 112 113 114 115 116
			filename = `${srcDir}/pages/_includes/${filename}`;

			if (!foundFiles.includes(filename)) {
				foundFiles.push(filename);
			}
		});
	});

	let includeFiles = glob.sync(`${srcDir}/pages/_includes/**/*.html`);

C
codecalm 已提交
117
	includeFiles.forEach((file) => {
C
codecalm 已提交
118 119 120 121 122
		if (!foundFiles.includes(file)) {
			console.log('file', file);
		}
	});

C
codecalm 已提交
123 124
	cb();
});
C
codecalm 已提交
125

C
codecalm 已提交
126
/**
C
codecalm 已提交
127
 * Clean `dist` folder before build
C
codecalm 已提交
128
 */
C
codecalm 已提交
129
gulp.task('clean-dirs', () => {
C
codecalm 已提交
130
	return gulp
C
codecalm 已提交
131
		.src(`{${distDir}/*,${demoDir}/*}`, { read: false })
C
codecalm 已提交
132 133 134
		.pipe(clean());
});

C
codecalm 已提交
135 136 137 138 139
gulp.task('clean-jekyll', (cb) => {
	return cp.spawn('bundle', ['exec', 'jekyll', 'clean'], { stdio: 'inherit' })
		.on('close', cb);
});

C
codecalm 已提交
140
/**
C
codecalm 已提交
141
 * Compile SASS to CSS and move it to dist directory
C
codecalm 已提交
142
 */
C
codecalm 已提交
143
gulp.task('sass', () => {
C
codecalm 已提交
144 145 146 147
	const g = gulp
		.src(`${srcDir}/scss/*.scss`)
		.pipe(sass({
			style: 'expanded',
C
codecalm 已提交
148
			precision: 7,
C
codecalm 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
			importer: (url, prev, done) => {
				if (url[0] === '~') {
					url = path.resolve('node_modules', url.substr(1));
				}

				return { file: url };
			},
		}).on('error', sass.logError))
		.pipe(postcss([
			require('autoprefixer'),
		]))
		.pipe(gulp.dest(`${distDir}/css/`))
		.pipe(browserSync.reload({
			stream: true,
		}));

	if (BUILD) {
		g.pipe(cleanCSS())
C
codecalm 已提交
167
			.pipe(rename((path) => {
C
codecalm 已提交
168 169 170 171 172 173 174 175
				path.basename += '.min';
			}))
			.pipe(gulp.dest(`${distDir}/css/`));
	}

	return g;
});

C
codecalm 已提交
176 177 178
/**
 * Compile JS files to dist directory
 */
C
codecalm 已提交
179
let cache;
C
codecalm 已提交
180
gulp.task('js', () => {
C
codecalm 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	const g = rollupStream({
		input: `${srcDir}/js/tabler.js`,
		cache,
		output: {
			name: 'tabler.js',
			format: 'umd',
		},
		plugins: [
			rollupBabel({
				exclude: 'node_modules/**'
			}),
			nodeResolve(),
			rollupCommonjs(),
			rollupCleanup()
		]
	})
		.on('bundle', (bundle) => {
			cache = bundle;
		})
		.pipe(vinylSource('tabler.js'))
		.pipe(vinylBuffer())
C
codecalm 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		.pipe(rename((path) => {
			path.dirname = '';
		}))
		.pipe(gulp.dest(`${distDir}/js/`))
		.pipe(browserSync.reload({
			stream: true,
		}));

	if (BUILD) {
		g.pipe(minifyJS({
			ext: {
				src: '.js',
				min: '.min.js'
			},
		}))
			.pipe(gulp.dest(`${distDir}/js/`));
	}

	return g;
C
codecalm 已提交
221 222
});

C
codecalm 已提交
223 224 225
/**
 * Watch Jekyll files and build it to demo directory
 */
C
codecalm 已提交
226
gulp.task('watch-jekyll', (cb) => {
C
codecalm 已提交
227
	browserSync.notify('Building Jekyll');
C
codecalm 已提交
228
	return spawn('bundle', ['exec', 'jekyll', 'build', '--watch', '--destination', demoDir, '--trace'], { stdio: 'inherit' })
C
codecalm 已提交
229 230 231
		.on('close', cb);
});

C
codecalm 已提交
232 233 234
/**
 * Build Jekyll files do demo directory
 */
C
codecalm 已提交
235 236
gulp.task('build-jekyll', (cb) => {
	var env = Object.create(process.env);
C
codecalm 已提交
237 238
	env.JEKYLL_ENV = 'production';

C
codecalm 已提交
239
	return spawn('bundle', ['exec', 'jekyll', 'build', '--destination', demoDir, '--trace'], { env: env, stdio: 'inherit' })
C
codecalm 已提交
240 241 242
		.on('close', cb);
});

C
codecalm 已提交
243 244 245
/**
 * Watch JS and SCSS files
 */
C
codecalm 已提交
246
gulp.task('watch', (cb) => {
C
codecalm 已提交
247 248 249 250 251
	gulp.watch('./src/scss/**/*.scss', gulp.series('sass'));
	gulp.watch('./src/js/**/*.js', gulp.series('js'));
	cb();
});

C
codecalm 已提交
252 253 254
/**
 * Create BrowserSync server
 */
C
codecalm 已提交
255
gulp.task('browser-sync', () => {
C
codecalm 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	browserSync({
		watch: true,
		server: {
			baseDir: demoDir,
			routes: {
				'/node_modules': 'node_modules',
				'/dist/css': `${distDir}/css`,
				'/dist/js': `${distDir}/js`,
				'/dist/img': `${srcDir}/img`,
				'/static': `${srcDir}/static`,
			},
		},
		port: 3000,
		open: false,
		host: 'localhost',
		notify: false,
		reloadOnRestart: true
	});
});

C
codecalm 已提交
276 277 278
/**
 * Copy libs used in tabler from npm to dist directory
 */
C
codecalm 已提交
279 280 281 282 283
gulp.task('copy-libs', (cb) => {
	const allLibs = require(`${srcDir}/pages/_data/libs`);

	let files = [];

C
codecalm 已提交
284
	Object.keys(allLibs.js).forEach((lib) => {
C
codecalm 已提交
285 286 287
		files.push(Array.isArray(allLibs.js[lib]) ? allLibs.js[lib] : [allLibs.js[lib]]);
	});

C
codecalm 已提交
288
	Object.keys(allLibs.css).forEach((lib) => {
C
codecalm 已提交
289 290 291 292 293
		files.push(Array.isArray(allLibs.css[lib]) ? allLibs.css[lib] : [allLibs.css[lib]]);
	});

	files = files.flat();

C
codecalm 已提交
294 295
	files.forEach((file) => {
		if (!file.match(/^https?/)) {
C
codecalm 已提交
296 297 298 299 300 301 302 303 304 305
			let dirname = path.dirname(file).replace('@', '');
			let cmd = `mkdir -p "dist/libs/${dirname}" && cp -r node_modules/${file} ${distDir}/libs/${file.replace('@', '')}`;

			cp.exec(cmd)
		}
	});

	cb();
});

C
codecalm 已提交
306 307 308
/**
 * Copy static files (flags, payments images, etc) to dist directory
 */
C
codecalm 已提交
309
gulp.task('copy-images', () => {
C
codecalm 已提交
310 311 312 313 314
	return gulp
		.src(`${srcDir}/img/**/*`)
		.pipe(gulp.dest(`${distDir}/img`));
});

C
codecalm 已提交
315 316 317
/**
 * Copy static files (demo images, etc) to demo directory
 */
C
codecalm 已提交
318
gulp.task('copy-static', () => {
C
codecalm 已提交
319 320 321 322 323
	return gulp
		.src(`${srcDir}/static/**/*`)
		.pipe(gulp.dest(`${demoDir}/static`));
});

C
codecalm 已提交
324 325 326
/**
 * Copy Tabler dist files to demo directory
 */
C
codecalm 已提交
327
gulp.task('copy-dist', () => {
C
codecalm 已提交
328 329 330 331 332
	return gulp
		.src(`${distDir}/**/*`)
		.pipe(gulp.dest(`${demoDir}/dist/`));
});

C
codecalm 已提交
333 334 335
/**
 * Add banner to build JS and CSS files
 */
C
codecalm 已提交
336 337 338 339 340 341
gulp.task('add-banner', () => {
	return gulp.src(`${distDir}/{css,js}/**/*.{js,css}`)
		.pipe(header(getBanner()))
		.pipe(gulp.dest(`${distDir}`))
});

C
codecalm 已提交
342 343
gulp.task('clean', gulp.series('clean-dirs', 'clean-jekyll'));

344
gulp.task('start', gulp.series('clean', 'sass', 'js', 'build-jekyll', gulp.parallel('watch-jekyll', 'watch', 'browser-sync')));
C
codecalm 已提交
345

C
codecalm 已提交
346
gulp.task('build-core', gulp.series('build-on', 'clean', 'sass', 'js', 'copy-images', 'copy-libs', 'add-banner'));
347
gulp.task('build-demo', gulp.series('build-on', 'build-jekyll', 'copy-static', 'copy-dist'));
C
codecalm 已提交
348
gulp.task('build', gulp.series('build-core', 'build-demo'));