Skip to content

Commit

Permalink
fix(vite)!: generate config with esm by default (#29270)
Browse files Browse the repository at this point in the history
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 #
  • Loading branch information
leosvelperez authored Dec 10, 2024
1 parent e82e691 commit 6684fc0
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/shared/packages/vite/configure-vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions e2e/react/src/react-vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`
);

Expand All @@ -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`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(`
Expand All @@ -1141,6 +1142,7 @@ module.exports = withNx(
"sourceRoot": "myjslib/src",
},
"types": "./src/index.js",
"version": "0.0.1",
}
`);
});
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/generators/library/library.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('Remix Library Generator', () => {
"sourceRoot": "packages/foo/src",
},
"types": "./src/index.ts",
"version": "0.0.1",
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },"
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/utils/generator-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -723,7 +723,7 @@ function handleViteConfigFileExists(
entry: 'src/index.ts',
name: options.project,
fileName: 'index',
formats: ['es', 'cjs'],
formats: ['es'],
},
rollupOptions: {
external: options.rollupOptionsExternal ?? [],
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/utils/test-files/test-vite-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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'],
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/utils/vite-config-edit-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"]},
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"]},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6684fc0

Please sign in to comment.