From 4250129cc39b75b3e894c3a30401ef47b931863d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20Veps=C3=A4l=C3=A4inen?= Date: Mon, 13 Mar 2017 15:44:18 +0200 Subject: [PATCH 1/2] Remove extra source map file + write source maps only if they are enabled (#453) * chore: Eliminate convert-source-map The needed functionality has been extracted to a function. * fix: Write sourcemaps only if `sourceMap` is enabled --- lib/convert-source-map.js | 141 -------------------------------------- lib/css-base.js | 31 ++++++--- lib/loader.js | 4 +- test/cssBaseTest.js | 2 +- 4 files changed, 26 insertions(+), 152 deletions(-) delete mode 100644 lib/convert-source-map.js diff --git a/lib/convert-source-map.js b/lib/convert-source-map.js deleted file mode 100644 index 03a9cbe0..00000000 --- a/lib/convert-source-map.js +++ /dev/null @@ -1,141 +0,0 @@ -/* eslint-disable */ -'use strict'; -// XXXXX: This file should not exist. Working around a core level bug -// that prevents using fs at loaders. -//var fs = require('fs'); // XXX -var path = require('path'); - -var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg; -var mapFileCommentRx = - /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg - -function decodeBase64(base64) { - return new Buffer(base64, 'base64').toString(); -} - -function stripComment(sm) { - return sm.split(',').pop(); -} - -function readFromFileMap(sm, dir) { - // NOTE: this will only work on the server since it attempts to read the map file - - mapFileCommentRx.lastIndex = 0; - var r = mapFileCommentRx.exec(sm); - - // for some odd reason //# .. captures in 1 and /* .. */ in 2 - var filename = r[1] || r[2]; - var filepath = path.resolve(dir, filename); - - try { - return fs.readFileSync(filepath, 'utf8'); - } catch (e) { - throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e); - } -} - -function Converter (sm, opts) { - opts = opts || {}; - - if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir); - if (opts.hasComment) sm = stripComment(sm); - if (opts.isEncoded) sm = decodeBase64(sm); - if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm); - - this.sourcemap = sm; -} - -Converter.prototype.toJSON = function (space) { - return JSON.stringify(this.sourcemap, null, space); -}; - -Converter.prototype.toBase64 = function () { - var json = this.toJSON(); - return new Buffer(json).toString('base64'); -}; - -Converter.prototype.toComment = function (options) { - var base64 = this.toBase64(); - var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64; - return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data; -}; - -// returns copy instead of original -Converter.prototype.toObject = function () { - return JSON.parse(this.toJSON()); -}; - -Converter.prototype.addProperty = function (key, value) { - if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead'); - return this.setProperty(key, value); -}; - -Converter.prototype.setProperty = function (key, value) { - this.sourcemap[key] = value; - return this; -}; - -Converter.prototype.getProperty = function (key) { - return this.sourcemap[key]; -}; - -exports.fromObject = function (obj) { - return new Converter(obj); -}; - -exports.fromJSON = function (json) { - return new Converter(json, { isJSON: true }); -}; - -exports.fromBase64 = function (base64) { - return new Converter(base64, { isEncoded: true }); -}; - -exports.fromComment = function (comment) { - comment = comment - .replace(/^\/\*/g, '//') - .replace(/\*\/$/g, ''); - - return new Converter(comment, { isEncoded: true, hasComment: true }); -}; - -exports.fromMapFileComment = function (comment, dir) { - return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true }); -}; - -// Finds last sourcemap comment in file or returns null if none was found -exports.fromSource = function (content) { - var m = content.match(commentRx); - return m ? exports.fromComment(m.pop()) : null; -}; - -// Finds last sourcemap comment in file or returns null if none was found -exports.fromMapFileSource = function (content, dir) { - var m = content.match(mapFileCommentRx); - return m ? exports.fromMapFileComment(m.pop(), dir) : null; -}; - -exports.removeComments = function (src) { - return src.replace(commentRx, ''); -}; - -exports.removeMapFileComments = function (src) { - return src.replace(mapFileCommentRx, ''); -}; - -exports.generateMapFileComment = function (file, options) { - var data = 'sourceMappingURL=' + file; - return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data; -}; - -Object.defineProperty(exports, 'commentRegex', { - get: function getCommentRegex () { - return commentRx; - } -}); - -Object.defineProperty(exports, 'mapFileCommentRegex', { - get: function getMapFileCommentRegex () { - return mapFileCommentRx; - } -}); diff --git a/lib/css-base.js b/lib/css-base.js index 6e46a4cc..b67c1d74 100644 --- a/lib/css-base.js +++ b/lib/css-base.js @@ -3,13 +3,13 @@ Author Tobias Koppers @sokra */ // css base code, injected by the css-loader -module.exports = function() { +module.exports = function(useSourceMap) { var list = []; // return the list of modules as css string list.toString = function toString() { return this.map(function (item) { - var content = cssWithMappingToString(item); + var content = cssWithMappingToString(item, useSourceMap); if(item[2]) { return "@media " + item[2] + "{" + content + "}"; } else { @@ -47,16 +47,29 @@ module.exports = function() { return list; }; -function cssWithMappingToString(item) { +function cssWithMappingToString(item, useSourceMap) { var content = item[1] || ''; var cssMapping = item[3]; if (!cssMapping) { return content; } - var convertSourceMap = require('./convert-source-map'); - var sourceMapping = convertSourceMap.fromObject(cssMapping).toComment({multiline: true}); - var sourceURLs = cssMapping.sources.map(function (source) { - return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */' - }); - return [content].concat(sourceURLs).concat([sourceMapping]).join('\n'); + + if (useSourceMap) { + var sourceMapping = toComment(cssMapping); + var sourceURLs = cssMapping.sources.map(function (source) { + return '/*# sourceURL=' + cssMapping.sourceRoot + source + ' */' + }); + + return [content].concat(sourceURLs).concat([sourceMapping]).join('\n'); + } + + return [content].join('\n'); +} + +// Adapted from convert-source-map (MIT) +function toComment(sourceMap) { + var base64 = new Buffer(JSON.stringify(sourceMap)).toString('base64'); + var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64; + + return '/*# ' + data + ' */'; } diff --git a/lib/loader.js b/lib/loader.js index edf273fd..8698160e 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -108,7 +108,9 @@ module.exports = function(content, map) { } // embed runtime - callback(null, "exports = module.exports = require(" + loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) + ")();\n" + + callback(null, "exports = module.exports = require(" + + loaderUtils.stringifyRequest(this, require.resolve("./css-base.js")) + + ")(" + query.sourceMap + ");\n" + "// imports\n" + importJs + "\n\n" + "// module\n" + diff --git a/test/cssBaseTest.js b/test/cssBaseTest.js index 4e659ac0..de176a66 100644 --- a/test/cssBaseTest.js +++ b/test/cssBaseTest.js @@ -35,7 +35,7 @@ describe("css-base", function() { "@media screen{body { a: 1; }}"); }); it("should toString with source mapping", function() { - var m = base(); + var m = base(true); m.push([1, "body { a: 1; }", "", { file: "test.scss", sources: [ From 76ee8c2d04de2cb5e7e08251376b79873d9190f3 Mon Sep 17 00:00:00 2001 From: Juho Vepsalainen Date: Mon, 13 Mar 2017 15:44:46 +0200 Subject: [PATCH 2/2] chore(release): 0.27.3 --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e6437d..59db8665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## [0.27.3](https://github.com/webpack/css-loader/compare/v0.27.2...v0.27.3) (2017-03-13) + + + # [0.27.2](https://github.com/webpack/css-loader/compare/v0.27.1...v0.27.2) (2017-03-12) diff --git a/package.json b/package.json index ffbb4574..c07b7fb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-loader", - "version": "0.27.2", + "version": "0.27.3", "author": "Tobias Koppers @sokra", "description": "css loader module for webpack", "engines": {