babel-plugin-transform-cx-imports
Advanced tools
Comparing version 16.12.0 to 16.12.1
36
index.js
@@ -12,3 +12,3 @@ "use strict"; | ||
module.exports = function(options) { | ||
module.exports = function(options, o1) { | ||
var t = options.types; | ||
@@ -21,4 +21,4 @@ | ||
var opts = scope.opts; | ||
var src = path.node.source.value; | ||
var importScss = opts.sass || opts.scss; | ||
@@ -28,16 +28,24 @@ if (src.indexOf("cx/") == 0) { | ||
var imports = []; | ||
if (opts.useSrc) { | ||
var imports = []; | ||
path.node.specifiers.forEach(function (s) { | ||
var expanded = remainder + '/' + s.imported.name; | ||
var srcFile = manifest[expanded]; | ||
if (srcFile) { | ||
if (srcFile.js) | ||
imports.push(t.importDeclaration([s], t.stringLiteral('cx-core/' + srcFile.js))); | ||
if (srcFile.scss && opts.scss) | ||
imports.push(t.importDeclaration([], t.stringLiteral('cx-core/' + srcFile.scss))); | ||
} | ||
}); | ||
path.node.specifiers.forEach(function (s) { | ||
var expanded = remainder + '/' + s.imported.name; | ||
var srcFile = manifest[expanded]; | ||
if (srcFile) { | ||
if (srcFile.js) | ||
imports.push(t.importDeclaration([s], t.stringLiteral('cx-core/' + srcFile.js))); | ||
path.replaceWithMultiple(imports); | ||
if (srcFile.scss && importScss) { | ||
imports.push(t.importDeclaration([], t.stringLiteral('cx-core/' + srcFile.scss))); | ||
} | ||
} | ||
}); | ||
path.replaceWithMultiple(imports); | ||
} | ||
else { | ||
//cx-core => cx | ||
path.replaceWith(t.importDeclaration(path.node.specifiers, t.stringLiteral("cx-core/" + remainder))); | ||
} | ||
} | ||
@@ -44,0 +52,0 @@ } |
@@ -0,0 +0,0 @@ # Third-Party Software Licenses |
@@ -0,0 +0,0 @@ This package is a part of the Cx framework, which is available under |
{ | ||
"name": "babel-plugin-transform-cx-imports", | ||
"version": "16.12.0", | ||
"version": "16.12.1", | ||
"description": "Rewrite imports from Cx for simplicity and optimal output size.", | ||
@@ -12,2 +12,5 @@ "repository": "https://github.com/codaxy/cx", | ||
}, | ||
"peerDependencies": { | ||
"cx-core": "*" | ||
}, | ||
"devDependencies": { | ||
@@ -14,0 +17,0 @@ "babel-core": "^6.4.5", |
# babel-plugin-transform-cx-imports | ||
This plugin rewrites application imports to use Cx source files | ||
and optionally include sass imports. | ||
This plugin does three things: | ||
1. rewrites `cx` based imports to `cx-core`. E.g. | ||
`import { TextField} from 'cx/widgets'` becomes `import { TextField} from 'cx-core/widgets'`. | ||
2. optionally rewrites imports to use `src` files: | ||
`import { TextField} from 'cx/widgets'` becomes `import { TextField} from 'cx-core/src/ui/form/TextField'`. | ||
3. optionally includes SCSS files for imported components. E.g. | ||
`import { TextField} from 'cx/widgets'` adds also `import 'cx-core/src/ui/form/TextField.scss'`. | ||
### Usage | ||
Standard: | ||
``` | ||
@@ -15,3 +24,3 @@ //.babelrc | ||
Optionally, if you want to include scss files for each plugin, use: | ||
To use src files, use: | ||
@@ -21,4 +30,14 @@ ``` | ||
"plugins": [ | ||
["transform-cx-imports", { useSrc: true }] | ||
] | ||
``` | ||
Note that if using src files, your babel/webpack configuration should whitelist `cx-core` path. | ||
Optionally, if you want to include .scss files, use: | ||
``` | ||
//.babelrc | ||
"plugins": [ | ||
["transform-cx-imports", { sass: true }] | ||
] | ||
``` |
34
test.js
@@ -16,3 +16,3 @@ "use strict"; | ||
describe('babel-plugin-transform-cx-imports', function() { | ||
describe.only('babel-plugin-transform-cx-imports', function() { | ||
@@ -30,5 +30,5 @@ it("skips non-cx import", function () { | ||
it("imports Cx widgets", function () { | ||
it("rewrites cx into cx-core", function () { | ||
let code = `import {TextField} from "cx/widgets"`; | ||
let code = `import { Widget } from "cx/ui"`; | ||
@@ -39,5 +39,27 @@ let output = babel.transform(code, { | ||
assert.equal(unwrap(output), 'import { TextField } from "cx-core/src/ui/form/TextField"'); | ||
assert.equal(unwrap(output), 'import { Widget } from "cx-core/ui"'); | ||
}); | ||
it("supports multiple imports", function () { | ||
let code = `import { Widget, VDOM } from "cx/ui"`; | ||
let output = babel.transform(code, { | ||
plugins: [plugin] | ||
}).code; | ||
assert.equal(unwrap(output), 'import { Widget, VDOM } from "cx-core/ui"'); | ||
}); | ||
it("imports Cx widgets from source", function () { | ||
let code = `import {TextField} from "cx/widgets"`; | ||
let output = babel.transform(code, { | ||
plugins: [[plugin, { useSrc: true }]] | ||
}).code; | ||
assert.equal(unwrap(output), 'import { TextField } from "cx-core/src/ui/form/TextField.js"'); | ||
}); | ||
it("imports scss file if available and option is set", function () { | ||
@@ -48,7 +70,7 @@ | ||
let output = babel.transform(code, { | ||
plugins: [[plugin, {scss: true}]] | ||
plugins: [[plugin, { useSrc: true, scss: true }]] | ||
}).code; | ||
assert.deepEqual(lines(output), [ | ||
'import { TextField } from "cx-core/src/ui/form/TextField";', | ||
'import { TextField } from "cx-core/src/ui/form/TextField.js";', | ||
'import "cx-core/src/ui/form/TextField.scss"' | ||
@@ -55,0 +77,0 @@ ]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6613
93
42
2