[英]Export global function using webpack
I'm trying to write an isomorphic module. 我正在尝试编写一个同构模块。 The server javascript is going to run inside of JINT.
服务器javascript将在JINT内部运行。 I have created a webpack bundle specifically to build the server version of the module.
我专门创建了一个webpack包来构建模块的服务器版本。 I want to expose a single function that I can get JINT to call.
我想公开一个我可以让JINT调用的函数。 I am using the scriptEngine.Invoke function from JINT however this is looking for a function on the global object.
我正在使用JINT的scriptEngine.Invoke函数,但是这是在全局对象上寻找一个函数。 I don't know how to get a function onto the global object.
我不知道如何将函数放到全局对象上。 I have tried using the expose-loader but this seems to export the entire module and I can't get it to just expose a single function.
我已经尝试使用expose-loader,但这似乎导出整个模块,我不能让它只暴露一个函数。
Here is my entry point: 这是我的切入点:
require("expose?FormValidator!./formValidator.js");
Here is my formValidator.js: 这是我的formValidator.js:
export default function validate () {
return 'HelloWorld';
}
When I load up the resulting bundle and examine the FormValidator global it is an object with a validate function. 当我加载生成的bundle并检查FormValidator全局时,它是一个带有validate函数的对象。 Is there a way I can get the validate function to be directly assigned to FormValidator?
有没有办法让validate函数直接分配给FormValidator?
Instead of using the ES6 export syntax, try simply using module.exports = function validate() {}
, which should work. 不要使用ES6导出语法,只需使用
module.exports = function validate() {}
,这应该可行。
Babel is probably why things don't work the way you expect them too right now. 巴贝尔可能就是为什么事情不会像你现在所期望的那样发挥作用。 Exporting with Babel does the following:
使用Babel导出会执行以下操作:
export default function testDefault() {}
export function testNamed() {}
Turns into 变成
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = testDefault;
exports.testNamed = testNamed;
function testDefault() {}
function testNamed() {}
In this case, your exported object would have a default() and testNamed(). 在这种情况下,导出的对象将具有default()和testNamed()。
Update for webpack 2: In webpack 2 you can't mix ES6 imports and CommonJS exports. webpack 2的更新:在webpack 2中,您无法混合ES6导入和CommonJS导出。 If you use module.exports you should also use require, and if you use import you should use export paired with it.
如果你使用module.exports,你也应该使用require,如果你使用import,你应该使用与它配对的导出。
In webpack 2, you cannot use ES6 default exports to make a global available. 在webpack 2中,您无法使用ES6默认导出来使全局可用。 The most-used solution right now is to make a small entry point that simply does the following:
现在最常用的解决方案是创建一个小入口点,只需执行以下操作:
// assuming myLibraryEntry is the default export in the required file.
const myLibraryEntry = require('./real-entry-point-in-es6.js').default;
// This makes myLibraryEntry available as whatever you set libraryName to in your config.
module.exports = myLibraryEntry;
I am in the same situation as you do.Here is my code: 我和你的情况一样。这是我的代码:
webpack.config.js: webpack.config.js:
module.exports = {
entry: './src/method/eTrack/index.js',
output: {
filename: 'eTrack.js',
path: path.resolve(__dirname, 'dist'),
library: 'eTrack',
libraryTarget: 'umd'
},
};
./src/method/eTrack/index.js: ./src/method/eTrack/index.js:
import create from './create';
import getAll from './getAll';
import getByName from './getByName';
import remove from './remove';
export function eTrack () {
}
eTrack.trackers = [];
eTrack.create = create;
eTrack.getAll = getAll;
eTrack.getByName = getByName;
eTrack.remove = remove;
Well, after bundled via webpack, I can access eTrack
in window but it turn out to be an object.That means I can't call eTrack()
directly, but should call like eTrack.eTrack()
. 好吧,在通过webpack捆绑后,我可以在窗口中访问
eTrack
,但结果却是一个对象。这意味着我不能直接调用eTrack()
,但应该调用eTrack.eTrack()
。
And I've tried @Ambroos's solution, change ./src/method/eTrack/index.js into: 我尝试了@ Ambroos的解决方案,将./src/method/eTrack/index.js更改为:
module.exports = function eTrack () {
}
This time after bundled, I can't access eTrack
in browser window, the eTrack
object gone and it throws eTrack is undefined
error in the console. 这次捆绑后,我无法在浏览器窗口中访问
eTrack
, eTrack
对象消失了,它在控制台中抛出了eTrack is undefined
错误。
Then I found an article that helps a lot: http://siawyoung.com/coding/javascript/exporting-es6-modules-as-single-scripts-with-webpack.html 然后我找到了一篇很有帮助的文章: http : //siawyoung.com/coding/javascript/exporting-es6-modules-as-single-scripts-with-webpack.html
And change my index.js
into: 并将我的
index.js
更改为:
function eTrack () {
}
module.exports = eTrack;
Then everything works as expected!I can call eTrack()
directly in <script>
tag.Though I don't know the difference between @Ambroos's answer and this solution. 然后一切都按预期工作!我可以直接在
<script>
标签中调用eTrack()
。虽然我不知道@ Ambroos的答案和这个解决方案之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.