I want webpack to load the library as a non-commonjs environment

Asked 1 years ago, Updated 1 years ago, 18 views

Some packages use require functions that are incompatible with standard require.js and commonjs, so webpack may not work properly.

I recognize Raphall.js and getsentry/raven-js.

In order to solve this problem, rather than patch the code, there are cases where you want to read it as a non-commonjs environment that is file-connected with an external requirement but not wrapped with a require polyfil, just like when you read it with a normal script tag.Does anyone know how to do this?

I know it's good to use raw-loader to eval JS as text, but I think it's too dirty.

webpack/raw-loader

javascript

2022-09-30 14:34

1 Answers

It may not change if it is dirty, but how about the following?

I've only tried it on Raphael, so maybe I need to adjust it for each library.

If you have three files in the same directory: index.js,raphael.js,webpack.config.js

index.js

require('raphael');

var paper = Raphael (10,50, 320, 200);
var circle = paper.circle(50,40,10);
circuit.attr("fill", "#f00";
circuit.attr("stroke", "#ffff";

webpack.config.js

varfs=require('fs');

module.exports={
  externals: [
    function(context, request, callback){
      if(/raphael/.test(request)){
        varsrc=fs.readFileSync(request+'.js');
        return callback(null, "(function(module){"+src+"}.bind(window)())"));
      }
      return callback();
    }
  ]
};


2022-09-30 14:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.