如何在 webpack 中包含外部文件 [英] How to include external file with webpack

查看:44
本文介绍了如何在 webpack 中包含外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 webpack 中包含外部文件(在上下文之外)并使该文件包含在构建的输出 bundle.js 中?

考虑这个设置,其中子应用程序"是 webpack 的上下文:

consider this setup where "sub-app" is context for webpack:

  • /sub-app/entry.js
  • /bower-components/zepto/zepto.js

以及带有花椰菜的 webpack 配置:

And webpack config with broccoli:

var webpackify = require('broccoli-webpack');
var path = require('path');
var webpack = require("webpack");

var bundler = webpackify(path.resolve('sub-app'), {
    entry: './entry',
    output: {filename: './bundle.js'},
    devtool: 'eval',

    module: {
      loaders: [
        {test: /.js$/, loader: 'babel-loader'},
        {test: /.hbs$/, loader: "handlebars-loader"}
      ]
    },
    plugins: [
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin()
    ]
});

我想在输出 bundle.js 中包含 zepto.js.但是我需要在子应用之外保留 bower_components.

推荐答案

好的,我自己找到了答案.无需特殊调整.仅在具有相对路径的代码中包含外部文件:

Ok found answer myself. No special adjustments are necessary. Only include external file in code with relative path:

就我而言:

import zepto from './../bower_components/zepto/zepto.js';

这篇关于如何在 webpack 中包含外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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