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

Commit

Permalink
feat(*): loader plugins (e.g. docscript)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Feb 6, 2016
1 parent bd1e0b0 commit 76d0a54
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
12 changes: 12 additions & 0 deletions src/checker-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ICompilerOptions, ICompilerInfo, IFile } from './host';
import { LoaderPlugin, LoaderPluginDef } from './instance';
import makeResolver from './resolver';
import * as colors from 'colors';
import * as path from 'path';
Expand All @@ -20,6 +21,7 @@ export interface IInitPayload {
compilerOptions: ICompilerOptions;
compilerInfo: ICompilerInfo;
webpackOptions: any;
plugins: LoaderPluginDef[];
}

export interface ICompilePayload {
Expand All @@ -37,6 +39,8 @@ export interface IEnv {
resolutionCache?: {[fileName: string]: ts.ResolvedModule};
program?: ts.Program;
service?: ts.LanguageService;
plugins?: LoaderPluginDef[];
initedPlugins?: LoaderPlugin[];
}

export interface SyncResolver {
Expand Down Expand Up @@ -185,6 +189,10 @@ function processInit(payload: IInitPayload) {
env.webpackOptions = payload.webpackOptions;
env.host = new Host();
env.service = env.compiler.createLanguageService(env.host, env.compiler.createDocumentRegistry());
env.plugins = payload.plugins;
env.initedPlugins = env.plugins.map(plugin => {
return require(plugin.file)(plugin.options);
});
}

function processCompile(payload: ICompilePayload) {
Expand Down Expand Up @@ -225,6 +233,10 @@ function processCompile(payload: ICompilePayload) {
}
}

env.initedPlugins.forEach(plugin => {
plugin.processProgram(program);
});

process.send({
messageType: 'progress',
payload: {
Expand Down
15 changes: 12 additions & 3 deletions src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import * as _ from 'lodash';
import * as childProcess from 'child_process';
import * as path from 'path';
import { ICompilerInfo, ICompilerOptions } from './host';
import { LoaderPluginDef } from './instance';

interface ChildProcess extends childProcess.ChildProcess {
inProgress?: boolean;
compilerInfo?: ICompilerInfo;
compilerOptions?: ICompilerOptions;
webpackOptions?: any;
plugins?: LoaderPluginDef[];
}

export function createChecker(
compilerInfo: ICompilerInfo,
compilerOptions: ICompilerOptions,
webpackOptions: any
webpackOptions: any,
plugins: LoaderPluginDef[]
): ChildProcess {
let checker: ChildProcess = childProcess.fork(path.join(__dirname, 'checker-runtime.js'));

Expand All @@ -22,7 +25,8 @@ export function createChecker(
payload: {
compilerInfo: _.omit(compilerInfo, 'tsImpl'),
compilerOptions,
webpackOptions
webpackOptions,
plugins
}
}, null);

Expand All @@ -42,7 +46,12 @@ export function createChecker(
export function resetChecker(checker: ChildProcess) {
if (checker.inProgress) {
checker.kill('SIGKILL');
return createChecker(checker.compilerInfo, checker.compilerOptions, checker.webpackOptions);
return createChecker(
checker.compilerInfo,
checker.compilerOptions,
checker.webpackOptions,
checker.plugins
);
} else {
return checker;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
try {
callback(null, resultText, resultSourceMap);
} catch (e) {
console.error('Error in bail mode:', e);
console.error('Error in bail mode:', e, e.stack.join('\n'));
process.exit(1);
}
} catch (err) {
Expand Down
40 changes: 38 additions & 2 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import makeResolver from './resolver';

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

export interface LoaderPlugin {
processProgram?: (program: ts.Program) => void;
}

export interface LoaderPluginDef {
file: string;
options: any;
}

export interface ICompilerInstance {
tsFlow: Promise<any>;
tsState: State;
Expand All @@ -22,6 +31,8 @@ export interface ICompilerInstance {
externalsInvoked: boolean;
checker: any;
cacheIdentifier: any;
plugins: LoaderPluginDef[];
initedPlugins: LoaderPlugin[];
}

interface ICompiler {
Expand All @@ -44,6 +55,11 @@ export interface IWebPack {
resolve: () => void;
addDependency: (dep: string) => void;
clearDependencies: () => void;
options: {
atl?: {
plugins: LoaderPluginDef[]
}
};
}

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

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

let atlOptions = webpack.options.atl;
let plugins: LoaderPluginDef[] = [];

if (atlOptions && atlOptions.plugins) {
plugins = atlOptions.plugins;
}

let initedPlugins = [];
if (!forkChecker) {
initedPlugins = plugins.map(plugin => {
return require(plugin.file)(plugin.options);
});
}

return getInstanceStore(webpack._compiler)[instanceName] = {
tsFlow,
tsState,
Expand All @@ -222,9 +252,11 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
options,
externalsInvoked: false,
checker: forkChecker
? createChecker(compilerInfo, options, webpackOptions)
? createChecker(compilerInfo, options, webpackOptions, plugins)
: null,
cacheIdentifier
cacheIdentifier,
plugins,
initedPlugins
};
}

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

let errors = formatErrors(instanceName, diagnostics);
errors.forEach(emitError);

instance.initedPlugins.forEach(plugin => {
plugin.processProgram(state.program);
});
}

let phantomImports = [];
Expand Down

0 comments on commit 76d0a54

Please sign in to comment.