Skip to content

Commit

Permalink
feat(cli): support plugin entrypoint when validating entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mediremi authored and KaiVandivier committed Aug 23, 2022
1 parent c64a082 commit 04ece0a
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 6 deletions.
32 changes: 26 additions & 6 deletions cli/src/lib/compiler/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,35 @@ exports.verifyEntrypoints = ({
resolveModule = require.resolve,
}) => {
if (config.type === 'app') {
verifyEntrypoint({
entrypoint: config.entryPoints.app,
basePath: paths.base,
resolveModule,
})
if (
!config.entryPoints ||
(!config.entryPoints.app && !config.entryPoints.plugin)
) {
throw new Error('Apps must define an app or plugin entrypoint')
}

if (config.entryPoints.app) {
verifyEntrypoint({
entrypoint: config.entryPoints.app,
basePath: paths.base,
resolveModule,
})
}
if (config.entryPoints.plugin) {
verifyEntrypoint({
entrypoint: config.entryPoints.plugin,
basePath: paths.base,
resolveModule,
})
}
return
}

const verifyLibraryEntrypoint = (entrypoint) => {
if (!config.entryPoints || !config.entryPoints.lib) {
throw new Error('Libraries must define a lib entrypoint')
}

const verifyLibraryEntrypoint = entrypoint => {
switch (typeof entrypoint) {
case 'string':
verifyEntrypoint({
Expand Down
162 changes: 162 additions & 0 deletions cli/src/lib/compiler/entrypoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,166 @@ describe('verifyEntrypoints', () => {
)
expect(resolveModule).toHaveBeenCalledTimes(3)
})

it(`does not throw an error if a plugin's entrypoint is located in the 'src' directory`, () => {
const resolveModule = jest.fn()

const appConfig1 = {
type: 'app',
entryPoints: {
plugin: './src/plugin.js',
},
}
expect(() =>
verifyEntrypoints({
config: appConfig1,
paths: {
base: '/the/base/path',
},
resolveModule,
})
).not.toThrow()

const appConfig2 = {
type: 'app',
entryPoints: {
plugin: 'src/plugin.js',
},
}
expect(() =>
verifyEntrypoints({
config: appConfig2,
paths: {
base: '/the/base/path',
},
resolveModule,
})
).not.toThrow()
})

it(`throws an error if a plugin's entrypoint is not located in the 'src' directory`, () => {
const appConfig = {
type: 'app',
entryPoints: {
plugin: 'plugin.js',
},
}
expect(() =>
verifyEntrypoints({
config: appConfig,
paths: {
base: '/the/base/path',
},
})
).toThrow(
`Entrypoint plugin.js must be located within the ./src directory`
)
})

it(`does not throw an error if a plugin's entrypoint exists`, () => {
const appConfig = {
type: 'app',
entryPoints: {
plugin: './src/plugin.js',
},
}
const resolveModule = jest.fn()

expect(() =>
verifyEntrypoints({
config: appConfig,
paths: {
base: '/the/base/path',
},
resolveModule,
})
).not.toThrow()
expect(resolveModule).toHaveBeenCalledWith(
'/the/base/path/src/plugin.js'
)
})

it(`throws an error if a plugin's entrypoint does not exist`, () => {
const appConfig = {
type: 'app',
entryPoints: {
plugin: './src/plugin.js',
},
}
const resolveModule = jest.fn(path => {
throw new Error(`Cannot find module '${path}'`)
})

const expectedError = 'Could not resolve entrypoint ./src/plugin.js'
expect(() =>
verifyEntrypoints({
config: appConfig,
paths: {
base: '/the/base/path',
},
resolveModule,
})
).toThrow(expectedError)
expect(reporter.error).toHaveBeenCalledWith(expectedError)
expect(resolveModule).toHaveBeenCalledWith(
'/the/base/path/src/plugin.js'
)
})

it(`throws an error if an app does not define any entrypoints`, () => {
const resolveModule = jest.fn()
const paths = {
base: '/the/base/path',
}

const appConfig1 = {
type: 'app',
entryPoints: {},
}
const appConfig2 = {
type: 'app',
entryPoints: undefined,
}

const expectedAppError = 'Apps must define an app or plugin entrypoint'
expect(() =>
verifyEntrypoints({
config: appConfig1,
paths,
resolveModule,
})
).toThrow(expectedAppError)
expect(() =>
verifyEntrypoints({
config: appConfig2,
paths,
resolveModule,
})
).toThrow(expectedAppError)

const libConfig1 = {
type: 'lib',
entryPoints: {},
}
const libConfig2 = {
type: 'lib',
entryPoints: undefined,
}

const expectedLibError = 'Libraries must define a lib entrypoint'
expect(() =>
verifyEntrypoints({
config: libConfig1,
paths,
resolveModule,
})
).toThrow(expectedLibError)
expect(() =>
verifyEntrypoints({
config: libConfig2,
paths,
resolveModule,
})
).toThrow(expectedLibError)
})
})

0 comments on commit 04ece0a

Please sign in to comment.