[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.

时间: 2024-08-09 10:34:35

[Webpack 2] Import a non-ES6 module with Webpack的相关文章

[Webpack + React] Import CSS Modules with TypeScript and webpack

If you try to use CSS Modules in TypeScript the same way you would use them in JavaScript, with webpack’s css-loader, you’ll see an error: ”TS2307: Cannot find module './styles.css'”. typings-for-css-modules-loader is a drop-in replacement for css-lo

es6 module export 和 import写法注意点

在使用es6 module时,注意以下几点: 1.当一个module文件中需要多个export时,使用import时,可以使用import {a,b,c ...} from '...'这种形式,a.b.c这些参数是module中export的, 也可以使用import * as xx from '..'这种形式,这样就是xx作为一个总是对象,export出来的参数作为这个总对象的属性存在 2.当一个module文件中使用export default形式暴露参数时,默认只能暴露一个参数,一个mod

编译Twitter的Heron时一直报错“heron/bazel_configure.py", line 25, in <module> import semver ImportError: No module named semver”如何处理。

今天编译heron的时候,从官方git到的源码bazel_configure的时候一直报错如下: Traceback (most recent call last): File "/comexData/package/heron/bazel_configure.py", line 25, in <module> import semver ImportError: No module named semver 查看了相关目录确实存在 实在没搞明白.就去看源码分析.发现,其对

from setuptools import setup ImportError: No module named setuptools【转】

转自:http://www.cnblogs.com/chinacloud/archive/2010/12/24/1915644.html from setuptools import setupImportError: No module named setuptools 解决方法: wget http://peak.telecommunity.com/dist/ez_setup.pypython ez_setup.py

[Webpack 2] Use Karma for Unit Testing with Webpack

When writing tests run by Karma for an application that’s bundled with webpack, it’s easiest to integrate webpack and Karma directly together. In this lesson we’ll see how to utilize the karma-webpack plugin and reuse our existing webpack configurati

vue-cli的webpack模版,相关配置文件dev-server.js与webpack.config.js配置解析

1.下载vue-cli [html] view plain copy npm install vue-cli -g vue-cli的使用与详细介绍,可以到github上获取https://github.com/vuejs/vue-cli 2.安装webpack项目模版 [html] view plain copy vue init <template-name> <project-name> 比如: [html] view plain copy vue init webpack m

webpack入坑之旅(三)webpack.config入门

这是一系列文章,此系列所有的练习都存在了我的github仓库中vue-webpack,在本人有了新的理解与认识之后,会对文章有不定时的更正与更新.下面是目前完成的列表: webpack入坑之旅(一)不是开始的开始 webpack入坑之旅(二)loader入门 webpack入坑之旅(三)webpack.config入门 webpack入坑之旅(四)扬帆起航 webpack入坑之旅(五)加载vue单文件组件 webpack入坑之旅(六)配合vue-router实现SPA 在上面我们已经尝试过了两种

webpack 打包报错:One CLI for webpack must be installed. These are recommended choices, delivered as separate packages

webpack 打包报错: One CLI for webpack must be installed. These are recommended choices, delivered as separate packages: 解决办法: 全局.局部安装个遍!具体什么原因还不清楚... 先全局安装webpack和webpack-clinpm install webpack -gnpm install webpack-cli -g再局部安装webpack和webpack-clinpm inst

webpack : 无法加载文件 D:\nodejs\node_global\webpack.ps1,因为在此系统上禁止运行脚本。

通过vs code 运行webpack进行打包时,报错webpack : 无法加载文件 D:\nodejs\node_global\webpack.ps1,因为在此系统上禁止运行脚本. 解决方案: 以管理员身份运行vs code 执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的 执行:set-ExecutionPolicy RemoteSigned 这时再执行get-ExecutionPolicy,就显示RemoteSigned 此时发现再进行打包就没有问