fix(vite)!: generate config with esm by default (#29270)

BREAKING CHANGE

When generating projects that use Vite, the Vite configuration will be
set to use the ESM format only. Previously, the configuration was set to
produce both ESM and CJS, but the dual format was not correctly
configured in the libraries' `package.json` files, nor was it producing
the correct declaration files.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Leosvel Pérez Espinosa 2024-12-10 11:58:51 +01:00 committed by GitHub
parent e82e69198f
commit 6684fc0688
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 31 additions and 34 deletions

View File

@ -218,7 +218,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.

View File

@ -116,7 +116,6 @@ describe('Build React applications and libraries with Vite', () => {
await runCLIAsync(`build ${viteLib}`);
checkFilesExist(
`dist/libs/${viteLib}/index.d.ts`,
`dist/libs/${viteLib}/index.js`,
`dist/libs/${viteLib}/index.mjs`
);
}, 300_000);
@ -132,7 +131,6 @@ describe('Build React applications and libraries with Vite', () => {
checkFilesExist(
`dist/libs/${viteLib}/index.d.ts`,
`dist/libs/${viteLib}/index.js`,
`dist/libs/${viteLib}/index.mjs`
);
@ -147,7 +145,6 @@ describe('Build React applications and libraries with Vite', () => {
await runCLIAsync(`build ${nonBuildableLib}`);
checkFilesExist(
`dist/libs/${nonBuildableLib}/index.d.ts`,
`dist/libs/${nonBuildableLib}/index.js`,
`dist/libs/${nonBuildableLib}/index.mjs`,
`dist/libs/${nonBuildableLib}/README.md`
);

View File

@ -68,7 +68,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.

View File

@ -967,7 +967,7 @@ module.exports = withNx(
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -1129,6 +1129,7 @@ module.exports = withNx(
"sourceRoot": "mylib/src",
},
"types": "./src/index.ts",
"version": "0.0.1",
}
`);
expect(readJson(tree, 'myjslib/package.json')).toMatchInlineSnapshot(`
@ -1141,6 +1142,7 @@ module.exports = withNx(
"sourceRoot": "myjslib/src",
},
"types": "./src/index.js",
"version": "0.0.1",
}
`);
});

View File

@ -75,6 +75,7 @@ export async function libraryGeneratorInternal(host: Tree, schema: Schema) {
: undefined;
writeJson(host, `${options.projectRoot}/package.json`, {
name: options.importPath,
version: '0.0.1',
main: sourceEntry,
types: sourceEntry,
nx: {

View File

@ -175,6 +175,7 @@ describe('Remix Library Generator', () => {
"sourceRoot": "packages/foo/src",
},
"types": "./src/index.ts",
"version": "0.0.1",
}
`);
});

View File

@ -39,7 +39,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -210,7 +210,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -262,7 +262,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -309,7 +309,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.

View File

@ -370,15 +370,15 @@ describe('@nx/vite:configuration', () => {
{
"exports": {
".": {
"default": "./dist/index.js",
"import": "./dist/index.mjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts",
},
"./package.json": "./package.json",
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"module": "./dist/index.js",
"name": "@proj/my-lib",
"type": "module",
"types": "./dist/index.d.ts",
"version": "0.0.1",
}

View File

@ -216,11 +216,7 @@ function updatePackageJson(
rootDir,
generateExportsField: true,
packageJsonPath,
format: ['esm', 'cjs'],
// when building both formats, we don't set the package.json "type" field, so
// we need to set the esm extension to ".mjs" to match vite output
// see the "File Extensions" callout in https://vite.dev/guide/build.html#library-mode
outputFileExtensionForEsm: '.mjs',
format: ['esm'],
});
}

View File

@ -16,7 +16,7 @@ exports[`ensureViteConfigIsCorrect should add build options if it is using condi
...{
my: 'option',
},
..."\\n // Configuration for building your library.\\n // See: https://vitejs.dev/guide/build.html#library-mode\\n build: {\\n lib: {\\n // Could also be a dictionary or array of multiple entry points.\\n entry: 'src/index.ts',\\n name: 'my-app',\\n fileName: 'index',\\n // Change this to the formats you want to support.\\n // Don't forget to update your package.json as well.\\n formats: ['es', 'cjs']\\n },\\n rollupOptions: {\\n // External packages that should not be bundled into your library.\\n external: ['react', 'react-dom', 'react/jsx-runtime']\\n }\\n },"
..."\\n // Configuration for building your library.\\n // See: https://vitejs.dev/guide/build.html#library-mode\\n build: {\\n lib: {\\n // Could also be a dictionary or array of multiple entry points.\\n entry: 'src/index.ts',\\n name: 'my-app',\\n fileName: 'index',\\n // Change this to the formats you want to support.\\n // Don't forget to update your package.json as well.\\n formats: ['es']\\n },\\n rollupOptions: {\\n // External packages that should not be bundled into your library.\\n external: ['react', 'react-dom', 'react/jsx-runtime']\\n }\\n },"
}
}
})

View File

@ -168,7 +168,7 @@ describe('generator utils', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.

View File

@ -409,7 +409,7 @@ export function createOrEditViteConfig(
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -723,7 +723,7 @@ function handleViteConfigFileExists(
entry: 'src/index.ts',
name: options.project,
fileName: 'index',
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
external: options.rollupOptionsExternal ?? [],

View File

@ -159,7 +159,7 @@ export const hasEverything = `
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -186,7 +186,7 @@ export const buildOption = `
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -198,7 +198,7 @@ export const buildOptionObject = {
entry: 'src/index.ts',
name: 'my-app',
fileName: 'index',
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
external: ['react', 'react-dom', 'react/jsx-runtime'],

View File

@ -66,7 +66,7 @@ describe('ensureViteConfigIsCorrect', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -126,7 +126,7 @@ describe('ensureViteConfigIsCorrect', () => {
build: {
'my': 'option',
'lib': {"entry":"src/index.ts","name":"my-app","fileName":"index","formats":["es","cjs"]},
'lib': {"entry":"src/index.ts","name":"my-app","fileName":"index","formats":["es"]},
'rollupOptions': {"external":["react","react-dom","react/jsx-runtime"]},
}
@ -176,7 +176,7 @@ describe('ensureViteConfigIsCorrect', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -254,7 +254,7 @@ describe('ensureViteConfigIsCorrect', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -326,7 +326,7 @@ describe('ensureViteConfigIsCorrect', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -379,7 +379,7 @@ describe('ensureViteConfigIsCorrect', () => {
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs']
formats: ['es']
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -433,7 +433,7 @@ describe('ensureViteConfigIsCorrect', () => {
build: {
'my': 'option',
'lib': {"entry":"src/index.ts","name":"my-app","fileName":"index","formats":["es","cjs"]},
'lib': {"entry":"src/index.ts","name":"my-app","fileName":"index","formats":["es"]},
'rollupOptions': {"external":["react","react-dom","react/jsx-runtime"]},
}

View File

@ -41,7 +41,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
@ -132,7 +132,7 @@ export default defineConfig({
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.