From 18aef5628d8dfd485f47afcf89cf972181d069c3 Mon Sep 17 00:00:00 2001 From: "alex.mm" Date: Fri, 17 Mar 2017 14:56:33 +0800 Subject: [PATCH 1/5] feat: auto parse variables: name and version --- index.js | 10 +++++++++- package.json | 5 +++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ab30bbea..82a82b73 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,19 @@ Author Tobias Koppers @sokra */ var loaderUtils = require("loader-utils"), - path = require("path"); + path = require("path"), + findPackage = require('find-package'); module.exports = function() {}; module.exports.pitch = function(remainingRequest) { if(this.cacheable) this.cacheable(); var query = loaderUtils.getOptions(this) || {}; + var pkg = findPackage(this.resourcePath) || {}; + if (query.attrs) { + Object.keys(query.attrs).forEach(function(key) { + query.attrs[key].replace(/\[name\]/ig, pkg.name).replace(/\[version\]/ig, pkg.version); + }); + } + return [ "// style-loader: Adds some css to the DOM by adding a `| +|**`attrs`**|`{Object}`|`{}`|Add custom attrs to ``, `[name]` `[version]` Can be replaced automatically| |**`transform`** |`{Function}`|`false`|Transform/Conditionally load CSS by passing a transform/condition function| |**`insertAt`**|`{String\|Object}`|`bottom`|Inserts `` at the given position| |**`insertInto`**|`{String\|Function}`|``|Inserts `` into the given position| @@ -199,6 +199,7 @@ This setting is primarily used as a workaround for [css clashes](https://github. ### `attrs` If defined, style-loader will attach given attributes with their values on `` + `` ].join("\n"); - runCompilerTest(expected, done); }); // it attrs From 6f74cdc239556454b27d13478b0f53a984227e32 Mon Sep 17 00:00:00 2001 From: "alex.mm" Date: Mon, 30 Jul 2018 11:43:08 +0800 Subject: [PATCH 4/5] fix: remove package "find-package" --- index.js | 30 ++++++++++++++++++++++++++++-- package.json | 1 - 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5fa206f5..03f598e3 100644 --- a/index.js +++ b/index.js @@ -3,18 +3,44 @@ Author Tobias Koppers @sokra */ var path = require("path"); -var findPackage = require('find-package'); +var fs = require("fs"); var loaderUtils = require("loader-utils"); var validateOptions = require('schema-utils'); module.exports = function () {}; +function exists(fileSystem, filename) { + var exists = false; + + try { + exists = fileSystem.statSync(filename).isFile(); + } catch (err) { + if (err.code !== "ENOENT") throw err; + } + + return exists; +}; + +function findPackage(fileSystem, start) { + var file = path.join(start, "package.json"); + if (exists(fileSystem, file)) { + return require(file); + } + + var up = path.dirname(start); + + // Reached root + if (up !== start) { + return findPackage(fileSystem, up); + } +}; + module.exports.pitch = function (request) { if (this.cacheable) this.cacheable(); var options = loaderUtils.getOptions(this) || {}; - var pkg = findPackage(this.resourcePath) || {}; + var pkg = findPackage(fs, path.dirname(this.resourcePath)) || {}; if (options.attrs) { Object.keys(options.attrs).forEach(function(key) { options.attrs[key] = options.attrs[key] diff --git a/package.json b/package.json index 422575a5..f9156ee4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "options.json" ], "dependencies": { - "find-package": "^1.0.0", "loader-utils": "^1.1.0", "schema-utils": "^0.4.5" }, From 0b31101de8baf448666f74db7bdaf3a18ef41296 Mon Sep 17 00:00:00 2001 From: "alex.mm" Date: Mon, 30 Jul 2018 22:01:27 +0800 Subject: [PATCH 5/5] fix: add utils test: add utils.test --- index.js | 27 +----------------------- lib/addStyleUrl.js | 6 +----- lib/utils.js | 40 ++++++++++++++++++++++++++++++++++++ test/utils.test.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 31 deletions(-) create mode 100644 lib/utils.js create mode 100644 test/utils.test.js diff --git a/index.js b/index.js index 03f598e3..a49acd43 100644 --- a/index.js +++ b/index.js @@ -6,35 +6,10 @@ var path = require("path"); var fs = require("fs"); var loaderUtils = require("loader-utils"); var validateOptions = require('schema-utils'); +var findPackage = require('./lib/utils').findPackage; module.exports = function () {}; -function exists(fileSystem, filename) { - var exists = false; - - try { - exists = fileSystem.statSync(filename).isFile(); - } catch (err) { - if (err.code !== "ENOENT") throw err; - } - - return exists; -}; - -function findPackage(fileSystem, start) { - var file = path.join(start, "package.json"); - if (exists(fileSystem, file)) { - return require(file); - } - - var up = path.dirname(start); - - // Reached root - if (up !== start) { - return findPackage(fileSystem, up); - } -}; - module.exports.pitch = function (request) { if (this.cacheable) this.cacheable(); diff --git a/lib/addStyleUrl.js b/lib/addStyleUrl.js index 581fecb3..f7595e3a 100644 --- a/lib/addStyleUrl.js +++ b/lib/addStyleUrl.js @@ -3,11 +3,7 @@ Author Tobias Koppers @sokra */ -function addAttrs (element, attrs) { - Object.keys(attrs).forEach(function (key) { - element.setAttribute(key, attrs[key]); - }); -} +var addAttrs = require('./utils').addAttrs; module.exports = function addStyleUrl (url, options) { if (typeof DEBUG !== "undefined" && DEBUG) { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..403c0155 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,40 @@ +var path = require("path"); + +var utils = { + exists: function (fileSystem, filename) { + var exists = false; + try { + exists = fileSystem.statSync(filename).isFile(); + } catch (err) { + if (err.code !== "ENOENT") throw err; + } + + return exists; + }, + + findPackage: function (fileSystem, start) { + var file = path.join(start, "package.json"); + if (utils.exists(fileSystem, file)) { + return require(file); + } + + var up = path.dirname(start); + + // Reached root + if (up !== start) { + return utils.findPackage(fileSystem, up); + } + return null; + }, + + addAttrs: function (element, attrs) { + Object.keys(attrs).forEach(function (key) { + element.setAttribute(key, attrs[key]); + }); + }, +}; + +module.exports = utils; + + + diff --git a/test/utils.test.js b/test/utils.test.js new file mode 100644 index 00000000..51bae06c --- /dev/null +++ b/test/utils.test.js @@ -0,0 +1,51 @@ +var assert = require("assert"); +var fs = require("fs"); +var path = require("path"); +var jsdom = require('jsdom'); +var utils = require("../lib/utils"); + +var exists = utils.exists; +var findPackage = utils.findPackage; +var addAttrs = utils.addAttrs; + +describe('Utils Test', () => { + it("exist", function(done) { + assert.equal(exists(fs, path.join(__dirname, 'basic.test.js')), true); + done(); + }); + + it('not exist', function(done) { + assert.equal(exists(fs, path.join(__dirname, 'xx.js')), false); + done(); + }); + + it('findPackage', function(done) { + assert.equal(findPackage(fs, __dirname).name, 'style-loader'); + done(); + }); + + it('findPackage null', function(done) { + assert.equal(findPackage(fs, '/'), null); + done(); + }); + + it('addAttrs', function(done) { + jsdom.env( + '

', + [], + function (err, window) { + var doc = window.document; + var ele = doc.createElement('div'); + var attrs = { + id: 'div', + width: '100px' + }; + addAttrs(ele, attrs); + assert.equal(ele.id, 'div'); + assert.equal(ele.getAttribute('width'), '100px'); + window.close(); + done(); + } + ); + }); +});