/
other.ts
826 lines (675 loc) Β· 30 KB
/
other.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
import build from "../";
import {
snapshotDirectory,
install,
testdir,
js,
getDist,
basicPkgJson,
ts,
repoNodeModules,
typescriptFixture,
getFiles,
} from "../../../test-utils";
import { doPromptInput } from "../../prompt";
jest.mock("../../prompt");
jest.setTimeout(30000);
afterEach(() => {
jest.resetAllMocks();
});
test("browser", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "browser",
main: "dist/browser.cjs.js",
module: "dist/browser.esm.js",
browser: {
"./dist/browser.cjs.js": "./dist/browser.browser.cjs.js",
"./dist/browser.esm.js": "./dist/browser.browser.esm.js",
},
}),
"src/index.js": js`
let thing = "wow";
if (typeof window !== "undefined") {
thing = "something";
}
if (typeof document !== undefined) {
thing += "other";
}
export default thing;
`,
});
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/browser.browser.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
let thing = "wow";
{
thing = "something";
}
{
thing += "other";
}
var thing$1 = thing;
exports.default = thing$1;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/browser.browser.esm.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
let thing = "wow";
{
thing = "something";
}
{
thing += "other";
}
var thing$1 = thing;
export default thing$1;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/browser.cjs.dev.js, dist/browser.cjs.prod.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
let thing = "wow";
if (typeof window !== "undefined") {
thing = "something";
}
if (typeof document !== undefined) {
thing += "other";
}
var thing$1 = thing;
exports.default = thing$1;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/browser.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./browser.cjs.prod.js");
} else {
module.exports = require("./browser.cjs.dev.js");
}
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/browser.esm.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
let thing = "wow";
if (typeof window !== "undefined") {
thing = "something";
}
if (typeof document !== undefined) {
thing += "other";
}
var thing$1 = thing;
export default thing$1;
`);
});
test("browser no module", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "browser-no-module",
main: "dist/browser-no-module.cjs.js",
browser: {
"./dist/browser-no-module.cjs.js":
"./dist/browser-no-module.browser.cjs.js",
},
}),
"src/index.js": js`
let thing = "wow";
if (typeof window !== "undefined") {
thing = "something";
}
if (typeof document !== undefined) {
thing += "other";
}
export default thing;
`,
});
await build(tmpPath);
await snapshotDirectory(tmpPath, { files: "all" });
});
test("typescript", async () => {
let dir = await testdir(typescriptFixture);
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/declarations/src/another-thing.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
export declare type SomeType = string;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/declarations/src/index.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
import { SomeType } from "./another-thing";
export * from "./one-more-thing";
import * as path from "path";
export { path };
declare let thing: SomeType;
export default thing;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/declarations/src/one-more-thing.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
declare var obj: object;
export { obj };
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/typescript.cjs.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
export * from "./declarations/src/index";
export { default } from "./declarations/src/index";
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/typescript.cjs.dev.js, dist/typescript.cjs.prod.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var path__namespace = /*#__PURE__*/_interopNamespace(path);
let obj = {};
let thing = "something";
exports.path = path__namespace;
exports.default = thing;
exports.obj = obj;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/typescript.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./typescript.cjs.prod.js");
} else {
module.exports = require("./typescript.cjs.dev.js");
}
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/typescript.esm.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
import * as path from 'path';
export { path };
let obj = {};
let thing = "something";
export default thing;
export { obj };
`);
});
test("typescript with forced dts emit", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "typescript-force-dts-emit",
main: "dist/typescript-force-dts-emit.cjs.js",
module: "dist/typescript-force-dts-emit.esm.js",
dependencies: {
"@babel/runtime": "^7.8.7",
"@reduxjs/toolkit": "^1.3.5",
},
devDependencies: {
"@types/node": "^12.7.1",
"@types/webpack-env": "^1.15.1",
typescript: "^3.8.3",
},
}),
".babelrc": JSON.stringify({
presets: [
require.resolve("@babel/preset-env"),
require.resolve("@babel/preset-typescript"),
],
}),
node_modules: {
kind: "symlink",
path: repoNodeModules,
},
"tsconfig.json": `{
"compilerOptions": {
/* Basic Options */
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "incremental": true, /* Enable incremental compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
"isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"noEmit": true
}
}
`,
"src/create-store.ts": ts`
// @ts-ignore (installed during test)
import { configureStore, Action } from "@reduxjs/toolkit";
import { ThunkAction } from "redux-thunk";
import { rootReducer, RootState } from "./root-reducer";
export type AppThunk = ThunkAction<void, RootState, unknown, Action<string>>;
export function createStore() {
return configureStore<RootState>({
reducer: rootReducer,
});
}
`,
"src/index.ts": ts`
export { createStore } from "./create-store";
export type { AppThunk } from "./create-store";
export type { RootState } from "./root-reducer";
`,
"src/root-reducer.ts": ts`
// @ts-ignore (installed during test)
import { combineReducers } from "@reduxjs/toolkit";
export const rootReducer = combineReducers({
/* blah blah blah */
});
export type RootState = ReturnType<typeof rootReducer>;
`,
});
await build(tmpPath);
await snapshotDirectory(tmpPath, {
files: "all",
filterPath: (fp) => fp.startsWith("dist/"),
});
});
test("package resolvable but not in deps", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "package-resolvable-but-not-in-deps",
main: "dist/package-resolvable-but-not-in-deps.cjs.js",
devDependencies: {
react: "16.8.6",
},
}),
"src/index.js": js`
import React from "react";
export default React.createContext("something");
`,
});
await install(tmpPath);
try {
await build(tmpPath);
} catch (err) {
expect(err.message).toMatchInlineSnapshot(
`"π package-resolvable-but-not-in-deps \\"react\\" is imported by \\"src/index.js\\" but the package is not specified in dependencies or peerDependencies"`
);
return;
}
expect(true).toBe(false);
});
test("package with exports resolvable", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "package-exports-resolvable-but-not-in-deps",
main: "dist/package-exports-resolvable-but-not-in-deps.cjs.js",
"umd:main": "dist/package-exports-resolvable-but-not-in-deps.umd.min.js",
dependencies: {
"@atomico/hooks": "0.0.0",
},
preconstruct: {
umdName: "packageExportsResolvableButNotInDeps",
},
}),
"node_modules/@atomico/hooks/package.json": JSON.stringify({
name: "@atomico/hooks/use-slot",
exports: {
"./use-slot": "./something/use-slot.js",
},
}),
"node_modules/@atomico/hooks/something/use-slot.js": js`
export function useSlot(ref) {
console.log(ref);
}
`,
"src/index.js": js`
import { useSlot } from "@atomico/hooks/use-slot";
export default function useChildren(ref) {
return useSlot(ref);
}
`,
});
await build(tmpPath);
expect(
await getFiles(tmpPath, [
"dist/package-exports-resolvable-but-not-in-deps.umd.min.js",
])
).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/package-exports-resolvable-but-not-in-deps.umd.min.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).packageExportsResolvableButNotInDeps=o()}(this,(function(){"use strict";return function(e){return function(e){console.log(e)}(e)}}));
//# sourceMappingURL=package-exports-resolvable-but-not-in-deps.umd.min.js.map
`);
});
test("entrypoint outside package directory", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "entrypoint-outside-pkg-dir",
preconstruct: {
packages: ["pkg-a"],
},
}),
"some-file.js": js`
export let something = true;
`,
"pkg-a/package.json": JSON.stringify({
name: "@entrypoint-outside-pkg-dir/pkg-a",
main: "dist/pkg-a.cjs.js",
preconstruct: {
entrypoints: ["../../some-file.js"],
},
}),
});
try {
await build(tmpPath);
} catch (err) {
expect(err.message).toMatchInlineSnapshot(
`"entrypoint source files must be inside of the src directory of a package but ../some-file.js is not"`
);
return;
}
expect(true).toBe(false);
});
test("module imported outside package directory", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "imports-outside-pkg-dir",
preconstruct: {
packages: ["pkg-a"],
},
}),
"some-file.js": js`
export let something = true;
`,
"pkg-a/package.json": JSON.stringify({
name: "@imports-outside-pkg-dir/pkg-a",
main: "dist/imports-outside-pkg-dir-pkg-a.cjs.js",
}),
"pkg-a/src/index.js": js`
export { something } from "../../some-file";
`,
});
try {
await build(tmpPath);
} catch (err) {
expect(err.message).toMatchInlineSnapshot(
`"π @imports-outside-pkg-dir/pkg-a all relative imports in a package should only import modules inside of their package directory but \\"src/index.js\\" is importing \\"../../some-file\\""`
);
return;
}
expect(true).toBe(false);
});
test("using external @babel/runtime helpers", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "external-babel-runtime",
main: "dist/external-babel-runtime.cjs.js",
module: "dist/external-babel-runtime.esm.js",
dependencies: {
"@babel/runtime": "^7.0.0",
},
}),
".babelrc": JSON.stringify({
presets: [require.resolve("@babel/preset-env")],
plugins: [require.resolve("@babel/plugin-transform-runtime")],
}),
"src/index.js": js`
export default class Foo {}
`,
});
await install(tmpPath);
await build(tmpPath);
await snapshotDirectory(tmpPath, { files: "all" });
});
test("should lazily get globals", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "umd-unused-peer-dep",
main: "dist/umd-unused-peer-dep.cjs.js",
"umd:main": "dist/umd-unused-peer-dep.umd.min.js",
preconstruct: {
umdName: "validPackage",
},
peerDependencies: {
react: "^16.11.0",
},
}),
"src/index.js": js`
export default "something";
`,
});
(doPromptInput as jest.MockedFunction<
typeof doPromptInput
>).mockImplementation((question, { name }, thing) => {
console.log("called");
throw new Error(
`this should never be called: ${JSON.stringify({
question,
name,
thing,
})}`
);
});
await build(tmpPath);
await snapshotDirectory(tmpPath);
});
test("batches build errors", async () => {
let tmpPath = await testdir({
"package.json": JSON.stringify({
name: "errors",
main: "index.js",
preconstruct: {
packages: ["packages/*"],
},
workspaces: ["packages/*"],
}),
"packages/package-one/package.json": JSON.stringify({
name: "@errors/package-one",
main: "dist/errors-package-one.cjs.js",
license: "MIT",
private: true,
}),
"packages/package-two/package.json": JSON.stringify({
name: "@errors/package-two",
main: "dist/errors-package-two.cjs.js",
license: "MIT",
private: true,
}),
"packages/package-one/src/index.js": js`
import "something";
import "something-2";
`,
"packages/package-two/src/index.js": js`
import "something";
import "something-2";
`,
});
let error;
try {
await build(tmpPath);
throw new Error("should never happen");
} catch (err) {
error = err;
}
expect(error).toMatchInlineSnapshot(`
[Error: π @errors/package-one "something-2" is imported by "src/index.js" but the package is not specified in dependencies or peerDependencies
π @errors/package-one "something" is imported by "src/index.js" but the package is not specified in dependencies or peerDependencies
π @errors/package-two "something-2" is imported by "src/index.js" but the package is not specified in dependencies or peerDependencies
π @errors/package-two "something" is imported by "src/index.js" but the package is not specified in dependencies or peerDependencies]
`);
});
test("builds package using eval", async () => {
let dir = await testdir({
"package.json": basicPkgJson(),
"src/index.js": js`
export default function compute(arg) {
return eval(arg);
}
`,
});
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.dev.js, dist/pkg.cjs.prod.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function compute(arg) {
return eval(arg);
}
exports.default = compute;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./pkg.cjs.prod.js");
} else {
module.exports = require("./pkg.cjs.dev.js");
}
`);
});
test("builds umd with a dependency containing top-level this in ESM", async () => {
let dir = await testdir({
"package.json": basicPkgJson({
umdName: "pkg",
dependencies: {
"with-top-level-this-in-esm": "*",
},
}),
"src/index.js": js`
export { default } from "with-top-level-this-in-esm";
`,
"node_modules/with-top-level-this-in-esm/package.json": JSON.stringify({
name: "with-top-level-this-in-esm",
}),
"node_modules/with-top-level-this-in-esm/index.js": js`
// output transpiled by TS with inlined tslib helper
var __assign =
(this && this.__assign) ||
function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var foo = { bar: 42 };
export default __assign({}, foo);
`,
});
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.dev.js, dist/pkg.cjs.prod.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var withTopLevelThisInEsm = require('with-top-level-this-in-esm');
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
var withTopLevelThisInEsm__default = /*#__PURE__*/_interopDefault(withTopLevelThisInEsm);
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return withTopLevelThisInEsm__default['default'];
}
});
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./pkg.cjs.prod.js");
} else {
module.exports = require("./pkg.cjs.dev.js");
}
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.umd.min.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).pkg=t()}(this,(function(){"use strict";var e=function(){return(e=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};return e({},{bar:42})}));
//# sourceMappingURL=pkg.umd.min.js.map
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.umd.min.js.map β―β―β―β―β―β―β―β―β―β―β―β―β―β―
{"version":3,"file":"pkg.umd.min.js","sources":["../node_modules/with-top-level-this-in-esm/index.js"],"sourcesContent":["// output transpiled by TS with inlined tslib helper\\nvar __assign =\\n (this && this.__assign) ||\\n function () {\\n __assign =\\n Object.assign ||\\n function (t) {\\n for (var s, i = 1, n = arguments.length; i < n; i++) {\\n s = arguments[i];\\n for (var p in s)\\n if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\\n }\\n return t;\\n };\\n return __assign.apply(this, arguments);\\n };\\nvar foo = { bar: 42 };\\nexport default __assign({}, foo);"],"names":["__assign","Object","assign","t","s","i","n","arguments","length","p","prototype","hasOwnProperty","call","apply","this","bar"],"mappings":"oOACA,IAAIA,EAEF,WAWE,OAVAA,EACEC,OAAOC,QACP,SAAUC,GACR,IAAK,IAAIC,EAAGC,EAAI,EAAGC,EAAIC,UAAUC,OAAQH,EAAIC,EAAGD,IAE9C,IAAK,IAAII,KADTL,EAAIG,UAAUF,GAERJ,OAAOS,UAAUC,eAAeC,KAAKR,EAAGK,KAAIN,EAAEM,GAAKL,EAAEK,IAE7D,OAAON,IAEKU,MAAMC,KAAMP,mBAGjBP,EAAS,GADd,CAAEe,IAAK"}
`);
});
test("fails for source files containing top-level this", async () => {
let dir = await testdir({
"package.json": basicPkgJson(),
"src/index.js": js`
export default this;
`,
});
try {
await build(dir);
} catch (err) {
expect(err.message).toMatchInlineSnapshot(`
"π pkg \\"src/index.js\\" used \`this\` keyword at the top level of an ES module. You can read more about this at https://rollupjs.org/guide/en/#error-this-is-undefined and fix this issue that has happened here:
π pkg
π pkg 1: export default this;
π pkg ^
π pkg"
`);
return;
}
expect(true).toBe(false);
});
test(".d.ts", async () => {
const dir = await testdir({
"package.json": JSON.stringify({
name: "pkg",
main: "dist/pkg.cjs.js",
module: "dist/pkg.esm.js",
}),
"src/index.js": js`
export const x = "hello";
`,
"src/index.d.ts": ts`
export const x: string;
`,
node_modules: { kind: "symlink", path: repoNodeModules },
"tsconfig.json": typescriptFixture["tsconfig.json"],
});
await build(dir);
expect(await getFiles(dir, ["dist/**"])).toMatchInlineSnapshot(`
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/declarations/src/index.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
export const x: string;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.d.ts β―β―β―β―β―β―β―β―β―β―β―β―β―β―
export * from "./declarations/src/index";
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.dev.js, dist/pkg.cjs.prod.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const x = "hello";
exports.x = x;
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.cjs.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./pkg.cjs.prod.js");
} else {
module.exports = require("./pkg.cjs.dev.js");
}
β―β―β―β―β―β―β―β―β―β―β―β―β―β― dist/pkg.esm.js β―β―β―β―β―β―β―β―β―β―β―β―β―β―
const x = "hello";
export { x };
`);
});