Skip to content

Commit

Permalink
Support @bentoproject/core (#37386)
Browse files Browse the repository at this point in the history
* begin building bentoproject/core
* surface extension css over npm
* fix remap dependencies to handle tsx
  * handle trailing slash in importPath (necessary when remap-ing non-node_modules)
* support building extensions using bentoproject/core
  • Loading branch information
kvchari authored Feb 8, 2022
1 parent 1d8443e commit 1ba0a82
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 45 deletions.
13 changes: 13 additions & 0 deletions build-system/compile/bundles.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ exports.jsBundles = {
aliasName: 'custom-elements-polyfill.js',
},
},
'bento.core.js': {
// This file is generated, so we find its source in the build/ dir
// See compileBentoCore() and generateBentoCoreEntrypoint()
srcDir: 'build/',
srcFilename: 'bento.shared.js',
destDir: './src/bento/core/dist',
minifiedDestDir: './src/bento/core/dist',
options: {
includePolyfills: false,
toName: 'bento.core.max.js',
minifiedName: 'bento.core.js',
},
},
'alp.max.js': {
srcDir: './ads/alp/',
srcFilename: 'install-alp.js',
Expand Down
44 changes: 35 additions & 9 deletions build-system/compile/generate/bento.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ const dedent = require('dedent');
const {getSharedBentoSymbols} = require('./shared-bento-symbols');

/**
* @param {Object<string, string[]>} packageSymbols
* @param {Object<string, string[]>} [packageSymbols]
* @return {string}
*/
function generateBentoRuntimeEntrypoint(
packageSymbols = getSharedBentoSymbols()
) {
function generateBentoRuntimeEntrypoint(packageSymbols) {
packageSymbols = packageSymbols || getSharedBentoSymbols();

assertNoDupes(Object.values(packageSymbols).flat());
return dedent(`
import {isEsm} from '#core/mode';
import {install as installCustomElements} from '#polyfills/custom-elements';
${Object.entries(packageSymbols)
.map(
([name, symbols]) => `import {${symbols.join(', ')}} from '${name}';`
)
.join('\n')}
${generateEsImportExports(packageSymbols)}
if (!isEsm()) {
installCustomElements(self, class {});
Expand Down Expand Up @@ -55,6 +51,35 @@ function generateBentoRuntimeEntrypoint(
`);
}

/**
* @param {Object<string, string[]>} [packageSymbols]
* @return {string}
*/
function generateBentoCoreEntrypoint(packageSymbols) {
packageSymbols = packageSymbols || getSharedBentoSymbols();

assertNoDupes(Object.values(packageSymbols).flat());
return dedent(`
${generateEsImportExports(packageSymbols, true)}
`);
}

/**
*
* @param {Object<string, string[]>} packageSymbols
* @param {boolean} generateExport
* @return {string}
*/
function generateEsImportExports(packageSymbols, generateExport = false) {
const esImportOrExportStatement = generateExport ? 'export' : 'import';
return Object.entries(packageSymbols)
.map(
([name, symbols]) =>
`${esImportOrExportStatement} {${symbols.join(', ')}} from '${name}';`
)
.join('\n');
}

/**
* @param {Object<string, string[]>} packageSymbols
* @return {string}
Expand Down Expand Up @@ -88,4 +113,5 @@ function assertNoDupes(symbols) {
module.exports = {
generateBentoRuntimeEntrypoint,
generateIntermediatePackage,
generateBentoCoreEntrypoint,
};
13 changes: 12 additions & 1 deletion build-system/compile/generate/shared-bento-symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,15 @@ function getSharedBentoSymbols() {
return sharedBentoSymbols;
}

module.exports = {getExportedSymbols, getSharedBentoSymbols};
/**
* @return {Array<string>}
*/
function getSharedBentoModules() {
return packages.map((pkg) => resolvePath(`src/${pkg}`));
}

module.exports = {
getExportedSymbols,
getSharedBentoSymbols,
getSharedBentoModules,
};
8 changes: 6 additions & 2 deletions build-system/npm-publish/write-package-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ async function getDescription() {

/**
* Write package.json
* @param {{useBentoCore: boolean}} options
* @return {Promise<void>}
*/
async function writePackageJson() {
async function writePackageJson({useBentoCore}) {
const version = getSemver(extensionVersion, ampVersion);
if (!valid(version) || ampVersion.length != 13) {
log(
Expand Down Expand Up @@ -165,6 +166,9 @@ async function writePackageJson() {
react: '^17.0.0',
},
};
if (useBentoCore) {
json.dependencies = {'@bentoproject/core': `tbd`};
}

try {
await writeFile(`${dir}/package.json`, JSON.stringify(json, null, 2));
Expand Down Expand Up @@ -227,7 +231,7 @@ async function main() {
if (await shouldSkip()) {
return;
}
await writePackageJson();
await writePackageJson({useBentoCore: false});
await writeReactJs();
await copyCssToRoot();
}
Expand Down
4 changes: 2 additions & 2 deletions build-system/tasks/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {
bootstrapThirdPartyFrames,
compileAllJs,
compileBentoRuntime,
compileBentoRuntimeAndCore,
compileCoreRuntime,
printConfigHelp,
printNobuildHelp,
Expand Down Expand Up @@ -49,7 +49,7 @@ async function build() {
if (argv.core_runtime_only) {
await compileCoreRuntime(options);
} else if (argv.bento_runtime_only) {
await compileBentoRuntime(options);
await compileBentoRuntimeAndCore(options);
} else {
await compileAllJs(options);
}
Expand Down
4 changes: 2 additions & 2 deletions build-system/tasks/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const {
bootstrapThirdPartyFrames,
compileAllJs,
compileBentoRuntime,
compileBentoRuntimeAndCore,
compileCoreRuntime,
compileJs,
endBuildStep,
Expand Down Expand Up @@ -116,7 +116,7 @@ async function dist() {
if (argv.core_runtime_only) {
await compileCoreRuntime(options);
} else if (argv.bento_runtime_only) {
await compileBentoRuntime(options);
await compileBentoRuntimeAndCore(options);
} else {
await Promise.all([
writeVersionFiles(),
Expand Down
53 changes: 53 additions & 0 deletions build-system/tasks/extension-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const {parse: pathParse} = require('path');
const {renameSelectorsToBentoTagNames} = require('./css/bento-css');
const {TransformCache, batchedRead} = require('../common/transform-cache');
const {watch} = require('chokidar');
const {
getSharedBentoModules,
} = require('../compile/generate/shared-bento-symbols');

const legacyLatestVersions = json5.parse(
fs.readFileSync(
Expand Down Expand Up @@ -473,6 +476,20 @@ async function buildExtension(name, version, hasCss, options) {
* @return {Promise<void>}
*/
async function buildNpmCss(extDir, options) {
await Promise.all([
buildNpmReactCss(extDir, options),
buildNpmBentoWebComponentCss(extDir, options),
]);
}

/**
* Writes an extensions's CSS to its npm dist folder.
*
* @param {string} extDir
* @param {Object} options
* @return {Promise<void>}
*/
async function buildNpmReactCss(extDir, options) {
const startCssTime = Date.now();
const filenames = await fastGlob(path.join(extDir, '**', '*.jss.js'));
if (!filenames.length) {
Expand All @@ -485,6 +502,24 @@ async function buildNpmCss(extDir, options) {
endBuildStep('Wrote CSS', `${options.name} → styles.css`, startCssTime);
}

/**
*
* @param {string} extDir
* @param {Object} options
* @return {Promise<void>}
*/
async function buildNpmBentoWebComponentCss(extDir, options) {
const srcFilepath = path.resolve(
`build/css/${getBentoName(options.name)}-${options.version}.css`
);
if (await fs.pathExists(srcFilepath)) {
const destFilepath = path.resolve(`${extDir}/dist/web-component.css`);
await fs.copyFile(srcFilepath, destFilepath);
} else {
await buildExtensionCss(extDir, options.name, options.version, options);
}
}

/** @type {TransformCache<string>} */
let jssCache;

Expand Down Expand Up @@ -629,6 +664,11 @@ async function buildNpmBinaries(extDir, name, options) {
wrapper: '',
},
};
if (options.useBentoCore) {
// remap all shared modules to the @bentoproject/core package and declare as external
npm.bento.remap = getSharedBentoPackageRemap();
npm.bento.external = ['@bentoproject/core'];
}
}
const binaries = Object.values(npm);
return buildBinaries(extDir, binaries, options);
Expand Down Expand Up @@ -662,6 +702,19 @@ function buildBinaries(extDir, binaries, options) {
return Promise.all(promises);
}

/**
* Creates configuration to remap shared bento modules
* Returns an Object of the form: `{'path/to/shared/module': '@bentoproject/core'}`.
* Uses require.resolve() to normalize directories to the appropriate index file
* @return {Object}
*/
function getSharedBentoPackageRemap() {
const remap = Object.fromEntries(
getSharedBentoModules().map((p) => [p, '@bentoproject/core'])
);
return remap;
}

/**
* @param {string} dir
* @param {string} name
Expand Down
Loading

0 comments on commit 1ba0a82

Please sign in to comment.