Skip to content
Permalink
Browse files

feat: Update Webpack plugin to accept existing processor (#535)

  • Loading branch information...
kevinkace authored and tivac committed Jan 11, 2019
1 parent 6d29447 commit 15f1e15ebceff0c9a0a7a5b2135c87961eaacbd6
@@ -17,6 +17,12 @@ This package contains two entry points, you will need to use **both** in tandem
- [Usage](#usage)
- [Options](#options)

## Install

```bash
> npm i @modular-css/webpack
```

## Usage

```js
@@ -58,6 +64,10 @@ Location to write the generated CSS file to, relative to `output.path` just like

Location to write out the JSON mapping file to, relative to `output.path` just like `output.filename`

#### `processor`

Pass an already-instantiated `Processor` instance to the Webpack plugin. It will then add any files found when traversing the modules to it and both the Webpack-discovered and any already-existing files will be output in the final CSS.

#### Shared Options

All other options are passed to the underlying `Processor` instance, see [Options](../processor/README.md#options).
@@ -26,7 +26,7 @@ function ModularCSS(args) {
}

this.prev = {};
this.processor = new Processor(options);
this.processor = options.processor || new Processor(options);
this.options = options;
}

@@ -47,10 +47,10 @@ ModularCSS.prototype.apply = function(compiler) {
// Runs before compilation begins
compiler.plugin("this-compilation", (compilation) => {
var files;

// Make processor instance available to the loader
compilation.options.processor = this.processor;

// This code is only useful when calling .run() multiple times
// watching handles its own invalidations
if(!watching) {
@@ -66,7 +66,7 @@ ModularCSS.prototype.apply = function(compiler) {

// Remove changed/removed files from processor instance
this.processor.remove(files);

this.prev = compilation.fileTimestamps;
}
});
@@ -86,15 +86,15 @@ ModularCSS.prototype.apply = function(compiler) {
new sources.RawSource(
data.css
);

// Write out external source map if it exists
if(data.map) {
compilation.assets[`${this.options.css}.map`] = new sources.RawSource(
data.map.toString()
);
}
}

if(this.options.json) {
compilation.assets[this.options.json] = new sources.RawSource(
JSON.stringify(data.compositions, null, 4)
@@ -1,5 +1,129 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/webpack.js should accept an existing processor instance 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = \\"\\";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = \\"./packages/webpack/test/specimens/simple.js\\");
/******/ })
/************************************************************************/
/******/ ({

/***/ \\"./packages/webpack/test/specimens/simple.css\\":
/*!****************************************************!*\\\\
!*** ./packages/webpack/test/specimens/simple.css ***!
\\\\****************************************************/
/*! exports provided: default, wooga */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

\\"use strict\\";
eval(\\"__webpack_require__.r(__webpack_exports__);\\\\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \\\\\\"wooga\\\\\\", function() { return wooga; });\\\\n/* harmony default export */ __webpack_exports__[\\\\\\"default\\\\\\"] = ({\\\\n \\\\\\"wooga\\\\\\": \\\\\\"mc8d99986b_wooga\\\\\\"\\\\n});\\\\nvar wooga = \\\\\\"mc8d99986b_wooga\\\\\\";\\\\n\\\\n//# sourceURL=webpack:///./packages/webpack/test/specimens/simple.css?\\");

/***/ }),

/***/ \\"./packages/webpack/test/specimens/simple.js\\":
/*!***************************************************!*\\\\
!*** ./packages/webpack/test/specimens/simple.js ***!
\\\\***************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

eval(\\"__webpack_require__(/*! ./simple.css */ \\\\\\"./packages/webpack/test/specimens/simple.css\\\\\\");\\\\n\\\\n\\\\n//# sourceURL=webpack:///./packages/webpack/test/specimens/simple.js?\\");

/***/ })

/******/ });"
`;

exports[`/webpack.js should accept an existing processor instance 2`] = `
"/* packages/webpack/test/specimens/fake.css */
.mc89f5ebd2_fake {
color: yellow;
}
/* packages/webpack/test/specimens/simple.css */
.mc8d99986b_wooga { color: red; }"
`;

exports[`/webpack.js should generate correct builds in watch mode when files change 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
@@ -1,20 +1,23 @@
/* eslint-disable max-statements */
"use strict";

var fs = require("fs"),
path = require("path"),
const fs = require("fs");
const path = require("path");

webpack = require("webpack"),
shell = require("shelljs"),
const webpack = require("webpack");
const dedent = require("dedent");
const shell = require("shelljs");

read = require("@modular-css/test-utils/read.js")(__dirname),
namer = require("@modular-css/test-utils/namer.js"),

Plugin = require("../plugin.js"),
const read = require("@modular-css/test-utils/read.js")(__dirname);
const namer = require("@modular-css/test-utils/namer.js");

output = path.resolve(__dirname, "./output"),
loader = require.resolve("../loader.js"),
test = /\.css$/;
const Processor = require("@modular-css/processor");

const Plugin = require("../plugin.js");

const output = path.resolve(__dirname, "./output");
const loader = require.resolve("../loader.js");
const test = /\.css$/;

function success(err, stats) {
expect(err).toBeFalsy();
@@ -207,7 +210,7 @@ describe("/webpack.js", () => {
it("should generate correct builds in watch mode when files change", (done) => {
var changed = 0,
compiler, watcher;

// Create v1 of the file
fs.writeFileSync(
path.join(__dirname, "./output/watched.css"),
@@ -224,10 +227,10 @@ describe("/webpack.js", () => {
// w/o it the build freezes forever!
setTimeout(done, 50);
});

watcher = compiler.watch(null, (err, stats) => {
changed++;

success(err, stats);

expect(read("output.js")).toMatchSnapshot();
@@ -247,18 +250,18 @@ describe("/webpack.js", () => {
it("should generate correct builds when files change", () => {
var changed = "./packages/webpack/test/output/changed.css",
compiler;

// wrap compiler.run in a promise for easier chaining
function run() {
return new Promise((resolve, reject) =>
compiler.run((err, stats) => {
if(stats.hasErrors()) {
return reject(stats);
}

expect(read("output.js")).toMatchSnapshot();
expect(read("output.css")).toMatchSnapshot();

return resolve(stats);
})
);
@@ -267,7 +270,7 @@ describe("/webpack.js", () => {
compiler = webpack(config({
entry : "./packages/webpack/test/specimens/change.js",
}));

// Create v1 of the file
fs.writeFileSync(changed, ".one { color: red; }");

@@ -287,10 +290,34 @@ describe("/webpack.js", () => {
// This build fails because the file is missing
.catch((stats) => {
expect(stats.toJson().errors[0]).toMatch("no such file or directory");

fs.writeFileSync(changed, ".three { color: green; }");

return run();
});
});

it("should accept an existing processor instance", async (done) => {
const processor = new Processor();

await processor.string("./packages/webpack/test/specimens/fake.css", dedent(`
.fake {
color: yellow;
}
`));

webpack(config({
entry : "./packages/webpack/test/specimens/simple.js",
plugin : {
processor
},
}), (err, stats) => {
success(err, stats);

expect(read("output.js")).toMatchSnapshot();
expect(read("output.css")).toMatchSnapshot();

done();
});
});
});

0 comments on commit 15f1e15

Please sign in to comment.
You can’t perform that action at this time.