Dan Van Brunt
01/19/2022, 10:53 PMsst.function
?
Seems like there are only two options:
1. add "type": "module",
to package.json
which don’t think works since we only have a single package.json for all out lambdas
2. save the file as extension *.cjs
do we have access to this with sst.Function
?thdxr
01/19/2022, 11:16 PMthdxr
01/19/2022, 11:16 PMDan Van Brunt
01/20/2022, 12:00 AMDan Van Brunt
01/20/2022, 12:01 AMcjs
so does that mean that all functions are already by default saved with file extension cjs
?Dan Van Brunt
01/20/2022, 12:03 AMthdxr
01/20/2022, 12:08 AMthdxr
01/20/2022, 12:08 AMthdxr
01/20/2022, 12:10 AMDan Van Brunt
01/20/2022, 12:14 AMthdxr
01/20/2022, 12:26 AMthdxr
01/20/2022, 12:26 AMDan Van Brunt
01/20/2022, 12:28 AMDan Van Brunt
01/20/2022, 12:28 AMthdxr
01/20/2022, 12:32 AMDan Van Brunt
01/20/2022, 12:33 AMRoss Gerbasi
01/20/2022, 12:44 AMthdxr
01/20/2022, 12:44 AMRoss Gerbasi
01/20/2022, 12:45 AMRoss Gerbasi
01/20/2022, 12:45 AMthdxr
01/20/2022, 12:45 AMthdxr
01/20/2022, 12:45 AMDan Van Brunt
01/20/2022, 12:46 AMthdxr
01/20/2022, 12:49 AMthdxr
01/20/2022, 12:50 AMRoss Gerbasi
01/20/2022, 12:50 AMRoss Gerbasi
01/20/2022, 12:50 AMthdxr
01/20/2022, 12:51 AMthdxr
01/20/2022, 12:52 AMthdxr
01/20/2022, 12:52 AMRoss Gerbasi
01/20/2022, 12:52 AMthdxr
01/20/2022, 12:52 AMRoss Gerbasi
01/20/2022, 12:52 AMRoss Gerbasi
01/20/2022, 12:53 AMapp.js
Ross Gerbasi
01/20/2022, 1:13 AMRoss Gerbasi
01/20/2022, 1:14 AMpackage.json
to type: module
, then swap my esbuild format to
bundle: {
format: 'esm',
},
Dan Van Brunt
01/20/2022, 1:15 AMRoss Gerbasi
01/20/2022, 1:15 AMDan Van Brunt
01/20/2022, 1:15 AMRoss Gerbasi
01/20/2022, 1:15 AMRoss Gerbasi
01/20/2022, 1:16 AMRoss Gerbasi
01/20/2022, 1:17 AMnode.js
file from this https://github.com/serverless-stack/serverless-stack/blob/master/packages/core/src/runtime/handler/node.ts#L156Ross Gerbasi
01/20/2022, 1:17 AMconst {commonjs} = await import ('@hyrious/esbuild-plugin-commonjs')
const result = await esbuild.build({
...config,
plugins: [commonjs()], //? require(plugins) : undefined,
metafile: true,
minify: false,
incremental: true,
});
thdxr
01/20/2022, 1:17 AMRoss Gerbasi
01/20/2022, 1:17 AMthdxr
01/20/2022, 1:17 AMRoss Gerbasi
01/20/2022, 1:18 AMthdxr
01/20/2022, 1:18 AMthdxr
01/20/2022, 1:18 AMRoss Gerbasi
01/20/2022, 1:19 AM@hyrious/esbuild-plugin-commonjs
with SST and flipping it on as aplugin when using ESM... maybe some other option we can pass in? transformCommonJS or something?Ross Gerbasi
01/20/2022, 1:20 AMRoss Gerbasi
01/20/2022, 1:20 AMRoss Gerbasi
01/20/2022, 1:20 AMthdxr
01/20/2022, 1:20 AMRoss Gerbasi
01/20/2022, 1:20 AMRoss Gerbasi
01/20/2022, 1:20 AMRoss Gerbasi
01/20/2022, 1:21 AMRoss Gerbasi
01/20/2022, 1:21 AMRoss Gerbasi
01/20/2022, 1:21 AMRoss Gerbasi
01/20/2022, 1:22 AM// node_modules/jwt-simple/lib/jwt.js
import __import_crypto from "crypto";
var require_jwt = __commonJS({
"node_modules/jwt-simple/lib/jwt.js"(exports, module) {
var crypto = __import_crypto;
Ross Gerbasi
01/20/2022, 1:22 AMthdxr
01/20/2022, 1:25 AMRoss Gerbasi
01/20/2022, 1:25 AMrequire('path')
or something? what the heck man hahathdxr
01/20/2022, 1:54 PMthdxr
01/24/2022, 8:43 PMthdxr
01/24/2022, 8:43 PMthdxr
01/24/2022, 8:43 PMswc
compiles more correctlyRoss Gerbasi
01/24/2022, 8:43 PMRoss Gerbasi
01/24/2022, 8:43 PMthdxr
01/25/2022, 4:12 AMRoss Gerbasi
01/25/2022, 4:43 AMthdxr
01/26/2022, 8:18 PMthdxr
01/26/2022, 8:19 PMconst ESM_HACK_REGEX =
/\b__require\("(_http_agent|_http_client|_http_common|_http_incoming|_http_outgoing|_http_server|_stream_duplex|_stream_passthrough|_stream_readable|_stream_transform|_stream_wrap|_stream_writable|_tls_common|_tls_wrap|assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|diagnostics_channel|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|wasi|worker_threads|zlib)"\)/gm;
function esmHack(target: string) {
const data = fs.readFileSync(target, "utf-8");
const modules: Record<string, string> = {};
const out = data.replace(ESM_HACK_REGEX, (_, mod) => {
const id = "__import_" + mod.toUpperCase();
modules[mod] = id;
return id;
});
fs.writeFileSync(
target,
[
...Object.entries(modules).map(
([key, val]) => `import ${val} from ${JSON.stringify(key)};`
),
out,
].join("\n")
);
}
thdxr
01/26/2022, 8:19 PMjsonwebtoken
+ uuid
librarythdxr
01/26/2022, 8:19 PMRoss Gerbasi
01/26/2022, 8:24 PMthdxr
01/26/2022, 8:25 PMRoss Gerbasi
01/26/2022, 8:26 PMthdxr
01/26/2022, 8:28 PMthdxr
01/26/2022, 8:28 PMRoss Gerbasi
01/26/2022, 8:28 PMthdxr
01/26/2022, 8:28 PMRoss Gerbasi
01/26/2022, 8:29 PMimport jwt from 'jwt-simple'
console.log(jwt)
Ross Gerbasi
01/26/2022, 8:29 PM<http://localhost:3000/node_modules/.vite/jwt-simple.js?v=1c43ceb5>
thdxr
01/26/2022, 8:30 PMRoss Gerbasi
01/26/2022, 8:30 PMthdxr
01/26/2022, 8:30 PMRoss Gerbasi
01/26/2022, 8:30 PMthdxr
01/26/2022, 8:30 PMRoss Gerbasi
01/26/2022, 8:32 PMRoss Gerbasi
01/26/2022, 8:32 PMjwt.decode
it throw an error in the browser Buffer
is not definedRoss Gerbasi
01/26/2022, 8:32 PMthdxr
01/26/2022, 8:32 PMRoss Gerbasi
01/26/2022, 8:33 PMthdxr
01/26/2022, 8:33 PMRoss Gerbasi
01/26/2022, 8:33 PMRoss Gerbasi
01/26/2022, 8:33 PMthdxr
01/26/2022, 8:33 PMRoss Gerbasi
01/26/2022, 8:35 PMRoss Gerbasi
01/26/2022, 8:36 PMnpx vite build --ssr main.js
Ross Gerbasi
01/26/2022, 8:36 PM'use strict'
var jwt = require('jwt-simple')
function _interopDefaultLegacy(e) {
return e && typeof e === 'object' && 'default' in e ? e : { default: e }
}
Ross Gerbasi
01/26/2022, 8:36 PMthdxr
01/26/2022, 8:37 PMRoss Gerbasi
01/26/2022, 8:39 PMthdxr
01/26/2022, 8:39 PMRoss Gerbasi
01/26/2022, 8:39 PMthdxr
01/26/2022, 8:39 PMRoss Gerbasi
01/26/2022, 8:40 PMRoss Gerbasi
01/26/2022, 8:40 PMvite --ssr
though...thdxr
01/26/2022, 8:41 PMRoss Gerbasi
01/26/2022, 8:42 PMthdxr
01/26/2022, 8:45 PMthdxr
01/26/2022, 8:45 PMRoss Gerbasi
01/26/2022, 8:45 PMRoss Gerbasi
01/26/2022, 8:45 PMRoss Gerbasi
01/26/2022, 8:45 PMthdxr
01/26/2022, 8:46 PMRoss Gerbasi
01/26/2022, 8:46 PMthdxr
01/26/2022, 8:46 PMRoss Gerbasi
01/26/2022, 8:46 PMRoss Gerbasi
01/26/2022, 8:47 PMthdxr
01/26/2022, 8:48 PMthdxr
01/26/2022, 8:48 PMthdxr
01/26/2022, 8:52 PMRoss Gerbasi
01/26/2022, 9:03 PMRoss Gerbasi
01/26/2022, 9:03 PMmain.ts
import JWT from 'jwt-simple'
console.log(JWT)
const sleep = () => new Promise((resolve) => setTimeout(resolve, 2000))
const g = await sleep()
console.log(g)
export const handler = async (event) => {
return {
event,
statusCode: 200,
body: 'Hello World!',
}
}
Ross Gerbasi
01/26/2022, 9:04 PMnpx esbuild main.ts --format=esm --platform=node > main-es.js
but not a bundle. simply just the TS transform to JSRoss Gerbasi
01/26/2022, 9:05 PMimport { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: './main-es.js',
output: {
format: 'es',
},
plugins: [nodeResolve(), commonjs()]
}
I run
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: './main-es.js',
output: {
format: 'es',
},
plugins: [nodeResolve(), commonjs()]
}
Ross Gerbasi
01/26/2022, 9:05 PMRoss Gerbasi
01/26/2022, 9:05 PMthdxr
01/26/2022, 9:06 PMRoss Gerbasi
01/26/2022, 9:06 PMRoss Gerbasi
01/26/2022, 9:06 PMRoss Gerbasi
01/26/2022, 9:06 PMRoss Gerbasi
01/26/2022, 9:06 PMthdxr
01/26/2022, 9:07 PMthdxr
01/26/2022, 9:08 PMthdxr
01/26/2022, 9:08 PMmysql2
will work locally but not in prod until you add it to bundle.nodeModules
Ross Gerbasi
01/26/2022, 9:09 PMRoss Gerbasi
01/26/2022, 9:09 PMRoss Gerbasi
01/26/2022, 9:09 PMthdxr
01/26/2022, 9:11 PMswc
as well but it doesn't do bundling well yet 😢thdxr
01/26/2022, 9:12 PMRoss Gerbasi
01/26/2022, 9:12 PMthdxr
01/26/2022, 9:13 PMRoss Gerbasi
01/26/2022, 9:16 PMRoss Gerbasi
01/27/2022, 3:21 PMRoss Gerbasi
01/27/2022, 3:24 PMRoss Gerbasi
01/27/2022, 3:25 PMmaint.ts
import JWT from 'jwt-simple'
console.log(JWT)
const sleep = () => new Promise((resolve) => setTimeout(() => resolve('yup'), 2000))
const h = await sleep()
console.log(h)
const handler = (event: string) => {
return {
statusCode: 200,
body: 'yay',
}
}
Ross Gerbasi
01/27/2022, 3:26 PMpackage.json
is set to "type": "module"
and "main":"main.bundle.js"
Ross Gerbasi
01/27/2022, 3:26 PMbuild.js
is
import esbuild from 'esbuild'
esbuild
.build({
entryPoints: ['./main.js'],
bundle: true,
platform: 'node',
format: 'esm',
outfile: './main.bundle.js',
banner: {
js: [
`import { createRequire as topLevelCreateRequire } from 'module'`,
`const require = topLevelCreateRequire(import.meta.url)`,
].join('\n'),
},
})
.catch(() => process.exit(1))
Ross Gerbasi
01/27/2022, 3:27 PMnode build.js
then node .
results in
{
version: '0.5.6',
decode: [Function: jwt_decode],
encode: [Function: jwt_encode]
}
yup
with the top level await delay just before the yup
Ross Gerbasi
01/27/2022, 3:29 PMesm
and we are good to go.Ross Gerbasi
01/31/2022, 3:15 PMthdxr
01/31/2022, 3:17 PMthdxr
01/31/2022, 3:17 PMthdxr
01/31/2022, 3:17 PMulid
alsoRoss Gerbasi
01/31/2022, 3:19 PMthdxr
01/31/2022, 6:18 PMthdxr
01/31/2022, 6:18 PMRoss Gerbasi
01/31/2022, 7:52 PMRoss Gerbasi
01/31/2022, 7:52 PM