numopt-js 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -0
- package/dist/index.browser.js +9806 -0
- package/dist/index.browser.js.map +1 -0
- package/package.json +16 -3
package/README.md
CHANGED
|
@@ -32,6 +32,76 @@ A flexible numerical optimization library for JavaScript/TypeScript that works s
|
|
|
32
32
|
npm install numopt-js
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
## Browser Usage
|
|
36
|
+
|
|
37
|
+
numopt-js is designed to work seamlessly in browser environments. The library automatically provides a browser-optimized bundle that includes all dependencies.
|
|
38
|
+
|
|
39
|
+
### Option 1: Automatic Detection (Recommended)
|
|
40
|
+
|
|
41
|
+
Modern bundlers and browsers with import maps support will automatically use the browser bundle (`dist/index.browser.js`) when importing numopt-js in a browser environment. No additional configuration is needed.
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
<script type="module">
|
|
45
|
+
import { gradientDescent } from './node_modules/numopt-js/dist/index.browser.js';
|
|
46
|
+
|
|
47
|
+
// Your code here
|
|
48
|
+
</script>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Option 2: Using Import Maps
|
|
52
|
+
|
|
53
|
+
If you're using import maps, you can explicitly specify the browser bundle:
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<script type="importmap">
|
|
57
|
+
{
|
|
58
|
+
"imports": {
|
|
59
|
+
"numopt-js": "./node_modules/numopt-js/dist/index.browser.js"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
</script>
|
|
63
|
+
<script type="module">
|
|
64
|
+
import { gradientDescent } from 'numopt-js';
|
|
65
|
+
|
|
66
|
+
// Your code here
|
|
67
|
+
</script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Option 3: Using a Bundler
|
|
71
|
+
|
|
72
|
+
If you're using a bundler like Vite, Webpack, or Rollup, the bundler will automatically resolve the browser bundle based on the `package.json` exports configuration:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// In your TypeScript/JavaScript file
|
|
76
|
+
import { gradientDescent } from 'numopt-js';
|
|
77
|
+
|
|
78
|
+
// The bundler automatically uses dist/index.browser.js in browser builds
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Example with Vite:**
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
// vite.config.ts
|
|
85
|
+
import { defineConfig } from 'vite';
|
|
86
|
+
|
|
87
|
+
export default defineConfig({
|
|
88
|
+
// Vite automatically handles browser bundle resolution
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Troubleshooting
|
|
93
|
+
|
|
94
|
+
**Problem**: `ReferenceError: exports is not defined` when using in browser
|
|
95
|
+
|
|
96
|
+
**Solution**: Make sure you're using `dist/index.browser.js` instead of `dist/index.js`. The browser bundle includes all dependencies and is pre-configured for browser environments.
|
|
97
|
+
|
|
98
|
+
**Problem**: Module not found errors
|
|
99
|
+
|
|
100
|
+
**Solution**:
|
|
101
|
+
- Ensure you're using a modern bundler that supports `package.json` exports
|
|
102
|
+
- For direct browser usage, use import maps or explicitly import from `dist/index.browser.js`
|
|
103
|
+
- Check that your build tool supports ES modules
|
|
104
|
+
|
|
35
105
|
## Examples
|
|
36
106
|
|
|
37
107
|
After installing dependencies with `npm install`, you can run the example scripts with `npm run <script>`:
|