Skip to content

Externalized 3rd-party dependencies #192

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/text-annotator-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"typescript": "5.8.3",
"vite": "^6.3.5",
"vite-plugin-dts": "^4.5.4",
"vite-plugin-externalize-deps": "^0.9.0",
"vite-tsconfig-paths": "^5.1.4"
},
"peerDependencies": {
Expand All @@ -52,4 +53,4 @@
"@recogito/text-annotator-tei": "3.0.3",
"CETEIcean": "^1.9.4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,33 @@ import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsConfigPaths from 'vite-tsconfig-paths';
import dts from 'vite-plugin-dts';
import { externalizeDeps } from 'vite-plugin-externalize-deps';

import * as packageJson from './package.json';

export default defineConfig(({ command, mode }) => ({
export default defineConfig({
plugins: [
react(),
tsConfigPaths(),
dts({
include: ['./src/'],
entryRoot: './src'
})
}),
externalizeDeps(),
],
server: {
open: '/test/index.html'
},
build: {
minify: false,
lib: {
entry: './src/index.ts',
name: 'ReactTextAnnotator',
formats: ['es'],
fileName: (format) => `react-text-annotator.${format}.js`
},
rollupOptions: {
external: [
...Object.keys(packageJson.peerDependencies),
"@annotorious/core",
"@annotorious/react",
"@recogito/text-annotator",
"@recogito/text-annotator-tei"
],
output: {
preserveModules: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this property is redundant here. Unfortunately, the preserveModules: true doesn't preserve the original file structure of the package in the output, but simply splits it into quite a few chunks. To actually preserve it a more complex config is required, for example:

...
rollupOptions: {
	/**
	 * If you want to convert a set of files to another format while maintaining the file structure
	 * and export signatures, the recommended way—instead of using output.preserveModules that
	 * may tree-shake exports as well as emit virtual files created by plugins—is to
	 * turn every file into an entry point.
	 * @see https://rollupjs.org/configuration-options/#input
	 */
	input: Object.fromEntries(
		globSync('src/**/*.{ts,tsx}', {
			ignore: ['src/**/*.d.ts', 'src/**/*.stories.tsx', 'src/storybook/**/*']
		}).map((file) => [
			// 1. The name of the entry point
			// lib/nested/foo.js becomes nested/foo
			relative('src', file.slice(0, file.length - extname(file).length)),
			// 2. The absolute path to the entry file
			// lib/nested/foo.ts becomes /project/lib/nested/foo.ts
			fileURLToPath(new URL(file, import.meta.url))
		])
	),
	...

However, I believe it's an overkill for the package, as it still has only a single entry point. Also having the sourcemaps will help the browser to navigate through the original structure.

assetFileNames: 'react-text-annotator.[ext]'
}
},
sourcemap: true
}
}));
});
5 changes: 3 additions & 2 deletions packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"@types/jsdom": "^21.1.7",
"@types/rbush": "^4.0.0",
"jsdom": "^26.1.0",
"typescript": "5.8.3",
"typescript": "^5.8.3",
"vite": "^6.3.5",
"vite-plugin-dts": "^4.5.4",
"vite-plugin-externalize-deps": "^0.9.0",
"vitest": "^3.1.3"
},
"dependencies": {
Expand All @@ -42,4 +43,4 @@
"rbush": "^4.0.1",
"uuid": "^11.1.0"
}
}
}
28 changes: 0 additions & 28 deletions packages/text-annotator/vite.config.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/text-annotator/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { externalizeDeps } from 'vite-plugin-externalize-deps';

export default defineConfig({
plugins: [
dts({ insertTypesEntry: true, entryRoot: '.' }),
externalizeDeps(),
],
server: {
open: '/test/index.html'
},
build: {
sourcemap: true,
lib: {
entry: './src/index.ts',
name: 'RecogitoJS',
formats: ['es', 'umd'],
fileName: (format) => `text-annotator.${format}.js`
},
rollupOptions: {
output: {
assetFileNames: 'text-annotator.[ext]',
globals: {
'colord': 'Colord',
'uuid': 'UUID',
'dequal/lite': 'DequalLite',
'@annotorious/core': 'AnnotoriousCore',
'rbush': "RBush",
'hotkeys-js': 'HotkeysJs',
}
Comment on lines +24 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The globals are needed to bundle the UMD package using predictable names for the external packages. Otherwise, they will be guessed by Vite:
image

}
}
}
});