Skip to content

Commit

Permalink
Restore ES2021 UMD browser build
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterKale committed Nov 24, 2024
1 parent 9c49b11 commit f2a1102
Showing 1 changed file with 82 additions and 38 deletions.
120 changes: 82 additions & 38 deletions packages/browser/build_npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ async function buildESMAndCJS() {
'fido',
'umd',
],
unpkg: 'dist/bundle/index.umd.min.js',
dependencies: {
// Deno workspaces maps this identifier locally, make sure it's defined in the NPM package
'@simplewebauthn/types': `^${typesDenoJSON.version}`,
Expand All @@ -75,28 +76,16 @@ async function buildESMAndCJS() {
}

/**
* Generate a UMD bundle using Rollup and Babel
* Generate UMD bundles using Rollup and Babel
*/
async function buildUMD() {
console.log('Building UMD bundle...');

// Rollup plugin to remove injected copyright notices
const cleanCopyrightCommentInUMDBundleTargetingES5 = () => {
return {
name: 'cleanCopyrightCommentInUMDBundleTargetingES5',
renderChunk: async (code: string) => {
const comment =
`/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */`;
return code.indexOf(comment) > -1 ? code.replace(comment, '') : null;
},
};
};
async function buildUMDES5() {
console.log('Building UMD (ES5) bundle...');

const rollupOptions: RollupOptions = {
const rollupOptionsES5: RollupOptions = {
// Process the ESM .js files generated by dnt
input: `${outDir}/esm/index.js`,
output: {
dir: `${outDir}`,
dir: `${outDir}/dist`,
// Output to ESM so Babel can take care of UMD generation
format: 'esm',
entryFileNames: 'bundle/[name].es5.umd.min.js',
Expand All @@ -120,27 +109,10 @@ async function buildUMD() {
],
],
}),
/**
* Strip out third-party copyright notices that might confuse people
*/
cleanCopyrightCommentInUMDBundleTargetingES5(),
/**
* Minify the bundled code
*/
// @ts-ignore: Rollup plugins are callable
terser(),
/**
* Add the package name and version to the top of the bundle
*/
// @ts-ignore: Rollup plugins are callable
versionInjector({
packageJson: `${outDir}/package.json`,
injectInComments: {
fileRegexp: /\.(js)$/,
// [@simplewebauthn/browser@2.1.0]
tag: '[@simplewebauthn/browser@{version}]',
},
}),
swanVersionInjector(),
],
},
plugins: [
Expand All @@ -162,11 +134,53 @@ async function buildUMD() {
try {
// Process inputs
// Generate a bundle
const bundle = await rollup(rollupOptions);
const bundle = await rollup(rollupOptionsES5);

console.log('Writing bundle...');
// Write the bundle to file
await bundle.write(rollupOptionsES5.output as OutputOptions);

// Close the bundle
await bundle.close();
} catch (error) {
throw new Error('Failed to generate Rollup bundle', { cause: error });
}

console.log('Complete!');
}

async function buildUMDES2021() {
console.log('Building UMD (ES2021) bundle...');

const rollupOptionsES2021: RollupOptions = {
// Process the ESM .js files generated by dnt
input: `${outDir}/esm/index.js`,
output: {
dir: `${outDir}/dist`,
// Output to ESM so Babel can take care of UMD generation
format: 'umd',
name: 'SimpleWebAuthnBrowser',
entryFileNames: 'bundle/[name].umd.min.js',
plugins: [
// @ts-ignore: Rollup plugins are callable
terser(),
swanVersionInjector(),
],
},
plugins: [
// @ts-ignore: Rollup plugins are callable
nodeResolve(),
],
};

try {
// Process inputs
// Generate a bundle
const bundle = await rollup(rollupOptionsES2021);

console.log('Writing bundle...');
// Write the bundle to file
await bundle.write(rollupOptions.output as OutputOptions);
await bundle.write(rollupOptionsES2021.output as OutputOptions);

// Close the bundle
await bundle.close();
Expand All @@ -181,4 +195,34 @@ async function buildUMD() {
* Generate the builds
*/
await buildESMAndCJS();
await buildUMD();
await buildUMDES5();
await buildUMDES2021();

/**
* Rollup plugin to remove injected copyright notices
*/
function cleanCopyrightCommentInUMDBundleTargetingES5() {
return {
name: 'cleanCopyrightCommentInUMDBundleTargetingES5',
renderChunk: (code: string) => {
const comment =
`/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */`;
return code.indexOf(comment) > -1 ? code.replace(comment, '') : null;
},
};
}

/**
* Add the package name and version to the top of the bundle
*/
function swanVersionInjector() {
// @ts-ignore: Rollup plugins are callable
return versionInjector({
packageJson: `${outDir}/package.json`,
injectInComments: {
fileRegexp: /\.(js)$/,
// [@simplewebauthn/browser@2.1.0]
tag: '[@simplewebauthn/browser@{version}]',
},
});
}

0 comments on commit f2a1102

Please sign in to comment.