feat: better process polyfill

This commit is contained in:
Pooya Parsa 2020-11-13 18:45:03 +01:00
parent eaf4603d9c
commit 989f6811c2
2 changed files with 7 additions and 5 deletions

View File

@ -56,6 +56,7 @@ export const getRollupConfig = (config: SLSOptions) => {
if (config.node === false) { if (config.node === false) {
// Globals // Globals
injects.Buffer = ['buffer', 'Buffer'] injects.Buffer = ['buffer', 'Buffer']
injects.process = '~mocks/node/process'
// Aliases // Aliases
Object.assign(aliases, { Object.assign(aliases, {
@ -63,6 +64,8 @@ export const getRollupConfig = (config: SLSOptions) => {
...mapArrToVal('~mocks/generic', Module.builtinModules), ...mapArrToVal('~mocks/generic', Module.builtinModules),
http: '~mocks/node/http', http: '~mocks/node/http',
fs: '~mocks/node/fs', fs: '~mocks/node/fs',
process: '~mocks/node/process',
'node-process': require.resolve('process/browser.js'),
buffer: require.resolve('buffer/index.js'), buffer: require.resolve('buffer/index.js'),
util: require.resolve('util/util.js'), util: require.resolve('util/util.js'),
events: require.resolve('events/events.js'), events: require.resolve('events/events.js'),
@ -97,9 +100,9 @@ export const getRollupConfig = (config: SLSOptions) => {
} }
if (config.logStartup) { if (config.logStartup) {
options.output.intro += 'global._startTime = process.hrtime();' options.output.intro += 'global._startTime = global.process.hrtime();'
// eslint-disable-next-line no-template-curly-in-string // eslint-disable-next-line no-template-curly-in-string
options.output.outro += 'global._endTime = process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms`);' options.output.outro += 'global._endTime = global.process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms`);'
} }
// https://github.com/rollup/plugins/tree/master/packages/replace // https://github.com/rollup/plugins/tree/master/packages/replace

View File

@ -5,10 +5,9 @@ import { SLSTarget } from '../config'
const polyfill = ` const polyfill = `
const exports = {}; const exports = {};
const module = { exports }; const module = { exports };
const process = { env: {} }; const global = { process: {} };
const global = { process };
const window = global; const window = global;
(function(){const o=Date.now(),t=()=>Date.now()-o;process.hrtime=o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,r=Math.floor(n%1*1e9);return o&&(a-=o[0],r-=o[1],r<0&&(a--,r+=1e9)),[a,r]};})(); (function(){const o=Date.now(),t=()=>Date.now()-o;global.process.hrtime=o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,r=Math.floor(n%1*1e9);return o&&(a-=o[0],r-=o[1],r<0&&(a--,r+=1e9)),[a,r]};})();
` `
export const worker: SLSTarget = { export const worker: SLSTarget = {