根据文件扩展名覆盖webpack中捆绑的内容

时间:2018-09-28 19:27:25

标签: javascript webpack

我试图弄清楚webpack是否可以做这样的事情。我有一些代码想绑定到特定设备上。所以我创建了一个ViewFactories.ios.tsx,我也有ViewFactories.tsx。我遇到的问题是,如果我在加载程序测试中忽略.ios.tsx,它仍然会被捆绑。我正在使用ignore-bundler插件忽略.ios.tsx文件,但是引用只是空的。即:

/** still getting loaded here: **/
const ViewFactories_ios_1 = __importDefault(__webpack_require__(/*! ./ViewFactories.ios */ "./build/app/src/modules/layouts/factories/ViewFactories.ios.tsx"));

/*** the referenced section, but blank now ***/
/***/ "./build/app/src/modules/layouts/factories/ViewFactories.ios.tsx":
/*!***********************************************************************!*\
  !*** ./build/app/src/modules/layouts/factories/ViewFactories.ios.tsx ***!
  \***********************************************************************/
/*! dynamic exports provided */
/*! all exports used */
/***/ (function(module, exports) {
/***/ }),

我真正想要的是对ViewFactories.tsx的引用,而不是对ViewFactories.ios.tsx的引用。 我可以访问webpack中的某种依赖关系图,以告诉加载程序使用默认值而不是.ios.tsx吗?

我的webpack配置:

   {
      test: (modulePath) => {
          if (/\.ios\./.test(modulePath)) {
              console.log(modulePath);
              return false;
          }
          return /\.tsx?$/.test(modulePath);
      },
      loader: "awesome-typescript-loader",
      options: {
          configFileName: 'tsconfig.web.json',
          transpileOnly: true,
          errorsAsWarnings: true,
      }
    },
    {
        test: (modulePath) => {
            if (/\.ios\./.test(modulePath)) {
                console.log('Ignored:  ', modulePath);
                return true;
            }
            return false;
        },
        loader: 'ignore-loader'
    },

1 个答案:

答案 0 :(得分:0)

只需构建两个捆绑包,使用相同的共享配置选项?

// webpack.config.js for two different bundles

let sharedModuleDef = {
  rules: ...
}

let sharedPlugins = ...

let iosBundle = {
  entry: "src/ios.entry.js",
  output: {
    path: ...
    filename: "ios.bundle.js"
  },
  module: sharedModuleDef,
  ...
};

let everythingElse = {
  entry: "src/main.entry.js",
  output: {
    path: ...
    filename: "standard.bundle.js"
  },
  module: sharedModuleDef,
  ...
};

module.exports = [iosBundle, everythingElse];