我的gulp应用程序将不会部署到Heroku。我在这里阅读了多个主题,并尝试了Heroku解决方案指南中的各种解决方案。每当我运行git push heroku master
时,终端都会返回以下日志输出:
remote: -----> Build
remote: Running build
remote:
remote: > foundation-emails-template@1.0.0 build
/tmp/build_d45d8863
remote: > gulp --production
remote:
remote: [03:53:59] Failed to load external module @babel/register
remote: [03:53:59] Requiring external module babel-register
remote: /tmp/build_d45d8863/gulpfile.babel.js:74
remote: _gulp2.default.task("default", _gulp2.default.series("build",
server, watch));
remote:
^
remote:
remote: ReferenceError: server is not defined
remote: at Object.<anonymous>
(/tmp/build_d45d8863/gulpfile.babel.js:29:43)
remote: at Module._compile (internal/modules/cjs/loader.js:778:30)
remote: at loader (/tmp/build_d45d8863/node_modules/babel-
register/lib/node.js:144:5)
remote: at Object.require.extensions.(anonymous function) [as .js]
(/tmp/build_d45d8863/node_modules/babel-register/lib/node.js:154:7)
remote: at Module.load (internal/modules/cjs/loader.js:653:32)
remote: at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
remote: at Function.Module._load
(internal/modules/cjs/loader.js:585:3)
remote: at Module.require (internal/modules/cjs/loader.js:692:17)
remote: at require (internal/modules/cjs/helpers.js:25:18)
remote: at execute (/tmp/build_d45d8863/node_modules/gulp-
cli/lib/versioned/^4.0.0/index.js:37:18)
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 1
remote: npm ERR! foundation-emails-template@1.0.0 build: `gulp --
production`
remote: npm ERR! Exit status 1
remote: npm ERR!
remote: npm ERR! Failed at the foundation-emails-template@1.0.0 build
script.
remote: npm ERR! This is probably not a problem with npm. There is
likely additional logging
output above.
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR! /tmp/npmcache.hAeKQ/_logs/2020-09-
05T03_54_00_278Z-debug.log
remote:
remote: -----> Build failed
remote:
remote: We're sorry this build is failing! You can troubleshoot
common issues here:
remote: https://devcenter.heroku.com/articles/troubleshooting-
node-deploys
remote:
remote: If you're stuck, please submit a ticket so we can help:
remote: https://help.heroku.com/
remote:
remote: Love,
remote: Heroku
remote:
remote: ! Push rejected, failed to compile Node.js app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to sheltered-beyond-55397.
remote:
To https://git.heroku.com/sheltered-beyond-55397.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/sheltered-
beyond-55397.git'
我确实看到日志中指出未定义“服务器”的地方,所以这也是我也运行'gulp --production'时的输出:
[00:05:35] Requiring external module babel-register
ReferenceError: server is not defined
at Object.<anonymous> (/home/kingconch/dev_toolbox/projects/frame-
email/frame-email/gulpfile.babel.js:29:43)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at loader (/home/kingconch/dev_toolbox/projects/frame-email/frame-
email/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js]
(/home/kingconch/dev_toolbox/projects/frame-email/frame-
email/node_modules/babel-
register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at requireOrImport (/usr/lib/node_modules/gulp-
cli/lib/shared/require-or-import.js:19:11)
我一直在错误树上努力,没有运气。老实说,我没有主意,而Google帮不上什么忙。谢谢
我正在添加gulpfile.babel.js的内容:
import gulp from 'gulp'
import plugins from 'gulp-load-plugins'
import browser from 'browser-sync'
import rimraf from 'rimraf'
import panini from 'panini'
import yargs from 'yargs'
import lazypipe from 'lazypipe'
import inky from 'inky'
import fs from 'fs'
import siphon from 'siphon-media-query'
import path from 'path'
import merge from 'merge-stream'
import beep from 'beepbeep'
import colors from 'colors'
const $ = plugins()
// Look for the --production flag
const PRODUCTION = !!yargs.argv.production
const EMAIL = yargs.argv.to
// Declare var so that both AWS and Litmus task can use it.
var CONFIG
// Build the 'dist' folder by running all of the below tasks
gulp.task('build', gulp.series(clean, pages, sass, images, inline))
// Build emails, run the server, and watch for file changes
gulp.task('default', gulp.series('build', server, watch))
// Build emails, then send to litmus
gulp.task('litmus', gulp.series('build', creds, aws, litmus))
// Build emails, then send to EMAIL
gulp.task('mail', gulp.series('build', creds, aws, mail))
// Build emails, then zip
gulp.task('zip', gulp.series('build', zip))
// Delete the 'dist' folder
// This happens every time a build starts
function clean (done) {
rimraf('dist', done)
}
// Compile layouts, pages, and partials into flat HTML files
// Then parse using Inky templates
function pages () {
return gulp
.src(['src/pages/**/*.html', '!src/pages/archive/**/*.html'])
.pipe(
panini({
root: 'src/pages',
layouts: 'src/layouts',
partials: 'src/partials',
helpers: 'src/helpers',
data: 'src/data'
})
)
.pipe(inky())
.pipe(gulp.dest('dist'))
}
// Reset Panini's cache of layouts and partials
function resetPages (done) {
panini.refresh()
done()
}
// Compile Sass into CSS
function sass () {
return gulp
.src('src/assets/scss/app.scss')
.pipe($.if(!PRODUCTION, $.sourcemaps.init()))
.pipe(
$.sass({
includePaths: ['node_modules/foundation-emails/scss']
}).on('error', $.sass.logError)
)
.pipe(
$.if(
PRODUCTION,
$.uncss({
html: ['dist/**/*.html']
})
)
)
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest('dist/css'))
}
// Copy and compress images
function images () {
return gulp
.src(['src/assets/img/**/*', '!src/assets/img/archive/**/*'])
.pipe($.imagemin())
.pipe(gulp.dest('./dist/assets/img'))
}
// Inline CSS and minify HTML
function inline () {
return gulp
.src('dist/**/*.html')
.pipe($.if(PRODUCTION, inliner('dist/css/app.css')))
.pipe(gulp.dest('dist'))
}
// Start a server with LiveReload to preview the site in
// function server(done) {
// browser.init({
// server: 'dist'
// })
// done()
// }
// Static server
gulp.task('browser-sync', function () {
browser.init({
server: {
baseDir: './'
}
})
})
// Watch for file changes
function watch () {
gulp
.watch('src/pages/**/*.html')
.on('all', gulp.series(pages, inline, browser.reload))
gulp
.watch(['src/layouts/**/*', 'src/partials/**/*'])
.on('all', gulp.series(resetPages, pages, inline, browser.reload))
gulp
.watch(['../scss/**/*.scss', 'src/assets/scss/**/*.scss'])
.on('all', gulp.series(resetPages, sass, pages, inline, browser.reload))
gulp
.watch('src/assets/img/**/*')
.on('all', gulp.series(images, browser.reload))
}
// Inlines CSS into HTML, adds media query CSS into the <style> tag of the email, and compresses the HTML
function inliner (css) {
var css = fs.readFileSync(css).toString()
var mqCss = siphon(css)
var pipe = lazypipe()
.pipe(
$.inlineCss,
{
applyStyleTags: false,
removeStyleTags: true,
preserveMediaQueries: true,
removeLinkTags: false
}
)
.pipe(
$.replace,
'<!-- <style> -->',
`<style>${mqCss}</style>`
)
.pipe(
$.replace,
'<link rel="stylesheet" type="text/css" href="css/app.css">',
''
)
.pipe(
$.htmlmin,
{
collapseWhitespace: true,
minifyCSS: true
}
)
return pipe()
}
// Ensure creds for Litmus are at least there.
function creds (done) {
var configPath = './config.json'
try {
CONFIG = JSON.parse(fs.readFileSync(configPath))
} catch (e) {
beep()
console.log(
'[AWS]'.bold.red +
' Sorry, there was an issue locating your config.json. Please see README.md'
)
process.exit()
}
done()
}
// Post images to AWS S3 so they are accessible to Litmus and manual test
function aws () {
var publisher = CONFIG.aws
? $.awspublish.create(CONFIG.aws)
: $.awspublish.create()
var headers = {
'Cache-Control': 'max-age=315360000, no-transform, public'
}
return (
gulp
.src('./dist/assets/img/*')
// publisher will add Content-Length, Content-Type and headers specified above
// If not specified it will set x-amz-acl to public-read by default
.pipe(publisher.publish(headers))
// create a cache file to speed up consecutive uploads
// .pipe(publisher.cache())
// print upload updates to console
.pipe($.awspublish.reporter())
)
}
// Send email to Litmus for testing. If no AWS creds then do not replace img urls.
function litmus () {
var awsURL =
!!CONFIG && !!CONFIG.aws && !!CONFIG.aws.url ? CONFIG.aws.url : false
return gulp
.src('dist/**/*.html')
.pipe($.if(!!awsURL, $.replace(/=('|')(\/?assets\/img)/g, '=$1' + awsURL)))
.pipe($.litmus(CONFIG.litmus))
.pipe(gulp.dest('dist'))
}
// Send email to specified email for testing. If no AWS creds then do not replace img urls.
function mail () {
var awsURL =
!!CONFIG && !!CONFIG.aws && !!CONFIG.aws.url ? CONFIG.aws.url : false
if (EMAIL) {
CONFIG.mail.to = [EMAIL]
}
return gulp
.src('dist/**/*.html')
.pipe($.if(!!awsURL, $.replace(/=('|')(\/?assets\/img)/g, '=$1' + awsURL)))
.pipe($.mail(CONFIG.mail))
.pipe(gulp.dest('dist'))
}
// Copy and compress into Zip
function zip () {
var dist = 'dist'
var ext = '.html'
function getHtmlFiles (dir) {
return fs.readdirSync(dir).filter(function (file) {
var fileExt = path.join(dir, file)
var isHtml = path.extname(fileExt) == ext
return fs.statSync(fileExt).isFile() && isHtml
})
}
var htmlFiles = getHtmlFiles(dist)
var moveTasks = htmlFiles.map(function (file) {
var sourcePath = path.join(dist, file)
var fileName = path.basename(sourcePath, ext)
var moveHTML = gulp.src(sourcePath).pipe(
$.rename(function (path) {
path.dirname = fileName
return path
})
)
var moveImages = gulp
.src(sourcePath)
.pipe($.htmlSrc({ selector: 'img' }))
.pipe(
$.rename(function (path) {
path.dirname = fileName + path.dirname.replace('dist', '')
return path
})
)
return merge(moveHTML, moveImages)
.pipe($.zip(fileName + '.zip'))
.pipe(gulp.dest('dist'))
})
return merge(moveTasks)
}