Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to bundle node_modules #580

Merged
merged 7 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pipeline {
withNodeJSEnv(){
dir("${BASE_DIR}"){
sh(label: 'install jest-unit',script: 'npm add --dev jest-junit')
sh(label: 'install e2e bundling deps',script: 'npm install --prefix ./__tests__/e2e')
sh(label: 'Runs the tests',script: './node_modules/.bin/jest --ci --reporters=default --reporters=jest-junit')
}
}
Expand Down
3 changes: 1 addition & 2 deletions __tests__/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
"@elastic/synthetics": "file:../../",
"axios": "^0.21.0",
"semver": "^7.3.5"
},
"dependencies": {}
}
}
30 changes: 26 additions & 4 deletions __tests__/push/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,58 @@ import { join } from 'path';
import { generateTempPath } from '../../src/helpers';
import { Bundler } from '../../src/push/bundler';

const PROJECT_DIR = join(__dirname, 'test-bundler');
const journeyFile = join(PROJECT_DIR, 'bundle.journey.ts');

async function validateZip(content) {
const partialPath = join(
'__tests__',
'push',
'test-bundler',
'bundle.journey.ts'
);
const decoded = Buffer.from(content, 'base64');
const pathToZip = generateTempPath();
await writeFile(pathToZip, decoded);

const files = [];

const entries = createReadStream(pathToZip).pipe(
unzipper.Parse({ forceStream: true })
);

let targetFileContent = null;
for await (const entry of entries) {
files.push(entry.path);

if (entry.path === partialPath) {
entry.on('data', d => (targetFileContent += d));
}
}

expect(files).toEqual(['__tests__/push/test-bundler/bundle.journey.ts']);
expect(files).toEqual([partialPath]);

expect(targetFileContent).toContain('__toESM');
expect(targetFileContent).toContain('node_modules/is-positive/index.js');

await unlink(pathToZip);
}

describe('Bundler', () => {
const PROJECT_DIR = join(__dirname, 'test-bundler');
const journeyFile = join(PROJECT_DIR, 'bundle.journey.ts');
const bundler = new Bundler();

beforeAll(async () => {
await mkdir(PROJECT_DIR, { recursive: true });
await writeFile(
journeyFile,
`import {journey, step, monitor} from '@elastic/synthetics';
import isPositive from 'is-positive';

journey('journey 1', () => {
monitor.use({ id: 'duplicate id' })
step("step1", () => {})
step("step1", () => {
isPositive(-1);
})
});`
);
});
Expand Down
9 changes: 8 additions & 1 deletion __tests__/push/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import * as esbuild from 'esbuild';
import { join } from 'path';
import NodeResolve from '@esbuild-plugins/node-resolve';
import {
MultiAssetPlugin,
commonOptions,
Expand All @@ -43,7 +44,13 @@ describe('Plugin', () => {
await esbuild.build({
...commonOptions(),
entryPoints: [join(E2E_DIR, 'uptime.journey.ts')],
plugins: [MultiAssetPlugin(callback)],
plugins: [
MultiAssetPlugin(callback),
NodeResolve({
extensions: ['.ts', '.js'],
resolveOptions: { basedir: E2E_DIR },
}),
],
});
});
});
104 changes: 81 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"license": "MIT",
"dependencies": {
"@cspotcode/source-map-support": "^0.8.1",
"@esbuild-plugins/node-resolve": "^0.1.4",
"archiver": "^5.3.0",
"commander": "^9.0.0",
"deepmerge": "^4.2.2",
Expand Down Expand Up @@ -68,6 +69,7 @@
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"husky": "^4.3.6",
"is-positive": "3.1.0",
"jest": "^28.1.3",
"lint-staged": "^10.5.3",
"prettier": "^2.4.1",
Expand Down
Loading