-
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
10 changed files
with
300 additions
and
74 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
113 changes: 113 additions & 0 deletions
113
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,113 @@ | ||
import { updatePackageJson } from './update-package-json'; | ||
import * as utils from 'nx/src/utils/fileutils'; | ||
|
||
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: {} as any, | ||
root: '', | ||
cwd: '', | ||
}; | ||
|
||
it('should support ESM', () => { | ||
const spy = jest.spyOn(utils, 'writeJsonFile'); | ||
|
||
updatePackageJson( | ||
{ | ||
...commonOptions, | ||
format: ['esm'], | ||
}, | ||
sharedContext, | ||
{ type: 'app', name: 'test', data: {} }, | ||
[], | ||
{} as any | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
import: './index.js', | ||
}, | ||
}, | ||
main: './index.js', | ||
module: './index.js', | ||
type: 'module', | ||
typings: './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 any | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
require: './index.cjs', | ||
}, | ||
}, | ||
main: './index.cjs', | ||
type: 'commonjs', | ||
typings: './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 any | ||
); | ||
|
||
expect(utils.writeJsonFile).toHaveBeenCalledWith(expect.anything(), { | ||
exports: { | ||
'.': { | ||
import: './index.js', | ||
require: './index.cjs', | ||
}, | ||
}, | ||
main: './index.js', | ||
module: './index.js', | ||
type: 'module', | ||
typings: './index.d.ts', | ||
}); | ||
|
||
spy.mockRestore(); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
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 type = options.format.includes('esm') ? 'module' : 'commonjs'; | ||
const exports = { | ||
'.': {}, | ||
}; | ||
|
||
if (type === 'module') { | ||
// `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'; | ||
packageJson.main = './index.js'; | ||
} else { | ||
// Only if ESM is not used at all. | ||
packageJson.main = './index.cjs'; | ||
} | ||
|
||
if (options.format.includes('esm')) { | ||
exports['.']['import'] = './index.js'; | ||
} | ||
if (options.format.includes('umd') || options.format.includes('cjs')) { | ||
exports['.']['require'] = './index.cjs'; | ||
} | ||
|
||
packageJson.typings = `./${relative( | ||
options.entryRoot, | ||
options.entryFile | ||
).replace(/\.[jt]sx?$/, '.d.ts')}`; | ||
packageJson.type = type; | ||
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.