1 | const path = require('path');
|
2 |
|
3 |
|
4 | const log = require('fancy-log');
|
5 | const glob = require('glob');
|
6 | const fs = require('fs-extra');
|
7 |
|
8 |
|
9 | const KarmaServer = require('karma').Server;
|
10 |
|
11 | const getWebpackConfig = require('./getWebpackConfig');
|
12 | const getBabelOptions = require('./getBabelOptions');
|
13 | const getDemoEntries = require('./getDemoEntries');
|
14 |
|
15 | const getAddTask = require('./taskAdd');
|
16 | const getDevTask = require('./taskDev');
|
17 |
|
18 |
|
19 | const webpack = require('webpack');
|
20 |
|
21 |
|
22 | const applicationTasks = function (
|
23 |
|
24 | {
|
25 |
|
26 | base = process.cwd(),
|
27 |
|
28 | // for dev
|
29 | demo = './demo',
|
30 | devSuffix = 'bundle',
|
31 | port = 3000,
|
32 | devCors = true,
|
33 | liveReload = false,
|
34 |
|
35 | // for build
|
36 | dist = './dist',
|
37 | buildSuffix = 'bundle',
|
38 | minify = true,
|
39 | publicPath = './',
|
40 |
|
41 | // for test
|
42 | testEntryPattern = 'src/**/*.spec.js',
|
43 | watchTest = false,
|
44 |
|
45 | // for add
|
46 | htmlTemplate = path.resolve(__dirname, '../space/html-template.handlebars'),
|
47 | jsTemplate = path.resolve(__dirname, '../space/js-template.handlebars'),
|
48 |
|
49 | // config
|
50 | babelPolyfill = false,
|
51 | typescript = false,
|
52 | react = false,
|
53 |
|
54 | commonsChunk = true,
|
55 | lint = false,
|
56 |
|
57 | loaders = [],
|
58 | plugins = [],
|
59 |
|
60 | } = {}
|
61 | ) {
|
62 |
|
63 | const demoEntryList = getDemoEntries({ demo, typescript });
|
64 | const babelOptions = getBabelOptions({ react, typescript });
|
65 |
|
66 | const dev = getDevTask({
|
67 | webpackConfig: getWebpackConfig({
|
68 | entrys: demoEntryList,
|
69 | base,
|
70 | demo,
|
71 | dist,
|
72 | suffix: devSuffix,
|
73 | babelOptions,
|
74 | typescript,
|
75 | react,
|
76 | loaders,
|
77 | plugins,
|
78 | babelPolyfill,
|
79 | commonsChunk,
|
80 | lint,
|
81 | liveReload
|
82 | }),
|
83 | demo,
|
84 | port,
|
85 | devCors,
|
86 | demoEntryList
|
87 | });
|
88 |
|
89 | const build = function (done = () => { }) {
|
90 |
|
91 | fs.emptyDirSync(dist);
|
92 |
|
93 | webpack(
|
94 | getWebpackConfig({
|
95 | entrys: demoEntryList,
|
96 | demo,
|
97 | base,
|
98 | dist,
|
99 | suffix: buildSuffix,
|
100 | babelOptions,
|
101 | minify,
|
102 | typescript,
|
103 | react,
|
104 | loaders,
|
105 | plugins,
|
106 | babelPolyfill,
|
107 | commonsChunk,
|
108 | publicPath,
|
109 | lint
|
110 | }),
|
111 | (err, stats) => {
|
112 | if (err || stats.hasErrors()) {
|
113 | throw new Error(err || stats.hasErrors())
|
114 | }
|
115 | copyHTMLs();
|
116 | log('Build Success');
|
117 | done();
|
118 | }
|
119 | )
|
120 |
|
121 | function copyHTMLs() {
|
122 | const htmlFiles = glob.sync(demo + '/*.html');
|
123 |
|
124 | htmlFiles.forEach(file => {
|
125 |
|
126 | const content = fs.readFileSync(file, 'utf-8');
|
127 | const baseName = path.basename(file);
|
128 |
|
129 | const modifiedContent = content.replace(/__TIMESTAMP__/g, 'timestamp=' + Date.now())
|
130 | .replace(/<script src=\".+livereload.js\"><\/script>/g, '');
|
131 |
|
132 | fs.outputFile(dist + `/${baseName}`, modifiedContent);
|
133 | });
|
134 | }
|
135 | }
|
136 |
|
137 | const test = function (done) {
|
138 | new KarmaServer({
|
139 | configFile: path.join(__dirname, '../space/karma.conf.js'),
|
140 | testEntryPattern,
|
141 | singleRun: watchTest ? false : true,
|
142 | webpack: getWebpackConfig({
|
143 | base,
|
144 | dist,
|
145 | suffix: buildSuffix,
|
146 | babelOptions,
|
147 | react,
|
148 | loaders,
|
149 | plugins,
|
150 | babelPolyfill,
|
151 | commonsChunk: false,
|
152 | lint: false,
|
153 | typescript
|
154 | }),
|
155 | webpackMiddleware: {}
|
156 | }, done).start();
|
157 | }
|
158 |
|
159 | const add = getAddTask({
|
160 | demo,
|
161 | htmlTemplate,
|
162 | jsTemplate,
|
163 | liveReload,
|
164 | suffix: devSuffix,
|
165 | commonsChunk
|
166 | });
|
167 |
|
168 | return { dev, build, test, add };
|
169 | }
|
170 |
|
171 | module.exports = applicationTasks; |
\ | No newline at end of file |