Skip to content

Commit 864f378

Browse files
committed
Merge pull request #55 from carrot/static
static webpack plugin
2 parents e3d8dd1 + 31b6f75 commit 864f378

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

lib/config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import path from 'path'
22
import Joi from 'joi'
33
import JadePlugin from './plugins/jade_plugin'
44
import CSSPlugin from './plugins/css_plugin'
5+
import StaticPlugin from './plugins/static_plugin'
56
import micromatch from 'micromatch'
67
import union from 'lodash.union'
78
import _eval from 'require-from-string'
89
import {transformFileSync} from 'babel-core'
910
import postcssImport from 'postcss-import'
1011
import BrowserSyncPlugin from 'browser-sync-webpack-plugin'
12+
import binaryExtensions from 'binary-extensions'
13+
import imageExtensions from 'image-extensions'
1114

1215
export default class Config {
1316
constructor (opts) {
@@ -29,7 +32,8 @@ export default class Config {
2932
matchers: Joi.object().default().keys({
3033
jade: Joi.string().default('**/*.jade'),
3134
css: Joi.string().default('**/*.css'),
32-
js: Joi.string().default('**/*.js')
35+
js: Joi.string().default('**/*.js'),
36+
static: Joi.string().default(`**/*.{${union(binaryExtensions, imageExtensions).join(',')}}`)
3337
}),
3438
postcss: Joi.object().default().keys({
3539
plugins: Joi.array().default([]),
@@ -130,10 +134,13 @@ export default class Config {
130134
loaders: [
131135
{ test: mmToRe(opts.matchers.css), exclude: opts.ignore.map(mmToRe), loader: `css!postcss?${JSON.stringify(opts.postcss.options)}` },
132136
{ test: mmToRe(opts.matchers.js), exclude: opts.ignore.map(mmToRe), loader: 'babel' },
133-
{ test: mmToRe(opts.matchers.jade), exclude: opts.ignore.map(mmToRe), loader: 'jade', query: { pretty: true, locals: opts.locals } }
137+
{ test: mmToRe(opts.matchers.jade), exclude: opts.ignore.map(mmToRe), loader: 'jade', query: { pretty: true, locals: opts.locals } },
138+
{ test: mmToRe(opts.matchers.static), exclude: opts.ignore.map(mmToRe), loader: 'file', query: { dumpDirs: opts.dumpDirs } }
134139
]
135140
}
136141

142+
// console.log(mmToRe(opts.matchers.static))
143+
137144
this.postcss = function (wp) {
138145
let res = [postcssImport({ addDependencyTo: wp })]
139146
res.push(...opts.postcss.plugins)
@@ -155,6 +162,10 @@ export default class Config {
155162
ignore: opts.ignore,
156163
dumpDirs: opts.dumpDirs,
157164
cssTemplates: opts.cssTemplates
165+
}), new StaticPlugin({
166+
matcher: opts.matchers.static,
167+
ignore: opts.ignore,
168+
dumpDirs: opts.dumpDirs
158169
}), new BrowserSyncPlugin(opts.server)
159170
]
160171

lib/plugins/css_plugin.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export default class CSSWebpackPlugin {
66
// Accepted Options
77
// - matcher (string)
88
// - dumpDirs (array)
9-
// - ignore (array)
109
// - cssTemplates (boolean)
11-
// - locals (object)
1210
constructor (opts) {
1311
this.opts = opts
1412
}

lib/plugins/static_plugin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import util from './plugin_utils'
2+
3+
export default class StaticWebpackPlugin {
4+
5+
// Accepted Options
6+
// - matcher (string)
7+
// - ignore (array)
8+
// - dumpDirs (array)
9+
constructor (opts) {
10+
this.opts = opts
11+
}
12+
13+
apply (compiler) {
14+
// read file tree and get all static files
15+
this.files = util.getFilesFromGlob(compiler, this.opts)
16+
17+
// inject static files into webpack's pipeline
18+
compiler.plugin('make', (compilation, done) => {
19+
util.addFilesAsWebpackEntries(this.files, this.opts, compiler, compilation)
20+
.then((res) => done())
21+
})
22+
23+
// remove static assets from webpack pipeline
24+
compiler.plugin('compilation', (compilation) => {
25+
compilation.plugin('optimize-chunk-assets', (chunks, done) => {
26+
util.removeAssets(compilation, this.opts, done)
27+
})
28+
})
29+
}
30+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
"babel-loader": "^6.2.0",
1616
"babel-preset-es2015": "^6.3.13",
1717
"babel-preset-stage-2": "^6.3.13",
18+
"binary-extensions": "^1.4.0",
1819
"browser-sync": "^2.11.1",
1920
"browser-sync-webpack-plugin": "^1.0.1",
2021
"chalk": "^1.1.1",
2122
"css-loader": "^0.23.1",
23+
"file-loader": "carrot/file-loader",
2224
"glob": "^6.0.1",
25+
"image-extensions": "^1.1.0",
2326
"jade": "^1.11.0",
24-
"jade-loader": "git:github.com/carrot/jade-loader",
27+
"jade-loader": "carrot/jade-loader",
2528
"joi": "^8.0.1",
2629
"lodash.union": "^4.2.0",
2730
"micromatch": "^2.3.7",

0 commit comments

Comments
 (0)