From 7e328e50ce1638cc924899a9f102295b71d6f5c6 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 4 Dec 2024 11:43:11 -0500 Subject: [PATCH] fix(react): point output to correct location --- .../library/lib/add-rollup-build-target.ts | 15 +-- .../src/generators/library/library.spec.ts | 93 +++++++++++++++++++ 2 files changed, 102 insertions(+), 6 deletions(-) diff --git a/packages/react/src/generators/library/lib/add-rollup-build-target.ts b/packages/react/src/generators/library/lib/add-rollup-build-target.ts index cb9b109cd5eec2..d6a5e8145af69a 100644 --- a/packages/react/src/generators/library/lib/add-rollup-build-target.ts +++ b/packages/react/src/generators/library/lib/add-rollup-build-target.ts @@ -8,7 +8,6 @@ import { readNxJson, readProjectConfiguration, runTasksInSerial, - stripIndents, updateProjectConfiguration, } from '@nx/devkit'; @@ -76,11 +75,15 @@ const svg = require('@svgr/rollup'); module.exports = withNx({ main: '${maybeJs(options, './src/index.ts')}', - outputPath: '${joinPathFragments( - offsetFromRoot(options.projectRoot), - 'dist', - options.projectRoot - )}', + outputPath: '${ + options.isUsingTsSolutionConfig + ? './dist' + : joinPathFragments( + offsetFromRoot(options.projectRoot), + 'dist', + options.projectRoot + ) + }', tsConfig: './tsconfig.lib.json', compiler: '${options.compiler ?? 'babel'}', external: ${JSON.stringify(external)}, diff --git a/packages/react/src/generators/library/library.spec.ts b/packages/react/src/generators/library/library.spec.ts index bac47f27032755..cf55fbd9b10058 100644 --- a/packages/react/src/generators/library/library.spec.ts +++ b/packages/react/src/generators/library/library.spec.ts @@ -934,6 +934,59 @@ module.exports = withNx( name: 'mylib', }); + expect(tree.read('mylib/vite.config.ts', 'utf-8')).toMatchInlineSnapshot(` + "/// + import { defineConfig } from 'vite'; + import react from '@vitejs/plugin-react'; + import dts from 'vite-plugin-dts'; + import * as path from 'path'; + + export default defineConfig({ + root: __dirname, + cacheDir: '../node_modules/.vite/mylib', + plugins: [react(), dts({ entryRoot: 'src', tsconfigPath: path.join(__dirname, 'tsconfig.lib.json') })], + // Uncomment this if you are using workers. + // worker: { + // plugins: [ nxViteTsPaths() ], + // }, + // Configuration for building your library. + // See: https://vitejs.dev/guide/build.html#library-mode + build: { + outDir: './dist', + emptyOutDir: true, + reportCompressedSize: true, + commonjsOptions: { + transformMixedEsModules: true, + }, + lib: { + // Could also be a dictionary or array of multiple entry points. + entry: 'src/index.ts', + name: 'mylib', + fileName: 'index', + // Change this to the formats you want to support. + // Don't forget to update your package.json as well. + formats: ['es', 'cjs'] + }, + rollupOptions: { + // External packages that should not be bundled into your library. + external: ['react','react-dom','react/jsx-runtime'] + }, + }, + test: { + watch: false, + globals: true, + environment: 'jsdom', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + reporters: ['default'], + coverage: { + reportsDirectory: '../coverage/mylib', + provider: 'v8', + } + }, + }); + " + `); + expect(readJson(tree, 'tsconfig.json').references).toMatchInlineSnapshot(` [ { @@ -1089,5 +1142,45 @@ module.exports = withNx( } `); }); + + it('should configure rollup correctly', async () => { + await libraryGenerator(tree, { + ...defaultSchema, + bundler: 'rollup', + unitTestRunner: 'none', + directory: 'mylib', + name: 'mylib', + }); + + expect(tree.read('mylib/rollup.config.cjs', 'utf-8')) + .toMatchInlineSnapshot(` + "const { withNx } = require('@nx/rollup/with-nx'); + const url = require('@rollup/plugin-url'); + const svg = require('@svgr/rollup'); + + module.exports = withNx({ + main: './src/index.ts', + outputPath: './dist', + tsConfig: './tsconfig.lib.json', + compiler: 'babel', + external: ["react","react-dom","react/jsx-runtime"], + format: ['esm'], + assets:[{ input: '.', output: '.', glob: 'README.md'}], + }, { + // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options + plugins: [ + svg({ + svgo: false, + titleProp: true, + ref: true, + }), + url({ + limit: 10000, // 10kB + }), + ], + }); + " + `); + }); }); });