Skip to content

Commit

Permalink
Merge pull request #1067 from stealjs/es-dev
Browse files Browse the repository at this point in the history
Make dev code removal work for bundled-es exports
  • Loading branch information
matthewp authored Aug 22, 2018
2 parents 251d7dd + 7d19918 commit 0feb516
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 113 deletions.
3 changes: 2 additions & 1 deletion lib/build/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ var transformImport = function(config, transformOptions){
var concatOptions = {
excludePlugins: options.format === "global",
sourceProp: "activeSource",
format: options.concatFormat
format: options.concatFormat,
removeDevelopmentCode: options.removeDevelopmentCode
};
return concatSource(bundle, concatOptions);
})
Expand Down
9 changes: 3 additions & 6 deletions lib/bundle/concat_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ var concatAMD = require("./concat_source_amd");
var concatES = require("./concat_source_es");

module.exports = function(bundle, options = {}){
var sourceProp = options.sourceProp;
var excludePlugins = options.excludePlugins;

var result;

if(options.format === "es") {
result = concatES(bundle, sourceProp, excludePlugins);
result = concatES(bundle, options);
} else if(options.format === "amd") {
result = concatAMD(bundle, sourceProp, excludePlugins);
result = concatAMD(bundle, options);
} else {
return Promise.reject(new Error(`Unsupported concat format: ${options.format}`));
}
Expand Down
5 changes: 4 additions & 1 deletion lib/bundle/concat_source_amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ var concat = require("../source-map-concat");
var sourceNode = require("../node/source").node;
var removeSourceMapUrl = require("../remove_source_map_url");

module.exports = function(bundle, sourceProp, excludePlugins){
module.exports = function(bundle, options){
var sourceProp = options.sourceProp;
var excludePlugins = options.excludePlugins;

var output = fileName(bundle);

var makeCode = function(name) {
Expand Down
19 changes: 14 additions & 5 deletions lib/bundle/concat_source_es.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const cleanSource = require("../node/clean");
const dependencyResolver = require("../node/dependency_resolver");
const isProcessShim = require("../node/is_process_shim");
const nodeSource = require("../node/source");
const pluginCommonjs = require("rollup-plugin-commonjs");
const removeActiveSourceKeys = require("../node/remove_active_source_keys");
const rollup = require("steal-rollup");

const moduleNameFromSpecifier = dependencyResolver.moduleNameFromSpecifier;
const CJS_PROXY_PREFIX = '\0commonjs-proxy:';

module.exports = function(bundle) {
module.exports = function(bundle, options) {
let removeDevelopmentCode = options.removeDevelopmentCode;
let main = bundle.bundles[0];
let firstNode = bundle.nodes[0];

Expand All @@ -20,7 +24,7 @@ module.exports = function(bundle) {
input: main,

plugins: [
loadFromGraph(getNode),
loadFromGraph(getNode, removeDevelopmentCode),
pluginCommonjs({})
]
})
Expand All @@ -43,7 +47,7 @@ module.exports = function(bundle) {
});
};

function loadFromGraph(getNode) {
function loadFromGraph(getNode, removeDevelopmentCode) {
return {
resolveId: function(id, importer) {
if(id.startsWith(CJS_PROXY_PREFIX)) {
Expand All @@ -68,8 +72,13 @@ function loadFromGraph(getNode) {
return undefined;
}

let source = node.load.source;
return source;
removeActiveSourceKeys(node);

if(removeDevelopmentCode) {
cleanSource(node, {});
}

return nodeSource(node);
}
};
}
91 changes: 1 addition & 90 deletions lib/graph/clean.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
var clone = require("lodash/clone");
var findLineColumn = require("find-line-column");
var makeTagRegEx = require("../build/clean").makeTagRegEx;
var SourceMapConsumer = require("bitovi-source-map").SourceMapConsumer;
var transformActiveSource = require("../node/transform_active_source");

var clean = function(node, options){
var removeTags = (options.removeTags || []).slice(0);

if(!removeTags.length) {
removeTags.push("steal-remove");
removeTags.push(/steal.dev.(?:log|warn|assert)\(.*\);?/g);
}

if(!node.load.metadata.buildType || node.load.metadata.buildType === "js") {
transformActiveSource(node, "clean-true", function(node, source){
source = clone(source);
var empty = makeUpdater(source);
removeTags.forEach(function(tag) {
if (tag instanceof RegExp) {
source.code = source.code.replace(tag, empty);
} else {
source.code = source.code.replace(makeTagRegEx(tag), empty);
}
});
return source;
});
}
};
var clean = require("../node/clean");

module.exports = function(graph, options) {
options = options || {};
Expand All @@ -42,64 +14,3 @@ module.exports = function(graph, options) {
}
}
};

var REGEX_NEWLINE = /(\r?\n)/g;
/**
* @function makeUpdater
* @description Creates function that acts as a callback to String.prototype.replace.
* Calculates the offset caused by the removal of a match and applies new mappings to
* the source map.
* @return {Function}
*/
function makeUpdater(source) {
var map = source.map;

if(!map) {
return function() { return ""; };
}

function offsetFor(string){
var parts = string.split(REGEX_NEWLINE);
return {
lines: 0 - (parts.length - 1),
columns: 0 - parts[parts.length - 1].length
};
}

var consumer;
return function(match) {
var string = arguments[arguments.length - 1];
var offset = arguments[arguments.length - 2];
consumer = consumer || new SourceMapConsumer(map.toJSON());

var position = findLineColumn(string, offset);
offset = offsetFor(match);

consumer.eachMapping(function(mapping) {
// Skip mappings that precede the change.
if(mapping.generatedLine < position.line ||
(mapping.generatedLine === position.line &&
mapping.generatedColumn < position.column)) {
return;
}

var offsetColumns = mapping.generatedColumn > Math.abs(offset.columns) ?
offset.columns : 0;
if(offset.lines > 0 && offsetColumns > 0) {
map.addMapping({
generated: {
line: offset.lines + mapping.generatedLine,
column: offsetColumns + mapping.generatedColumn
},
original: {
line: mapping.originalLine,
column: mapping.originalColumn
},
source: mapping.source
});
}
});

return "";
};
}
11 changes: 1 addition & 10 deletions lib/graph/remove_active_source_keys.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
var clean = function(node, options){
delete node.activeSourceKeys;
node.activeSource = {
code: node.load.source
};
if(options.removeSourceNodes) {
delete node.sourceNode;
}
};
var clean = require("../node/remove_active_source_keys");

module.exports = function(graph, options) {
options = options || {};
Expand All @@ -22,4 +14,3 @@ module.exports = function(graph, options) {
}
}
};

92 changes: 92 additions & 0 deletions lib/node/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var clone = require("lodash/clone");
var findLineColumn = require("find-line-column");
var makeTagRegEx = require("../build/clean").makeTagRegEx;
var SourceMapConsumer = require("bitovi-source-map").SourceMapConsumer;
var transformActiveSource = require("../node/transform_active_source");

var clean = function(node, options){
var removeTags = (options.removeTags || []).slice(0);

if(!removeTags.length) {
removeTags.push("steal-remove");
removeTags.push(/steal.dev.(?:log|warn|assert)\(.*\);?/g);
}

if(!node.load.metadata.buildType || node.load.metadata.buildType === "js") {
transformActiveSource(node, "clean-true", function(node, source){
source = clone(source);
var empty = makeUpdater(source);
removeTags.forEach(function(tag) {
if (tag instanceof RegExp) {
source.code = source.code.replace(tag, empty);
} else {
source.code = source.code.replace(makeTagRegEx(tag), empty);
}
});
return source;
});
}
};

module.exports = clean;

var REGEX_NEWLINE = /(\r?\n)/g;
/**
* @function makeUpdater
* @description Creates function that acts as a callback to String.prototype.replace.
* Calculates the offset caused by the removal of a match and applies new mappings to
* the source map.
* @return {Function}
*/
function makeUpdater(source) {
var map = source.map;

if(!map) {
return function() { return ""; };
}

function offsetFor(string){
var parts = string.split(REGEX_NEWLINE);
return {
lines: 0 - (parts.length - 1),
columns: 0 - parts[parts.length - 1].length
};
}

var consumer;
return function(match) {
var string = arguments[arguments.length - 1];
var offset = arguments[arguments.length - 2];
consumer = consumer || new SourceMapConsumer(map.toJSON());

var position = findLineColumn(string, offset);
offset = offsetFor(match);

consumer.eachMapping(function(mapping) {
// Skip mappings that precede the change.
if(mapping.generatedLine < position.line ||
(mapping.generatedLine === position.line &&
mapping.generatedColumn < position.column)) {
return;
}

var offsetColumns = mapping.generatedColumn > Math.abs(offset.columns) ?
offset.columns : 0;
if(offset.lines > 0 && offsetColumns > 0) {
map.addMapping({
generated: {
line: offset.lines + mapping.generatedLine,
column: offsetColumns + mapping.generatedColumn
},
original: {
line: mapping.originalLine,
column: mapping.originalColumn
},
source: mapping.source
});
}
});

return "";
};
}
10 changes: 10 additions & 0 deletions lib/node/remove_active_source_keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

module.exports = function(node, options = {}){
delete node.activeSourceKeys;
node.activeSource = {
code: node.load.source
};
if(options.removeSourceNodes) {
delete node.sourceNode;
}
};
23 changes: 23 additions & 0 deletions test/export_bundled_es_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var assert = require("assert");
var fs = require("fs");
var stealExport = require("../lib/build/export");
var rmdir = require("rimraf");
var testHelpers = require("./helpers");
Expand Down Expand Up @@ -72,4 +73,26 @@ describe("+bundled-es", function(){
done();
}, done);
});

it("Removes dev code", function(done){
this.timeout(10000);
stealExport({
steal: {
config: __dirname+"/exports_basics/package.json!npm",
main: "app/index_es"
},
options: { quiet: true },
"outputs": {
"+bundled-es": {
dest: __dirname + "/exports_basics/dist/es.js"
}
}
})
.then(function() {
var code = fs.readFileSync(__dirname + "/exports_basics/dist/es.js", "utf8");
assert.ok(!/window\.REMOVEME/.test(code), "Removed the dev code");
done();
})
.catch(done);
});
});
4 changes: 4 additions & 0 deletions test/exports_basics/index_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import foo from "foo";

let name = "index";

//!steal-remove-start
window.REMOVEME = true;
//!steal-remove-end

export {
name,
foo
Expand Down

0 comments on commit 0feb516

Please sign in to comment.