typescript - Webpack - 提供 core-js polyfills

标签 typescript webpack karma-runner polyfills

我需要创建一个 bundle,上面有 es6 polyfill,以便像 Object.assign 这样的方法, Array.prototype.filter等等由 core-js 支持以防它们不是本地定义的。

我创建了一个简单的示例应用程序来隔离我遇到的这个问题,并将其推送到此处的要点:https://gist.github.com/tiberiucorbu/fb9acd2f286e3452ef06df9d6bae9210

现在webpack当涉及到 polyfills 时非常困惑,我尝试了不同的方法,但似乎没有任何效果像我想象的那样:

使用提供者插件

灵感:https://webpack.github.io/docs/shimming-modules.html

尝试:

webpack.config.js

    new webpack.ProvidePlugin({
        '': 'imports?this=>window!core-js/index.js'
    })

结果:

没有什么不同。 就好像我什么都没改变一样。没有它, bundle 是一样的。我的小测试失败了:

PhantomJS 2.1.1 (Windows 7 0.0.0) Application extend should use Object.assign FAILED
        Failed: undefined is not a constructor (evaluating 'Object.assign(a, b)')
        extend@index.spec.ts:81:30
        index.spec.ts:59:44
        loaded@http://localhost:9876/context.js:151:17

定义core-js文件到 webpacks entry配置:

尝试:

entry: {
    'app': [
        'core-js',
        './index.ts'
    ]
},

结果: 该包包含 core-js 文件,但它们未运行。规范继续失败。

那么我怎样才能确保 bundle 包含 polyfills 并且它们在这个(webpack、karma、typescript)星座中正确运行?


更新:(不知何故)工作解决方案

我尝试了另一种使用另一种思维方式的方法:导入 polyfill 并执行 polyfill,同时让 webpack/typescript 处理导入,让我的脚本处理执行。 感觉有点hackish但它有效。

与初始发布版本相比的变化:

@@ -3,8 +3,10 @@ var webpack = require('webpack');
 module.exports = {
     entry: {
         'app': [
-            'core-js',
             './index.ts'
+        ],
+        'polyfills': [
+            'core-js'
         ]
     },
     output: {
@@ -30,4 +32,4 @@ module.exports = {
         //   '': 'imports?this=>window!core-js/index.js'
         // })
     ]
-};
\ No newline at end of file
+};

<罢工> 创建了一个将 core-js 导入为 name 的新文件并执行 name :

<罢工>
import * as polyfills from 'core-js';

polyfills();

<罢工>

然后在 karma 配置中导入包和测试文件:

@@ -4,6 +4,7 @@ module.exports = function (config) {
     config.set({
         frameworks: ['jasmine'],
         files: [
+            './dist/polyfills.js',
             './**/*.spec.ts'

         ],
@@ -26,4 +27,4 @@ module.exports = function (config) {
         concurrency: Infinity,
         plugins: ['karma-phantomjs-launcher', 'karma-sourcemap-loader', 'karma-webpack', 'karma-jasmine']
     })
-};
\ No newline at end of file
+};

我的小测试成功了:

26 01 2017 01:30:40.594:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
26 01 2017 01:30:40.596:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
26 01 2017 01:30:40.613:INFO [launcher]: Starting browser PhantomJS
26 01 2017 01:30:41.081:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#c9shUPdbgFg0XwJbAAAA with id 77560741
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 SUCCESS (0.04 secs / 0.002 secs)

Gist 现在处于工作状态,并进行了上述更改:

https://gist.github.com/tiberiucorbu/fb9acd2f286e3452ef06df9d6bae9210

如果您知道没有中间文件的更优雅的解决方案,那将是一件很好的事情,但此时我可以按原样使用它。

最佳答案

你可以使用不同的方法,让 webpack 使用 babel 确保 ES6 兼容性:

webpack.config.js

module.exports = {
    ...
    module: { 
       loaders: [
          { 
              test: /\.jsx?$/, 
              loader: 'babel-loader', 
              exclude: /node_modules/, 
              query: { 
                 presets: ['es2015'] 
              }
          }
       ]
    }
} 

安装babel loader:

npm install babel-loader babel-core babel-preset-es2015 webpack --save-dev

使用 babel,您可以充分利用所有 ES6:

import $ from 'jquery'

const cool = w => 42

而且不需要 polyfill

关于typescript - Webpack - 提供 core-js polyfills,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860304/

相关文章:

karma-runner - 添加代码覆盖率后,Karma 命令行不显示测试结果

javascript - Typescript - 使用 Frame.js 同步 ajax 调用(解决方案 : JQueryDeferred)

android - Ionic 2 - 如何在 Android 上使用后退按钮退出应用程序?

javascript - 如何使用 CommonJS 模块将 webpack 和 ES6 与依赖项一起使用?

javascript - .babelrc 配置放在 package.JSON

Angular:测试返回可观察值的守卫

reactjs - Karma 和 React,有导致错误的警告

angular - typescript : Can't set default parameter value as false

javascript - 将 SweetAlert2 与 TypeScript 一起使用,找不到模块 'sweetalert2/dist/sweetalert2.js' 的声明文件

javascript - 从 Webpack 1 迁移到 2 错误 - Angular (4.1.3) 应用程序