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

support to set source map target file neme, by a getFileName option #92

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "optimize-css-assets-webpack-plugin",
"version": "5.0.4",
"description": "A Webpack plugin to optimize \\ minimize CSS assets.",
"version": "5.0.3",
"description": "A Webpack plugin to optimize \\ minimize CSS assets, with sorce map file name support",
"keywords": [
"CSS",
"duplicate",
Expand All @@ -11,14 +11,9 @@
"remove",
"webpack"
],
"homepage": "http://github.com/NMFR/optimize-css-assets-webpack-plugin",
"license": "MIT",
"author": "Nuno Rodrigues",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "http://github.com/NMFR/optimize-css-assets-webpack-plugin.git"
},
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
Expand Down
25 changes: 20 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const url = require('url');
const path = require('path')

const LastCallWebpackPlugin = require('last-call-webpack-plugin');

Expand Down Expand Up @@ -35,10 +35,16 @@ class OptimizeCssAssetsWebpackPlugin extends LastCallWebpackPlugin {
}

processCss(assetName, asset, assets) {
const parse = url.parse(assetName);
let filename = assetName;
let query = '';
const idx = filename.indexOf('?');
if (idx >= 0) {
query = filename.substr(idx);
filename = filename.substr(0, idx);
}
const assetInfo = {
path: parse.pathname,
query: parse.query ? `?${parse.query}` : '',
path: filename,
query: query,
};

const css = asset.sourceAndMap ? asset.sourceAndMap() : { source: asset.source() };
Expand Down Expand Up @@ -73,7 +79,16 @@ class OptimizeCssAssetsWebpackPlugin extends LastCallWebpackPlugin {
.cssProcessor.process(css.source, processOptions, this.options.cssProcessorPluginOptions)
.then(r => {
if (processOptions.map && r.map && r.map.toString) {
assets.setAsset(`${assetInfo.path}.map${assetInfo.query}`, r.map.toString());
let filename = `${assetInfo.path}.map${assetInfo.query}`
if (processOptions.getFileName) {
filename = processOptions.getFileName(assetInfo)
}
assets.setAsset(filename, r.map.toString());

if (processOptions.append !== false) { // true or undefined
let reletivePath = path.relative(path.dirname(assetName), filename).replace(/\\/g, '/')
r.css += `\n/*# sourceMappingURL=${reletivePath}*/`
}
}
return r.css;
});
Expand Down