kmck / lodash-template-webpack-loader

Webpack loader for Lodash templates
MIT License
5 stars 7 forks source link

Can't pipeline to babel-loader 8 #6

Open kiku1monji opened 4 years ago

kiku1monji commented 4 years ago

Hi. First of all, sorry about my terrible English. ( almost all Japanese is. )

We are trying to transpile emdebedded javascript code from lodash/template by babel 8.

// webpack.config.js (webpack 4)
module.exports = {
  devtool: 'eval-source-map'
  module: {
    rules: [
        test: /\.tpl$/,
        use: [
          'babel-loader',
          'lodash-template-webpack-loader'
        ]
      },

above setting cause bable-loader Error.

Error: .inputSourceMap must be a boolean, object, or undefined

because of babel-loader does not accept SourceMap as 'string' type.

// code inside babel (sorry no link. can't Find in GitHub)
function assertInputSourceMap(loc, value) {
  if (value !== undefined && typeof value !== "boolean" && (typeof value !== "object" || !value)) {
    throw new Error(`${msg(loc)} must be a boolean, object, or undefined`);
  }

  return value;
}

I tried patch to lodash-template-webpack-loader (local). https://github.com/kmck/lodash-template-webpack-loader/blob/48c67eddd2f2a159258a6f7fee75219ad33670f5/index.js#L68

// index.js
    // Source map
    if (!map && (this.sourceMap || params.sourceMap || options.sourceMap)) {
        var sourceMapGenerator = new SourceMapGenerator({
            file: this.resourcePath,
        });
        sourceMapGenerator.setSourceContent(this.resourcePath, content);
        sourceMapGenerator.addMapping({
            source: this.resourcePath,
            original: {line: 1, column: 0},
            generated: {line: 1, column: 0},
        });
        // map = sourceMapGenerator.toString();  // -> OUT
        map = sourceMapGenerator.toJSON();  // <- IN        
    }

and works fine.

below works too, result is different however.

// webpack.config.js
    rules: [
        test: /\.tpl$/,
        use: [
          'babel-loader',
          'test-loader',
          'lodash-template-webpack-loader'
        ]
      },

// test-loader.js
module.exports = function(content, map, meta) {
  // this.callback(null, content, JSON.parse(map), meta); // -> cause parse Error
  this.callback(null, content, null, meta);
};

We hope that this module to be available switching sourceMap type to Json object.

kiku1monji commented 4 years ago

@kmck @duhaime I appreciate SO MUCH if you considered this. could you pls some comment ?