-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add support for Node ESM when used to package SSR-ready li…
…brary
- Loading branch information
Showing
12 changed files
with
372 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/web/src/executors/rollup/lib/update-package-json.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { updatePackageJson } from './update-package-json'; | ||
import * as utils from 'nx/src/utils/fileutils'; | ||
import { PackageJson } from 'nx/src/utils/package-json'; | ||
|
||
jest.mock('nx/src/utils/fileutils', () => ({ | ||
writeJsonFile: () => {}, | ||
})); | ||
|
||
describe('updatePackageJson', () => { | ||
const commonOptions = { | ||
outputPath: 'dist/index.js', | ||
tsConfig: './tsconfig.json', | ||
project: './package.json', | ||
entryFile: './index.js', | ||
entryRoot: '.', | ||
projectRoot: '.', | ||
assets: [], | ||
rollupConfig: [], | ||
}; | ||
|
||
const sharedContext = { | ||
isVerbose: false, | ||
workspace: { version: 2, projects: {} }, | ||
root: '', | ||
cwd: '', | ||
}; | ||
|
||
it('should support ESM', () => { | ||
const spy = jest.spyOn(utils, 'writeJsonFile'); | ||
|
||
updatePackageJson( | ||
{ | ||
...commonOptions, | ||
format: ['esm'], | ||
}, | ||
sharedContext, | ||
{ type: 'app', name: 'test', data: {} }, | ||
[], | ||
{} as unknown as PackageJson | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
types: './index.d.ts', | ||
import: './index.js', | ||
}, | ||
}, | ||
main: './index.js', | ||
module: './index.js', | ||
type: 'module', | ||
types: './index.d.ts', | ||
}); | ||
|
||
spy.mockRestore(); | ||
}); | ||
|
||
it('should support CJS', () => { | ||
const spy = jest.spyOn(utils, 'writeJsonFile'); | ||
|
||
updatePackageJson( | ||
{ | ||
...commonOptions, | ||
format: ['cjs'], | ||
}, | ||
sharedContext, | ||
{ type: 'app', name: 'test', data: {} }, | ||
[], | ||
{} as unknown as PackageJson | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
types: './index.d.ts', | ||
require: './index.cjs', | ||
}, | ||
}, | ||
main: './index.cjs', | ||
type: 'commonjs', | ||
types: './index.d.ts', | ||
}); | ||
|
||
spy.mockRestore(); | ||
}); | ||
|
||
it('should support ESM + CJS', () => { | ||
const spy = jest.spyOn(utils, 'writeJsonFile'); | ||
|
||
updatePackageJson( | ||
{ | ||
...commonOptions, | ||
format: ['esm', 'cjs'], | ||
}, | ||
sharedContext, | ||
{ type: 'app', name: 'test', data: {} }, | ||
[], | ||
{} as unknown as PackageJson | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
types: './index.d.ts', | ||
import: './index.js', | ||
require: './index.cjs', | ||
}, | ||
}, | ||
main: './index.cjs', | ||
module: './index.js', | ||
type: 'module', | ||
types: './index.d.ts', | ||
}); | ||
|
||
spy.mockRestore(); | ||
}); | ||
|
||
it('should support custom exports field', () => { | ||
const spy = jest.spyOn(utils, 'writeJsonFile'); | ||
|
||
updatePackageJson( | ||
{ | ||
...commonOptions, | ||
format: ['esm'], | ||
}, | ||
sharedContext, | ||
{ type: 'app', name: 'test', data: {} }, | ||
[], | ||
{ | ||
exports: { | ||
foo: { | ||
import: './foo.js', | ||
}, | ||
}, | ||
} as unknown as PackageJson | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
types: './index.d.ts', | ||
import: './index.js', | ||
}, | ||
foo: { | ||
import: './foo.js', | ||
}, | ||
}, | ||
main: './index.js', | ||
module: './index.js', | ||
type: 'module', | ||
types: './index.d.ts', | ||
}); | ||
|
||
spy.mockRestore(); | ||
}); | ||
}); |
Oops, something went wrong.