Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

javascript - how to generate js file without webpackJsonp

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...