【问题标题】:Override what's bundled in webpack based on file extension根据文件扩展名覆盖 webpack 中捆绑的内容
【发布时间】:2018-09-28 19:27:24
【问题描述】:

我试图弄清楚 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'
    },

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

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

    // 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];
    

    【讨论】:

    • 问题是主入口点是共享的,并且在依赖关系图中的某个地方,在 ios 文件和默认文件之间有一个分支。所以我不认为仅仅有一个单独的入口点就可以(除非我弄错了)。
    • 您可以使用 github.com/nippur72/ifdef-loader 之类的东西来实际删除文件本身,但通常大小差异完全没有必要,因为您可能会缩小代码,服务器设置为用于传输的 gzip 纯文本内容(......它设置为,对吗?)
    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多