Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not use pre-existing source map #27

Open
youngderekm opened this issue Dec 1, 2021 · 3 comments
Open

does not use pre-existing source map #27

youngderekm opened this issue Dec 1, 2021 · 3 comments

Comments

@youngderekm
Copy link

I am trying to use gulp-terser to run terser against js files that were compiled from TypeScript. The TypeScript compiler generated sourcemaps when it ran, and in this source map, "sources" lists my .ts file, as I would expect. When gulp-terser runs, however, it's as if the source map is generated from scratch. "sources" ends up listing the .js file instead of the original .ts file as expected.

Looking at the code, I don't see terser's sourceMap.content being set from chunk.sourceMap. Only sourceMap.filename is set. The terser doc says "The value of filename is only used to set file attribute (see the spec) in source map file.... If you're compressing compiled JavaScript and have a source map for it, you can use sourceMap.content".

Does this plugin support source maps that were previously loaded?

@duan602728596
Copy link
Owner

How did you configure it? I just tested it, and the source map it generates restores the original ts code in the browser.

@jeteon
Copy link

jeteon commented Feb 11, 2022

@duan602728596 Did you compile TS to JS then through Terser using Gulp? Would you mind sharing a sample Gulpfile that does this if you did?

From looking at the code, it really doesn't seem like the sourceMap.content property is supported at all. I kind of understand since it would need to also provide you some way to specify the input sourcemap that diverges from the Terser API. I expect it would need a function along the lines of:

function es(){
  return gulp.src('./src/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(terser({
      sourceMap: {
        content: function(inputFilePath) {
          return inputFilePath + '.orig.map';
        }
      }
    }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));
}

This is something like what gulp-sourcemaps does. I think it needs to be a function because you can't have a static input source map like if you were using it directly.

@duan602728596
Copy link
Owner

@duan602728596 Did you compile TS to JS then through Terser using Gulp? Would you mind sharing a sample Gulpfile that does this if you did?

From looking at the code, it really doesn't seem like the sourceMap.content property is supported at all. I kind of understand since it would need to also provide you some way to specify the input sourcemap that diverges from the Terser API. I expect it would need a function along the lines of:

function es(){
  return gulp.src('./src/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(terser({
      sourceMap: {
        content: function(inputFilePath) {
          return inputFilePath + '.orig.map';
        }
      }
    }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));
}

This is something like what gulp-sourcemaps does. I think it needs to be a function because you can't have a static input source map like if you were using it directly.

Gulp's sourcemaps use vinyl-sourcemaps-apply. If you want to add sourcemaps, sourcemaps must have file attribute.
So the sourceMap option of terser must configure filename.
I can set terser's options as a function to suit your needs.

function es(){
  return gulp.src('./src/**/*.js')
    .pipe(sourcemaps.init())
    .pipe(terser(async function(opt) {
        return {
          sourceMap: {
            content: ''
          }
        }
    }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));
}

But I still recommend that if you want to modify sourcemaps, you can use gulp-sourcemaps to modify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants