如何在不使用webpackJsonp的情况下生成js文件 [英] how to generate js file without webpackJsonp

查看:112
本文介绍了如何在不使用webpackJsonp的情况下生成js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望webpack处理js文件(最小化/uglify),但不将其格式化为模块-因此,这将是仅包含初始代码(最小化/uglified)而没有任何webpackJsonp的原始js文件.在将webpack加载到浏览器中之前,我需要这样的文件来加载它,以检测是否加载了自定义字体.如果我使用webpack模块来执行此操作,则在加载字体文件后即会加载我的捆绑软件.

I want webpack to process js file (minify/uglify) but not format it as a module - so it would be just raw js file containing only the initial code (minified/uglified) without any webpackJsonp. I need such a file to load it before webpack is loaded in a browser, to detect if custom font is loaded. If I use webpack module to do it then my bundle is loaded after font file is loaded.

推荐答案

设置您的目标到webpack.config.js中的 node (默认为 web )

Set your target to node in webpack.config.js (the default is web)

module.exports = {
  target: 'node'
};

在上面的示例中,使用节点webpack将编译以用于类似于Node.js的环境(使用Node.js要求加载块而不是加载触摸任何内置模块(例如fs或path).

In the example above, using node webpack will compile for usage in a Node.js-like environment (uses Node.js require to load chunks and not touch any built in modules like fs or path).

或者,如果这不适合您使用,您也可以只在输出(假设您使用的是CommonJS):

Alternatively, if this is not appropriate for your use, you can also just change the libraryTarget in the output (assuming you are using CommonJS):

output: {
    path: path.resolve(__dirname, 'build'),
    filename: '[name].js',
    libraryTarget: 'commonjs'
},

libraryTarget:"commonjs"-入口点的返回值将使用output.library值分配给导出对象.作为顾名思义,这是在CommonJS环境中使用的.

libraryTarget: "commonjs" - The return value of your entry point will be assigned to the exports object using the output.library value. As the name implies, this is used in CommonJS environments.

这篇关于如何在不使用webpackJsonp的情况下生成js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