Make build process repeatable. Fixes #7

Previously when you ran `npm run build`, it would change index.html
in a way that prevented you from running `npm run build` again.
This change makes the builder more sophisticated, ensuring that
START and STOP markers stay in the index.html file, letting you
re-run the command.
This commit is contained in:
Tom MacWright
2016-03-22 11:06:43 -04:00
parent 2364301ee6
commit a06e6a8ffa
2 changed files with 11 additions and 4 deletions

View File

@ -10,7 +10,7 @@
<link href='css/railscasts.css' rel='stylesheet' />
</head>
<body>
<div id='app'>APP</div>
<!--START--><div id='app'></div><!--STOP-->
<script src='bundle.js'></script>
</body>
</html>

View File

@ -14,7 +14,14 @@ var template = fs.readFileSync('./index.html', 'utf8');
var target = process.argv[2];
var startDiv = `<div id='app'>`;
var stopDiv = '</div>';
var startMarker = '<!--START-->' + startDiv;
var stopMarker = stopDiv + '<!--STOP-->';
var startIdx = template.indexOf(startMarker) + startMarker.length;
var stopIdx = template.indexOf(stopMarker);
fs.writeFileSync(target,
template.replace('APP',
ReactDOMServer.renderToString(
<App ast={ast} content={content} />)));
template.substring(0, startIdx) +
ReactDOMServer.renderToString(<App ast={ast} content={content} />) +
template.substring(stopIdx));