Skip to content

Commit

Permalink
Merge pull request #450 from effector/feat/446-add-names-option-for-b…
Browse files Browse the repository at this point in the history
…abel-plugin

add `addNames` option for babel-plugin, by default `true` #446
  • Loading branch information
zerobias committed Jan 27, 2021
2 parents d0227d3 + 0f49d8c commit 0e27e93
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 16 deletions.
10 changes: 10 additions & 0 deletions docs/api/effector/babel-plugin.md
Expand Up @@ -146,6 +146,16 @@ const foo = createInputField('-', {
Specify import to process by plugin. By default has value `["effector", "effector/compat"]`

### addNames

> `addNames: boolean`
:::note since
effector 21.8.0
:::

Add names to units factories calls. By default has value `true`. Useful for minification and obfuscation of production builds.

### addLoc

> `addLoc: boolean`
Expand Down
314 changes: 314 additions & 0 deletions src/babel/__tests__/__snapshots__/babel-plugin.test.js.snap
Expand Up @@ -1357,6 +1357,42 @@ const g1_val = withFactory({
const g2_val = g2(0);"
`;

exports[`babel-plugin should factories without name with options 1`] = `
"import { withFactory } from \\"effector\\";
import { itIsALongName, Randomizing } from 'c/d';
import Defaulting, { AnotherImport } from '@/f';
import { splitMap } from 'patronum/split-map';
const longer = withFactory({
sid: \\"mn9l4u\\",
fn: () => itIsALongName(0)
});
const arcade = withFactory({
sid: \\"xpbz5w\\",
fn: () => Defaulting({
source: longer,
condition: withFactory({
sid: \\"lijhe7\\",
fn: () => AnotherImport({
test: true
})
})
})
});
withFactory({
sid: \\"-yfq3bv\\",
fn: () => Randomizing({
arcade
})
});
const result = withFactory({
sid: \\"lwwnia\\",
fn: () => splitMap({
longer,
source: arcade
})
});"
`;

exports[`babel-plugin should forward 1`] = `
"var _effectorFileName = \\"/src/babel/__tests__/fixtures/forward.js\\";
//@flow
Expand Down Expand Up @@ -2056,3 +2092,281 @@ const f = () => split({
}
});"
`;

exports[`babel-plugin should units without name with options 1`] = `
"import { createDomain, createEffect, createEvent, createStore, restore } from 'effector';
{
const domain = createDomain({
sid: \\"-nzaa7a\\"
});
const a = domain.store('bar', {
sid: \\"-r4da5h\\"
});
const b = domain.store('h', {
ɔ: {
option: 'test',
['na' + 'me']: 'LOL'
},
sid: \\"ehfy55\\"
});
const c = domain.store('h', {
ɔ: {
name: 'test'
},
sid: \\"-exuvjd\\"
}); //$off
const d = domain.store('h', {
ɔ: null,
sid: \\"r4zz5g\\"
}); //$off
const e = domain.store('h', {
ɔ: 4234,
sid: \\"-1t984v\\"
});
const f = domain.store('h', {
ɔ: {},
sid: \\"-v8k1td\\"
});
const aAlias = domain.createStore('bar', {
sid: \\"rsi1ca\\"
});
const bAlias = domain.createStore('h', {
ɔ: {
option: 'test',
['na' + 'me']: 'LOL'
},
sid: \\"i7igmi\\"
});
const cAlias = domain.createStore('h', {
ɔ: {
name: 'testAlias'
},
sid: \\"8mivwq\\"
}); //$off
const dAlias = domain.createStore('h', {
ɔ: null,
sid: \\"-hf2ev\\"
}); //$off
const eAlias = domain.createStore('h', {
ɔ: 4234,
sid: \\"-9ld0qg\\"
});
const fAlias = domain.createStore('h', {
ɔ: {},
sid: \\"-j6clg8\\"
});
const {
sid
} = domain.createStore(null, {
sid: \\"-ls4ila\\"
});
const {
shortName
} = domain.createStore(null, {
ɔ: {
name: 'foo'
},
sid: \\"-lb2vju\\"
});
domain.createStore(null, {
sid: \\"tgv4rt\\"
});
domain.createStore(null, {
sid: \\"thypyj\\"
});
}
{
const foo = createEffect({
sid: \\"9vguxc\\"
});
const bar = createEffect('hello', {
sid: \\"-t87e9q\\"
});
const baz = createEffect({
handler() {
return 0;
}
}, {
sid: \\"g6ivfd\\"
});
const quux = createEffect('nice', {
ɔ: {
handler() {
return 0;
}
},
sid: \\"-26njv2\\"
});
const fa = () => createEffect({
sid: \\"nsvob9\\"
});
const {
sid
} = createEffect({
sid: \\"ixqzbg\\"
});
const {
name
} = createEffect('foo', {
sid: \\"jeslqi\\"
});
const {
shortName
} = createEffect({
name: 'foo'
}, {
sid: \\"jvu8r3\\"
});
createEffect({
sid: \\"uh5jxn\\"
});
createEffect({
sid: \\"ui954d\\"
});
}
{
const foo = createStore('foo', {
sid: \\"-jyq2qv\\"
});
const a = createStore('h', {
sid: \\"-oagam9\\"
});
const b = createStore('h', {
ɔ: {},
sid: \\"hbcxod\\"
}); //$off
const c = createStore('h', {
ɔ: 23020,
sid: \\"-bmw9ly\\"
});
const config = {
option: 0
};
const dod = createStore(null, {
ɔ: config,
sid: \\"sja6dc\\"
});
const f = a => createStore(a, {
sid: \\"-sen25u\\"
});
const {
sid
} = createStore(null, {
sid: \\"-mqki7m\\"
});
const {
shortName
} = createStore(null, {
ɔ: {
name: 'foo'
},
sid: \\"-m9iv66\\"
});
createStore(null, {
sid: \\"vep04o\\"
});
createStore(null, {
sid: \\"vfslbe\\"
});
}
{
const foo = createEvent({
sid: \\"8x0vb0\\"
});
const bar = createEvent('hello', {
sid: \\"-u6ndw2\\"
});
const baz = createEvent({
name: 'nice'
}, {
sid: \\"f82vt1\\"
});
const f = () => createEvent({
sid: \\"-1w4823\\"
});
const {
sid
} = createEvent({
sid: \\"-65zqet\\"
});
const {
name
} = createEvent('foo', {
sid: \\"-5oy3zr\\"
});
const {
shortName
} = createEvent({
name: 'foo'
}, {
sid: \\"-57wgz6\\"
});
createEvent({
sid: \\"vyhlho\\"
});
createEvent({
sid: \\"wb4v4z\\"
});
}
{
const foo = createEvent({
sid: \\"pyn9i0\\"
});
const a = restore(foo, null, {
sid: \\"lmx1mm\\"
});
const b = restore(foo, null, {
ɔ: {},
sid: \\"-7sds1w\\"
}); //$off
const c = restore(foo, null, {
ɔ: 23020,
sid: \\"yah2mx\\"
});
const config = {
option: 0
};
const dod = restore(foo, null, {
ɔ: config,
sid: \\"10dc0p\\"
});
const f = a => restore(foo, null, {
sid: \\"-gu50wv\\"
});
const {
sid
} = restore(foo, null, {
sid: \\"zcd1uy\\"
});
const {
shortName
} = restore(foo, null, {
ɔ: {
name: 'bar'
},
sid: \\"-z7pd2q\\"
});
restore(foo, null, {
sid: \\"vif224\\"
});
restore(foo, null, {
sid: \\"vv2bpf\\"
});
}"
`;
38 changes: 31 additions & 7 deletions src/babel/__tests__/babel-plugin.test.js
Expand Up @@ -15,13 +15,37 @@ describe('babel-plugin', () => {
.split('-')
.join(' ')
.slice(0, -3)
it(`should ${caseName}`, () => {
const fixturePath = path.join(fixturesDir, caseFile)
const fixture = transformFileSync(fixturePath, {
configFile: path.join(__dirname, '.babelrc'),
})?.code

expect(fixture).toMatchSnapshot()
})
const optionsName = `${caseFile.slice(0, -3)}.options.json`
const optionsPath = path.join(fixturesDir, optionsName)
const hasOptions = fs.existsSync(optionsPath)

if (hasOptions) {
const options = JSON.parse(
fs.readFileSync(optionsPath, {encoding: 'utf8'}).toString(),
)

it(`should ${caseName} with options`, () => {
const fixturePath = path.join(fixturesDir, caseFile)
const fixture = transformFileSync(fixturePath, {
// configFile: path.resolve(__dirname, '../../../babel.config.js'),
configFile: false,
babelrc: false,
envName: 'test',
plugins: [[path.resolve(__dirname, '../babel-plugin.js'), options]],
})?.code

expect(fixture).toMatchSnapshot()
})
} else {
it(`should ${caseName}`, () => {
const fixturePath = path.join(fixturesDir, caseFile)
const fixture = transformFileSync(fixturePath, {
configFile: path.join(__dirname, '.babelrc'),
})?.code

expect(fixture).toMatchSnapshot()
})
}
}
})

0 comments on commit 0e27e93

Please sign in to comment.