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

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

问题描述

我正在阅读这个 webpack 教程:

I was reading this webpack tutorial:

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

它说它捆绑了 src 文件和 node_modules.如果我想在那里添加另一个 .js 文件,我该怎么做?这是一个第三方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天全站免登陆