Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 76d0a54

Browse files
committed
feat(*): loader plugins (e.g. docscript)
1 parent bd1e0b0 commit 76d0a54

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

.node-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

src/checker-runtime.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ICompilerOptions, ICompilerInfo, IFile } from './host';
2+
import { LoaderPlugin, LoaderPluginDef } from './instance';
23
import makeResolver from './resolver';
34
import * as colors from 'colors';
45
import * as path from 'path';
@@ -20,6 +21,7 @@ export interface IInitPayload {
2021
compilerOptions: ICompilerOptions;
2122
compilerInfo: ICompilerInfo;
2223
webpackOptions: any;
24+
plugins: LoaderPluginDef[];
2325
}
2426

2527
export interface ICompilePayload {
@@ -37,6 +39,8 @@ export interface IEnv {
3739
resolutionCache?: {[fileName: string]: ts.ResolvedModule};
3840
program?: ts.Program;
3941
service?: ts.LanguageService;
42+
plugins?: LoaderPluginDef[];
43+
initedPlugins?: LoaderPlugin[];
4044
}
4145

4246
export interface SyncResolver {
@@ -185,6 +189,10 @@ function processInit(payload: IInitPayload) {
185189
env.webpackOptions = payload.webpackOptions;
186190
env.host = new Host();
187191
env.service = env.compiler.createLanguageService(env.host, env.compiler.createDocumentRegistry());
192+
env.plugins = payload.plugins;
193+
env.initedPlugins = env.plugins.map(plugin => {
194+
return require(plugin.file)(plugin.options);
195+
});
188196
}
189197

190198
function processCompile(payload: ICompilePayload) {
@@ -225,6 +233,10 @@ function processCompile(payload: ICompilePayload) {
225233
}
226234
}
227235

236+
env.initedPlugins.forEach(plugin => {
237+
plugin.processProgram(program);
238+
});
239+
228240
process.send({
229241
messageType: 'progress',
230242
payload: {

src/checker.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import * as _ from 'lodash';
22
import * as childProcess from 'child_process';
33
import * as path from 'path';
44
import { ICompilerInfo, ICompilerOptions } from './host';
5+
import { LoaderPluginDef } from './instance';
56

67
interface ChildProcess extends childProcess.ChildProcess {
78
inProgress?: boolean;
89
compilerInfo?: ICompilerInfo;
910
compilerOptions?: ICompilerOptions;
1011
webpackOptions?: any;
12+
plugins?: LoaderPluginDef[];
1113
}
1214

1315
export function createChecker(
1416
compilerInfo: ICompilerInfo,
1517
compilerOptions: ICompilerOptions,
16-
webpackOptions: any
18+
webpackOptions: any,
19+
plugins: LoaderPluginDef[]
1720
): ChildProcess {
1821
let checker: ChildProcess = childProcess.fork(path.join(__dirname, 'checker-runtime.js'));
1922

@@ -22,7 +25,8 @@ export function createChecker(
2225
payload: {
2326
compilerInfo: _.omit(compilerInfo, 'tsImpl'),
2427
compilerOptions,
25-
webpackOptions
28+
webpackOptions,
29+
plugins
2630
}
2731
}, null);
2832

@@ -42,7 +46,12 @@ export function createChecker(
4246
export function resetChecker(checker: ChildProcess) {
4347
if (checker.inProgress) {
4448
checker.kill('SIGKILL');
45-
return createChecker(checker.compilerInfo, checker.compilerOptions, checker.webpackOptions);
49+
return createChecker(
50+
checker.compilerInfo,
51+
checker.compilerOptions,
52+
checker.webpackOptions,
53+
checker.plugins
54+
);
4655
} else {
4756
return checker;
4857
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
174174
try {
175175
callback(null, resultText, resultSourceMap);
176176
} catch (e) {
177-
console.error('Error in bail mode:', e);
177+
console.error('Error in bail mode:', e, e.stack.join('\n'));
178178
process.exit(1);
179179
}
180180
} catch (err) {

src/instance.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import makeResolver from './resolver';
1313

1414
let pkg = require('../package.json');
1515

16+
export interface LoaderPlugin {
17+
processProgram?: (program: ts.Program) => void;
18+
}
19+
20+
export interface LoaderPluginDef {
21+
file: string;
22+
options: any;
23+
}
24+
1625
export interface ICompilerInstance {
1726
tsFlow: Promise<any>;
1827
tsState: State;
@@ -22,6 +31,8 @@ export interface ICompilerInstance {
2231
externalsInvoked: boolean;
2332
checker: any;
2433
cacheIdentifier: any;
34+
plugins: LoaderPluginDef[];
35+
initedPlugins: LoaderPlugin[];
2536
}
2637

2738
interface ICompiler {
@@ -44,6 +55,11 @@ export interface IWebPack {
4455
resolve: () => void;
4556
addDependency: (dep: string) => void;
4657
clearDependencies: () => void;
58+
options: {
59+
atl?: {
60+
plugins: LoaderPluginDef[]
61+
}
62+
};
4763
}
4864

4965
function getRootCompiler(compiler) {
@@ -214,6 +230,20 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
214230

215231
let webpackOptions = _.pick(webpack._compiler.options, 'resolve');
216232

233+
let atlOptions = webpack.options.atl;
234+
let plugins: LoaderPluginDef[] = [];
235+
236+
if (atlOptions && atlOptions.plugins) {
237+
plugins = atlOptions.plugins;
238+
}
239+
240+
let initedPlugins = [];
241+
if (!forkChecker) {
242+
initedPlugins = plugins.map(plugin => {
243+
return require(plugin.file)(plugin.options);
244+
});
245+
}
246+
217247
return getInstanceStore(webpack._compiler)[instanceName] = {
218248
tsFlow,
219249
tsState,
@@ -222,9 +252,11 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
222252
options,
223253
externalsInvoked: false,
224254
checker: forkChecker
225-
? createChecker(compilerInfo, options, webpackOptions)
255+
? createChecker(compilerInfo, options, webpackOptions, plugins)
226256
: null,
227-
cacheIdentifier
257+
cacheIdentifier,
258+
plugins,
259+
initedPlugins
228260
};
229261
}
230262

@@ -298,6 +330,10 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) {
298330

299331
let errors = formatErrors(instanceName, diagnostics);
300332
errors.forEach(emitError);
333+
334+
instance.initedPlugins.forEach(plugin => {
335+
plugin.processProgram(state.program);
336+
});
301337
}
302338

303339
let phantomImports = [];

0 commit comments

Comments
 (0)