diff --git a/CHANGELOG.md b/CHANGELOG.md index 09d844f42..b532635d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ ## Main +Ensure that [babel ignores](https://babeljs.io/docs/options#ignore) do not cause the transpiler to fall over, by supporting the +`null` return from `loadOptions` which occurs when a file is ignored. ## 12.3.3 @@ -2118,6 +2120,7 @@ Not usable for others, only stubs of classes etc. - [@orta] [@tibdex]: https://github.com/tibdex [@tim3trick]: https://github.com/tim3trick [@thawankeane]: https://github.com/thawankeane +[@tomstrepsil: https://github.com/TomStrepsil] [@tychota]: https://github.com/tychota [@urkle]: https://github.com/urkle [@valscion]: https://github.com/valscion diff --git a/source/platforms/_tests/fixtures/bbc-dsl-input.json b/source/platforms/_tests/fixtures/bbc-dsl-input.json index ef8bdfae5..d24a73674 100644 --- a/source/platforms/_tests/fixtures/bbc-dsl-input.json +++ b/source/platforms/_tests/fixtures/bbc-dsl-input.json @@ -2542,7 +2542,8 @@ "settings": { "github": { "accessToken": "12345", - "additionalHeaders": {} + "additionalHeaders": {}, + "baseURL": "https://api.github.com" }, "cliArgs": {} } diff --git a/source/platforms/_tests/fixtures/bbs-dsl-input.json b/source/platforms/_tests/fixtures/bbs-dsl-input.json index aa8549c62..4f6c6e773 100644 --- a/source/platforms/_tests/fixtures/bbs-dsl-input.json +++ b/source/platforms/_tests/fixtures/bbs-dsl-input.json @@ -1087,7 +1087,8 @@ "settings": { "github": { "accessToken": "12345", - "additionalHeaders": {} + "additionalHeaders": {}, + "baseURL": "https://api.github.com" }, "cliArgs": {} } diff --git a/source/runner/runners/utils/_tests/_transpiler.test.ts b/source/runner/runners/utils/_tests/_transpiler.test.ts index f50324c52..657874467 100644 --- a/source/runner/runners/utils/_tests/_transpiler.test.ts +++ b/source/runner/runners/utils/_tests/_transpiler.test.ts @@ -2,13 +2,15 @@ jest.mock("fs", () => ({ readFileSync: jest.fn(), realpathSync: {}, existsSync: jest.fn(), + statSync: jest.fn(), + stat: jest.fn(), })) jest.mock("path", () => { const path = jest.requireActual("path") return { ...path, resolve: jest.fn(path.resolve) } }) -import { typescriptify, lookupTSConfig, dirContains } from "../transpiler" +import { typescriptify, lookupTSConfig, dirContains, babelify } from "../transpiler" import * as fs from "fs" import * as path from "path" @@ -87,3 +89,25 @@ describe("dirContains", () => { }) } }) + +describe("babelify", () => { + it("does not throw when a filepath is ignored due to babel options, and should return the content unchanged", () => { + const dangerfile = `import { a } from 'lodash'; +a();` + + const existsSyncMock = fs.existsSync as jest.Mock + const actualFs = jest.requireActual("fs") as typeof fs + existsSyncMock.mockImplementation((path) => path === "/a/b/babel.config.js" || actualFs.existsSync(path)) + jest.mock( + "/a/b/babel.config.js", + () => { + return { ignore: ["/a/b"] } + }, + { virtual: true } + ) + const act = () => babelify(dangerfile, "a/b/", []) + + expect(act).not.toThrow() + expect(act()).toEqual(dangerfile) + }) +}) diff --git a/source/runner/runners/utils/transpiler.ts b/source/runner/runners/utils/transpiler.ts index 15ca593fd..46d325b9e 100644 --- a/source/runner/runners/utils/transpiler.ts +++ b/source/runner/runners/utils/transpiler.ts @@ -172,7 +172,7 @@ export const babelify = (content: string, filename: string, extraPlugins: string return content } - const options = babel.loadOptions ? babel.loadOptions({ filename }) : { plugins: [] } + const options = babel.loadOptions?.({ filename }) ?? { plugins: [] } const fileOpts = { filename, @@ -186,7 +186,7 @@ export const babelify = (content: string, filename: string, extraPlugins: string const result = transformSync(content, fileOpts) d("Result from Babel:") d(result) - return result.code + return result?.code ?? content } export default (code: string, filename: string, remoteFile: boolean = false) => {