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

How can we set source map target file name to a different path? #91

Open
hzsrc opened this issue Jan 12, 2019 · 1 comment
Open

How can we set source map target file name to a different path? #91

hzsrc opened this issue Jan 12, 2019 · 1 comment

Comments

@hzsrc
Copy link

hzsrc commented Jan 12, 2019

For a security purpose, we'd like to build the source map file to an different path. Like this:
/css/main.css => /src-map-files/main.css.map
As to js files, by source-map-dev-tool-plugin, we can set a filename option to resolve this:

        new webpack.SourceMapDevToolPlugin({
            filename: 'src-map-files/[filebase].map',
            append: false
        })

It is implemented in this file node_modules/webpack/lib/SourceMapDevToolPlugin.js:

    let sourceMapFile = compilation.getPath(sourceMapFilename, {
        chunk,
        filename: options.fileContext
            ? path.relative(options.fileContext, filename)
            : filename,
        query,
        basename: basename(filename),
        contentHash: createHash("md4")
            .update(sourceMapString)
            .digest("hex")
    });

I read the source code of optimize-css-assets-webpack-plugin, I found the target assets name is fixed:

    const parse = url.parse(assetName);
    const assetInfo = {
      path: parse.pathname,
      query: parse.query ? `?${parse.query}` : '',
    };
    // ......
    assets.setAsset(`${assetInfo.path}.map${assetInfo.query}`, r.map.toString());

How can we set source map target file name to a different path? Will you implement this filename option in the future?

@hzsrc
Copy link
Author

hzsrc commented Jan 12, 2019

I have just send a PR here, to resolve this by a getFileName function like this:

        new OptimizeCssPlugin({
            cssProcessorOptions: {
                map: config.build.productionSourceMap,
                append: false,
                getFileName(assetInfo) {
                    return `src-map-files/${path.basename(assetInfo.path)}.map${assetInfo.query}`
                }
            }
        })

Also we need an append option, and set to false when we don't want to load source map automaticly (just like source-map-dev-tool-plugin).

Please check.

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

1 participant