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

Commit

Permalink
feat(*): play well with ExtractTextPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Panferov committed May 30, 2015
1 parent e17cf79 commit 33807f1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 27 deletions.
48 changes: 35 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/extract-text/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
2 changes: 2 additions & 0 deletions examples/extract-text/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

require('./index.css');
21 changes: 21 additions & 0 deletions examples/extract-text/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "tsjsx-example",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"node-libs-browser": "^0.5.0",
"webpack": "^1.9.4"
},
"devDependencies": {
"css-loader": "^0.14.4",
"extract-text-webpack-plugin": "^0.8.1",
"style-loader": "^0.12.3",
"webpack": "^1.9.10"
}
}
31 changes: 31 additions & 0 deletions examples/extract-text/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var ExtractTextPlugin = require("extract-text-webpack-plugin")

module.exports = {
resolve: {
extensions: ['', '.ts', '.js']
},
devtool: 'source-map',
plugins: [
//new ExtractTextPlugin("[name].css")
],
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript-loader'
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
//loader: ExtractTextPlugin.extract("style-loader", "css-loader")
}
]
},
entry: {
index: ['./index.ts']
},
output: {
path: './dist',
filename: './[name].js'
}
};
50 changes: 37 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../node_modules/typescript/bin/typescriptServices.d.ts" />
/// <reference path="../typings/tsd.d.ts" />
/// <reference path='../node_modules/typescript/bin/typescriptServices.d.ts' />
/// <reference path='../typings/tsd.d.ts' />

import Promise = require("bluebird");
import Promise = require('bluebird');
import path = require('path');
import fs = require('fs');
import _ = require('lodash');
Expand Down Expand Up @@ -36,16 +36,40 @@ interface CompilerInstance {
options: CompilerOptions;
}

function getRootCompiler(compiler) {
if (compiler.parentCompilation) {
return getRootCompiler(compiler.parentCompilation.compiler)
} else {
return compiler;
}
}

function getInstanceStore(compiler): {[key:string]: CompilerInstance} {
var store = getRootCompiler(compiler)._tsInstances;
if (store) {
return store
} else {
throw new Error('Can not resolve instance store')
}
}

function ensureInstanceStore(compiler) {
getRootCompiler(compiler)._tsInstances = {};
}

function resolveInstance(compiler, instanceName) {
return getInstanceStore(compiler)[instanceName];
}

/**
* Creates compiler instance
*/
function ensureInstance(webpack: WebPack, options: CompilerOptions, instanceName: string): CompilerInstance {
if (typeof webpack._compiler._tsInstances === 'undefined') {
webpack._compiler._tsInstances = {};
}
ensureInstanceStore(webpack._compiler);

if (typeof webpack._compiler._tsInstances[instanceName] !== "undefined") {
return webpack._compiler._tsInstances[instanceName];
var exInstance = resolveInstance(webpack._compiler, instanceName);
if (exInstance) {
return exInstance
}

var tsFlow = Promise.resolve();
Expand Down Expand Up @@ -101,9 +125,9 @@ function ensureInstance(webpack: WebPack, options: CompilerOptions, instanceName

var compiler = (<any>webpack._compiler);

compiler.plugin("watch-run", (watching, callback) => {
compiler.plugin('watch-run', (watching, callback) => {
var resolver = <deps.Resolver>Promise.promisify(watching.compiler.resolvers.normal.resolve);
var instance: CompilerInstance = watching.compiler._tsInstances[instanceName];
var instance: CompilerInstance = resolveInstance(watching.compiler, instanceName);
var state = instance.tsState;
var mtimes = watching.compiler.watchFileSystem.watcher.mtimes;
var changedFiles = Object.keys(mtimes);
Expand All @@ -125,8 +149,8 @@ function ensureInstance(webpack: WebPack, options: CompilerOptions, instanceName
.catch((err) => console.error(err))
});

compiler.plugin("after-compile", function(compilation, callback) {
var instance: CompilerInstance = compilation.compiler._tsInstances[instanceName];
compiler.plugin('after-compile', function(compilation, callback) {
var instance: CompilerInstance = resolveInstance(compilation.compiler, instanceName);
var state = instance.tsState;
var diagnostics = state.ts.getPreEmitDiagnostics(state.program);
var emitError = (err) => {
Expand All @@ -149,7 +173,7 @@ function ensureInstance(webpack: WebPack, options: CompilerOptions, instanceName
callback();
});

return webpack._compiler._tsInstances[instanceName] = {
return getInstanceStore(webpack._compiler)[instanceName] = {
tsFlow,
tsState,
compiledFiles: {},
Expand Down

0 comments on commit 33807f1

Please sign in to comment.