Skip to content

Commit

Permalink
feat: support esm (#8)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: support ESM and remove options

This version make `sudachi-synonyms-dictionary` simple ESM/CommonJS module

fix #7
  • Loading branch information
azu authored May 13, 2021
1 parent 38966bf commit df1fe4c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 56 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ It is a npm package for Sudachi's [synonyms dictionary](https://github.com/Works

## Feature

- Work on browser
- Use another dictionary url via `options`
- In-Memoery cached
- Work on Browser(ESM) and Node.js(CommonJS)
- Includes `sudachi-synonyms-dictionary.json` file

## Install

Expand Down
31 changes: 0 additions & 31 deletions index-browser.js

This file was deleted.

19 changes: 1 addition & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
let cachedJSON = null;

function fetchDictionary(options) {
// for browser that depended on `fetch` API
if (options && options.url) {
if (cachedJSON) {
return Promise.resolve(cachedJSON);
}
return fetch(options.url).then(res => {
if (!res.ok) {
return Promise.reject(new Error(res.statusText));
}
return res.json();
}).then(json => {
cachedJSON = json;
return json;
});
}
function fetchDictionary() {
return Promise.resolve(require("./sudachi-synonyms-dictionary"));
}

Expand Down
3 changes: 3 additions & 0 deletions index.module.js

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
"author": "azu",
"files": [
"index.js",
"index.d.ts",
"index.modules.js",
"sudachi-synonyms-dictionary.json"
],
"main": "index.js",
"types": "index.d.ts",
"browser": "index-browser.js",
"main": "./index.js",
"module": "./index.module.js",
"types": "./index.d.ts",
"exports": {
"require": "./index.js",
"import": "./index.module.js"
},
"scripts": {
"update": "node update.js",
"test": "node test/index.test.js"
Expand Down
6 changes: 6 additions & 0 deletions update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require("path");
const fetch = require("node-fetch");
const { parse } = require("sudachi-synonyms-parser");
const semver = require("semver");
const OUTPUT_ESM_PATH = path.join(__dirname, "index.module.js");
const OUTPUT_PATH = path.join(__dirname, "sudachi-synonyms-dictionary.json");
const DICT_URL = "https://raw.githubusercontent.com/WorksApplications/SudachiDict/develop/src/main/text/synonyms.txt";
(async function () {
Expand All @@ -13,7 +14,12 @@ const DICT_URL = "https://raw.githubusercontent.com/WorksApplications/SudachiDic
if (util.isDeepStrictEqual(JSON.parse(JSON.stringify(json)), prevJSON)) {
return;
}
// perf: https://www.youtube.com/watch?v=ff4fgQxPaO0
const esm = `export function fetchDictionary(){
return Promise.resolve(JSON.parse(\`${JSON.stringify(json)}\`))
}`;
fs.writeFileSync(OUTPUT_PATH, JSON.stringify(json), "utf-8");
fs.writeFileSync(OUTPUT_ESM_PATH, esm, "utf-8");
// update major
const pkg = require("./package.json");
pkg.version = semver.inc(pkg.version, "major");
Expand Down

0 comments on commit df1fe4c

Please sign in to comment.