Webpack - provides multi-user fields

I need to create a package that has es6 top polygons on it, so that type methods Object.assign, Array.prototype.filteretc. supported core-jsif they are not initially defined.

I created a simple sample program that isolates this problem that I have, and it is clicked here: https://gist.github.com/tiberiucorbu/fb9acd2f286e3452ef06df9d6bae9210

Now it is webpackvery confusing when it comes to polyfills, I tried different approaches, but nothing works as I imagined:

Using a vendor plugin

Inspiration: https://webpack.imtqy.com/docs/shimming-modules.html

Try :

webpack.config.js

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

Result :

. . . :

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 - entry config:

:

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

: core-js, . .

, , , (webpack, karma, typescript) ?


: (- )

aproach, : polyfills, leting webpack/typescript script . , .

:

@@ -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();

a >

:

@@ -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

, /, .

+4
1

, webpack ES6 babel:

webpack.config.js

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

Babel:

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

babel, ES6 :

import $ from 'jquery'

const cool = w => 42

+1

Source: https://habr.com/ru/post/1667807/


All Articles