This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
151 lines (137 loc) · 4.55 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
const {dewBuild} = require('@idealworld/plugin-gulp')
const gulp = require('gulp')
const path = require('path')
const browserify = require('browserify')
const babelify = require('babelify')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const {series, parallel} = require('gulp')
const rm = require('rimraf')
const concat = require('gulp-concat')
const cheerio = require('gulp-cheerio')
const sass = require('gulp-sass')
const uglify = require('gulp-uglify-es').default
const cssmin = require('gulp-cssmin')
const minifyHtml = require('gulp-minify-html')
const rename = require('gulp-rename')
const resversion = require('gulp-res-version')
const connect = require('gulp-connect')
const gulpif = require('gulp-if')
const eslint = require('gulp-eslint')
const sourcemaps = require('gulp-sourcemaps')
const exorcist = require('exorcist')
let ts = require("gulp-typescript")
let tsProject = ts.createProject('tsconfig.json')
const PORT = process.env.PORT || 8080
const NODE_ENV = process.env.NODE_ENV
const isDev = NODE_ENV === 'dev'
const isProd = NODE_ENV === 'prod'
const _path = {
dist: './dist',
action: './dist/actions',
release: './release',
main_js: './index.js',
js: ['./index.js', './src/**/*.ts', './src/**/*.tsx'],
lint_js: ['./src/**/*.tsx'],
scss: ['./src/**/*.scss'],
html: './index.html'
}
function _clean(done) {
rm(_path.dist, error => {
if (error) throw error
rm(_path.release, error => {
if (error) throw error
done()
})
})
}
function _ts() {
return tsProject.src()
.pipe(tsProject()).js
.pipe(gulp.dest(_path.dist))
}
function _dewBuild() {
return dewBuild(_path.action, isProd)
}
function _lint() {
return gulp.src(_path.lint_js)
.pipe(eslint({
useEslintrc: true
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
}
function _css() {
return gulp.src(_path.scss)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(concat('app.css'))
.pipe(gulpif(isProd, cssmin()))
.pipe(gulpif(isProd, rename({suffix: '.min'})))
.pipe(gulpif(isDev, sourcemaps.write('/')))
.pipe(gulp.dest(gulpif(isProd, _path.release, _path.dist) + '/css'))
.pipe(gulpif(isDev, connect.reload()))
}
function _script() {
return browserify({
entries: _path.main_js,
debug: isDev,
extensions: ['.js', '.jsx', 'tsx', '.json'],
paths: [_path.dist],
})
.transform(babelify, {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
'@babel/plugin-transform-runtime',
['@babel/plugin-proposal-decorators', {'legacy': true}],
['@babel/plugin-proposal-class-properties', {'loose': true}]
]
})
.bundle()
.pipe(gulpif(isDev, exorcist(path.resolve(__dirname, gulpif(isProd, _path.release, _path.dist) + '/js/app.js.map')))) // 生成外部map
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulpif(isProd, uglify()))
.pipe(gulpif(isProd, rename({suffix: '.min'})))
.pipe(gulp.dest(gulpif(isProd, _path.release, _path.dist) + '/js'))
.pipe(gulpif(isDev, connect.reload()))
}
function _html() {
return gulp.src(_path.html)
.pipe(cheerio(($ => {
$('script').remove()
$('link').remove()
$('body').append(`<script src="./js/app${isDev ? '' : '.min'}.js"></script>`)
$('head').append(`<link rel="stylesheet" href="./css/app${isDev ? '' : '.min'}.css">`)
})))
.pipe(gulpif(isProd, minifyHtml({
empty: true,
spare: true
})))
.pipe(gulp.dest(gulpif(isProd, _path.release, _path.dist)))
.pipe(gulpif(isDev, connect.reload()))
}
function _version() {
return gulp.src(gulpif(isProd, _path.release, _path.dist) + '/*.html')
.pipe(resversion({
rootdir: gulpif(isProd, _path.release, _path.dist),
ignore: [/#data$/i]
}))
.pipe(gulp.dest(_path.dist))
}
function _server() {
connect.server({
root: _path.dist,
port: PORT,
livereload: true
})
}
function _watch() {
gulp.watch(_path.html, _html)
gulp.watch(_path.js, series(_ts, _script))
gulp.watch(_path.scss, _css)
}
module.exports = {
build: series(_clean, _ts, _dewBuild, parallel(_script, _css, _html), _version),
dev: series(_clean, _ts, _dewBuild, _lint, parallel(_script, _css, _html), _version, parallel(_server, _watch)),
}