-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
112 lines (100 loc) · 3.64 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
const {dest, src, watch, series, task, parallel} = require('gulp');
const pug = require('gulp-pug');
const {Transform} = require('stream');
const File = require('vinyl');
const rename = require("gulp-rename");
const browserSync = require('browser-sync').create();
task('browser-sync', done => {
browserSync.init({
server: {
baseDir: "./dist/"
}
});
done();
});
task('browser-reload', done => {
browserSync.reload();
done();
});
task('watch-browser', () => watch('dist/**/*', task('browser-reload')))
task('build-pug', () => {
return src(['./src/**/*.pug', '!./src/**/_*.pug'])
.pipe(pug())
.pipe(rename((name) => {
const match = name.basename.match(/\.([a-z]{2})$/)
if (match) {
name.dirname = `${match[1]}/${name.dirname}`
name.basename = name.basename.substring(0, name.basename.length-3);
}
}))
.pipe(new RedirectPageGenerator())
.pipe(dest('./dist'));
});
task('watch-pug', () => watch('src/**/*', task('build-pug')));
task('watch', parallel(task('watch-pug'), task('watch-browser')));
task('build-bootstrap-css', () => src('node_modules/bootstrap/dist/css/bootstrap.min.css').pipe(dest('dist/css/')))
task('build-bootstrap-js', () => src('node_modules/bootstrap/dist/js/bootstrap.bundle.min.js').pipe(dest('dist/js/')))
task('build-CNAME', () => src('CNAME').pipe(dest('dist/')))
task('build', series([
task('build-pug'),
task('build-bootstrap-css'),
task('build-bootstrap-js'),
task('build-CNAME'),
]))
task('default', series([
task('build'),
task('browser-sync'),
task('watch'),
]));
class RedirectPageGenerator extends Transform {
constructor() {
super({objectMode: true});
}
/**
* @param file {File}
* @param encoding
* @param callback
* @private
*/
_transform(file, encoding, callback) {
function encodeURL(path) {
return path.split(/[\/\\]/g)
.map(encodeURIComponent)
.join('/');
}
function encodeHTML(body) {
return body.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
}
if (file.relative.match(/^[a-z]{2}\//)) {
const relative = file.relative.substring(3);
const relativeUrl = relative.replace(/(?<=\/|\\|^)index\.html$/, '');
const html = `<!doctype html><head><title>Redirecting by Locale...</title><script>`
+`const findLanguage = (langs) => {`
+`for (let locale of navigator.languages)`
+`for (let lang of langs)`
+`if (locale.startsWith(lang)) return lang;`
+`return langs[0]`
+`};`
+`location.href = '/' + findLanguage(['en', 'ja']) + '/' + ${JSON.stringify(encodeURL(relativeUrl))};`
+`</script><body>`
+`<noscript>`
+`JavaScript is disabled. Please go to page depends on your language.`
+`<ul>`
+`<li><a href="${encodeHTML(`/en/${relativeUrl}`)}">english</a></li>`
+`<li><a href="${encodeHTML(`/ja/${relativeUrl}`)}">japanese</a></li>`
+`</ul>`
+`</noscript>`
const redirectFile = new File({
cwd: file.cwd,
base: file.base,
path: `${file.base}/${relative}`,
contents: Buffer.from(html),
});
this.push(redirectFile);
}
callback(null, file.clone({contents: false}));
}
}