Skip to content

Commit

Permalink
chore(headless): add case-assist sub package (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
samisayegh authored Apr 23, 2021
1 parent 6ed7b15 commit 257001d
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 76 deletions.
6 changes: 3 additions & 3 deletions packages/atomic/cypress/utils/network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Interception} from 'cypress/types/net-stubbing';
import {Result} from '../../../headless/dist/api/search/search/result';
import {SearchRequest} from '../../../headless/dist/api/search/search/search-request';
import {SearchResponseSuccess} from '../../../headless/dist/api/search/search/search-response';
import {Result} from '../../../headless/dist/definitions/api/search/search/result';
import {SearchRequest} from '../../../headless/dist/definitions/api/search/search/search-request';
import {SearchResponseSuccess} from '../../../headless/dist/definitions/api/search/search/search-response';
import {searchEndpoint} from './setupComponent';

function getApiCall(selector: string): Promise<Interception> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
Breadcrumb,
BreadcrumbValue,
} from '@coveo/headless';
import {RangeFacetValue} from '@coveo/headless/dist/features/facets/range-facets/generic/interfaces/range-facet';
import {BaseFacetValue} from '@coveo/headless/dist/features/facets/facet-api/response';
import {RangeFacetValue} from '@coveo/headless/dist/definitions/features/facets/range-facets/generic/interfaces/range-facet';
import {BaseFacetValue} from '@coveo/headless/dist/definitions/features/facets/facet-api/response';
import mainclear from '../../images/main-clear.svg';
import dayjs from 'dayjs';

Expand Down
9 changes: 9 additions & 0 deletions packages/headless/case-assist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "case-assist",
"description": "Headless Case Assist Module",
"main": "../dist/case-assist/headless.js",
"module": "../dist/case-assist/headless.esm.js",
"browser": "../dist/browser/case-assist/headless.esm.js",
"types": "../dist/definitions/case-assist.index.d.ts",
"license": "Apache-2.0"
}
36 changes: 36 additions & 0 deletions packages/headless/contributors/adding-a-sub-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Adding a sub-package

Headless provides exports through multiple sub packages. A sub-package groups together exports (i.e. controllers, actions, reducers, engines) that work together as a cohesive unit. By separating exports into sub-packages, it becomes clear to users of headless what exports are available to build a use-case.


## To add a new sub-package:

1. Create an entry file for your sub-package inside the `src` directory (e.g. case-assist.ts).
2. Configure nodejs and browser bundles inside `rollup.config.js` for your entry file.
3. Create a new directory with the name of your sub-package at the project root.
4. Inside the new directory, add a `package.json` file and fill in the paths to your bundled files and type definitions.
5. Add the directory name to the `files` array in the project root `package.json` file.

```json
pkg/case-assist/package.json

{
"name": "case-assist",
"description": "Headless Case Assist Module",
"main": "../dist/case-assist/headless.js",
"module": "../dist/case-assist/headless.esm.js",
"browser": "../dist/browser/case-assist/headless.esm.js",
"types": "../dist/definitions/case-assist.index.d.ts",
"license": "Apache-2.0"
}
```

## Testing your sub-package:

1. Build the headless project: `npm run build`.
2. Create a tarball: `npm pack`.
3. Install the tarball as a dependency of a different project: `npm i <path to the tarball>`.
4. Import an export from your sub-package: `import {...} from '@coveo/headless/<sub-package>'`


