-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Customize sourcemap sources paths #2168
Comments
Certainly we can look at a configuration option for this. How does it look in Webpack in this scenario? |
With browserify you can use a plugin https://github.com/thlorenz/mold-source-map. It is really easy to configure. |
With // src/AppBundle/gulpfile.babel.js
import concat from 'gulp-concat';
import gulp from 'gulp';
import gulpif from 'gulp-if';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'uglify';
import upath from 'upath';
const env = process.env.GULP_ENV;
const adminRootPath = '../../web/assets/app/admin';
const paths = {
admin: {
js: [
'Resources/private/admin/js/**',
],
},
};
const sourcePathMap = [
{
sourceDir: 'Resources/private/',
destPath: '/AppBundle',
},
];
const mapSourcePath = function mapSourcePath(sourcePath /* , file */) {
const match = sourcePathMap.find(({ sourceDir }) => (
sourcePath.substring(0, sourceDir.length) === sourceDir
));
if (!match) {
return sourcePath;
}
const { sourceDir, destPath } = match;
return upath.joinSafe(destPath, sourcePath.substring(sourceDir.length));
};
export const buildAdminJs = function buildAdminJs() {
return gulp.src(paths.admin.js, { base: './' })
.pipe(gulpif(env !== 'prod', sourcemaps.init()))
.pipe(concat('app.js'))
.pipe(gulpif(env === 'prod', uglify()))
.pipe(gulpif(env !== 'prod', sourcemaps.mapSources(mapSourcePath)))
.pipe(gulpif(env !== 'prod', sourcemaps.write('./')))
.pipe(gulp.dest(upath.joinSafe(adminRootPath, 'js')));
};
buildAdminJs.description = 'Build admin js assets.'; |
@teohhanhui would you like to propose a solution here you think might be appropriate for Rollup? |
Offtopic, but I've just realized you're the creator of SystemJS, jspm and Rollup. That's incredible! p/s: I used jspm a couple years ago. |
@teohhanhui I thought I recognized your name there :) I'm not the creator of Rollup though - that is @Rich-Harris, although I am a core contributor currently. |
@guybedford, I would think that an API allowing to configure the root of the sourcemap would be well fitted. export default {
input: 'src/main.js',
output: {
file: 'myLib.min.js',
format: 'cjs',
sourcemap: true,
sourcemapRoot: 'foo'
}
} I would expect the sourcemaps to declare files relative to
By default the sourcemap root would be |
I think a |
@teohhanhui good point. I guess we don't need export default {
input: 'src/main.js',
output: {
file: 'dist/myLib.min.js',
format: 'cjs',
sourcemap: true,
sourcemapPathTranform: (file) => {
return `foo/${file}`
}
}
} |
I don't think that works. Because we need to have a fixed base / root path to be able to perform the transform. See my (incomplete) code example above. |
Uhh... I see you had something else in mind with the |
I've updated the above example to show how the whole thing works for us now with |
So, if I were to imagine the API for // src/AppBundle/gulpfile.babel.js
import { rollup } from 'rollup';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import gulp from 'gulp';
import resolve from 'rollup-plugin-node-resolve';
import upath from 'upath';
const env = process.env.GULP_ENV;
const adminRootPath = '../../web/assets/app/admin';
const nodeModulesPath = '../../node_modules';
const paths = {
admin: {
js: [
'Resources/private/admin/js/**',
],
},
};
const sourcePathMap = [
{
sourceDir: 'Resources/private/',
destPath: '/AppBundle',
},
];
const mapSourcePath = function mapSourcePath(sourcePath) {
const match = sourcePathMap.find(({ sourceDir }) => (
sourcePath.substring(0, sourceDir.length) === sourceDir
));
if (!match) {
return sourcePath;
}
const { sourceDir, destPath } = match;
return upath.joinSafe(destPath, sourcePath.substring(sourceDir.length));
};
export const buildAdminJs = async function buildAdminJs() {
const bundle = await rollup({
input: 'Resources/private/admin/js/app.js',
external: [
'jquery',
],
plugins: [
resolve({
jail: upath.resolve(nodeModulesPath),
}),
commonjs({
include: `${nodeModulesPath}/**`,
}),
babel({
exclude: `${nodeModulesPath}/**`,
runtimeHelpers: true,
}),
],
});
await bundle.write({
file: upath.joinSafe(adminRootPath, 'js/app.js'),
format: 'iife',
name: '_sylius.app.admin',
globals: {
jquery: 'jQuery',
},
sourcemap: true,
sourcemapBaseDir: '.',
sourcemapPathTransform: mapSourcePath,
});
};
buildAdminJs.description = 'Build admin js assets.'; |
I've also wished for this feature. If you are using If you eventually bundle these into a webpack build this will cause everything to belong to a single |
@guybedford @Rich-Harris Anything wrong with applying a function here to further transform the It seemed to work if I did the above and then passed an extra config option Is this solution okay? I could create a PR if it is. |
@sebinsua As long as all paths are first normalized to the same base directory... Or is that already the case? |
… name This fixes an issue where all sourcemap paths are relative to the dist directory (eg '../src/foo/bar.ts'), which causes them to all point to the same (wrong) directory when bundled by a consumer's webpack rollup/rollup#2168 (comment)
… name This fixes an issue where all sourcemap paths are relative to the dist directory (eg '../src/foo/bar.ts'), which causes them to all point to the same (wrong) directory when bundled by a consumer's webpack rollup/rollup#2168 (comment)
It doesn't seem possible at the moment to customize the path the sources within the sourcemap.
I would like the sources within the sourcemap to be relative to the output file.
Instead of :
I would like :
This makes the sourcemaps easier to be read by consumers of the library as the paths of the sources will always be relative to location of the bundle.
This kind of configuration is possible with browserify and Webpack (although a bit buggy with Webpack). So I guess it should be achieveable by the amazing Rollup.
The text was updated successfully, but these errors were encountered: