[Webpack 2] Import a non-ES6 module with Webpack

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Webpack 2] Import a non-ES6 module with Webpack相关的知识,希望对你有一定的参考价值。

When you have a dependency that does not export itself properly, you can use the exports-loader to force it to export the pieces of the file that you need.

 

Install:

npm i -D exports-loader

 

Add exports-loader to the module you want:

 module: {
      loaders: [
       ...
        {
          test: require.resolve(./src/js/non_node_modules/left-pad),
          loaders: [
            exports?leftPad,
          ],
        }
      ],

 

There is no problem, the module still exists on ‘window‘ object, we want it be es6 module which not exists on ‘window‘ object.

Install:

npm i -D imports-loader

Add imports-loader the the module:

    module: {
      loaders: [
        ...
        {
          test: require.resolve(./src/js/non_node_modules/left-pad),
          loaders: [
            imports?window=>{},
            exports?leftPad,
          ],
        }
      ],
    },

Here it sets ‘windows‘ object to empty object to clean the left-pad module.

 

以上是关于[Webpack 2] Import a non-ES6 module with Webpack的主要内容,如果未能解决你的问题,请参考以下文章

python相对包导入报“Attempted relative import in non-package”错误

Webpack 2 + React - 使用 System.import 进行代码拆分时的嵌套路由

在 React 项目中使用 System.import 进行 Tree Shaking 和延迟加载的 Webpack 2 配置

SyntaxError: Unexpected token import Webpack 2 Babel 6 Reactjs

vite 动态 import 引入打包报错解决方案

webpack 打包如何 ,按需导入,如echart那样?