Skip to content

Commit

Permalink
🧹 tidy(none): cleanup bud.extensions options (#1885)
Browse files Browse the repository at this point in the history
refers:

- none

## Type of change

**PATCH: backwards compatible change**



This PR includes breaking changes to the following core packages:

- none

This PR includes breaking changes to the follow extensions:

- none

## Dependencies

### Adds

- none

### Removes

- none
  • Loading branch information
kellymears committed Nov 17, 2022
1 parent c5f322b commit 6abb302
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 206 deletions.
79 changes: 14 additions & 65 deletions sources/@roots/bud-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,74 +37,22 @@
"lib/"
],
"type": "module",
"module": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": "./lib/index.js",
"./service": "./lib/service.js",
"./extensions": "./lib/extensions/index.js",
"./cdn": "./lib/extensions/cdn/index.js",
"./esm": "./lib/extensions/esm/index.js",
"./clean-webpack-plugin": "./lib/extensions/clean-webpack-plugin/index.js",
"./copy-webpack-plugin": "./lib/extensions/copy-webpack-plugin/index.js",
"./fix-style-only-entrypoints": "./lib/extensions/fix-style-only-entrypoints/index.js",
"./html-webpack-plugin": "./lib/extensions/html-webpack-plugin/index.js",
"./interpolate-html-webpack-plugin": "./lib/extensions/interpolate-html-webpack-plugin/index.js",
"./mini-css-extract-plugin": "./lib/extensions/mini-css-extract-plugin/index.js",
"./webpack-define-plugin": "./lib/extensions/webpack-define-plugin/index.js",
"./webpack-hot-module-replacement-plugin": "./lib/extensions/webpack-hot-module-replacement-plugin/index.js",
"./webpack-manifest-plugin": "./lib/extensions/webpack-manifest-plugin/index.js",
"./webpack-provide-plugin": "./lib/extensions/webpack-provide-plugin/index.js"
},
"typesVersions": {
"*": {
".": [
"./lib/index.d.ts"
],
"service": [
"./lib/service.d.ts"
],
"extensions": [
"./lib/extensions/index.d.ts"
],
"cdn": [
"./lib/extensions/cdn/index.d.ts"
],
"esm": [
"./lib/extensions/esm/index.d.ts"
],
"fix-style-only-entrypoints": [
"./lib/extensions/fix-style-only-entrypoints/index.d.ts"
],
"clean-webpack-plugin": [
"./lib/extensions/clean-webpack-plugin/index.d.ts"
],
"copy-webpack-plugin": [
"./lib/extensions/copy-webpack-plugin/index.d.ts"
],
"html-webpack-plugin": [
"./lib/extensions/html-webpack-plugin/index.d.ts"
],
"interpolate-html-webpack-plugin": [
"./lib/extensions/interpolate-html-webpack-plugin/index.d.ts"
],
"mini-css-extract-plugin": [
"./lib/extensions/mini-css-extract-plugin/index.d.ts"
],
"webpack-define-plugin": [
"./lib/extensions/webpack-define-plugin/index.d.ts"
],
"webpack-hot-module-replacement-plugin": [
"./lib/extensions/webpack-hot-module-replacement-plugin/index.d.ts"
],
"webpack-manifest-plugin": [
"./lib/extensions/webpack-manifest-plugin/index.d.ts"
],
"webpack-provide-plugin": [
"./lib/extensions/webpack-provide-plugin/index.d.ts"
]
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"./service": {
"types": "./lib/service/index.d.ts",
"default": "./lib/service/index.js"
},
"./*": {
"types": "./lib/extensions/*/index.d.ts",
"default": "./lib/extensions/*/index.js"
}
},
"module": "./lib/index.js",
"types": "./lib/index.d.ts",
"devDependencies": {
"@skypack/package-check": "0.2.2",
"@types/node": "16.18.3",
Expand All @@ -114,6 +62,7 @@
"@roots/bud-framework": "workspace:sources/@roots/bud-framework",
"@roots/bud-support": "workspace:sources/@roots/bud-support",
"@roots/bud-terser": "workspace:sources/@roots/bud-terser",
"@roots/container": "workspace:sources/@roots/container",
"clean-webpack-plugin": "4.0.0",
"copy-webpack-plugin": "11.0.0",
"html-webpack-plugin": "5.5.0",
Expand Down
12 changes: 9 additions & 3 deletions sources/@roots/bud-extensions/src/extensions/cdn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ export interface Options {
}

/**
* `@roots/bud-extensions/cdn
*
* @remarks
* Include remote modules in compilation
*
* @public
* @decorator `@label`
* @decorator `@expose`
* @decorator `@options`
* @decorator `@disabled`
*/
@label(`@roots/bud-extensions/cdn`)
@expose(`cdn`)
Expand All @@ -49,7 +53,7 @@ export interface Options {
@disabled
export default class Cdn extends Extension<Options, null> {
/**
* CDN manifest key to URL mapping
* CDN key to URL mapping
*
* @public
*/
Expand Down Expand Up @@ -99,13 +103,14 @@ export default class Cdn extends Extension<Options, null> {
> {
return Array.from(
new Set([
...(this.app.maybeCall(this.getOption(`allowedUris`)) ?? []),
...this.getOption(`allowedUris`),
...(this.sources.values() ?? []),
]),
).filter(
v => typeof v === `string` || v instanceof RegExp || isFunction(v),
)
}

public set allowedUris(
value:
| Array<string | RegExp | ((uri: string) => boolean)>
Expand Down Expand Up @@ -275,7 +280,8 @@ export default class Cdn extends Extension<Options, null> {
bud.hooks.on(`build.experiments`, experiments => ({
...(experiments ?? {}),
buildHttp: {
allowedUris: this.allowedUris,
allowedUris:
this.allowedUris.length > 0 ? this.allowedUris : undefined,
cacheLocation: this.cacheEnabled ? this.cacheLocation : false,
frozen: this.frozen,
lockfileLocation: this.lockfileLocation,
Expand Down
9 changes: 0 additions & 9 deletions sources/@roots/bud-extensions/src/index.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion sources/@roots/bud-extensions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

import './types.js'

import Extensions from './service.js'
import Extensions from './service/index.js'

export default Extensions
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1

exports[`@roots/bud-extensions > [development] bud.extensions.repository options matches snapshot 1`] = `
exports[`@roots/bud-extensions > bud.extensions.repository options should match snapshot in development 1`] = `
[
"@roots/bud-extensions/cdn",
"@roots/bud-extensions/clean-webpack-plugin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import {factory} from '@repo/test-kit/bud'
import type {WebpackPluginInstance} from '@roots/bud-support/webpack'
import type {Bud} from '@roots/bud-framework/bud'
import type {Modules} from '@roots/bud-framework'
import type {ApplyPlugin} from '@roots/bud-framework/extension'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import Extensions from './index.js'

describe(`@roots/bud-extensions`, () => {
let bud
let extensions

let mockWebpackPlugin: WebpackPluginInstance = {
apply: vi.fn(),
}

let options = {
test: `foo`,
}

let mockModule: any = {
label: `mock_extension`,
register: vi.fn(async () => null),
boot: vi.fn(async () => null),
options: options,
make: vi.fn(async () => mockWebpackPlugin),
when: vi.fn(async () => true),
}
let bud: Bud
let extensions: Extensions

beforeEach(async () => {
bud = await factory()
Expand All @@ -37,9 +22,22 @@ describe(`@roots/bud-extensions`, () => {
it(`add fn registers a module`, async () => {
extensions.repository = {} as any

const options = {test: `foo`}

const mockWebpackPlugin: ApplyPlugin = {apply: vi.fn()}

const mockModule: any = {
label: `mock_extension`,
register: vi.fn(async () => null),
boot: vi.fn(async () => null),
options: options,
make: vi.fn(async () => mockWebpackPlugin),
when: vi.fn(async () => true),
}

await extensions.add(mockModule)

const instance = extensions.get(`mock_extension`)
const instance = extensions.get(`mock_extension` as keyof Modules)
expect(instance.label).toBe(`mock_extension`)

expect(extensions.get(mockModule.label)?.options?.test).toEqual(
Expand All @@ -50,24 +48,34 @@ describe(`@roots/bud-extensions`, () => {
it(`should assign a uuid as key for extensions without names`, async () => {
extensions.repository = {} as any

await extensions.add(
// @ts-ignore
{
register: () => {
// noop
},
const mockExtension = {
register: async () => {
/*noop*/
},
)
}

await extensions.add(mockExtension)

expect(Object.keys(extensions.repository).sort().pop()).toMatch(
/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/,
)
})

it(`should accept a plugin signifier`, async () => {
extensions.repository = {} as any

// @ts-ignore
await extensions.add(`palette-webpack-plugin`)

expect(
Object.values(extensions.repository).sort().pop().constructor.name,
).toBe(`PaletteWebpackPlugin`)
})

it(`should accept a plugin definition`, async () => {
extensions.repository = {} as any

const plugin = await import('palette-webpack-plugin')
const plugin = await import(`palette-webpack-plugin`)
await extensions.add(plugin.default)

expect(
Expand All @@ -78,7 +86,7 @@ describe(`@roots/bud-extensions`, () => {
it(`should accept a plugin instance`, async () => {
extensions.repository = {} as any

const plugin = await import('palette-webpack-plugin')
const plugin = await import(`palette-webpack-plugin`)
// @ts-ignore
const instance = new plugin.default()
await extensions.add(instance)
Expand All @@ -94,7 +102,7 @@ describe(`@roots/bud-extensions`, () => {
await extensions.add(
// @ts-ignore
{
register: () => {
register: async () => {
// noop
},
},
Expand All @@ -108,7 +116,7 @@ describe(`@roots/bud-extensions`, () => {
)
})

it(`[development] bud.extensions.repository options matches snapshot`, async () => {
it(`bud.extensions.repository options should match snapshot in development`, async () => {
bud = await factory({mode: `development`})

const extensions = new Extensions(() => bud)
Expand Down
Loading

0 comments on commit 6abb302

Please sign in to comment.