Skip to content

Commit

Permalink
Improvements for library targets (#6570)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jul 9, 2021
1 parent 278cfe8 commit 946b141
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 80 deletions.
93 changes: 82 additions & 11 deletions packages/core/core/src/requests/TargetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,36 @@ export class TargetResolver {
});
}

if (descriptor.scopeHoist === false) {
let contents: string =
typeof pkgContents === 'string'
? pkgContents
: // $FlowFixMe
JSON.stringify(pkgContents, null, '\t');
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Scope hoisting cannot be disabled for library targets.',
origin: '@parcel/core',
codeFrames: [
{
language: 'json',
filePath: pkgFilePath ?? undefined,
code: contents,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: `/targets/${targetName}/scopeHoist`,
type: 'value',
},
]),
},
],
hints: [
`The "${targetName}" target is meant for libraries. Either remove the "scopeHoist" option, or use a different target name.`,
],
},
});
}

targets.set(targetName, {
name: targetName,
distDir,
Expand All @@ -690,10 +720,8 @@ export class TargetResolver {
isLibrary: true,
shouldOptimize:
this.options.defaultTargetOptions.shouldOptimize &&
descriptor.optimize !== false,
shouldScopeHoist:
this.options.defaultTargetOptions.shouldScopeHoist &&
descriptor.scopeHoist !== false,
descriptor.optimize === true,
shouldScopeHoist: true,
sourceMap: normalizeSourceMap(this.options, descriptor.sourceMap),
}),
loc: toInternalSourceLocation(this.options.projectRoot, loc),
Expand Down Expand Up @@ -780,6 +808,46 @@ export class TargetResolver {
pkgContents,
);

if (descriptor.scopeHoist === false && descriptor.isLibrary) {
let contents: string =
typeof pkgContents === 'string'
? pkgContents
: // $FlowFixMe
JSON.stringify(pkgContents, null, '\t');
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Scope hoisting cannot be disabled for library targets.',
origin: '@parcel/core',
codeFrames: [
{
language: 'json',
filePath: pkgFilePath ?? undefined,
code: contents,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: `/targets/${targetName}/scopeHoist`,
type: 'value',
},
{
key: `/targets/${targetName}/isLibrary`,
type: 'value',
},
]),
},
],
hints: [`Either remove the "scopeHoist" or "isLibrary" option.`],
},
});
}

let isLibrary =
descriptor.isLibrary ??
this.options.defaultTargetOptions.isLibrary ??
false;
let shouldScopeHoist = isLibrary
? true
: this.options.defaultTargetOptions.shouldScopeHoist;

targets.set(targetName, {
name: targetName,
distDir: toProjectPath(
Expand All @@ -800,15 +868,15 @@ export class TargetResolver {
this.options.defaultTargetOptions.outputFormat ??
inferredOutputFormat ??
undefined,
isLibrary:
descriptor.isLibrary ??
this.options.defaultTargetOptions.isLibrary,
isLibrary,
shouldOptimize:
this.options.defaultTargetOptions.shouldOptimize &&
descriptor.optimize !== false,
// Libraries are not optimized by default, users must explicitly configure this.
(isLibrary
? descriptor.optimize === true
: descriptor.optimize !== false),
shouldScopeHoist:
this.options.defaultTargetOptions.shouldScopeHoist &&
descriptor.scopeHoist !== false,
shouldScopeHoist && descriptor.scopeHoist !== false,
sourceMap: normalizeSourceMap(this.options, descriptor.sourceMap),
}),
loc: toInternalSourceLocation(this.options.projectRoot, loc),
Expand All @@ -833,7 +901,10 @@ export class TargetResolver {
outputFormat: this.options.defaultTargetOptions.outputFormat,
isLibrary: this.options.defaultTargetOptions.isLibrary,
shouldOptimize: this.options.defaultTargetOptions.shouldOptimize,
shouldScopeHoist: this.options.defaultTargetOptions.shouldScopeHoist,
shouldScopeHoist:
this.options.defaultTargetOptions.shouldScopeHoist ??
(this.options.mode === 'production' &&
!this.options.defaultTargetOptions.isLibrary),
sourceMap: this.options.defaultTargetOptions.sourceMaps
? {}
: undefined,
Expand Down
4 changes: 1 addition & 3 deletions packages/core/core/src/resolveOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ export default async function resolveOptions(
detailedReport: initialOptions.detailedReport,
defaultTargetOptions: {
shouldOptimize,
shouldScopeHoist:
initialOptions?.defaultTargetOptions?.shouldScopeHoist ??
initialOptions.mode === 'production',
shouldScopeHoist: initialOptions?.defaultTargetOptions?.shouldScopeHoist,
sourceMaps: initialOptions?.defaultTargetOptions?.sourceMaps ?? true,
publicUrl,
...(distDir != null
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export type ParcelOptions = {|

+defaultTargetOptions: {|
+shouldOptimize: boolean,
+shouldScopeHoist: boolean,
+shouldScopeHoist?: boolean,
+sourceMaps: boolean,
+publicUrl: string,
+distDir?: ProjectPath,
Expand Down

0 comments on commit 946b141

Please sign in to comment.