Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support require('style9') #89

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions __tests__/code/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* eslint-env jest */
const compile = require('../compile.js');

describe("support require('style9')", () => {
it('#1', () => {
const input = `
const style9 = require('style9');
const styles = style9.create({
default: {
paddingLeft: 2
}
});
styles('default');
`;
const { styles, code } = compile(input);

expect(styles).toBe(`.hhTkCv{padding-left:2px}`);
expect(code).toMatchInlineSnapshot(`
"const style9 = require('style9');

const styles = {};
\\"hhTkCv \\";"
`);
});

it('#2', () => {
const input = `
let style9 = require('style9');
const styles = style9.create({
default: {
paddingLeft: 2
}
});
styles('default');
`;
const { styles, code } = compile(input);

expect(styles).toBe(`.hhTkCv{padding-left:2px}`);
expect(code).toMatchInlineSnapshot(`
"let style9 = require('style9');

const styles = {};
\\"hhTkCv \\";"
`);
});

it('#2', () => {
const input = `
var style9 = require('style9');
const styles = style9.create({
default: {
paddingLeft: 2
}
});
styles('default');
`;
const { styles, code } = compile(input);

expect(styles).toBe(`.hhTkCv{padding-left:2px}`);
expect(code).toMatchInlineSnapshot(`
"var style9 = require('style9');

const styles = {};
\\"hhTkCv \\";"
`);
});

it('should ignore invalid require #1', () => {
const input = `
const style9 = foo.require('style9');
const style8 = require(style9);
const styles = style9.create({
default: {
paddingLeft: 2
}
});
styles('default');`;
const { code } = compile(input);
expect(code).toMatchInlineSnapshot(`
"const style9 = foo.require('style9');

const style8 = require(style9);

const styles = style9.create({
default: {
paddingLeft: 2
}
});
styles('default');"
`);
});

it('should ignore invalid require #2', () => {
const input = `
const style9 = foo.require('style9');
const style8 = foo(require('style9'));
const style7 = require(style9);
const style6 = require(style9)();
const stylesA = style9.create({
default: {
paddingLeft: 2
}
});
const stylesB = style8.create({
default: {
paddingLeft: 2
}
});
const stylesC = style7.create({
default: {
paddingLeft: 2
}
});
const stylesD = style6.create({
default: {
paddingLeft: 2
}
});
stylesA('default');
stylesB('default');
stylesC('default');
stylesD('default');`;
const { code } = compile(input);
expect(code).toMatchInlineSnapshot(`
"const style9 = foo.require('style9');

const style8 = foo(require('style9'));

const style7 = require(style9);

const style6 = require(style9)();

const stylesA = style9.create({
default: {
paddingLeft: 2
}
});
const stylesB = style8.create({
default: {
paddingLeft: 2
}
});
const stylesC = style7.create({
default: {
paddingLeft: 2
}
});
const stylesD = style6.create({
default: {
paddingLeft: 2
}
});
stylesA('default');
stylesB('default');
stylesC('default');
stylesD('default');"
`);
});
});
28 changes: 25 additions & 3 deletions babel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const NAME = require('./package.json').name;
const processReferences = require('./src/process-references.js');

/**
* @returns {import('@babel/core').PluginObj}
*/
module.exports = function style9BabelPlugin() {
return {
name: NAME,
Expand All @@ -12,10 +15,29 @@ module.exports = function style9BabelPlugin() {
const bindings = path.scope.bindings[importName].referencePaths;

const css = processReferences(bindings, state.opts).join('');
if (!state.file.metadata.style9) {
state.file.metadata.style9 = '';
}
state.file.metadata.style9 = state.file.metadata.style9 || '';
state.file.metadata.style9 += css;
},
VariableDeclaration(path, state) {
for (const { node } of path.get('declarations')) {
// process require('style9')
if (
node.init?.type === 'CallExpression' &&
node.init?.callee.type === 'Identifier' &&
node.init?.callee.name === 'require' &&
node.init?.arguments.length === 1 &&
node.init?.arguments[0].type === 'StringLiteral' &&
node.init?.arguments[0].value === NAME &&
node.id.type === 'Identifier'
) {
const importName = node.id.name;
const bindings = path.scope.bindings[importName].referencePaths;

const css = processReferences(bindings, state.opts).join('');
state.file.metadata.style9 = state.file.metadata.style9 || '';
state.file.metadata.style9 += css;
}
}
}
}
};
Expand Down