UNPKG

4.69 kBJavaScriptView Raw
1const path = require('path');
2
3const log = require('fancy-log');
4const glob = require('glob');
5const fs = require('fs-extra');
6
7const babel = require('@babel/core');
8
9const KarmaServer = require('karma').Server;
10
11const getWebpackConfig = require('./getWebpackConfig');
12const getBabelOptions = require('./getBabelOptions');
13const getDemoEntries = require('./getDemoEntries');
14
15const getAddTask = require('./taskAdd');
16const getDevTask = require('./taskDev');
17
18
19const webpack = require('webpack');
20
21const libraryTasks = function (
22 {
23
24 base = process.cwd(),
25 entry = './src/index.js',
26 src = './src',
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 umdName = 'Unnamed',
38 buildSuffix = 'min',
39 minify = true,
40
41 // for compile
42 lib = './lib',
43
44 // for test
45 testEntryPattern = 'src/**/*.spec.js',
46 watchTest = false,
47
48 // for add
49 htmlTemplate = path.resolve(__dirname, '../space/html-template.handlebars'),
50 jsTemplate = path.resolve(__dirname, '../space/js-template.handlebars'),
51
52 // configs
53
54 babelPolyfill = false,
55 typescript = false,
56 react = false,
57
58 lint = false,
59
60 loaders = [],
61 plugins = [],
62
63 } = {}
64) {
65
66 const demoEntryList = getDemoEntries({ demo, typescript });
67 const babelOptions = getBabelOptions({ react, typescript });
68
69 const dev = demoEntryList ? getDevTask({
70 webpackConfig: getWebpackConfig({
71 entrys: demoEntryList,
72 base,
73 demo,
74 dist,
75 suffix: devSuffix,
76 babelOptions,
77 loaders,
78 plugins,
79 babelPolyfill,
80 commonsChunk: false,
81 react,
82 lint,
83 liveReload,
84 typescript,
85 }),
86 demo,
87 port,
88 devCors,
89 demoEntryList
90 }) : function () {
91 log.error(`Warning : There's no demo entries in directory ${demo}, the dev task does nothing.`);
92 };
93
94 const noop = () => { };
95
96 const build = function (done = noop) {
97
98 fs.emptyDirSync(dist);
99
100 webpack(
101 getWebpackConfig({
102 entry,
103 base,
104 umdName,
105 dist,
106 suffix: buildSuffix,
107 babelOptions,
108 minify,
109 react,
110 loaders,
111 plugins,
112 babelPolyfill,
113 commonsChunk: false,
114 lint,
115 typescript,
116 }),
117 (err, stats) => {
118 if (err) {
119 throw err;
120 }
121 if (stats.hasErrors()) {
122 throw stats.toJson().errors;
123 }
124 log('Build Success');
125 done();
126 }
127 )
128 };
129
130 const compile = function (done = noop) {
131
132 if (typescript) {
133 throw new Error('Compile task not support for typescript projects.');
134 }
135 if (loaders.length !== 0) {
136 throw new Error('Compile task not support extra loaders');
137 }
138
139 fs.emptyDirSync(lib);
140 const files = glob.sync(src + '/**/*.js');
141 files.filter(file => !file.endsWith('spec.js')).forEach(function (file) {
142 const res = babel.transformFileSync(file, babelOptions);
143 const target = file.replace(src, lib);
144 fs.outputFileSync(target, res.code, 'utf-8');
145 });
146
147 done();
148 }
149
150 const test = function (done) {
151 new KarmaServer({
152 configFile: path.join(__dirname, '../space/karma.conf.js'),
153 testEntryPattern,
154 singleRun: watchTest ? false : true,
155 webpack: getWebpackConfig({
156 base,
157 umdName,
158 dist,
159 suffix: buildSuffix,
160 babelOptions,
161 react,
162 loaders,
163 plugins,
164 babelPolyfill,
165 commonsChunk: false,
166 lint: false,
167 typescript,
168 }),
169 webpackMiddleware: {
170 quiet: true
171 }
172 }, done).start();
173 }
174
175 const add = getAddTask({
176 demo,
177 htmlTemplate,
178 jsTemplate,
179 liveReload,
180 suffix: devSuffix,
181 commonsChunk: false
182 });
183
184 return {
185 dev,
186 build,
187 compile,
188 test,
189 add
190 }
191}
192
193module.exports = libraryTasks;
\No newline at end of file