Skip to content

Commit 9a3ff7a

Browse files
committed
Updated README.md
Removed lodash dependency Updated specs
1 parent 200cd98 commit 9a3ff7a

21 files changed

+677
-224
lines changed

README.md

Lines changed: 190 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,193 @@
66
<h1>CSS Entry Plugin</h1>
77
</div>
88

9-
# css-entry-webpack-plugin
10-
A Webpack plugin that allows CSS files as the entry.
9+
[![Build Status][plugin-travis-shield]][plugin-travis-url]
10+
[![License][plugin-license-shield]][plugin-npm-url]
11+
12+
A [Webpack][webpack-url] plugin that simplifies creation of CSS-only bundles.
13+
14+
Installation
15+
------------
16+
[![NPM Version][plugin-npm-version-shield]][plugin-npm-url]
17+
[![Dependency Status][plugin-npm-dependencies-shield]][plugin-npm-dependencies-url]
18+
19+
Install the plugin using [npm][npm-url]:
20+
```shell
21+
$ npm install css-entry-webpack-plugin --save-dev
22+
```
23+
24+
[![npm](https://nodei.co/npm/css-entry-webpack-plugin.png?downloads=true&downloadRank=true&stars=true)][plugin-npm-url]
25+
26+
Basic Usage
27+
-----------
28+
The plugin will identify the entries that contain only CSS resources and will generate CSS bundles for them.
29+
30+
webpack.config.js
31+
```js
32+
const CssEntryPlugin = require("css-entry-webpack-plugin");
33+
34+
module.exports = {
35+
entry: {
36+
"styles": ["src/style1.css", "src/style2.css"],
37+
"main": "src/index.js"
38+
},
39+
output: {
40+
path: "dist",
41+
filename: "[name].bundle.js"
42+
},
43+
module: {
44+
rules: [
45+
// This is required
46+
{
47+
test: /\.css$/,
48+
use: "css-loader"
49+
}
50+
]
51+
},
52+
plugins: [
53+
new CssEntryPlugin({
54+
output: {
55+
filename: "[name].bundle.css"
56+
}
57+
})
58+
]
59+
};
60+
```
61+
62+
will output two files
63+
64+
`main.bundle.js` and `styles.bundle.css`
65+
66+
API
67+
---
68+
69+
```js
70+
new CssEntryPlugin(options: String | Object)
71+
```
72+
73+
#### `options`
74+
Type: `String | Function | Object`<br>
75+
Optional
76+
77+
Specifies the options for the `CssEntryPlugin`.
78+
79+
The shorthand version allows you to specify the `output.filename` directly as a `String` or a `Function`, this will be equivalent to passing an object with `output.filename`. See [`output.filename`](#outputfilename) for details on the possible values.
80+
81+
```js
82+
new CssEntryPlugin(/* option: String | Function */)
83+
// is equivalent to
84+
new CssEntryPlugin({
85+
output: {
86+
filename: /* option */
87+
}
88+
})
89+
```
90+
91+
When specified as an `Object`, the following options are available:
92+
93+
##### `output`
94+
Type: `Object`<br>
95+
Optional
96+
97+
Specifies a set of options instructing the plugin on how and where to output your CSS bundles. It works in a similar fashion to [Webpack's `output` option](https://webpack.js.org/configuration/output/#output-filename).
98+
99+
```js
100+
new CssEntryPlugin({
101+
output: { /* output options */ }
102+
})
103+
```
104+
105+
##### `output.filename`
106+
Type: `String | Function`<br>
107+
Default: `[name].css`<br>
108+
Optional
109+
110+
This option determines the name of each CSS output bundle. The bundle is written to the directory specified by the [Webpack `output.path` option](https://webpack.js.org/configuration/output/#output-path). It works in a similar fashion to [Webpack's `output.filename` option](https://webpack.js.org/configuration/output/#output-filename) and [`ExtractTextPlugin`'s `filename` option](https://github.com/webpack-contrib/extract-text-webpack-plugin#options).
111+
112+
For a single [`entry`](https://webpack.js.org/configuration/entry-context#entry) point, this can be a static name.
113+
114+
```js
115+
filename: "bundle.css"
116+
```
117+
118+
However, when creating multiple bundles via more than one entry point, you should use a [template string](https://github.com/webpack/webpack/blob/master/lib/TemplatedPathPlugin.js) with one of the following substitutions to give each bundle a unique name.
119+
120+
Using the entry name:
121+
122+
```js
123+
filename: "[name].bundle.css"
124+
```
125+
126+
Using the internal chunk id:
127+
128+
```js
129+
filename: "[id].bundle.css"
130+
```
131+
132+
The following substitutions are available in template strings:
133+
134+
|Substitution|Description|
135+
|:----------:|:----------|
136+
|`[name]`|The module name or name of the chunk|
137+
|`[id]`|The number of the chunk or module identifier|
138+
|`[contenthash]`|The hash of the content of the extracted file|
139+
140+
Any combination of these substitutions is allowed (eg. `"[name].[id].css"`).
141+
142+
The option can also be specified as a `Function` which should return the `filename` as a string without substitutions.
143+
144+
```js
145+
filename: function (getPath /* (template: string) => string */) {
146+
return "prefix-" + getPath("[name].[id].css");
147+
}
148+
```
149+
150+
The `Function` has the signature `(getPath: ((template: string) => string)) => string` where `getPath` is a function passed as the first argument, that can be used to perform the substitutions on a given template string to retrieve the original path.
151+
152+
Note this option is called `filename` but you are still allowed to use or return something like `"css/[name]/bundle.css"` to create a folder structure.
153+
154+
Note this option only affects CSS output files for entries matched by this plugin (CSS entries).
155+
156+
##### `entries`
157+
Type: `String | String[] | RegExp | Function`<br>
158+
Optional and mutually exclusive with [`ignoreEntries`](#ignoreentries)
159+
160+
Specifies the entry or entries to consider as possible CSS entries. Other entries will be ignored.
161+
162+
##### `ignoreEntries`
163+
Type: `String | String[] | RegExp | Function`<br>
164+
Optional and mutually exclusive with [`entries`](#entries)
165+
166+
Specifies the entry or entries to ignore. Other entries will be considered as possible CSS entries.
167+
168+
##### `extensions`
169+
Type: `String | String[]`<br>
170+
Default: `[".css", ".scss", ".less", ".styl"]`<br>
171+
Optional and mutually exclusive with [`test`](#test)
172+
173+
Specifies which file extensions are valid for files/resources inside considered CSS entries.
174+
175+
##### `test`
176+
Type: `RegExp | Function`<br>
177+
Optional and mutually exclusive with [`extensions`](#extensions)
178+
179+
Specifies which files/resources are valid for considered CSS entries.
180+
181+
##### `disable`
182+
Type: `Boolean`<br>
183+
Default: `false`<br>
184+
Optional
185+
186+
Disables the plugin.
187+
188+
[webpack-url]: https://webpack.js.org/
189+
[npm-url]: https://www.npmjs.com/
190+
191+
[plugin-npm-url]: https://npmjs.com/package/css-entry-webpack-plugin
192+
[plugin-npm-dependencies-url]: https://david-dm.org/tomachristian/css-entry-webpack-plugin
193+
[plugin-travis-url]: https://travis-ci.org/tomachristian/css-entry-webpack-plugin
194+
195+
[plugin-license-shield]: https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square
196+
[plugin-npm-version-shield]: https://img.shields.io/npm/v/css-entry-webpack-plugin.svg?style=flat-square
197+
[plugin-npm-dependencies-shield]: https://david-dm.org/tomachristian/css-entry-webpack-plugin.svg?style=flat-square
198+
[plugin-travis-shield]: https://img.shields.io/travis/tomachristian/css-entry-webpack-plugin/develop.svg?style=flat-square

gulpfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ gulp.task("test", gulp.series("build", buildConfig.cover ? gulp.series("test:cov
107107
displayStacktrace: true
108108
}
109109
});
110-
111-
return gulp.src(path.join(specsDir, "**/*-spec.js"))
110+
// TODO: Load glob from spec/support/jasmine.json (config)
111+
return gulp.src(path.join(specsDir, "**/*.spec.js"))
112112
.pipe(jasmine({
113113
config: config,
114114
reporter: reporter

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "css-entry-webpack-plugin",
3-
"version": "1.0.0",
3+
"version": "1.0.0-beta.1",
44
"author": "Cristian Toma",
55
"description": "A Webpack plugin that allows CSS files as the entry.",
66
"main": "lib/index.js",
77
"files": [
88
"lib/"
99
],
1010
"scripts": {
11-
"run": "gulp watch",
11+
"start": "gulp watch",
1212
"clean": "gulp clean",
1313
"build": "gulp build",
1414
"test": "gulp test",
@@ -73,7 +73,6 @@
7373
"dependencies": {
7474
"bluebird": "^3.5.0",
7575
"extract-text-webpack-plugin": "^2.1.0",
76-
"lodash": "^4.17.4",
7776
"tslib": "^1.6.0",
7877
"webpack": "^2.2.1"
7978
},

packages/@types/extract-text-webpack-plugin/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class ExtractTextPlugin implements CompilerPlugin {
1919

2020
export interface Options {
2121
/** the filename of the result file. May contain `[name]`, `[id]` and `[contenthash]` */
22-
filename: string;
22+
filename: string | ((getPath: ((template: string) => string)) => string);
2323
/** extract from all additional chunks too (by default it extracts only from the initial chunk(s)) */
2424
allChunks?: boolean;
2525
/** disables the plugin */
File renamed without changes.
File renamed without changes.

spec/default-options.spec.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
describe("Running CssEntryPlugin", () => {
2+
beforeEach(done => {
3+
this.webpack = webpackTestFixture(jasmine)
4+
.cleanOutput(done);
5+
});
6+
7+
describe("without options", () => {
8+
beforeEach(() => {
9+
this.webpack
10+
.withCssEntryPlugin(null, true);
11+
});
12+
13+
describe("configured with a shorthand single entry (.css)", () => {
14+
beforeEach(done => {
15+
this.webpack
16+
.config({
17+
entry: fixtures.style1.path
18+
})
19+
.run(done);
20+
});
21+
22+
it("generates a single css bundle with the default filename", () => {
23+
expect(this.webpack).toSucceed();
24+
25+
expect(this.webpack).toOutput({
26+
fileCount: 1,
27+
file: "main.css",
28+
withContent: fixtures.style1.content
29+
});
30+
});
31+
});
32+
33+
describe("configured with multi entry (1: .js, 2: .css)", () => {
34+
beforeEach(done => {
35+
this.webpack
36+
.config({
37+
entry: {
38+
"style": fixtures.style1.path,
39+
"script": fixtures.script1.path
40+
}
41+
})
42+
.run(done);
43+
});
44+
45+
it("generates one js bundle and one css bundle with the default filename", () => {
46+
expect(this.webpack).toSucceed();
47+
48+
expect(this.webpack).toOutput({
49+
fileCount: 2
50+
});
51+
52+
expect(this.webpack).toOutput({
53+
file: "style.css",
54+
withContent: fixtures.style1.content
55+
});
56+
57+
expect(this.webpack).toOutput({
58+
file: "script.bundle.js",
59+
withContent: fixtures.script1.content
60+
});
61+
});
62+
});
63+
});
64+
65+
describe("configured with empty object options", () => {
66+
beforeEach(done => {
67+
this.webpack = webpackTestFixture(jasmine)
68+
.withCssEntryPlugin({}, true)
69+
.cleanOutput(done);
70+
});
71+
72+
describe("configured with a shorthand single entry (.css)", () => {
73+
beforeEach(done => {
74+
this.webpack
75+
.config({
76+
entry: fixtures.style1.path
77+
})
78+
.run(done);
79+
});
80+
81+
it("generates a single css bundle with the default filename", () => {
82+
expect(this.webpack).toSucceed();
83+
84+
expect(this.webpack).toOutput({
85+
fileCount: 1,
86+
file: "main.css",
87+
withContent: fixtures.style1.content
88+
});
89+
});
90+
});
91+
92+
describe("configured with multi entry (1: .js, 2: .css)", () => {
93+
beforeEach(done => {
94+
this.webpack
95+
.config({
96+
entry: {
97+
"style": fixtures.style1.path,
98+
"script": fixtures.script1.path
99+
}
100+
})
101+
.run(done);
102+
});
103+
104+
it("generates one js bundle and one css bundle with the default filename", () => {
105+
expect(this.webpack).toSucceed();
106+
107+
expect(this.webpack).toOutput({
108+
fileCount: 2
109+
});
110+
111+
expect(this.webpack).toOutput({
112+
file: "style.css",
113+
withContent: fixtures.style1.content
114+
});
115+
116+
expect(this.webpack).toOutput({
117+
file: "script.bundle.js",
118+
withContent: fixtures.script1.content
119+
});
120+
});
121+
});
122+
});
123+
});

0 commit comments

Comments
 (0)