简体   繁体   English

如何使用 webpack 添加一个 js 文件?

[英]How to add a js file with webpack?

I was reading this webpack tutorial:我正在阅读这个 webpack 教程:

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

It says it bundles the src files and node_modules.它说它捆绑了 src 文件和 node_modules。 If I want to add another .js file there, how can I do this?如果我想在那里添加另一个 .js 文件,我该怎么做? This is a thirdpartyjs file that is not part of the source and not part of the node_modules files.这是一个第三方js文件,它不属于源文件,也不属于node_modules文件。 This is my current webpack.config.js:这是我当前的 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
}

The start point for code is the entry field in config. 代码的起点是config中的entry字段。 In your config entry point is the list of files. 在您的配置入口点是文件列表。 Webpack gets all, resolve their dependencies and output in one file. Webpack获取所有内容,解决其依赖关系并在一个文件中输出。

You have two options for adding third party script: 您有两种添加第三方脚本的选项:

  • add the file path to entry list before app.js 在app.js之前将文件路径添加到条目列表中
  • require this file from app.js 需要来自app.js的此文件

In response to Dmitry's answer: 回应德米特里的回答:

  • add the file path to entry list before app.js 在app.js之前将文件路径添加到条目列表中

This has the effect that you will get a bundled .js file for each entry point, which you might not want. 这样可以为每个入口点获取一个捆绑的.js文件,这可能是您不想要的。

  • require this file from app.js 需要来自app.js的此文件

You might not have access to app.js if it is written dynamically, or for whatever reason you might not want to edit app.js . 如果app.js是动态编写的,或者出于任何原因您可能不想编辑app.js ,则可能无法访问它。

Another option: 另外一个选项:

You can use webpack-inject-plugin to inject any JS code as string into the resulting .js bundle created by webpack. 您可以使用webpack-inject-plugin将任何JS代码作为字符串注入到由webpack创建的结果.js包中。 This way you can read the File you want to inject as a string (eg fs.readFile in nodejs) and inject it with the plugin. 这样,您就可以读取你要注入作为字符串的文件(如fs.readFile中的NodeJS),并与插件注入它。

Another solution but without using any extra plugins:另一个解决方案,但不使用任何额外的插件:

//Webpack.config.js
entry: {
  main: './src/index',
 /**
 /* object is passed to load script at global scope and exec immediately
 /* but if you don't need then simply do:
 /* myCustomScriptEntry: './src/myCustomScript'
 */
  myCustomScriptEntry: { 
    import: './src/myCustomScript',
    library: {
      name: 'myCustomScriptEntry',
      type: 'var',
    },
  },
},
new HtmlWebpackPlugin({
  template: './public/index.html',
  excludeChunks: ['myCustomScriptEntry'], //exclude it from being autoreferenced in script tag
  favicon: './public/favicon.svg',
  title: 'Alida',
}),

and

//index.html
<script type="text/javascript" src="<%= compilation.namedChunks.get('myCustomScriptEntry').files[0] %>"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM