Vuejs / Webpack从bundle中排除json文件

时间:2017-02-21 09:48:04

标签: javascript node.js webpack vue.js

我的项目根目录中有一个no-sql数据库,我不希望webpack将它包含在它的包中。现在每次db更新时我都要重建。我正在使用webpack 2.2.1。这就是我所拥有的:

var path = require('path')
var utils = require('./utils')
var config = require('../config')
var vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

module.exports = {
  entry: {
    app: './resources/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    modules: [
      resolve('resources'),
      resolve('node_modules')
    ],
    alias: {
      'vue$': 'vue/dist/vue.common.js',
      'src': resolve('resources'),
      'assets': resolve('resources/assets'),
      'components': resolve('resources/components')
    }
  },
  module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        enforce: "pre",
        include: [resolve('resources'), resolve('test')],
        options: {
          formatter: require('eslint-friendly-formatter')
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('resources'), resolve('test')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  }
}

我试图通过将其添加到外部,如

来排除它
externals: ['./../db.json'],

或者像这样添加规则

{
  test: /\.json$/,
  exclude: resolve('db.json')
}

1 个答案:

答案 0 :(得分:-1)

只要您require() / import它,您将始终需要重新构建,因为webpack会将其转换为新文件,作为webpack模块。

您真正要做的是在应用程序启动后通过AJAX从浏览器加载它,并使用您选择的AJAX工具。