Skip to content
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

Fix resolution on Atom editor #8

Merged
merged 4 commits into from
Nov 17, 2015
Merged

Fix resolution on Atom editor #8

merged 4 commits into from
Nov 17, 2015

Conversation

azu
Copy link
Contributor

@azu azu commented Nov 14, 2015

@takuyaa Hi, I fixed require("kuromoji"); // undefeined issue on some env like Atom.

Purpose

Currently, kuromoji.js doesn't work on Atom editor(plugin).
This pull request fix this issue.

require("kuromoji"); // undefeined in Atom.

Changes

  • Remove window detection from src/kuromoji.js and DictionaryLoader
  • Split up DictionaryLoader into BrowserDictionaryLoader and NodeDictionaryLoader

Atom has window object... it means that module.exports = undefined @ src/kuromoji.js

I refactor this feature detection and move feature detection to browserify's build phase.
(So, feature detection code was removed)

  • src/ code use directly NodeDictionaryLoader.
  • dist/browser/kuromoji.js use directly BrowserDictionaryLoader ( dist/browser/kuromoji.js doesn't contain NodeDictionaryLoader)

I'v added a macro that replace NodeDictionaryLoader to BrowserDictionaryLoader into gulpfile.js and package.json

This feature detection may fail on Atom env.
use browserify's standalone instead of `window` detection
use NodeDictionaryLoader as a default.
if use BrowserDictionaryLoader, build files by browserify.
browser/kuromoji.js use BrowserDictionaryLoader instead of NodeDictionaryLoader.
@@ -0,0 +1,46 @@
var zlib = require("zlibjs/bin/gunzip.min.js");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Browser use BrowserDictionaryLoader.js (dist/browser/kuromoji.js)

@@ -4,6 +4,9 @@
"author": "Takuya Asano <takuya.a@gmail.com>",
"description": "JavaScript implementation of Japanese morphological analyzer",
"main": "./dist/node/kuromoji.js",
"browser": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@takuyaa takuyaa self-assigned this Nov 17, 2015
@takuyaa takuyaa added this to the 0.0.5 milestone Nov 17, 2015
@@ -4,6 +4,9 @@
"author": "Takuya Asano <takuya.a@gmail.com>",
"description": "JavaScript implementation of Japanese morphological analyzer",
"main": "./dist/node/kuromoji.js",
"browser": {
"./dist/node/loader/NodeDictionaryLoader.js": "./dist/node/loader/BrowserDictionaryLoader.js"
Copy link
Owner

Choose a reason for hiding this comment

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

I don't know about browser field in details, but if this line is replaced by

"./src/loader/NodeDictionaryLoader.js": "./src/loader/BrowserDictionaryLoader.js"

, perhaps we don't need this line of code in gulpfile.js:

b.require(__dirname + "/src/loader/BrowserDictionaryLoader.js", {expose: "./loader/NodeDictionaryLoader.js"});

(I think browserify will make this rearrangement)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Edit: Ah, I agree with that.
But, this change cause external transform not work.


browser field for external.(This affect external browserify command)

external is a user that use kuromoji as a module.

b.require for internal.(This affect only gulp build)

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm... gulp build does not seem to need b.require in my environment.

my package.json:

  "browser": {
    "./src/loader/NodeDictionaryLoader.js": "./src/loader/BrowserDictionaryLoader.js"
  },

my gulpfile.js:

    var b = browserify({
        entries: ["./src/kuromoji.js"],
        standalone: "kuromoji" // window.kuromoji
    });
    // Commented out b.require
    // b.require(__dirname + "/src/loader/BrowserDictionaryLoader.js", {expose: "./loader/NodeDictionaryLoader.js"});
    b.bundle()
        .pipe(source("kuromoji.js"))
        .pipe(gulp.dest("./dist/browser/"));

I executed

$ gulp build

then I got preferred dist/browser/kuromoji.js with BrowserDictionaryLoader.

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

I got it!
b.require is needed for such that modules:

  1. require kuromoji.js (in node.js manner)
  2. are browserified (for working on browser)
    (but not tested... If you encountered problems, please open an issue)

@takuyaa
Copy link
Owner

takuyaa commented Nov 17, 2015

Thanks a lot @azu
This is pretty good PR and some tests on my browsers work well.
I never known about browser field, but it seems to be suitable in this situation.

I have a question or comment on this point.
I'd like to get your opinion about that.

takuyaa added a commit that referenced this pull request Nov 17, 2015
Fix resolution on Atom editor
@takuyaa takuyaa merged commit ea954a6 into takuyaa:master Nov 17, 2015
@azu azu deleted the fix-window branch November 17, 2015 22:55
@azu
Copy link
Contributor Author

azu commented Nov 17, 2015

Thanks!
I can't wait for new release!

@takuyaa takuyaa added the bug label Nov 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants