-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(react): provide only umd & esm bundles when packaging #2524
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { | |
} from '@angular-devkit/architect'; | ||
import { JsonObject } from '@angular-devkit/core'; | ||
import { from, Observable, of } from 'rxjs'; | ||
import { map, mergeScan, switchMap, tap } from 'rxjs/operators'; | ||
import { switchMap, tap, last, mergeMap, catchError } from 'rxjs/operators'; | ||
import { runRollup } from './run-rollup'; | ||
import { createBabelConfig as _createBabelConfig } from '../../utils/babel-config'; | ||
import * as autoprefixer from 'autoprefixer'; | ||
|
@@ -42,15 +42,13 @@ export default createBuilder<BundleBuilderOptions & JsonObject>(run); | |
|
||
interface OutputConfig { | ||
format: rollup.ModuleFormat; | ||
esm: boolean; | ||
extension: string; | ||
declaration?: boolean; | ||
} | ||
|
||
const outputConfigs: OutputConfig[] = [ | ||
{ format: 'umd', esm: false, extension: 'umd' }, | ||
{ format: 'esm', esm: true, extension: 'esm2015' }, | ||
{ format: 'esm', esm: false, extension: 'esm5' } | ||
{ format: 'umd', extension: 'umd' }, | ||
{ format: 'esm', extension: 'esm' } | ||
]; | ||
|
||
const fileExtensions = ['.js', '.jsx', '.ts', '.tsx']; | ||
|
@@ -72,7 +70,7 @@ export function run( | |
} | ||
const options = normalizeBundleOptions(_options, context.workspaceRoot); | ||
const rollupOptions: rollup.InputOptions[] = outputConfigs.map( | ||
({ format, esm, extension }) => { | ||
({ format, extension }, index) => { | ||
const parsedTSConfig = ts.readConfigFile( | ||
options.tsConfig, | ||
ts.sys.readFile | ||
|
@@ -83,6 +81,18 @@ export function run( | |
updatePaths(dependencies, parsedTSConfig.compilerOptions.paths); | ||
|
||
const plugins = [ | ||
typescript({ | ||
check: true, | ||
tsconfig: options.tsConfig, | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
rootDir: options.entryRoot, | ||
allowJs: false, | ||
declaration: index === 0, | ||
paths: parsedTSConfig.compilerOptions.paths | ||
} | ||
} | ||
}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we want to invoke the typescript compiler each time, so it can fail the build if something is wrong There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. I just wanna check to make sure the type error is only printed once rather than multiple times. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested the build and it does output the same errors twice (e.g. type errors) if both rollup runs fail the same way. See my other comment for more on this. |
||
peerDepsExternal({ | ||
packageJsonPath: options.project | ||
}), | ||
|
@@ -95,31 +105,15 @@ export function run( | |
localResolve(), | ||
resolve({ extensions: fileExtensions }), | ||
babel({ | ||
...createBabelConfig(options, options.projectRoot, esm), | ||
...createBabelConfig(options, options.projectRoot), | ||
extensions: fileExtensions, | ||
externalHelpers: false, | ||
exclude: 'node_modules/**' | ||
}), | ||
commonjs(), | ||
filesize() | ||
]; | ||
if (esm) { | ||
// TS plugin has to come first before types are stripped, otherwise | ||
plugins.unshift( | ||
typescript({ | ||
check: true, | ||
tsconfig: options.tsConfig, | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
rootDir: options.entryRoot, | ||
allowJs: false, | ||
declaration: true, | ||
paths: parsedTSConfig.compilerOptions.paths | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
|
||
const entryFileTmpl = `${options.outputPath}/${context.target.project}.<%= extension %>.js`; | ||
const rollupConfig = { | ||
input: options.entryFile, | ||
|
@@ -166,21 +160,20 @@ export function run( | |
} else { | ||
context.logger.info('Bundling...'); | ||
return from(rollupOptions).pipe( | ||
mergeScan( | ||
(acc, options) => | ||
runRollup(options).pipe( | ||
map(result => { | ||
return { | ||
success: acc.success && result.success | ||
}; | ||
}) | ||
), | ||
{ success: true } | ||
), | ||
mergeMap(options => runRollup(options)), | ||
catchError(e => { | ||
console.error(e); | ||
return of({ success: false }); | ||
}), | ||
last(), | ||
tap({ | ||
complete: () => { | ||
updatePackageJson(options, context, target, dependencies); | ||
context.logger.info('Bundle complete.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previously, if compilation (or anything else) failed, we'd still generate a |
||
next: result => { | ||
if (result.success) { | ||
updatePackageJson(options, context, target, dependencies); | ||
context.logger.info('Bundle complete.'); | ||
} else { | ||
context.logger.error('Bundle failed.'); | ||
} | ||
} | ||
}) | ||
); | ||
|
@@ -191,12 +184,8 @@ export function run( | |
|
||
// ----------------------------------------------------------------------------- | ||
|
||
function createBabelConfig( | ||
options: BundleBuilderOptions, | ||
projectRoot: string, | ||
esm: boolean | ||
) { | ||
let babelConfig: any = _createBabelConfig(projectRoot, esm, false); | ||
function createBabelConfig(options: BundleBuilderOptions, projectRoot: string) { | ||
let babelConfig: any = _createBabelConfig(projectRoot, false, false); | ||
if (options.babelConfig) { | ||
babelConfig = require(options.babelConfig)(babelConfig, options); | ||
} | ||
|
@@ -242,8 +231,7 @@ function updatePackageJson(options, context, target, dependencies) { | |
); | ||
const packageJson = readJsonFile(options.project); | ||
packageJson.main = entryFileTmpl.replace('<%= extension %>', 'umd'); | ||
packageJson.module = entryFileTmpl.replace('<%= extension %>', 'esm5'); | ||
packageJson.es2015 = entryFileTmpl.replace('<%= extension %>', 'esm2015'); | ||
packageJson.module = entryFileTmpl.replace('<%= extension %>', 'esm'); | ||
packageJson.typings = `./${typingsFile}`; | ||
writeJsonFile(`${options.outputPath}/package.json`, packageJson); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we only want to emit the declaration files the first time