如何在 4 个文件上禁用 AMD 并使用 webpack 按顺序加载它们

How to disable AMD on 4 files and load them in order with webpack

我需要在 4 个文件上禁用 AMD 并在加载其他 3 个文件之前先加载 video.js,因为它们依赖它。当我像这样在 webpack.config.js 中尝试这样做时:

const path = require('path')
const webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: __dirname + '/public',
    filename: 'bundle.js'
  },
  devServer: {
    inline: true,
    contentBase: './src',
    port: 3333
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    })
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules|lib/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react', 'stage-2'],
          plugins: ['transform-class-properties']
        }
      },
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      {
        test: /[\/\]lib[\/\](video|playlist|vpaid|overlay)\.js$/,
        exclude: /node_modules|src/
        loader: 'imports?define=>false'
      }
    ]
  }
}

它实际上不起作用,因为它只是加载 video.js(禁用 AMD)并完全忽略其他 3 个文件。

我的文件夹结构是这样的:

▾ lib/
    overlay.js
    playlist.js
    video.js
    vpaid.js
▸ node_modules/
▾ public/
    200.html
    bundle.js
▾ src/
    App.js
    index.html
    main.js
  LICENSE
  package.json
  README.md
  webpack.config.js

现在,我发现了一些让我退后一步的东西,因为现在甚至 video.js 都没有加载:

require('imports?define=>false!../lib/video.js')
require('imports?define=>false!../lib/playlist.js')
require('imports?define=>false!../lib/vpaid.js')
require('imports?define=>false!../lib/overlay.js')

而只是抛出这些类型的警告:

WARNING in ./~/imports-loader?define=>false!./lib/video.js
Critical dependencies:
15:415-422 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
 @ ./~/imports-loader?define=>false!./lib/video.js 15:415-422

WARNING in ./~/imports-loader?define=>false!./lib/playlist.js
Critical dependencies:
10:417-424 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
 @ ./~/imports-loader?define=>false!./lib/playlist.js 10:417-424

WARNING in ./~/imports-loader?define=>false!./lib/vpaid.js
Critical dependencies:
4:113-120 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
 @ ./~/imports-loader?define=>false!./lib/vpaid.js 4:113-120

WARNING in ./~/imports-loader?define=>false!./lib/overlay.js
Critical dependencies:
10:416-423 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
 @ ./~/imports-loader?define=>false!./lib/overlay.js 10:416-423

所以,我的问题是,我怎样才能在 webpack.config.js 中完成这项工作,这样我就不会收到这些警告?

问题我解决了!要完成这项工作,您需要:

 {
    test: /[\/\]lib[\/\](video|playlist|vpaid|overlay)\.js$/,
    exclude: /node_modules|src/
    loader: 'imports?define=>false'
  }

还有这个

require('script-loader!../lib/video.js')
require('script-loader!../lib/playlist.js')
require('script-loader!../lib/vpaid.js')
require('script-loader!../lib/overlay.js')

在一起!

现在,如果您使用这个(而不是脚本加载器):

require('imports?define=>false!../lib/video.js')
require('imports?define=>false!../lib/playlist.js')
require('imports?define=>false!../lib/vpaid.js')
require('imports?define=>false!../lib/overlay.js')

这是行不通的! (你需要 imports-loader 和 script-loader 一起工作。