Skip to content

Commit

Permalink
Merge pull request #3306 from justingrant/prod-sourcemaps-2
Browse files Browse the repository at this point in the history
Enable sourcemaps in prod builds (for easier debugging of apps using Swiper)
  • Loading branch information
nolimits4web authored Oct 26, 2019
2 parents f964bef + cd6f30d commit 9ebaa89
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 4 additions & 4 deletions scripts/build-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function es(components, cb) {
format: 'es',
name: 'Swiper',
strict: true,
sourcemap: env === 'development',
sourcemap: true,
sourcemapFile: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.bundle.js.map`,
banner,
file: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.bundle.js`,
Expand Down Expand Up @@ -60,7 +60,7 @@ function es(components, cb) {
format: 'es',
name: 'Swiper',
strict: true,
sourcemap: env === 'development',
sourcemap: true,
sourcemapFile: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.browser.bundle.js.map`,
banner,
file: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.browser.bundle.js`,
Expand Down Expand Up @@ -90,7 +90,7 @@ function es(components, cb) {
name: 'Swiper',
strict: true,
banner,
sourcemap: env === 'development',
sourcemap: true,
sourcemapFile: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.js.map`,
file: `./${env === 'development' ? 'build' : 'package'}/js/swiper.esm.js`,
})).then(() => {
Expand Down Expand Up @@ -120,7 +120,7 @@ function umd(components, cb) {
format: 'umd',
name: 'Swiper',
strict: true,
sourcemap: env === 'development',
sourcemap: true,
sourcemapFile: `./${env === 'development' ? 'build' : 'package'}/js/swiper.js.map`,
banner,
file: `./${env === 'development' ? 'build' : 'package'}/js/swiper.js`,
Expand Down
32 changes: 31 additions & 1 deletion scripts/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,37 @@ gulp.task('styles', (cb) => {
buildStyles(cb);
});

gulp.task('build', gulp.series(['js', 'styles']));

// in prod builds, copy /src folder into /package
gulp.task('prod-source-copy', (cb) => {
const env = process.env.NODE_ENV || 'development';
if (env === 'production') {
gulp.src(['./src/**/*']).pipe(gulp.dest('./package/src/'));
}
if (cb) cb();
});

// in prod builds, adjust sourcemap paths to actual src location
gulp.task('prod-source-sourcemap-fix-paths', (cb) => {
const env = process.env.NODE_ENV || 'development';
if (env === 'production') {
const jsDir = path.resolve(__dirname, '../package/js/');
const mapFiles = fs
.readdirSync(jsDir)
.filter((file) => file.toLowerCase().endsWith('.map'));
mapFiles.forEach((mapFile) => {
const mapFilePath = path.resolve(jsDir, mapFile);
let content = fs.readFileSync(mapFilePath, 'utf8');
content = content
.replace(/"\.\.\/\.\.\//g, '"../')
.replace(/"\.\.\/node_modules\//g, '"~/');
fs.writeFileSync(mapFilePath, content);
});
}
if (cb) cb();
});

gulp.task('build', gulp.series(['js', 'styles', 'prod-source-copy', 'prod-source-sourcemap-fix-paths']));

gulp.task('watch', () => {
gulp.watch('./src/**/**/*.js', gulp.series('js'));
Expand Down

0 comments on commit 9ebaa89

Please sign in to comment.