Skip to content

Commit

Permalink
ci: fix and make runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Oct 9, 2024
1 parent b132073 commit 94a1883
Show file tree
Hide file tree
Showing 13 changed files with 1,555 additions and 1,165 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN apk add --no-cache libstdc++ dumb-init git

USER node

COPY --from=builder /app/out2.js /app/index.js
COPY --from=builder /app/out2.cjs /app/index.js

ENV CONFIG_LOCATION=/app/config/config.yml
ENV SECRETS_LOCATION=/app/config/secrets.yml
Expand Down
14 changes: 12 additions & 2 deletions esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import esbuild from "esbuild";

const externalizedModules = {};

await esbuild.build({
//inject: ["cjs-shim.ts"],
entryPoints: ["index.ts"],
bundle: true,
sourcemap: "inline",
platform: "node",
target: "node20",
//external: ["fs", "child_process", "crypto", "os", "path"],
//plugins: [externalNativeModulesPlugin(externalizedModules)],

format: "cjs",
external: ["fs", "child_process", "crypto", "os", "path"],
outfile: "out2.js",
outfile: "out2.cjs",

//format: "esm",
//outfile: "out2.mjs",
});
3 changes: 3 additions & 0 deletions native_modules/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://esbuild.github.io/plugins/[esbuild plugin] to handle https://nodejs.org/dist/latest-v16.x/docs/api/addons.html[NodeJS native modules]

The source code here was shamelessly taken directly from the https://github.com/netlify/zip-it-and-ship-it/tree/v4.23.6/src/runtimes/node/native_modules[Netlify zip-it-and-ship-it repository].
19 changes: 19 additions & 0 deletions native_modules/detector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { extname } from 'node:path';

const markerModules = ['bindings', 'nan', 'node-gyp', 'node-gyp-build', 'node-pre-gyp', 'prebuild']

export const isNativeModule = ({ binary, dependencies = {}, devDependencies = {}, files = [], gypfile }) => {
if (binary || gypfile) {
return true
}

const hasMarkerModule = markerModules.some((marker) => dependencies[marker] || devDependencies[marker])

if (hasMarkerModule) {
return true
}

const hasBinaryFile = files.some((path) => !path.startsWith('!') && extname(path) === '.node')

return hasBinaryFile
}
67 changes: 67 additions & 0 deletions native_modules/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import path from 'node:path';

import readPackageJson from 'read-package-json-fast';

import { isNativeModule } from './detector.js';

// Filters out relative or absolute file paths.
const packageFilter = /^([^./]*)$/

// Filters valid package names and extracts the base directory.
const packageName = /^([^@][^/]*|@[^/]*\/[^/]+)(?:\/|$)/

const findNativeModule = (packageJsonPath, cache) => {
if (cache[packageJsonPath] === undefined) {
cache[packageJsonPath] = readPackageJson(packageJsonPath).then(
(data) => [Boolean(isNativeModule(data), data), data],
() => [],
)
}

return cache[packageJsonPath]
}

export const externalNativeModulesPlugin = (externalizedModules) => ({
name: 'external-native-modules',
setup(build) {
const cache = {}

build.onResolve({ filter: packageFilter }, async (args) => {
const packageJson = packageName.exec(args.path)

if (!packageJson) return

let directory = args.resolveDir

while (true) {
if (path.basename(directory) !== 'node_modules') {
const modulePath = path.join(directory, 'node_modules', packageJson[1])
const packageJsonPath = path.join(modulePath, 'package.json')
const [isNative, packageJsonData] = await findNativeModule(packageJsonPath, cache)

if (isNative === true) {
if (externalizedModules[args.path] === undefined) {
externalizedModules[args.path] = {}
}

externalizedModules[args.path][modulePath] = packageJsonData.version

return { path: args.path, external: true }
}

if (isNative === false) {
return
}
}

const parentDirectory = path.dirname(directory)

if (parentDirectory === directory) {
break
}

directory = parentDirectory
}
})
},
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@infinite-debugger/swagger-to-ts": "0.1.0-alpha.124",
"axios": "1.7.7",
"dotenv": "16.4.5",
"node-ts": "6.1.3",
Expand All @@ -28,9 +27,11 @@
},
"devDependencies": {
"@hyrious/esbuild-plugin-commonjs": "^0.2.4",
"@infinite-debugger/swagger-to-ts": "0.1.0-alpha.124",
"@playwright/test": "1.47.2",
"@types/node": "22.7.4",
"@vitest/coverage-v8": "2.1.1",
"esbuild": "^0.24.0",
"prettier": "3.3.3",
"typescript": "5.6.2",
"vitest": "2.1.1"
Expand Down
Loading

0 comments on commit 94a1883

Please sign in to comment.