Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Loading