如何使用webpack添加js文件? [英] How to add a js file with webpack?

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

问题描述

我正在阅读此webpack教程:

I was reading this webpack tutorial:

https://webpack.github.io/docs/usage.html

它说它捆绑了src文件和node_modules.如果要在其中添加另一个.js文件,该怎么办?这是第三方脚本文件,它不是源文件,也不是node_modules文件的一部分.这是我当前的webpack.config.js:

It says it bundles the src files and node_modules. If I want to add another .js file there, how can I do this? This is a thirdpartyjs file that is not part of the source and not part of the node_modules files. This is my current webpack.config.js:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://localhost:8080',
        'webpack/hot/only-dev-server',
        './app/app.js'
    ],
    output: {
        path: path.resolve(__dirname, "dist"),
        publicPath: "/dist/",
        filename: "dist.js",
        sourceMapFilename: "dist.map"
    },
    devtool: 'source-map',
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('development')
            }
        }),
    ],
    module: {
        loaders: [{
            loader: 'babel',
            exclude: /node_modules/
        }]
    },
    devServer: {
        inline: true
    },
    node: {
        fs: "empty"
    },
    watch: false
}

推荐答案

代码的起点是config中的 entry 字段.在您的配置入口点是文件列表.Webpack可以获取所有内容,解决它们的依赖关系并在一个文件中输出.

The start point for code is the entry field in config. In your config entry point is the list of files. Webpack gets all, resolve their dependencies and output in one file.

您有两个添加第三方脚本的选项:

You have two options for adding third party script:

  • 将文件路径添加到app.js之前的条目列表中
  • 从app.js中获取此文件

这篇关于如何使用webpack添加js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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