includeFilesLoader.js 651 Bytes
Newer Older
YazhouChen's avatar
YazhouChen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// @flow
import loaderUtils from 'loader-utils';

// Note: no export default here cause of Babel 6
module.exports = function includeFilesLoader(sourceCode: string) {
  if (this.cacheable) {
    this.cacheable();
  }
  const loaderOptions = loaderUtils.getOptions(this);

  if (loaderOptions.include && loaderOptions.include.length) {
    const includes = loaderOptions.include
      .map((modPath) => `require(${loaderUtils.stringifyRequest(this, modPath)});`)
      .join('\n');

    const code = [
      includes,
      sourceCode,
    ].join('\n');

    this.callback(null, code, null);
    return;
  }

  this.callback(null, sourceCode, null);
};