Skip to content

Commit 3a566fd

Browse files
committed
Error when building a library target in serve mode
1 parent 9dec3d8 commit 3a566fd

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

packages/core/core/src/requests/TargetRequest.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,40 @@ export class TargetResolver {
372372
this.options.serveOptions.distDir,
373373
);
374374
let mainTarget = targets.length === 1 ? targets[0] : null;
375+
376+
if (mainTarget?.env.isLibrary) {
377+
let loc = mainTarget.loc;
378+
throw new ThrowableDiagnostic({
379+
diagnostic: {
380+
origin: '@parcel/core',
381+
message: md`
382+
Library targets are not supported in serve mode.
383+
`,
384+
codeFrames: loc
385+
? [
386+
{
387+
filePath: fromProjectPath(
388+
this.options.projectRoot,
389+
loc.filePath,
390+
),
391+
codeHighlights: [
392+
convertSourceLocationToHighlight(
393+
loc,
394+
'Target declared here',
395+
),
396+
],
397+
},
398+
]
399+
: [],
400+
hints: [
401+
`The "${mainTarget.name}" field is meant for libraries, not applications. Either remove the "${mainTarget.name}" field or choose a different target name.`,
402+
],
403+
documentationURL:
404+
'https://parceljs.org/features/targets/#library-targets',
405+
},
406+
});
407+
}
408+
375409
let context = mainTarget?.env.context ?? 'browser';
376410
let engines = BROWSER_ENVS.has(context)
377411
? {browsers: DEFAULT_ENGINES.browsers}

packages/core/integration-tests/test/integration/sourcemap/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"version": "1.0.0",
44
"license": "MIT",
55
"private": true,
6-
"browser": "dist/index.js",
6+
"default": "dist/index.js",
77
"targets": {
8-
"browser": {
8+
"default": {
9+
"context": "browser",
910
"engines": {
1011
"browsers": [
1112
"> 0.25%"

packages/core/integration-tests/test/sourcemaps.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ describe('sourcemaps', function () {
154154
__dirname,
155155
'/integration/sourcemap/index.js',
156156
);
157-
await bundle(sourceFilename);
157+
await bundle(sourceFilename, {
158+
defaultTargetOptions: {
159+
shouldScopeHoist: true,
160+
},
161+
});
158162

159163
let distDir = path.join(__dirname, '/integration/sourcemap/dist/');
160164

@@ -175,6 +179,7 @@ describe('sourcemaps', function () {
175179
let sourcePath = 'index.js';
176180

177181
let name = raw.match(/function (\$.*\$var\$helloWorld)/)[1];
182+
let exports = raw.match(/var (\$.*\$exports)/)[1];
178183

179184
checkSourceMapping({
180185
map: sourceMap,
@@ -190,7 +195,7 @@ describe('sourcemaps', function () {
190195
source: input,
191196
generated: raw,
192197
str: 'module.exports = helloWorld;',
193-
generatedStr: 'module.exports = ' + name + ';',
198+
generatedStr: exports + ' = ' + name + ';',
194199
sourcePath,
195200
});
196201

0 commit comments

Comments
 (0)