Skip to content
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

Improvements for library targets #6570

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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