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

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

问题描述

我希望 webpack 处理 js 文件 (minify/uglify) 但不将其格式化为模块 - 所以它只是包含初始代码 (minified/uglified) 没有任何 webpackJsonp 的原始 js 文件.我需要这样一个文件来在浏览器中加载 webpack 之前加载它,以检测是否加载了自定义字体.如果我使用 webpack 模块来做,那么我的包会在加载字体文件后加载.

解决方案

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

module.exports = {目标:'节点'};

<块引用>

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

或者,如果这不适合您的使用,您也可以更改 output(假设您使用的是 CommonJS):

输出:{路径:path.resolve(__dirname, 'build'),文件名:'[名称].js',图书馆目标:'commonjs'},

<块引用>

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

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.

解决方案

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

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

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).

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" - 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天全站免登陆