Skip to content

Commit

Permalink
fix(vite)!: generate config with esm by default
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Dec 9, 2024
1 parent 28c53f9 commit c73f4ad
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 31 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
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 c73f4ad

Please sign in to comment.