1 Star 0 Fork 0

hu-snail/html-docx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gulpfile.coffee 4.58 KB
一键复制 编辑 原始数据 按行查看 历史
forever-chen 提交于 2020-01-20 15:40 . update
gulp = require 'gulp'
vinyl = require 'vinyl-source-stream'
browserify = require 'browserify'
watchify = require 'watchify'
gutil = require 'gulp-util'
prettyHrtime = require 'pretty-hrtime'
notify = require 'gulp-notify'
mocha = require 'gulp-mocha'
mochaPhantomJS = require 'gulp-mocha-phantomjs'
template = require 'gulp-lodash-template'
coffee = require 'gulp-coffee'
del = require 'del'
uglify = require 'gulp-uglify'
fsExtra = require 'fs-extra'
pipeline = require('readable-stream').pipeline
walkSync = require 'klaw-sync'
path = require 'path'
startTime = null
logger =
start: ->
startTime = process.hrtime()
gutil.log 'Running', gutil.colors.green("'bundle'") + '...'
end: ->
taskTime = process.hrtime startTime
prettyTime = prettyHrtime taskTime
gutil.log 'Finished', gutil.colors.green("'bundle'"), 'in', gutil.colors.magenta(prettyTime)
handleErrors = ->
notify.onError
title: 'Compile error'
message: '<%= error.message %>'
.apply this, arguments
@emit 'end'
build = (test) ->
[output, entry, options] = if test
['tests.js', './test/index', debug: true]
else
['html-docx.js', './src/api', standalone: 'html-docx']
bundleMethod = if global.isWatching then watchify else browserify
bundler = bundleMethod
entries: [entry]
extensions: ['.coffee', '.tpl']
bundle = ->
logger.start()
bundler
.transform 'jstify', engine: 'lodash-micro', minifierOpts: false
.bundle options
.on 'error', handleErrors
.pipe vinyl(output)
.pipe gulp.dest('./build')
.on 'end', logger.end
if global.isWatching
bundler.on 'update', bundle
bundle()
testsBundle = './test/index.coffee'
clean = (cb) -> del 'build', cb
gulp.task 'clean', clean
gulp.task 'setWatch', -> global.isWatching = true
gulp.task 'build', -> build()
gulp.task 'watch', ['setWatch', 'build']
minify = ->
builtFile = './build/html-docx.js'
entry = './build/html-docx.min.js'
dest = './build'
fsExtra.copySync builtFile, entry
pipeline(
gulp.src(entry),
uglify(),
gulp.dest(dest)
)
gulp.task 'minify', minify
buildNode = (compileCoffee = true) ->
logger.start()
gulp.src('src/**/*', base: 'src').pipe(gulp.dest('build'))
gulp.src('src/templates/*.tpl').pipe(template commonjs: true).pipe(gulp.dest('build/templates'))
if compileCoffee
gulp.src('src/**/*.coffee').pipe(coffee bare: true)
.on('error', handleErrors).on('end', logger.end).pipe(gulp.dest('build'))
else
logger.end()
gulp.task 'build-node', buildNode
gulp.task 'test-node', (growl = false) ->
buildNode(false)
gulp.src(testsBundle, read: false).pipe mocha {reporter: 'spec', growl}
gulp.task 'test-node-watch', ->
sources = ['src/**', 'test/**']
gulp.watch sources, ['test-node']
gulp.task 'build-test-browserify', -> build(true)
gulp.task 'run-phantomjs', -> gulp.src('test/testbed.html').pipe(mochaPhantomJS reporter: 'spec')
gulp.task 'test-phantomjs', ['build-test-browserify', 'run-phantomjs']
cleanRelease = (cb) ->
del 'dist', cb
copyRelease = ->
jsFilesFilterFn = (item) ->
item.path.indexOf('.js') is item.path.length - '.js'.length
fsExtra.copySync './build/assets', './dist/node/assets'
templateFiles = walkSync './build/templates', filter: jsFilesFilterFn, noDir: true
templateFiles.forEach ({ path: filePath }) ->
fileName = path.basename(filePath)
fsExtra.copySync filePath, "./dist/node/templates/#{fileName}"
jsFiles = walkSync './build', filter: jsFilesFilterFn, noDir: true
jsFiles.forEach ({ path: filePath }) ->
fileName = path.basename(filePath)
outputPath = if fileName.indexOf('html-docx') isnt 0 then './dist/node' else './dist'
fsExtra.copySync filePath, "#{outputPath}/#{fileName}"
transportFile = ->
# del './test/publish'
fsExtra.copySync './build/html-docx.min.js', './test/publish/html-docx.min.js'
fsExtra.copySync './test/index.js', './test/publish/index.js'
fsExtra.copySync './test/jquery.js', './test/publish/jquery.js'
fsExtra.copySync './test/generate.js', './test/publish/generate.js'
fsExtra.copySync './test/sample.html', './test/publish/sample.html'
minFile = ->
builtFile = './test/index.js'
entry = './test/publish/index.js'
dest = './test/publish'
fsExtra.copySync builtFile, entry
pipeline(
gulp.src(entry),
uglify(),
gulp.dest(dest)
)
deletePublish = (cb) ->
del './test/publish', cb
gulp.task 'minify', minify
gulp.task 'clean-release', cleanRelease
gulp.task 'copy-release', copyRelease
gulp.task 'clean-publish', deletePublish
gulp.task 'min-file', minFile
gulp.task 'transport-file', transportFile
gulp.task 'default', ['test-node', 'test-node-watch']
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hu-snail/html-docx.git
[email protected]:hu-snail/html-docx.git
hu-snail
html-docx
html-docx
master

搜索帮助