From 28e80154252f274174ee68a327dadfe462012d79 Mon Sep 17 00:00:00 2001 From: Jason English Date: Tue, 21 Aug 2018 09:37:29 -0700 Subject: [PATCH 1/3] feat(useable): support insertInto --- useable.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/useable.js b/useable.js index 01320053..8fabe263 100644 --- a/useable.js +++ b/useable.js @@ -18,6 +18,21 @@ module.exports.pitch = function (request) { options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr; + // The variable is needed, because the function should be inlined. + // If is just stored it in options, JSON.stringify will quote + // the function and it would be just a string at runtime + var insertInto; + + if (typeof options.insertInto === "function") { + insertInto = options.insertInto.toString(); + } + + // We need to check if it a string, or variable will be "undefined" + // and the loader crashes + if (typeof options.insertInto === "string") { + insertInto = '"' + options.insertInto + '"'; + } + var hmr = [ // Hot Module Replacement "if(module.hot) {", @@ -48,6 +63,8 @@ module.exports.pitch = function (request) { "var refs = 0;", "var dispose;", "var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");", + "var options = " + JSON.stringify(options) + ";", + "options.insertInto = " + insertInto + ";", "", "if(typeof content === 'string') content = [[module.id, content, '']];", // Export CSS Modules @@ -55,7 +72,7 @@ module.exports.pitch = function (request) { "", "exports.use = exports.ref = function() {", " if(!(refs++)) {", - " dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, " + JSON.stringify(options) + ");", + " dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, options);", " }", "", " return exports;", From c6ddf34c64cfad6a564bac801ed30133e7ac0e16 Mon Sep 17 00:00:00 2001 From: Jason English Date: Fri, 24 Aug 2018 10:52:27 -0700 Subject: [PATCH 2/3] add test --- test/useable.test.js | 142 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 124 insertions(+), 18 deletions(-) diff --git a/test/useable.test.js b/test/useable.test.js index 981da074..4a4c997e 100644 --- a/test/useable.test.js +++ b/test/useable.test.js @@ -8,30 +8,136 @@ var loaderUtils = require('loader-utils'); var useable = require("../useable"); describe("useable tests", function () { - var sandbox = sinon.sandbox.create(); - var getOptions; + describe('hmr', function () { + var sandbox = sinon.sandbox.create(); + var getOptions; - beforeEach(() => { - // Mock loaderUtils to override options - getOptions = sandbox.stub(loaderUtils, 'getOptions'); - }); + beforeEach(() => { + // Mock loaderUtils to override options + getOptions = sandbox.stub(loaderUtils, 'getOptions'); + }); - afterEach(() => { - sandbox.restore(); - }); + afterEach(() => { + sandbox.restore(); + }); - it("should output HMR code by default", function () { - assert.equal(/(module\.hot)/g.test(useable.pitch()), true); - }); + it("should output HMR code by default", function () { + assert.equal(/(module\.hot)/g.test(useable.pitch()), true); + }); - it("should NOT output HMR code when options.hmr is false", function () { - getOptions.returns({hmr: false}); - assert.equal(/(module\.hot)/g.test(useable.pitch()), false); + it("should NOT output HMR code when options.hmr is false", function () { + getOptions.returns({hmr: false}); + assert.equal(/(module\.hot)/g.test(useable.pitch()), false); + }); + + it("should output HMR code when options.hmr is true", function () { + getOptions.returns({hmr: true}); + assert.equal(/(module\.hot)/g.test(useable.pitch()), true); + }); }); - it("should output HMR code when options.hmr is true", function () { - getOptions.returns({hmr: true}); - assert.equal(/(module\.hot)/g.test(useable.pitch()), true); + describe('insert into', function () { + var path = require("path"); + + var utils = require("./utils"), + runCompilerTest = utils.runCompilerTest; + + var fs; + + var requiredCss = ".required { color: blue }", + requiredCssTwo = ".requiredTwo { color: cyan }", + localScopedCss = ":local(.className) { background: red; }", + localComposingCss = ` + :local(.composingClass) { + composes: className from './localScoped.css'; + color: blue; + } + `, + requiredStyle = ``, + existingStyle = ``, + checkValue = '
check
', + rootDir = path.resolve(__dirname + "/../") + "/", + jsdomHtml = [ + "", + "", + existingStyle, + "", + "", + "
", + checkValue, + "
", + "