godaddy-wordpress / godaddy-wordpress/sake

Built .min.js files in plugins retain //# sourceMappingURL comments despite .map files being excluded

Open
#130 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Howdy! 👋

With https://github.com/godaddy-wordpress/sake/pull/100 ensuring .map files not included in builds I found out there might be a lingering issue around.

When running npx sake build or npx sake zip, the compiled .min.js files in the build/ output contain `//# sourceMappingURL=.map` comments, but the corresponding .map files are correctly excluded by the copy:build task. This causes some browser console warnings like "DevTools failed to load source map" on merchant sites.

**Expected behavior:**

The `//# sourceMappingURL` comments should be stripped from the compiled output (since .map files are excluded from the build)

The code in pipes/scripts.js and tasks/compile.js appears to intend skipping sourcemap generation during builds:

```
// tasks/compile.js — compileJsTask
gulp.src(sake.config.paths.assetPaths.javascriptSources)
.pipe(gulpif(! sake.isBuildTask(), sourcemaps.init())) // skipped during build
.pipe(scriptPipes().compileJs())
.pipe(gulp.dest(sake.config.paths.assetPaths.js))

// pipes/scripts.js — compileJs lazypipe
lazypipe()
.pipe(babel, { presets: [...] })
.pipe(() => gulpif(sake.options.minify, uglify()))
.pipe(rename, { suffix: '.min' })
.pipe(() => gulpif(! sake.isBuildTask(), sourcemaps.mapSources(...))) // skipped during build
.pipe(() => gulpif(! sake.isBuildTask(), sourcemaps.write('.', ...))) // skipped during build
```

While sourcemaps.init() and sourcemaps.write() are both correctly skipped when isBuildTask() returns true, the output .min.js files still contain the //# sourceMappingURL trailing comment. Meanwhile, tasks/copy.js excludes .map files from the build directory, leaving orphaned references.

(The same issue may apply to CSS — tasks/compile.js has the same conditional pattern for compileScssTask, and the resulting .min.css files may contain /*# sourceMappingURL=...*/ comments pointing to excluded .map files. )

**Reproduction:**

1. Run npx sake build on any plugin with JS assets
2. Check the last line of any .min.js file in the build/ output:
tail -1 build//assets/js/frontend/*.min.js
3. Observe the //# sourceMappingURL=.min.js.map comment
4. Confirm no .map files exist in the build output

**Suggested fix:**

* Option A: Add a gulp-replace or similar pipe step in copy:build to strip sourcemap comments from .min.js and .min.css files when copying to the build directory. The gulpFilter for ['**/*.min.css', '**/*.min.js'] already exists in `copyBuildTask` but is currently a no-op — it could be used for this:

```
const filter = gulpFilter(['**/*.min.css', '**/*.min.js'], { restore: true })

return gulp.src(paths, { base: sake.config.paths.src, allowEmpty: true, encoding: false })
.pipe(filter)
.pipe(replace(/\/\/# sourceMappingURL=.*$/gm, ''))
.pipe(replace(/\/\*# sourceMappingURL=.*?\*\//g, ''))
.pipe(filter.restore)
.pipe(gulp.dest(`${sake.config.paths.build}/${sake.config.plugin.id}`))
```

* Option B: Investigate why sourcemaps.init() being skipped doesn't prevent the comment from appearing in the output — there may be a deeper issue in the gulp-sourcemaps / gulp-uglify interaction.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.