Skip to content

Commit 86721e9

Browse files
committed
rename export and provide cjs
1 parent 7040405 commit 86721e9

File tree

7 files changed

+249
-28
lines changed

7 files changed

+249
-28
lines changed

"dist/index.cjs.js

Lines changed: 203 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ main()
4747
In the browser, you can use the following code:
4848

4949
```js
50+
import { Buffer } from "buffer";
5051
import { SentencePieceProcessor, cleanText, llama_3_1_tokeniser_b64 } from "@sctg/sentencepiece-js";
52+
53+
// eslint-disable-next-line no-undef
54+
globalThis.Buffer = Buffer;
5155
// built in models: llama_3_1_tokeniser_b64, clean_30k_b64, smart_b64
5256
async function main() {
5357

@@ -67,7 +71,8 @@ async function main() {
6771
main()
6872
```
6973

70-
## Note
74+
See https://github.com/sctg-development/ai-outlook/blob/HEAD/src/aipane/aipane.ts#L11-L23 for an example of how to use this in a react app.
75+
Look also at webpack.config.js for the configuration of the webpack bundler.
7176

7277
- devilyouwei updated this repo to make this module support the js `require` keyword and added the using example.
7378
- 2023-1-10, devilyouwei added `encodePieces`.

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ cd build
33
emcmake cmake ../sentencepiece
44
emmake make sentencepiece-static
55
emcc --bind -o ../src/sentencepiece.js -Wl,--whole-archive src/libsentencepiece.a -Wl,--no-whole-archive ../bindings/sentencepiece.cpp \
6-
-O3 -s EXPORT_ES6=1 -s MODULARIZE=1 -s SINGLE_FILE=1 -s EXPORTED_RUNTIME_METHODS=FS -s ALLOW_MEMORY_GROWTH=1
6+
-O3 -s EXPORT_ES6=1 -s MODULARIZE=1 -s SINGLE_FILE=1 -s EXPORTED_RUNTIME_METHODS=FS -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_NAME="createSentencePieceModule"
77
cd ..

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "@sctg/sentencepiece-js",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Sentencepiece tokenization for natural language processing, JS version.",
55
"main": "dist/index.js",
6+
"type": "module",
67
"exports": {
78
"imports": "./dist/index.js",
89
"default": "./dist/index.js"
@@ -16,6 +17,7 @@
1617
},
1718
"files": [
1819
"./dist/index.js",
20+
"./dist/index.cjs",
1921
"./dist/index.d.ts"
2022
],
2123
"repository": {
@@ -36,6 +38,7 @@
3638
"homepage": "https://github.com/sctg-development/sentencepiece-js",
3739
"devDependencies": {
3840
"@esm-bundle/chai": "^4.3.4-fix.0",
41+
"@rollup/plugin-babel": "^6.0.4",
3942
"@rollup/plugin-commonjs": "^28.0.0",
4043
"@rollup/plugin-node-resolve": "^15.3.0",
4144
"@rollup/plugin-typescript": "^12.1.0",

rollup.config.mjs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
import { nodeResolve } from '@rollup/plugin-node-resolve';
2-
import commonjs from '@rollup/plugin-commonjs';
3-
import { wasm } from '@rollup/plugin-wasm';
4-
import typescript from '@rollup/plugin-typescript';
5-
import dts from 'rollup-plugin-dts';
1+
import { nodeResolve } from "@rollup/plugin-node-resolve";
2+
import commonjs from "@rollup/plugin-commonjs";
3+
import { babel } from "@rollup/plugin-babel";
4+
import { wasm } from "@rollup/plugin-wasm";
5+
import typescript from "@rollup/plugin-typescript";
6+
import dts from "rollup-plugin-dts";
67

78
export default [
8-
{
9-
input: 'src/index.ts',
10-
output: {
11-
dir: 'dist',
12-
format: 'cjs'
13-
},
14-
plugins: [nodeResolve({ browser: true }), commonjs(), wasm(), typescript({ target: "es5", downlevelIteration: true })]
9+
{
10+
input: "src/index.ts",
11+
output: [
12+
{
13+
dir: "dist",
14+
format: "es",
15+
},
16+
{ file: "dist/index.cjs", format: "cjs" },
17+
],
18+
plugins: [
19+
nodeResolve({ browser: true }),
20+
commonjs(),
21+
babel({ babelHelpers: "bundled" }),
22+
wasm(),
23+
typescript({ target: "es6", downlevelIteration: true }),
24+
],
25+
},
26+
{
27+
input: "src/index.ts",
28+
output: {
29+
file: "dist/index.d.ts",
30+
format: "es",
1531
},
16-
{
17-
input: 'src/index.ts',
18-
output: {
19-
file: 'dist/index.d.ts',
20-
format: 'es'
21-
},
22-
plugins: [dts()]
23-
}
24-
];
32+
plugins: [dts()],
33+
},
34+
];

src/sentencePieceProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Module from "./sentencepiece"
1+
import createSentencePieceModule from "./sentencepiece.js"
22
import * as fs from "fs"
33

44
export class SentencePieceProcessor {
@@ -31,7 +31,7 @@ export class SentencePieceProcessor {
3131
// private function to load model
3232
private async _loadModel(model: Buffer) {
3333
const tempName = this.uuidv4() + ".model";
34-
this.sentencepiece = await Module();
34+
this.sentencepiece = await createSentencePieceModule();
3535
this.sentencepiece.FS.writeFile(tempName, model);
3636
const string_view = new this.sentencepiece.StringView(tempName);
3737
const absl_string_view = string_view.getView();

src/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { SentencePieceProcessor, cleanText, llama_3_1_tokeniser_b64 } = require("../dist");
2-
const ROOT = require('app-root-path')
1+
import { SentencePieceProcessor, cleanText, llama_3_1_tokeniser_b64 } from "../dist/index.js";
2+
import ROOT from 'app-root-path';
33

44
async function main() {
55

0 commit comments

Comments
 (0)