@shuvi/toolpack 0.0.1-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/babel/plugins/auto-css-modules.d.ts +7 -0
- package/lib/babel/plugins/auto-css-modules.js +24 -0
- package/lib/babel/plugins/jsx-pragma.d.ts +4 -0
- package/lib/babel/plugins/jsx-pragma.js +77 -0
- package/lib/babel/plugins/loadable-plugin.d.ts +24 -0
- package/lib/babel/plugins/loadable-plugin.js +105 -0
- package/lib/babel/plugins/optimize-hook-destructuring.d.ts +4 -0
- package/lib/babel/plugins/optimize-hook-destructuring.js +59 -0
- package/lib/babel/preset.d.ts +14 -0
- package/lib/babel/preset.js +106 -0
- package/lib/constants.d.ts +1 -0
- package/lib/constants.js +3 -0
- package/lib/utils/emptyComponent.d.ts +1 -0
- package/lib/utils/emptyComponent.js +7 -0
- package/lib/utils/forkTsCheckerWebpackPlugin.d.ts +4 -0
- package/lib/utils/forkTsCheckerWebpackPlugin.js +9 -0
- package/lib/utils/formatWebpackMessages.d.ts +5 -0
- package/lib/utils/formatWebpackMessages.js +91 -0
- package/lib/utils/hotDevClient/eventsource.d.ts +1 -0
- package/lib/utils/hotDevClient/eventsource.js +63 -0
- package/lib/utils/hotDevClient/index.d.ts +4 -0
- package/lib/utils/hotDevClient/index.js +304 -0
- package/lib/utils/verifyTypeScriptSetup.d.ts +5 -0
- package/lib/utils/verifyTypeScriptSetup.js +229 -0
- package/lib/webpack/config/base.d.ts +16 -0
- package/lib/webpack/config/base.js +236 -0
- package/lib/webpack/config/browser.d.ts +8 -0
- package/lib/webpack/config/browser.js +147 -0
- package/lib/webpack/config/index.d.ts +4 -0
- package/lib/webpack/config/index.js +9 -0
- package/lib/webpack/config/node.d.ts +7 -0
- package/lib/webpack/config/node.js +55 -0
- package/lib/webpack/config/parts/external.d.ts +4 -0
- package/lib/webpack/config/parts/external.js +91 -0
- package/lib/webpack/config/parts/helpers.d.ts +3 -0
- package/lib/webpack/config/parts/helpers.js +48 -0
- package/lib/webpack/config/parts/resolve.d.ts +1 -0
- package/lib/webpack/config/parts/resolve.js +10 -0
- package/lib/webpack/config/parts/style.d.ts +9 -0
- package/lib/webpack/config/parts/style.js +217 -0
- package/lib/webpack/loaders/export-global-loader.d.ts +7 -0
- package/lib/webpack/loaders/export-global-loader.js +26 -0
- package/lib/webpack/loaders/route-component-loader.d.ts +7 -0
- package/lib/webpack/loaders/route-component-loader.js +14 -0
- package/lib/webpack/loaders/shuvi-babel-loader.d.ts +1 -0
- package/lib/webpack/loaders/shuvi-babel-loader.js +60 -0
- package/lib/webpack/plugins/build-manifest-plugin.d.ts +27 -0
- package/lib/webpack/plugins/build-manifest-plugin.js +215 -0
- package/lib/webpack/plugins/chunk-names-plugin.d.ts +4 -0
- package/lib/webpack/plugins/chunk-names-plugin.js +43 -0
- package/lib/webpack/plugins/fix-watching-plugin.d.ts +4 -0
- package/lib/webpack/plugins/fix-watching-plugin.js +23 -0
- package/lib/webpack/plugins/module-replace-plugin/index.d.ts +1 -0
- package/lib/webpack/plugins/module-replace-plugin/index.js +4 -0
- package/lib/webpack/plugins/module-replace-plugin/plugin.d.ts +31 -0
- package/lib/webpack/plugins/module-replace-plugin/plugin.js +177 -0
- package/lib/webpack/plugins/module-replace-plugin/stub-loader.d.ts +1 -0
- package/lib/webpack/plugins/module-replace-plugin/stub-loader.js +34 -0
- package/lib/webpack/plugins/prefer-resolver-plugin.d.ts +10 -0
- package/lib/webpack/plugins/prefer-resolver-plugin.js +47 -0
- package/lib/webpack/plugins/require-cache-hot-reloader-plugin.d.ts +6 -0
- package/lib/webpack/plugins/require-cache-hot-reloader-plugin.js +48 -0
- package/lib/webpack/types.d.ts +40 -0
- package/lib/webpack/types.js +2 -0
- package/package.json +80 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable camelcase */
|
|
3
|
+
/**
|
|
4
|
+
MIT License
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2013-present, Facebook, Inc.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
// This file is based on https://github.com/facebook/create-react-app/blob/v1.1.4/packages/react-dev-utils/webpackHotDevClient.js
|
|
27
|
+
// It's been edited to rely on webpack-hot-middleware.
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
38
|
+
if (mod && mod.__esModule) return mod;
|
|
39
|
+
var result = {};
|
|
40
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
41
|
+
result["default"] = mod;
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
const ErrorOverlay = __importStar(require("react-error-overlay"));
|
|
49
|
+
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
50
|
+
const formatWebpackMessages_1 = __importDefault(require("../formatWebpackMessages"));
|
|
51
|
+
const eventsource_1 = require("./eventsource");
|
|
52
|
+
// This alternative WebpackDevServer combines the functionality of:
|
|
53
|
+
// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
|
|
54
|
+
// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
|
|
55
|
+
// It only supports their simplest configuration (hot updates on same server).
|
|
56
|
+
// It makes some opinionated choices on top, like adding a syntax error overlay
|
|
57
|
+
// that looks similar to our console output. The error overlay is inspired by:
|
|
58
|
+
// https://github.com/glenjamin/webpack-hot-middleware
|
|
59
|
+
// This is a modified version of create-react-app's webpackHotDevClient.js
|
|
60
|
+
// It implements webpack-hot-middleware's EventSource events instead of webpack-dev-server's websocket.
|
|
61
|
+
// https://github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-dev-utils/webpackHotDevClient.js
|
|
62
|
+
let hadRuntimeError = false;
|
|
63
|
+
let customHmrEventHandler;
|
|
64
|
+
function connect(options) {
|
|
65
|
+
// Open stack traces in an editor.
|
|
66
|
+
ErrorOverlay.setEditorHandler(function editorHandler({ fileName, lineNumber, colNumber }) {
|
|
67
|
+
// Resolve invalid paths coming from react-error-overlay
|
|
68
|
+
const resolvedFilename = fileName.replace(/^webpack:\/\/[^/]+/ /* webpack://namaspcae/resourcepath */, '');
|
|
69
|
+
fetch(options.launchEditorEndpoint +
|
|
70
|
+
'?fileName=' +
|
|
71
|
+
window.encodeURIComponent(resolvedFilename) +
|
|
72
|
+
'&lineNumber=' +
|
|
73
|
+
window.encodeURIComponent(lineNumber || 1) +
|
|
74
|
+
'&colNumber=' +
|
|
75
|
+
window.encodeURIComponent(colNumber || 1));
|
|
76
|
+
});
|
|
77
|
+
// We need to keep track of if there has been a runtime error.
|
|
78
|
+
// Essentially, we cannot guarantee application state was not corrupted by the
|
|
79
|
+
// runtime error. To prevent confusing behavior, we forcibly reload the entire
|
|
80
|
+
// application. This is handled below when we are notified of a compile (code
|
|
81
|
+
// change).
|
|
82
|
+
// See https://github.com/facebook/create-react-app/issues/3096
|
|
83
|
+
ErrorOverlay.startReportingRuntimeErrors({
|
|
84
|
+
onError: function () {
|
|
85
|
+
hadRuntimeError = true;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
if (module.hot && typeof module.hot.dispose === 'function') {
|
|
89
|
+
module.hot.dispose(function () {
|
|
90
|
+
// TODO: why do we need this?
|
|
91
|
+
ErrorOverlay.stopReportingRuntimeErrors();
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
eventsource_1.getEventSourceWrapper(options).addMessageListener(event => {
|
|
95
|
+
// This is the heartbeat event
|
|
96
|
+
if (event.data === '\uD83D\uDC93') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
processMessage(event);
|
|
101
|
+
}
|
|
102
|
+
catch (ex) {
|
|
103
|
+
console.warn('Invalid HMR message: ' + event.data + '\n' + ex);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
subscribeToHmrEvent(handler) {
|
|
108
|
+
customHmrEventHandler = handler;
|
|
109
|
+
},
|
|
110
|
+
reportRuntimeError(err) {
|
|
111
|
+
ErrorOverlay.reportRuntimeError(err);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
exports.default = connect;
|
|
116
|
+
// Remember some state related to hot module replacement.
|
|
117
|
+
var isFirstCompilation = true;
|
|
118
|
+
var mostRecentCompilationHash = null;
|
|
119
|
+
var hasCompileErrors = false;
|
|
120
|
+
function clearOutdatedErrors() {
|
|
121
|
+
// Clean up outdated compile errors, if any.
|
|
122
|
+
if (typeof console !== 'undefined' && typeof console.clear === 'function') {
|
|
123
|
+
if (hasCompileErrors) {
|
|
124
|
+
console.clear();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function afterApplyUpdate() {
|
|
129
|
+
tryDismissErrorOverlay();
|
|
130
|
+
console.log('[Fast Refresh] Done.');
|
|
131
|
+
}
|
|
132
|
+
// Successful compilation.
|
|
133
|
+
function handleSuccess() {
|
|
134
|
+
clearOutdatedErrors();
|
|
135
|
+
const isHotUpdate = !isFirstCompilation;
|
|
136
|
+
isFirstCompilation = false;
|
|
137
|
+
hasCompileErrors = false;
|
|
138
|
+
// Attempt to apply hot updates or reload.
|
|
139
|
+
if (isHotUpdate) {
|
|
140
|
+
tryApplyUpdates(function onHotUpdateSuccess() {
|
|
141
|
+
// Only dismiss it when we're sure it's a hot update.
|
|
142
|
+
// Otherwise it would flicker right before the reload.
|
|
143
|
+
afterApplyUpdate();
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Compilation with warnings (e.g. ESLint).
|
|
148
|
+
function handleWarnings(warnings) {
|
|
149
|
+
clearOutdatedErrors();
|
|
150
|
+
var isHotUpdate = !isFirstCompilation;
|
|
151
|
+
isFirstCompilation = false;
|
|
152
|
+
hasCompileErrors = false;
|
|
153
|
+
// Print warnings to the console.
|
|
154
|
+
const formatted = formatWebpackMessages_1.default({
|
|
155
|
+
warnings: warnings,
|
|
156
|
+
errors: []
|
|
157
|
+
});
|
|
158
|
+
if (typeof console !== 'undefined' && typeof console.warn === 'function') {
|
|
159
|
+
for (let i = 0; i < formatted.warnings.length; i++) {
|
|
160
|
+
if (i === 5) {
|
|
161
|
+
console.warn('There were more warnings in other files.\n' +
|
|
162
|
+
'You can find a complete log in the terminal.');
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
console.warn(strip_ansi_1.default(formatted.warnings[i]));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Attempt to apply hot updates or reload.
|
|
169
|
+
if (isHotUpdate) {
|
|
170
|
+
tryApplyUpdates(function onSuccessfulHotUpdate() {
|
|
171
|
+
// Only dismiss it when we're sure it's a hot update.
|
|
172
|
+
// Otherwise it would flicker right before the reload.
|
|
173
|
+
tryDismissErrorOverlay();
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Compilation with errors (e.g. syntax error or missing modules).
|
|
178
|
+
function handleErrors(errors) {
|
|
179
|
+
clearOutdatedErrors();
|
|
180
|
+
isFirstCompilation = false;
|
|
181
|
+
hasCompileErrors = true;
|
|
182
|
+
// "Massage" webpack messages.
|
|
183
|
+
var formatted = formatWebpackMessages_1.default({
|
|
184
|
+
errors: errors,
|
|
185
|
+
warnings: []
|
|
186
|
+
});
|
|
187
|
+
// Only show the first error.
|
|
188
|
+
ErrorOverlay.reportBuildError(formatted.errors[0]);
|
|
189
|
+
// Also log them to the console.
|
|
190
|
+
if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
|
191
|
+
for (var i = 0; i < formatted.errors.length; i++) {
|
|
192
|
+
console.error(strip_ansi_1.default(formatted.errors[i]));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function tryDismissErrorOverlay() {
|
|
197
|
+
if (!hasCompileErrors) {
|
|
198
|
+
ErrorOverlay.dismissBuildError();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// There is a newer version of the code available.
|
|
202
|
+
function handleAvailableHash(hash) {
|
|
203
|
+
// Update last known compilation hash.
|
|
204
|
+
mostRecentCompilationHash = hash;
|
|
205
|
+
}
|
|
206
|
+
// Handle messages from the server.
|
|
207
|
+
function processMessage(e) {
|
|
208
|
+
const obj = JSON.parse(e.data);
|
|
209
|
+
switch (obj.action) {
|
|
210
|
+
case 'building': {
|
|
211
|
+
console.log('[Fast Refresh] bundle ' +
|
|
212
|
+
(obj.name ? "'" + obj.name + "' " : '') +
|
|
213
|
+
'rebuilding');
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case 'built':
|
|
217
|
+
case 'sync': {
|
|
218
|
+
if (obj.hash) {
|
|
219
|
+
handleAvailableHash(obj.hash);
|
|
220
|
+
}
|
|
221
|
+
const { errors, warnings } = obj;
|
|
222
|
+
const hasErrors = Boolean(errors && errors.length);
|
|
223
|
+
if (hasErrors) {
|
|
224
|
+
return handleErrors(errors);
|
|
225
|
+
}
|
|
226
|
+
const hasWarnings = Boolean(warnings && warnings.length);
|
|
227
|
+
if (hasWarnings) {
|
|
228
|
+
return handleWarnings(warnings);
|
|
229
|
+
}
|
|
230
|
+
handleSuccess();
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case 'warnings':
|
|
234
|
+
handleWarnings(obj.data);
|
|
235
|
+
break;
|
|
236
|
+
case 'errors':
|
|
237
|
+
handleErrors(obj.data);
|
|
238
|
+
break;
|
|
239
|
+
default: {
|
|
240
|
+
if (customHmrEventHandler) {
|
|
241
|
+
customHmrEventHandler(obj);
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Is there a newer version of this code available?
|
|
249
|
+
function isUpdateAvailable() {
|
|
250
|
+
/* globals __webpack_hash__ */
|
|
251
|
+
// __webpack_hash__ is the hash of the current compilation.
|
|
252
|
+
// It's a global variable injected by Webpack.
|
|
253
|
+
return mostRecentCompilationHash !== __webpack_hash__;
|
|
254
|
+
}
|
|
255
|
+
// Webpack disallows updates in other states.
|
|
256
|
+
function canApplyUpdates() {
|
|
257
|
+
return module.hot.status() === 'idle';
|
|
258
|
+
}
|
|
259
|
+
// Attempt to update code on the fly, fall back to a hard reload.
|
|
260
|
+
function tryApplyUpdates(onHotUpdateSuccess) {
|
|
261
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
+
if (!module.hot) {
|
|
263
|
+
// HotModuleReplacementPlugin is not in Webpack configuration.
|
|
264
|
+
console.error('HotModuleReplacementPlugin is not in Webpack configuration.');
|
|
265
|
+
// window.location.reload();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (!isUpdateAvailable() || !canApplyUpdates()) {
|
|
269
|
+
ErrorOverlay.dismissBuildError();
|
|
270
|
+
// HMR failed, need to refresh
|
|
271
|
+
if (module.hot.status() === 'fail') {
|
|
272
|
+
window.location.reload();
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
function handleApplyUpdates(err, updatedModules) {
|
|
277
|
+
const needForcedReload = err || !updatedModules || hadRuntimeError;
|
|
278
|
+
if (needForcedReload) {
|
|
279
|
+
ErrorOverlay.reportRuntimeError(err);
|
|
280
|
+
if (hadRuntimeError) {
|
|
281
|
+
hadRuntimeError = false;
|
|
282
|
+
window.location.reload();
|
|
283
|
+
}
|
|
284
|
+
hadRuntimeError = true;
|
|
285
|
+
}
|
|
286
|
+
if (typeof onHotUpdateSuccess === 'function') {
|
|
287
|
+
// Maybe we want to do something.
|
|
288
|
+
onHotUpdateSuccess();
|
|
289
|
+
}
|
|
290
|
+
if (isUpdateAvailable()) {
|
|
291
|
+
// While we were updating, there was a new update! Do it again.
|
|
292
|
+
tryApplyUpdates();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// https://webpack.js.org/api/hot-module-replacement/#check
|
|
296
|
+
try {
|
|
297
|
+
const updatedModules = yield module.hot.check(/* autoApply */ true);
|
|
298
|
+
handleApplyUpdates(null, updatedModules);
|
|
299
|
+
}
|
|
300
|
+
catch (err) {
|
|
301
|
+
handleApplyUpdates(err, null);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
18
|
+
result["default"] = mod;
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
const chalk_1 = __importDefault(require("@shuvi/utils/lib/chalk"));
|
|
23
|
+
const resolve_1 = __importDefault(require("@shuvi/utils/lib/resolve"));
|
|
24
|
+
const recursiveReaddir_1 = require("@shuvi/utils/lib/recursiveReaddir");
|
|
25
|
+
const fs_1 = __importDefault(require("fs"));
|
|
26
|
+
const os_1 = __importDefault(require("os"));
|
|
27
|
+
const path_1 = __importDefault(require("path"));
|
|
28
|
+
const util_1 = require("util");
|
|
29
|
+
const fileExists = util_1.promisify(fs_1.default.exists);
|
|
30
|
+
const writeFile = util_1.promisify(fs_1.default.writeFile);
|
|
31
|
+
const readFile = util_1.promisify(fs_1.default.readFile);
|
|
32
|
+
function writeJson(fileName, object) {
|
|
33
|
+
return writeFile(fileName, JSON.stringify(object, null, 2).replace(/\n/g, os_1.default.EOL) + os_1.default.EOL);
|
|
34
|
+
}
|
|
35
|
+
function hasTypeScript(dir) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const typescriptFiles = yield recursiveReaddir_1.recursiveReadDir(dir, {
|
|
38
|
+
filter: /.*\.(ts|tsx)$/,
|
|
39
|
+
ignore: /(node_modules|.*\.d\.ts)/
|
|
40
|
+
});
|
|
41
|
+
return typescriptFiles.length > 0;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function checkDependencies({ dir, isYarn }) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const requiredPackages = [
|
|
47
|
+
{ file: 'typescript', pkg: 'typescript' },
|
|
48
|
+
{ file: '@types/react/index.d.ts', pkg: '@types/react' },
|
|
49
|
+
{ file: '@types/node/index.d.ts', pkg: '@types/node' }
|
|
50
|
+
];
|
|
51
|
+
let resolutions = new Map();
|
|
52
|
+
const missingPackages = requiredPackages.filter(p => {
|
|
53
|
+
try {
|
|
54
|
+
resolutions.set(p.pkg, resolve_1.default.sync(p.file, { basedir: `${dir}/` }));
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
catch (_) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (missingPackages.length < 1) {
|
|
62
|
+
return resolutions.get('typescript');
|
|
63
|
+
}
|
|
64
|
+
const packagesHuman = missingPackages
|
|
65
|
+
.map((p, index, { length }) => (index > 0
|
|
66
|
+
? index === length - 1
|
|
67
|
+
? length > 2
|
|
68
|
+
? ', and '
|
|
69
|
+
: ' and '
|
|
70
|
+
: ', '
|
|
71
|
+
: '') + p.pkg)
|
|
72
|
+
.join('');
|
|
73
|
+
const packagesCli = missingPackages.map(p => p.pkg).join(' ');
|
|
74
|
+
console.error(chalk_1.default.bold.red(`It looks like you're trying to use TypeScript but do not have the required package(s) installed.`));
|
|
75
|
+
console.error();
|
|
76
|
+
console.error(chalk_1.default.bold(`Please install ${chalk_1.default.bold(packagesHuman)} by running:`));
|
|
77
|
+
console.error();
|
|
78
|
+
console.error(`\t${chalk_1.default.bold.cyan((isYarn ? 'yarn add --dev' : 'npm install --save-dev') + ' ' + packagesCli)}`);
|
|
79
|
+
console.error();
|
|
80
|
+
console.error(chalk_1.default.bold('If you are not trying to use TypeScript, please remove the ' +
|
|
81
|
+
chalk_1.default.cyan('tsconfig.json') +
|
|
82
|
+
' file from your package root (and any TypeScript files).'));
|
|
83
|
+
console.error();
|
|
84
|
+
process.exit(1);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function verifyTypeScriptSetup({ projectDir, srcDir, onTsConfig }) {
|
|
88
|
+
var _a;
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const tsConfigPath = path_1.default.join(projectDir, 'tsconfig.json');
|
|
91
|
+
const yarnLockFile = path_1.default.join(projectDir, 'yarn.lock');
|
|
92
|
+
const hasTsConfig = yield fileExists(tsConfigPath);
|
|
93
|
+
const isYarn = yield fileExists(yarnLockFile);
|
|
94
|
+
let firstTimeSetup = false;
|
|
95
|
+
if (hasTsConfig) {
|
|
96
|
+
const tsConfig = yield readFile(tsConfigPath, 'utf8').then(val => val.trim());
|
|
97
|
+
firstTimeSetup = tsConfig === '' || tsConfig === '{}';
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const hasTypeScriptFiles = yield hasTypeScript(srcDir);
|
|
101
|
+
if (hasTypeScriptFiles) {
|
|
102
|
+
firstTimeSetup = true;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const tsPath = yield checkDependencies({ dir: projectDir, isYarn });
|
|
109
|
+
const ts = (yield Promise.resolve().then(() => __importStar(require(tsPath))));
|
|
110
|
+
const compilerOptions = {
|
|
111
|
+
// These are suggested values and will be set when not present in the
|
|
112
|
+
// tsconfig.json
|
|
113
|
+
// 'parsedValue' matches the output value from ts.parseJsonConfigFileContent()
|
|
114
|
+
target: {
|
|
115
|
+
parsedValue: ts.ScriptTarget.ES5,
|
|
116
|
+
suggested: 'es5'
|
|
117
|
+
},
|
|
118
|
+
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
|
|
119
|
+
allowJs: { suggested: true },
|
|
120
|
+
skipLibCheck: { suggested: true },
|
|
121
|
+
strict: { suggested: false },
|
|
122
|
+
forceConsistentCasingInFileNames: { suggested: true },
|
|
123
|
+
noEmit: { suggested: true },
|
|
124
|
+
// These values are required and cannot be changed by the user
|
|
125
|
+
// Keep this in sync with the webpack config
|
|
126
|
+
esModuleInterop: {
|
|
127
|
+
value: true,
|
|
128
|
+
reason: 'requirement for babel'
|
|
129
|
+
},
|
|
130
|
+
module: {
|
|
131
|
+
parsedValue: ts.ModuleKind.ESNext,
|
|
132
|
+
value: 'esnext',
|
|
133
|
+
reason: 'for dynamic import() support'
|
|
134
|
+
},
|
|
135
|
+
moduleResolution: {
|
|
136
|
+
parsedValue: ts.ModuleResolutionKind.NodeJs,
|
|
137
|
+
value: 'node',
|
|
138
|
+
reason: 'to match webpack resolution'
|
|
139
|
+
},
|
|
140
|
+
resolveJsonModule: { value: true },
|
|
141
|
+
isolatedModules: {
|
|
142
|
+
value: true,
|
|
143
|
+
reason: 'requirement for babel'
|
|
144
|
+
},
|
|
145
|
+
jsx: { parsedValue: ts.JsxEmit.Preserve, value: 'preserve' }
|
|
146
|
+
};
|
|
147
|
+
const formatDiagnosticHost = {
|
|
148
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
149
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
150
|
+
getNewLine: () => os_1.default.EOL
|
|
151
|
+
};
|
|
152
|
+
if (firstTimeSetup) {
|
|
153
|
+
console.log(chalk_1.default.yellow(`We detected TypeScript in your project and created a ${chalk_1.default.bold('tsconfig.json')} file for you.`));
|
|
154
|
+
console.log();
|
|
155
|
+
yield writeJson(tsConfigPath, {});
|
|
156
|
+
}
|
|
157
|
+
const messages = [];
|
|
158
|
+
let appTsConfig;
|
|
159
|
+
let parsedTsConfig;
|
|
160
|
+
let parsedCompilerOptions;
|
|
161
|
+
try {
|
|
162
|
+
const { config: readTsConfig, error } = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
|
|
163
|
+
if (error) {
|
|
164
|
+
throw new Error(ts.formatDiagnostic(error, formatDiagnosticHost));
|
|
165
|
+
}
|
|
166
|
+
appTsConfig = readTsConfig;
|
|
167
|
+
// Get TS to parse and resolve any "extends"
|
|
168
|
+
// Calling this function also mutates the tsconfig, adding in "include" and
|
|
169
|
+
// "exclude", but the compilerOptions remain untouched
|
|
170
|
+
parsedTsConfig = JSON.parse(JSON.stringify(readTsConfig));
|
|
171
|
+
const result = ts.parseJsonConfigFileContent(parsedTsConfig, ts.sys, path_1.default.dirname(tsConfigPath));
|
|
172
|
+
if (result.errors) {
|
|
173
|
+
result.errors = result.errors.filter(({ code }) =>
|
|
174
|
+
// No inputs were found in config file
|
|
175
|
+
code !== 18003);
|
|
176
|
+
}
|
|
177
|
+
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
|
|
178
|
+
throw new Error(ts.formatDiagnostic(result.errors[0], formatDiagnosticHost));
|
|
179
|
+
}
|
|
180
|
+
parsedCompilerOptions = result.options;
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (e && e.name === 'SyntaxError') {
|
|
184
|
+
console.error(chalk_1.default.red.bold('Could not parse', chalk_1.default.cyan('tsconfig.json') + '.', 'Please make sure it contains syntactically correct JSON.'));
|
|
185
|
+
}
|
|
186
|
+
console.info((e === null || e === void 0 ? void 0 : e.message) ? `${e.message}` : '');
|
|
187
|
+
process.exit(1);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (appTsConfig.compilerOptions == null) {
|
|
191
|
+
appTsConfig.compilerOptions = {};
|
|
192
|
+
firstTimeSetup = true;
|
|
193
|
+
}
|
|
194
|
+
for (const option of Object.keys(compilerOptions)) {
|
|
195
|
+
const { parsedValue, value, suggested, reason } = compilerOptions[option];
|
|
196
|
+
const valueToCheck = parsedValue === undefined ? value : parsedValue;
|
|
197
|
+
const coloredOption = chalk_1.default.cyan('compilerOptions.' + option);
|
|
198
|
+
if (suggested != null) {
|
|
199
|
+
if (parsedCompilerOptions[option] === undefined) {
|
|
200
|
+
appTsConfig.compilerOptions[option] = suggested;
|
|
201
|
+
messages.push(`${coloredOption} to be ${chalk_1.default.bold('suggested')} value: ${chalk_1.default.cyan.bold(suggested)} (this can be changed)`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (parsedCompilerOptions[option] !== valueToCheck) {
|
|
205
|
+
appTsConfig.compilerOptions[option] = value;
|
|
206
|
+
messages.push(`${coloredOption} ${chalk_1.default.bold(valueToCheck == null ? 'must not' : 'must')} be ${valueToCheck == null ? 'set' : chalk_1.default.cyan.bold(value)}` +
|
|
207
|
+
(reason != null ? ` (${reason})` : ''));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (onTsConfig) {
|
|
211
|
+
onTsConfig(appTsConfig, parsedTsConfig, parsedCompilerOptions);
|
|
212
|
+
}
|
|
213
|
+
if (messages.length > 0) {
|
|
214
|
+
if (firstTimeSetup) {
|
|
215
|
+
console.info(chalk_1.default.bold('Your', chalk_1.default.cyan('tsconfig.json'), 'has been populated with default values.'));
|
|
216
|
+
console.info();
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
console.warn(chalk_1.default.bold('The following changes are being made to your', chalk_1.default.cyan('tsconfig.json'), 'file:'));
|
|
220
|
+
messages.forEach(message => {
|
|
221
|
+
console.warn(' - ' + message);
|
|
222
|
+
});
|
|
223
|
+
console.warn();
|
|
224
|
+
}
|
|
225
|
+
yield writeJson(tsConfigPath, appTsConfig);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
exports.verifyTypeScriptSetup = verifyTypeScriptSetup;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import WebpackChain from 'webpack-chain';
|
|
2
|
+
export interface BaseOptions {
|
|
3
|
+
dev: boolean;
|
|
4
|
+
name: string;
|
|
5
|
+
projectRoot: string;
|
|
6
|
+
srcDirs: string[];
|
|
7
|
+
mediaFilename: string;
|
|
8
|
+
buildManifestFilename: string;
|
|
9
|
+
target?: string;
|
|
10
|
+
publicPath?: string;
|
|
11
|
+
env?: {
|
|
12
|
+
[x: string]: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export { WebpackChain };
|
|
16
|
+
export declare function baseWebpackChain({ dev, projectRoot, srcDirs, mediaFilename, name, buildManifestFilename, publicPath, env }: BaseOptions): WebpackChain;
|