-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
157 lines (148 loc) · 6.13 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var fs = require("fs")
var path = require("path")
var chalk = require("chalk")
var yargs = require("yargs")
var jsesc = require("jsesc")
var cssesc = require("cssesc")
var rimraf = require("rimraf")
var ansiup = require("ansi_up")
var Webpack = require("webpack")
var WebpackExtract = require("extract-text-webpack-plugin")
var NodeWebkitBuilder = require("nw-builder")
var BrowserSync = require("browser-sync")
var Inliner = require("inliner")
var PORT = 1234
var PATH = path.join(__dirname, "./source")
var STAGE = yargs.argv.production ? "PRODUCTION" : "DEVELOPMENT"
var MODE = yargs.argv._.indexOf("server") != -1 ? "SERVER" : yargs.argv._.indexOf("bundle") != -1 ? "BUNDLE" : null
var server = null
rimraf("./builds", function() {
Webpack({
entry: {
"index.js": "./source/index.js",
"index.css": "./source/index.css",
"index.html": "./source/index.html",
},
output: {
filename: "[name]",
path: "./builds/web",
},
watch: MODE == "SERVER",
module: {
preLoaders: [
{test:/\.js$/, exclude: /node_modules/, loader: "eslint-loader"}
],
loaders: [
{test: /\.js$/i, exclude: /(node_modules)/i, loader: "babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0"},
{test: /\.s?css$/i, loader: new WebpackExtract("css", "index.css").extract(["css-loader", "autoprefixer-loader", "sass-loader"])},
{test: /\.html$/i, loader: new WebpackExtract("html", "index.html").extract(["html-loader"])},
{test: /\.(png|jpe?g|gif|svg)$/i, loaders: ["url-loader", "image-webpack-loader"]},
{test: /\.tiled\.json$/i, exclude: /(node_modules)/i, loader: "tiled-webpack-loader"},
{test: /\.(ttf|otf|woff|svg)$/i, loader: "url-loader"},
{test: /\.mp3$/i, loader: "url-loader"},
]
},
resolve: {
root: ["./source"]
},
plugins: [
new WebpackExtract("css", "index.css"),
new WebpackExtract("html", "index.html"),
new Webpack.DefinePlugin({
PATH: JSON.stringify(PATH),
STAGE: JSON.stringify(STAGE),
VERSION: JSON.stringify(JSON.parse(fs.readFileSync("./package.json")).version),
}),
STAGE == "PRODUCTION" ? new Webpack.optimize.UglifyJsPlugin() : new Webpack.IgnorePlugin(/^$/),
]
}, function(error, results) {
if(results.compilation.errors.length > 0) {
var jserrors = [], csserrors = [], htmlerrors = []
results.compilation.errors.forEach(function(error) {
if(/\.js$/.test(error.module.resource)) {
jserrors.push(error.toString())
} else if(/\.css$/.test(error.module.resource)) {
csserrors.push(error.toString())
} else if(/\.html$/.test(error.module.resource)) {
htmlerrors.push(error.toString())
}
})
if(jserrors.length > 0) {
jserrors.forEach(function(jserror) {console.log(jserror)})
fs.writeFileSync("./builds/web/index.js", injectviajs(jserrors))
}
if(csserrors.length > 0) {
csserrors.forEach(function(csserror) {console.log(csserror)})
}
}
if(MODE == "SERVER") {
if(server == null) {
server = BrowserSync({
server: "./builds/web",
logLevel: "silent",
notify: false,
port: PORT
})
} else {
server.reload()
}
} else if(MODE == "BUNDLE") {
new Inliner("./builds/web/index.html", function(error, data) {
fs.mkdir("./builds/web1", function() {
fs.writeFile("builds/web1/index.html", data)
})
})
new NodeWebkitBuilder({
version: "v0.12.2",
platforms: ["win", "osx", "linux"],
files: ["./builds/web/**/**", "./package.json"],
buildType: function() {return ""},
cacheDir: "node_webkit_builds",
buildDir: "./builds",
}).build()
}
})
})
var print = function(message) {
console.log(time(), message)
}
var time = function() {
var date = new Date()
var hours = (date.getHours() < 10 ? "0" : "") + date.getHours()
var minutes = (date.getMinutes() < 10 ? "0" : "") + date.getMinutes()
var seconds = (date.getSeconds() < 10 ? "0" : "") + date.getSeconds()
return "[" + chalk.green(hours + ":" + minutes + ":" + seconds) + "]"
}
var injectviajs = function(content) {
var styles = {
"color": "#000 !important",
"padding": "2em !important",
"font-size": "medium !important",
"font-style": "normal !important",
"font-weight": "normal !important",
"font-variant": "normal !important",
"font-stretch": "normal !important",
"font-family": "monospace !important",
"line-height": "normal !important",
"white-space": "pre-wrap !important",
"background-color": "#FFF !important",
}
styles = Object.keys(styles).map(function(key) {
return key + ":" + styles[key] + ";"
}).join(" ")
content = content instanceof Array ? content : [content]
content = content.map(function(message) {
return "<div>" + ansiup.ansi_to_html(message) + "</div>"
})
content = "<div style=\"" + styles + "\">" + content.join("\n") + "</div>"
content = "document.body.innerHTML=\"" + jsesc(content, {quotes: "double"}) + "\""
content = "window.addEventListener(\"DOMContentLoaded\", function() {" + content + "})"
return content
}
var injectviacss = function(content) {
content = content instanceof Array ? content : [content]
content = content.join("\n")
content = "body:before{content:\"" + cssesc(content, {quotes: "double"}) + "\";}"
content = "body{font-family:monospace;white-space:pre;padding:2em;}" + content
return content
}