Created
March 11, 2016 15:38
-
-
Save clinyong/b28ff4a8fa7906d01723 to your computer and use it in GitHub Desktop.
webpack build script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webpack from 'webpack' | |
import config from './webpack' | |
import swig from 'swig' | |
import fs from 'fs' | |
function writeTemplate (name) { | |
let sourceTemplate = config.templatePath + '/prod.tmpl' | |
let targetTemplate = config.buildTemplatePath + '/index.tmpl' | |
let content = swig.renderFile(sourceTemplate, {name}) | |
fs.writeFile(targetTemplate, content, err => { | |
if (err) { | |
console.log(err) | |
} | |
}) | |
} | |
const bundler = webpack(config) | |
bundler.run((err, stats) => { | |
if (err !== null) { | |
console.log(err) | |
return | |
} | |
let assets = stats.toJson().assets | |
let name | |
for (let i = 0; i < assets.length; i++) { | |
if (assets[i].name.startsWith('main')) { | |
name = assets[i].name | |
break | |
} | |
} | |
fs.stat(config.buildTemplatePath, (err, stats) => { | |
if (err) { | |
fs.mkdirSync(config.buildTemplatePath) | |
} | |
writeTemplate(name) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<div id="container"></div> | |
<script src="/build/vendor.js"></script> | |
<script src="/build/{{name}}"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment