Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
chore: stub out project and make 1 test work
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbcross committed Dec 24, 2016
0 parents commit 18b8e31
Show file tree
Hide file tree
Showing 9 changed files with 2,505 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
dist/
node_modules/

1 change: 1 addition & 0 deletions index.d.ts
@@ -0,0 +1 @@
export * from './dist/critical';
5 changes: 5 additions & 0 deletions index.js
@@ -0,0 +1,5 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require('./dist/critical'));
33 changes: 33 additions & 0 deletions package.json
@@ -0,0 +1,33 @@
{
"name": "webpack-plugin-critical",
"version": "1.0.0",
"description": "Webpack plugin for the critical CSS inliner library.",
"main": "index.js",
"scripts": {
"test": "tsc && jasmine"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nrwl/webpack-plugin-critical.git"
},
"keywords": [
"webpack",
"critical"
],
"author": "jeffbcross",
"license": "MIT",
"bugs": {
"url": "https://github.com/nrwl/webpack-plugin-critical/issues"
},
"homepage": "https://github.com/nrwl/webpack-plugin-critical#readme",
"devDependencies": {
"jasmine": "^2.5.2",
"typescript": "^2.1.4"
},
"dependencies": {
"@types/jasmine": "^2.5.38",
"@types/node": "^6.0.53",
"critical": "^0.8.1",
"webpack-util-cleantmp": "^1.0.0"
}
}
11 changes: 11 additions & 0 deletions spec/support/jasmine.json
@@ -0,0 +1,11 @@
{
"spec_dir": "dist",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
30 changes: 30 additions & 0 deletions src/critical.spec.ts
@@ -0,0 +1,30 @@
import { CriticalPlugin } from './critical';

const mockCompilation = {
assets: {
'foo.json': {
source: () => '{"foo": "bar"}',
size: () => 14
}
}
};

const mockCompiler = {
plugin: (lifecycle) => {

}
}

describe('CriticalPlugin', () => {
describe('apply', () => {
it('should register for emit lifecycle', () => {
let plugin = new CriticalPlugin();
let compiler = jasmine.createSpyObj('compiler', ['plugin']);
plugin.apply(compiler);

let args = (<jasmine.Spy>compiler.plugin).calls.first().args;
expect(args[0]).toBe('emit');
expect(typeof args[1]).toBe('function');
});
});
});
52 changes: 52 additions & 0 deletions src/critical.ts
@@ -0,0 +1,52 @@
import { resolve } from 'path';
const critical = require('critical');
import { cleantmp } from 'webpack-util-cleantmp';

export interface CriticalPluginOptions {
base?: string;
inline?: boolean;
minify?: boolean;
extract?: boolean;
timeout?: number
}

export class CriticalPlugin {
constructor(public options: CriticalPluginOptions = {}) {
const opts = {...this.options};
this.options = {...{
base: '',
inline: true,
minify: true,
extract: true,
timeout: 30000
}, ...options};
}


emit(compilation, callback) {
const options = this.options;
const subscription = cleantmp({
prefix: 'criticalcss',
globToDisk: '**/*.+(css|html)',
globFromDisk: '**/*.+(css|html)',
assets: compilation.assets
})
.subscribe((tmp) => {
const opts = {...options, ...{
base: resolve(tmp, options.base)
}};

critical.generate(opts, (err, output) => {
// TODO: Make recursive and start at root of tmp
subscription.unsubscribe();
callback(err);
});
})
}

apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
this.emit(compilation, callback);
});
}
}
24 changes: 24 additions & 0 deletions tsconfig.json
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"sourceRoot": "src",
"lib": [
"es2015",
"dom"
],
"typeRoots": [
"node_modules/@types"
]
},
"files": [
"src/critical.ts",
"src/critical.spec.ts"
]
}

0 comments on commit 18b8e31

Please sign in to comment.