-
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
366 additions
and
78 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(); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
packages/web/src/executors/rollup/lib/update-package-json.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,76 @@ | ||
import { relative } from 'path'; | ||
import { ExecutorContext } from 'nx/src/config/misc-interfaces'; | ||
import { ProjectGraphProjectNode } from 'nx/src/config/project-graph'; | ||
import { | ||
DependentBuildableProjectNode, | ||
updateBuildableProjectPackageJsonDependencies, | ||
} from '@nrwl/workspace/src/utilities/buildable-libs-utils'; | ||
import { writeJsonFile } from 'nx/src/utils/fileutils'; | ||
import { PackageJson } from 'nx/src/utils/package-json'; | ||
import { NormalizedWebRollupOptions } from './normalize'; | ||
|
||
export function updatePackageJson( | ||
options: NormalizedWebRollupOptions, | ||
context: ExecutorContext, | ||
target: ProjectGraphProjectNode, | ||
dependencies: DependentBuildableProjectNode[], | ||
packageJson: PackageJson | ||
) { | ||
const hasEsmFormat = options.format.includes('esm'); | ||
const hasCjsFormat = | ||
options.format.includes('umd') || options.format.includes('cjs'); | ||
|
||
const types = `./${relative(options.entryRoot, options.entryFile).replace( | ||
/\.[jt]sx?$/, | ||
'.d.ts' | ||
)}`; | ||
const exports = { | ||
// TS 4.5+ | ||
'.': { | ||
types, | ||
}, | ||
}; | ||
|
||
if (hasEsmFormat) { | ||
// `module` field is used by bundlers like rollup and webpack to detect ESM. | ||
// May not be required in the future if type is already "module". | ||
packageJson.module = './index.js'; | ||
exports['.']['import'] = './index.js'; | ||
|
||
if (!hasCjsFormat) { | ||
packageJson.main = './index.js'; | ||
} | ||
} | ||
|
||
if (hasCjsFormat) { | ||
packageJson.main = './index.cjs'; | ||
exports['.']['require'] = './index.cjs'; | ||
} | ||
|
||
packageJson.type = options.format.includes('esm') ? 'module' : 'commonjs'; | ||
|
||
// Support for older TS versions < 4.5 | ||
packageJson.types = types; | ||
|
||
packageJson.exports = { | ||
...packageJson.exports, | ||
...exports, | ||
}; | ||
|
||
writeJsonFile(`${options.outputPath}/package.json`, packageJson); | ||
|
||
if ( | ||
dependencies.length > 0 && | ||
options.updateBuildableProjectDepsInPackageJson | ||
) { | ||
updateBuildableProjectPackageJsonDependencies( | ||
context.root, | ||
context.projectName, | ||
context.targetName, | ||
context.configurationName, | ||
target, | ||
dependencies, | ||
options.buildableProjectDepsInPackageJsonType | ||
); | ||
} | ||
} |
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
Oops, something went wrong.