That's all!
7 changes: 4 additions & 3 deletions packages/headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
"./dist/headless.js": "./dist/browser/headless.js",
"./dist/headless.esm.js": "./dist/browser/headless.esm.js"
},
"types": "./dist/index.d.ts",
"types": "./dist/definitions/index.d.ts",
"license": "Apache-2.0",
"version": "0.11.0-alpha.8",
"files": [
"dist/"
"dist/",
"case-assist/"
],
"scripts": {
"start": "concurrently \"npm run typedefinitions -- -w\" \"rollup -c -w\"",
"build": "npm run clean && npm run build:prod",
"build:prod": "npm run typedefinitions && rollup -c --environment BUILD:production",
"typedefinitions": "tsc -p src/tsconfig.build.json -d --emitDeclarationOnly --declarationDir dist",
"typedefinitions": "tsc -p src/tsconfig.build.json -d --emitDeclarationOnly --declarationDir dist/definitions",
"clean": "rimraf -f -r dist/*",
"test": "jest",
"test:watch": "jest --watch --colors --no-cache --silent=false",
Expand Down
179 changes: 115 additions & 64 deletions packages/headless/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,132 @@ function onWarn(warning, warn) {
warn(warning);
}

const nodeConfig = {
input: 'src/index.ts',
output: [
{file: 'dist/headless.js', format: 'cjs'},
{file: 'dist/headless.esm.js', format: 'es'},
],
plugins: [
resolve({modulesOnly: true}),
commonjs({
// https://github.com/pinojs/pino/issues/688
ignore: ['pino-pretty'],
}),
typescript(),
replace(),
],
external: ['cross-fetch', 'web-encoding'],
onwarn: onWarn,
};

const browserConfig = {

// Node Bundles

const nodejs = [
{
input: 'src/index.ts',
outDir: 'dist',
},
{
input: 'src/case-assist.index.ts',
outDir: 'dist/case-assist'
}
].map(buildNodeConfiguration);

function buildNodeConfiguration({input, outDir}) {
return {
input,
output: [
{file: `${outDir}/headless.js`, format: 'cjs'},
{file: `${outDir}/headless.esm.js`, format: 'es'},
],
plugins: [
resolve({modulesOnly: true}),
commonjs({
// https://github.com/pinojs/pino/issues/688
ignore: ['pino-pretty'],
}),
typescript(),
replace(),
],
external: ['cross-fetch', 'web-encoding'],
onwarn: onWarn,
};
}


// Browser Bundles

const browser = [
{
input: 'src/index.ts',
output: [
buildUmdOutput('dist/browser', 'CoveoHeadless'),
buildEsmOutput('dist/browser')
]
},
{
input: 'src/case-assist.index.ts',
output: [
buildUmdOutput('dist/browser/case-assist', 'CoveoHeadlessCaseAssist'),
buildEsmOutput('dist/browser/case-assist')
]
}
].map(buildBrowserConfiguration);

function buildBrowserConfiguration({input, output}) {
return {
input,
output,
plugins: [
alias({
entries: [
{
find: 'coveo.analytics',
replacement: pathResolve(
__dirname,
'./node_modules/coveo.analytics/dist/library.es.js'
),
},
{
find: 'cross-fetch',
replacement: pathResolve(__dirname, './fetch-ponyfill.js'),
},
{
find: 'web-encoding',
replacement: pathResolve(__dirname, './node_modules/web-encoding/src/lib.js'),
}
],
}),
resolve({browser: true}),
commonjs(),
typescript(),
replace(),
isProduction && sizeSnapshot(),
isProduction && terser(),
],
}
}

function buildUmdOutput(outDir, name) {
return {
file: `${outDir}/headless.js`,
format: 'umd',
name,
sourcemap: isProduction
}
}

function buildEsmOutput(outDir) {
return {
file: `${outDir}/headless.esm.js`,
format: 'es',
sourcemap: isProduction,
}
}



// For Atomic's development purposes only
const dev = buildBrowserConfiguration({
input: 'src/index.ts',
output: [
{
file: 'dist/browser/headless.js',
format: 'umd',
name: 'CoveoHeadless',
sourcemap: isProduction,
},
{
file: 'dist/browser/headless.esm.js',
format: 'es',
sourcemap: isProduction,
},
// For Atomic's development purposes only
{file: '../atomic/src/external-builds/headless.esm.js', format: 'es'},
],
plugins: [
alias({
entries: [
{
find: 'coveo.analytics',
replacement: pathResolve(
__dirname,
'./node_modules/coveo.analytics/dist/library.es.js'
),
},
{
find: 'cross-fetch',
replacement: pathResolve(__dirname, './fetch-ponyfill.js'),
},
{
find: 'web-encoding',
replacement: pathResolve(__dirname, './node_modules/web-encoding/src/lib.js'),
}
],
}),
resolve({browser: true}),
commonjs(),
typescript(),
replace(),
isProduction && sizeSnapshot(),
isProduction && terser(),
],
};
buildEsmOutput('../atomic/src/external-builds')
]
});


// Api-extractor cannot resolve import() types, so we use dts to create a file that api-extractor
// can consume. When the api-extractor limitation is resolved, this step will not be necessary.
// [https://github.com/microsoft/rushstack/issues/1050]
const typeDefinitions = {
input: "./dist/index.d.ts",
input: "./dist/definitions/index.d.ts",
output: [{file: "temp/headless.d.ts", format: "es"}],
plugins: [dts()]
}

const config = isProduction ? [nodeConfig, typeDefinitions, browserConfig] : [browserConfig];
const config = isProduction ? [...nodejs, typeDefinitions, ...browser] : [dev];

export default config;
3 changes: 3 additions & 0 deletions packages/headless/src/case-assist.index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function test() {
return 'test';
}
8 changes: 4 additions & 4 deletions packages/quantic/coveo.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable node/no-unpublished-import */
import * as HeadlessTypes from './force-app/main/default/staticresources/coveoheadless/index';
export * from './force-app/main/default/staticresources/coveoheadless/index';
import * as HeadlessTypes from './force-app/main/default/staticresources/coveoheadless/definitions/index';
export * from './force-app/main/default/staticresources/coveoheadless/definitions/index';
import {LightningElement} from 'lwc';
import {HeadlessEngine} from '../headless/dist/index';

declare global {
// eslint-disable-next-line no-undef
const CoveoHeadless: typeof HeadlessTypes;
Expand All @@ -15,7 +15,7 @@ declare global {
initialized: boolean;
}[];
config: HeadlessConfigurationOptions;
engine: HeadlessEngine;
engine: HeadlessTypes.HeadlessEngine;
};
};
}
Expand Down

0 comments on commit 257001d

Please sign in to comment.