Last active
January 2, 2018 12:05
-
-
Save chemzqm/7ceee3040f1377a534e6 to your computer and use it in GitHub Desktop.
webpack plugin for tern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// drop this into <tern folder>/plugin | |
// run `npm i enhanced-resolve` in you tern folder | |
// add config "plugins": { "webpack": {}} to your .tern_project | |
// done | |
(function(mod) { | |
if (typeof exports == "object" && typeof module == "object") // CommonJS | |
return mod(require("../lib/infer"), require("../lib/tern"), require("./commonjs"), require) | |
if (typeof define == "function" && define.amd) // AMD | |
return define(["../lib/infer", "../lib/tern", "./commonjs"], mod) | |
mod(tern, tern) | |
})(function(infer, tern, _, require) { | |
"use strict" | |
function resolve(name, parentFile) { | |
var resolved = resolveToFile(name, parentFile) | |
return resolved && infer.cx().parent.normalizeFilename(resolved) | |
} | |
function findDeclaredDeps() {} | |
var path = require("path"); | |
var req = require("enhanced-resolve"); | |
var Resolver = req.Resolver; | |
var DirectoryDescriptionFileFieldAliasPlugin = require("enhanced-resolve/lib/DirectoryDescriptionFileFieldAliasPlugin"); | |
var ModulesInDirectoriesPlugin = req.ModulesInDirectoriesPlugin; | |
var ModuleAsFilePlugin = req.ModuleAsFilePlugin; | |
var ModuleAsDirectoryPlugin = req.ModuleAsDirectoryPlugin; | |
var DirectoryDescriptionFilePlugin = req.DirectoryDescriptionFilePlugin; | |
var DirectoryDefaultFilePlugin = req.DirectoryDefaultFilePlugin; | |
var FileAppendPlugin = req.FileAppendPlugin; | |
var SyncNodeJsInputFileSystem =req.SyncNodeJsInputFileSystem; | |
var resolver = new Resolver(new SyncNodeJsInputFileSystem()); | |
resolver.apply( | |
new DirectoryDescriptionFileFieldAliasPlugin("package.json", "browser"), | |
new ModulesInDirectoriesPlugin("node", ["node_modules"]), | |
new ModuleAsFilePlugin("node"), | |
new ModuleAsDirectoryPlugin("node"), | |
new DirectoryDescriptionFilePlugin("package.json", ["main"]), | |
new DirectoryDefaultFilePlugin(["index"]), | |
new FileAppendPlugin(["", ".js"]) | |
); | |
function resolveToFile(name, parentFile) { | |
try { | |
var projectDir = infer.cx().parent.projectDir; | |
var fullParent = path.resolve(projectDir, parentFile); | |
var fullpath = resolver.resolveSync(fullParent, name); | |
} catch(e) { | |
return ''; | |
} | |
return fullpath; | |
} | |
if (require) (function() { | |
})(); else (function() { | |
function resolvePath(base, path) { | |
if (path[0] == "/") return path; | |
var slash = base.lastIndexOf("/"), m; | |
if (slash >= 0) path = base.slice(0, slash + 1) + path; | |
while (m = /[^\/]*[^\/\.][^\/]*\/\.\.\//.exec(path)) | |
path = path.slice(0, m.index) + path.slice(m.index + m[0].length); | |
return path.replace(/(^|[^\.])\.\//g, "$1"); | |
} | |
resolveToFile = function(name, parentFile) { | |
return /^\.\.?\//.test(name) ? resolvePath(parentFile, name) : name | |
} | |
})() | |
tern.registerPlugin("webpack", function(server) { | |
server.loadPlugin("commonjs") | |
server.mod.modules.resolvers.push(resolve) | |
findDeclaredDeps(server.projectDir, server.mod.modules.knownModules) | |
}) | |
}) |
@mikewootc enhanced-resolve got updated, don't use this script any more, use latest tern instead.
How should I configure my .tern-project file. I use npm+webpack+es6 with some alias defined in my webpack.configure.js:
${module_name} -> src/${module_name}
and I found tern can not complete names from my scripts and locally installed plugins
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used your plugin, but I got "Cannot find enhanced-resolve/lib/DirectoryDescriptionFileFieldAliasPlugin" error. And there is no this lib in the project "enhanced-resolve": (https://github.com/webpack/enhanced-resolve/tree/master/lib).