refactor: use a custom replacement build step instead of broccoli-replace

This commit is contained in:
Matias Niemelä 2015-06-08 11:05:32 -07:00
parent e77710a372
commit cd6175827f
7 changed files with 292 additions and 431 deletions

View File

@ -1374,87 +1374,6 @@
}
}
},
"broccoli-replace": {
"version": "0.2.0",
"resolved": "git://github.com/alexeagle/broccoli-replace.git#cd39a543b17b40db765a07762f099fbc85518290",
"dependencies": {
"minimatch": {
"version": "1.0.0",
"dependencies": {
"lru-cache": {
"version": "2.6.2"
},
"sigmund": {
"version": "1.0.0"
}
}
},
"applause": {
"version": "0.3.4",
"dependencies": {
"cson": {
"version": "1.6.2",
"dependencies": {
"ambi": {
"version": "2.2.0",
"dependencies": {
"typechecker": {
"version": "2.0.8"
}
}
},
"coffee-script": {
"version": "1.8.0",
"dependencies": {
"mkdirp": {
"version": "0.3.5"
}
}
},
"extract-opts": {
"version": "2.2.0",
"dependencies": {
"typechecker": {
"version": "2.0.8"
}
}
},
"js2coffee": {
"version": "0.3.5",
"dependencies": {
"coffee-script": {
"version": "1.7.1",
"dependencies": {
"mkdirp": {
"version": "0.3.5"
}
}
},
"file": {
"version": "0.2.2"
},
"nopt": {
"version": "3.0.1",
"dependencies": {
"abbrev": {
"version": "1.0.5"
}
}
},
"underscore": {
"version": "1.6.0"
}
}
},
"requirefresh": {
"version": "1.1.2"
}
}
}
}
}
}
},
"broccoli-slow-trees": {
"version": "1.1.0"
},

520
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -49,7 +49,6 @@
"broccoli-funnel": "igorminar/broccoli-funnel#perf-files",
"broccoli-lodash": "^0.1.1",
"broccoli-merge-trees": "^0.2.1",
"broccoli-replace": "alexeagle/broccoli-replace#angular_patch",
"broccoli-slow-trees": "^1.1.0",
"broccoli-stew": "^0.2.1",
"broccoli-writer": "^0.1.1",

View File

@ -0,0 +1,56 @@
import fs = require('fs');
import fse = require('fs-extra');
import path = require('path');
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
var minimatch = require('minimatch');
var exec = require('child_process').exec;
var FILE_ENCODING = {encoding: 'utf-8'};
/**
* Intercepts each changed file and replaces its contents with
* the associated changes.
*/
class DiffingReplace implements DiffingBroccoliPlugin {
constructor(private inputPath, private cachePath, private options) {}
rebuild(treeDiff: DiffResult) {
var patterns = this.options.patterns;
var files = this.options.files;
treeDiff.changedPaths.forEach((changedFilePath) => {
var sourceFilePath = path.join(this.inputPath, changedFilePath);
var destFilePath = path.join(this.cachePath, changedFilePath);
var destDirPath = path.dirname(destFilePath);
if (!fs.existsSync(destDirPath)) {
fse.mkdirpSync(destDirPath);
}
var fileMatches = files.some((filePath) => minimatch(changedFilePath, filePath));
if (fileMatches) {
var content = fs.readFileSync(sourceFilePath, FILE_ENCODING);
patterns.forEach((pattern) => {
var replacement = pattern.replacement;
if (typeof replacement === 'function') {
replacement = function(content) {
return pattern.replacement(content, changedFilePath);
};
}
content = content.replace(pattern.match, replacement);
});
fs.writeFileSync(destFilePath, content, FILE_ENCODING);
} else if (!fs.existsSync(destFilePath)) {
fs.symlinkSync(sourceFilePath, destFilePath);
}
});
treeDiff.removedPaths.forEach((removedFilePath) => {
var destFilePath = path.join(this.cachePath, removedFilePath);
fs.unlinkSync(destFilePath);
});
}
}
export default wrapDiffingPlugin(DiffingReplace);

View File

