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

This reverts commit d5c528ac2b.
This commit is contained in:
Matias Niemelä
2015-06-06 00:58:57 -07:00
parent a418397174
commit 72736a1b09
7 changed files with 426 additions and 288 deletions

View File

@ -1,56 +0,0 @@
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 {
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,36 +146,25 @@ module.exports = function makeBrowserTree(options, destinationPath) {
return funnels;
}
var scriptPathPatternReplacement = {
match: '@@FILENAME_NO_EXT',
replacement: function(replacement, relativePath) {
return relativePath.replace(/\.\w+$/, '');
}
};
function writeScriptsForPath(relativePath, result) {
return result.replace('@@FILENAME_NO_EXT', relativePath.replace(/\.\w+$/, ''));
}
var htmlTree = new Funnel(modulesTree, {include: ['*/src/**/*.html'], destDir: '/'});
htmlTree = replace(htmlTree, {
files: ['examples*/**'],
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS')},
scriptPathPatternReplacement
]
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS')}],
replaceWithPath: writeScriptsForPath
});
htmlTree = replace(htmlTree, {
files: ['benchmarks/**'],
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks')},
scriptPathPatternReplacement
]
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks')}],
replaceWithPath: writeScriptsForPath
});
htmlTree = replace(htmlTree, {
files: ['benchmarks_external/**'],
patterns: [
{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks_external')},
scriptPathPatternReplacement
]
patterns: [{match: /\$SCRIPTS\$/, replacement: htmlReplace('SCRIPTS_benchmarks_external')}],
replaceWithPath: writeScriptsForPath
});
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');
import replace from '../broccoli-replace';
var replace = require('broccoli-replace');
var stew = require('broccoli-stew');
var projectRootDir = path.normalize(path.join(__dirname, '..', '..', '..', '..'));
@ -84,16 +84,19 @@ module.exports = function makeNodeTree(destinationPath) {
// TODO: remove this when we no longer use traceur
var traceurCompatibleTsModulesTree = replace(modulesTree, {
files: ['**/*.ts'],
patterns: [{
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;
patterns: [
{
// Empty replacement needed so that replaceWithPath gets triggered...
match: /$/g,
replacement: ""
}
}]
],
replaceWithPath: function(path, content) {
if (!path.endsWith('.d.ts')) {
content += '\r\nexport var __esModule = true;\n';
}
return content;
}
});
var typescriptTree = compileWithTypescript(traceurCompatibleTsModulesTree, {