Skip to content

Commit

Permalink
Add .js extension for better module portability (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
tushuhei authored Jul 29, 2022
1 parent 6d4bd13 commit b767d79
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 316 deletions.
276 changes: 0 additions & 276 deletions javascript/package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
},
"scripts": {
"build": "npm run build:es && npm run build:cjs",
"build:cjs": "tsc --outDir dist --module CommonJS --sourceMap false",
"build:es": "tsc --outDir module --module ES6 --sourceMap false && cp module/dom-browser.js module/dom.js",
"build:cjs": "tsc --outDir dist --module CommonJS --sourceMap false && cp -r src/tests/models dist/tests/models",
"build:es": "tsc --outDir module --module ES6 --sourceMap false && cp module/dom-browser.js module/dom.js && cp -r src/tests/models module/tests/models",
"bundle": "npm run bundle:webcomponent:ja && npm run bundle:webcomponent:zh-hans",
"bundle:webcomponent:ja": "esbuild module/webcomponents/budoux-ja.js --bundle --minify --sourcemap --outfile=bundle/budoux-ja.min.js",
"bundle:webcomponent:zh-hans": "esbuild module/webcomponents/budoux-zh-hans.js --bundle --minify --sourcemap --outfile=bundle/budoux-zh-hans.min.js",
"copy": "node ./scripts/copy-data.js",
"prepare": "npm run copy && npm run build && npm run bundle",
"test": "ts-node node_modules/jasmine/bin/jasmine tests/*.ts",
"test": "npm run test:jasmine && npm run test:cli-version",
"test:jasmine": "jasmine dist/tests/test_*.js",
"test:cli-version": "node ./scripts/check-cli-version.js",
"lint": "gts lint",
"fix": "gts fix",
"clean": "rm -rf dist module src/data"
Expand All @@ -39,7 +41,6 @@
"esbuild": "^0.14.49",
"gts": "^4.0.0",
"jasmine": "^4.2.1",
"ts-node": "^10.8.2",
"typescript": "^4.7.4"
},
"dependencies": {
Expand Down
52 changes: 52 additions & 0 deletions javascript/scripts/check-cli-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license
* Copyright 2021 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const assert = require('assert');
const path = require('path');
const childProcess = require('child_process');
const package = require('../package.json');

const packageVersion = package.version;
const runCli = args =>
new Promise(resolve => {
childProcess.execFile(
'node',
[path.resolve(__dirname, '..', 'bin', 'budoux.js'), ...args],
(error, stdout, stderr) => {
resolve({
error,
stdout,
stderr,
});
}
);
});

runCli(['-V']).then(({stdout}) => {
assert.equal(
stdout.replace('\n', ''),
packageVersion,
'Package version and CLI version output (-V) should match.'
);
});

runCli(['--version']).then(({stdout}) => {
assert.equal(
stdout.replace('\n', ''),
packageVersion,
'Package version and CLI version output (--version) should match.'
);
});
2 changes: 1 addition & 1 deletion javascript/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {readFileSync} from 'fs';
import {resolve} from 'path';
import * as readline from 'readline';
import {Command} from 'commander';
import {Parser, loadDefaultJapaneseParser} from './parser';
import {Parser, loadDefaultJapaneseParser} from './parser.js';

/**
* Run the command line interface program.
Expand Down
2 changes: 1 addition & 1 deletion javascript/src/html_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Parser} from './parser';
import {Parser} from './parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './parser';
export * from './parser.js';
12 changes: 6 additions & 6 deletions javascript/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import {unicodeBlocks} from './data/unicode_blocks';
import {model as jaKNBCModel} from './data/models/ja-knbc';
import {model as zhHansModel} from './data/models/zh-hans';
import {parseFromString} from './dom';
import {HTMLProcessor} from './html_processor';
import {bisectRight, INVALID, sum} from './utils';
import {unicodeBlocks} from './data/unicode_blocks.js';
import {model as jaKNBCModel} from './data/models/ja-knbc.js';
import {model as zhHansModel} from './data/models/zh-hans.js';
import {parseFromString} from './dom.js';
import {HTMLProcessor} from './html_processor.js';
import {bisectRight, INVALID, sum} from './utils.js';

// We could use `Node.TEXT_NODE` and `Node.ELEMENT_NODE` in a browser context,
// but we define the same here for Node.js environments.
Expand Down
34 changes: 17 additions & 17 deletions javascript/tests/test_cli.ts → javascript/src/tests/test_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import {cli} from '../src/cli';
import {version} from '../package.json';
import {cli} from '../cli.js';
import {execFile, ExecFileException} from 'child_process';
import * as path from 'path';
import stream from 'stream';
Expand Down Expand Up @@ -64,7 +63,11 @@ describe('cli', () => {

it('should output the separated sentence with custom model when execute budoux command with --model option.', () => {
const inputText = 'abcdeabcd';
const customModelPath = 'tests/models/separate_right_before_a.json';
const customModelPath = path.resolve(
__dirname,
'models',
'separate_right_before_a.json'
);
const argv = ['node', 'budoux', '--model', customModelPath, inputText];
const expectedStdOuts = 'abcde\nabcd'.split('\n');
cli(argv);
Expand All @@ -75,7 +78,11 @@ describe('cli', () => {

it('should output the separated sentence with custom model when execute budoux command with -m option alias.', () => {
const inputText = 'abcdeabcd';
const customModelPath = 'tests/models/separate_right_before_a.json';
const customModelPath = path.resolve(
__dirname,
'models',
'separate_right_before_a.json'
);
const argv = ['node', 'budoux', '-m', customModelPath, inputText];
const expectedStdOuts = 'abcde\nabcd'.split('\n');
cli(argv);
Expand Down Expand Up @@ -164,11 +171,16 @@ describe('cli', () => {
});

it('should output the error message when get extra option argument.', () => {
const customModelPath = path.resolve(
__dirname,
'models',
'separate_right_before_a.json'
);
const argv = [
'node',
'budoux',
'--model',
'tests/models/separate_right_before_a.json',
customModelPath,
'<extra model option arguments>',
'今日は天気です。',
];
Expand All @@ -179,18 +191,6 @@ describe('cli', () => {
);
});

it('should output the version number when execute budoux command with --veriosn option.', async () => {
const {stdout} = await runCli(['--version']);

expect(stdout).toBe(`${version}\n`);
}, 3000);

it('should output the version number when execute budoux command with -V option alias.', async () => {
const {stdout} = await runCli(['-V']);

expect(stdout).toBe(`${version}\n`);
}, 3000);

it('should output the unknown option error when execute budoux command with -v option.', async () => {
const {stderr} = await runCli(['-v']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import 'jasmine';
import {JSDOM} from 'jsdom';
import {loadDefaultJapaneseParser} from '../src/parser';
import {HTMLProcessor, HTMLProcessorOptions} from '../src/html_processor';
import {loadDefaultJapaneseParser} from '../parser.js';
import {HTMLProcessor, HTMLProcessorOptions} from '../html_processor.js';

let emulateNotConnected = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import 'jasmine';
import {JSDOM} from 'jsdom';
import {Parser} from '../src/parser';
import {INVALID} from '../src/utils';
import {Parser} from '../parser.js';
import {INVALID} from '../utils.js';

describe('Parser.getUnicodeBlockFeature', () => {
const testFeature = (character: string, feature: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import 'jasmine';
import {bisectRight} from '../src/utils';
import {bisectRight} from '../utils.js';

describe('utils.bisectRight', () => {
const arr = [1, 3, 8, 12, 34];
Expand Down
2 changes: 1 addition & 1 deletion javascript/src/webcomponents/budoux-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Parser} from '..';
import {Parser} from '../parser.js';

/**
* Base BudouX Web component.
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/webcomponents/budoux-ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import {loadDefaultJapaneseParser} from '../parser';
import {BudouXBaseElement} from './budoux-base';
import {loadDefaultJapaneseParser} from '../parser.js';
import {BudouXBaseElement} from './budoux-base.js';

/**
* BudouX Japanese Web component.
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/webcomponents/budoux-zh-hans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import {loadDefaultSimplifiedChineseParser} from '../parser';
import {BudouXBaseElement} from './budoux-base';
import {loadDefaultSimplifiedChineseParser} from '../parser.js';
import {BudouXBaseElement} from './budoux-base.js';

/**
* BudouX Simplified Chinese Web component.
Expand Down

0 comments on commit b767d79

Please sign in to comment.