@ -5,12 +5,12 @@ var flatten = require('broccoli-flatten');
var htmlReplace = require('../html-replace');
import mergeTrees from '../broccoli-merge-trees';
var path = require('path');
var replace = require('broccoli-replace');
var stew = require('broccoli-stew');
import compileWithTypescript from '../broccoli-typescript';
import destCopy from '../broccoli-dest-copy';
import {default as transpileWithTraceur, TRACEUR_RUNTIME_PATH} from '../traceur/index';
import replace from '../broccoli-replace';
var projectRootDir = path.normalize(path.join(__dirname, '..', '..', '..', '..'));
@ -146,25 +146,34 @@ module.exports = function makeBrowserTree(options, destinationPath) {
return funnels;
}
function writeScriptsForPath(relativePath, result) {
return result.replace('@@FILENAME_NO_EXT', relativePath.replace(/\.\w+$/, ''));
}
var scriptPathPatternReplacement = {
match: '@@FILENAME_NO_EXT',
replacement: function(replacement, relativePath) { return relativePath.replace(/\.\w+$/, ''); }
};
var htmlTree = new Funnel(modulesTree, {include: ['*/src/**/*.html'], destDir: '/'});
htmlTree = replace(htmlTree, {
files: ['examples*/**'],
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS')}],
replaceWithPath: writeScriptsForPath
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS')},
scriptPathPatternReplacement
]
});
htmlTree = replace(htmlTree, {
files: ['benchmarks/**'],
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks')}],
replaceWithPath: writeScriptsForPath
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks')},
scriptPathPatternReplacement
]
});
htmlTree = replace(htmlTree, {
files: ['benchmarks_external/**'],
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks_external')}],
replaceWithPath: writeScriptsForPath
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks_external')},
scriptPathPatternReplacement
]
});
var scripts = mergeTrees(servingTrees);

View File

@ -9,10 +9,10 @@ var glob = require('glob');
import mergeTrees from '../broccoli-merge-trees';
var path = require('path');
var renderLodashTemplate = require('broccoli-lodash');
var replace = require('broccoli-replace');
var stew = require('broccoli-stew');
import ts2dart from '../broccoli-ts2dart';
import dartfmt from '../broccoli-dartfmt';
import replace from '../broccoli-replace';
/**
* A funnel starting at modules, including the given filters, and moving into the root.

View File

@ -7,7 +7,7 @@ var Funnel = require('broccoli-funnel');
import mergeTrees from '../broccoli-merge-trees';
var path = require('path');
var renderLodashTemplate = require('broccoli-lodash');
var replace = require('broccoli-replace');
import replace from '../broccoli-replace';
var stew = require('broccoli-stew');
var projectRootDir = path.normalize(path.join(__dirname, '..', '..', '..', '..'));
@ -86,17 +86,16 @@ module.exports = function makeNodeTree(destinationPath) {
files: ['**/*.ts'],
patterns: [
{
// Empty replacement needed so that replaceWithPath gets triggered...
match: /$/g,
replacement: ""
match: /$/,
replacement: function(_, relativePath) {
var content = ""; // we're matching an empty line
if (!relativePath.endsWith('.d.ts')) {
content += '\r\nexport var __esModule = true;\n';
}
return content;
}
}
],
replaceWithPath: function(path, content) {
if (!path.endsWith('.d.ts')) {
content += '\r\nexport var __esModule = true;\n';
}
return content;
}
]
});
var typescriptTree = compileWithTypescript(traceurCompatibleTsModulesTree, {
@ -118,15 +117,14 @@ module.exports = function makeNodeTree(destinationPath) {
// Transform all tests to make them runnable in node
nodeTree = replace(nodeTree, {
files: ['**/test/**/*_spec.js'],
replaceWithPath: function(path, content) {
return "var parse5Adapter = require('angular2/src/dom/parse5_adapter'); " +
"parse5Adapter.Parse5DomAdapter.makeCurrent();" + content;
},
patterns: [
{
// Append main() to all tests since all of our tests are wrapped in exported main fn
match: /$/g,
replacement: "\r\n main();"
match: /$/,
replacement: function(_, relativePath) {
return "\r\n main(); \n\r" +
"var parse5Adapter = require('angular2/src/dom/parse5_adapter'); " +
"parse5Adapter.Parse5DomAdapter.makeCurrent();";
}
}
]
});