Created
July 17, 2017 12:42
-
-
Save jordanyaker/772874bd53c8bcf73dbd30e6111a518a to your computer and use it in GitHub Desktop.
Using Gulp to pack Node.js Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.env.NODE_CONFIG_DIR = `${process.cwd()}/config/env`; | |
import gulp from 'gulp'; | |
import requireDir from 'require-dir'; | |
requireDir('./lib/tasks'); | |
gulp.task('default', ['update-project', 'run-webpack'], () => { | |
gulp.src('host.json') | |
.pipe(gulp.dest('.deploy/')); | |
const devBuild = process.env.NODE_ENV !== 'production'; | |
if (devBuild) { | |
gulp.src('local.settings.json') | |
.pipe(gulp.dest('.deploy/')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PackhostGenerator } from 'azure-functions-pack'; | |
import cfg from 'config'; | |
import fs from 'fs'; | |
import glob from 'glob' | |
import gulp from 'gulp'; | |
import tap from 'gulp-tap'; | |
import util from 'gulp-util'; | |
import webpack from 'gulp-webpack'; | |
import path from 'path'; | |
const config = { | |
externals: [], | |
node: { | |
__dirname: false, | |
__filename: false, | |
}, | |
module: { | |
loaders: [ | |
{ test: /\.json$/, loader: 'json-loader' }, | |
{ | |
test: /\.js$/, | |
exclude: /node_modules/, | |
loader: 'babel-loader', | |
query: { | |
presets: ["latest"] | |
} | |
} | |
] | |
}, | |
output: { | |
filename: "index.js", | |
library: "index", | |
libraryTarget: "commonjs2" | |
}, | |
plugins: [], | |
target: "node", | |
}; | |
gulp.task('run-webpack', () => { | |
const ignoredModules = {}; | |
// This will take the config based on the current NODE_ENV and save it to 'build/client.json' | |
// Note: If '/build' does not exist, this command will error; alternatively, write to '/config'. | |
// The webpack alias below will then build that file into the client build. | |
fs.writeFileSync(path.resolve(__dirname, '..', '..', '.deploy/config.json'), JSON.stringify(cfg)); | |
config.resolve = { | |
alias: { | |
config$: path.resolve(__dirname, '..', '..', '.deploy/config.json') | |
} | |
}; | |
gulp.src('.deploy/index.js') | |
.pipe(webpack(config)) | |
.pipe(gulp.dest('.deploy/')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PackhostGenerator } from 'azure-functions-pack'; | |
import fs from 'fs'; | |
import glob from 'glob' | |
import gulp from 'gulp'; | |
import tap from 'gulp-tap'; | |
import path from 'path'; | |
gulp.task('create-host-file', () => { | |
const files = glob.sync('**/function.json'); | |
const importStrings = files.map(file => { | |
const json = JSON.parse(fs.readFileSync(file)); | |
const fnName = path.dirname(file); | |
let fnScript = json.scriptFile; | |
if (typeof fnScript !== 'undefined') { | |
fnScript = path.basename(fnScript); | |
} | |
const fnPath = path.join('..', fnName, fnScript); | |
return `import ${fnName.replace(/\-/g, '$dash')} from '${fnPath}';`; | |
}); | |
let importString = importStrings.reduce( | |
(acc, str, i) => `${acc}${str}\n` | |
, ""); | |
const exportStrings = glob.sync('**/function.json') | |
.map(file => path.dirname(file).replace(/\-/g, '$dash')); | |
let exportString = exportStrings.reduce((acc, str, i) => { | |
return ` ${acc}${str}${(i !== exportStrings.length - 1) ? "," : ""}\n`; | |
} , ""); | |
exportString = `module.exports = {\n${exportString}}`; | |
fs.writeFileSync('.deploy/index.js', `${importString}\n${exportString}`); | |
}); | |
gulp.task('update-project', ['create-host-file'], () => { | |
gulp.src('**/function.json', {read: true}) | |
.pipe(tap((file) => { | |
const fnName = path.basename(path.dirname(file.path)); | |
const fnJson = JSON.parse(file.contents); | |
fnJson.scriptFile = '../index.js'; | |
fnJson.entryPoint = fnName.replace(/\-/g,'$dash'); | |
file.contents = new Buffer(JSON.stringify(fnJson)); | |
})) | |
.pipe(gulp.dest('.deploy/')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment