GithubHelp home page GithubHelp logo

skleeschulte / babel-plugin-transform-semantic-ui-react-imports Goto Github PK

View Code? Open in Web Editor NEW
30.0 2.0 5.0 97 KB

Babel plugin that can convert semantic-ui-react module imports to default imports and add semantic-ui-css/-less imports.

Home Page: https://www.npmjs.com/package/babel-plugin-transform-semantic-ui-react-imports

License: MIT License

JavaScript 100.00%

babel-plugin-transform-semantic-ui-react-imports's Introduction

babel-plugin-transform-semantic-ui-react-imports

Build Status

Compatibility notice: As of version 1.4.0 this plugin is compatible with Babel 7. Compatibility with Babel 6 remains intact.

This plugin can convert module imports from semantic-ui-react to default imports. Example:

// Input:
import { Button, Container } from 'semantic-ui-react';

// Output:
import Button from 'semantic-ui-react/dist/es/elements/Button/Button.js';
import Container from 'semantic-ui-react/dist/es/elements/Container/Container.js';

In addition, the plugin can add import statements for CSS files from semantic-ui-css and LESS files from semantic-ui-less. For the example input above, this would produce:

// semantic-ui-css imports:
import 'semantic-ui-css/components/button.css';
import 'semantic-ui-css/components/container.css';

// semantic-ui-less imports:
import 'semantic-ui-less/definitions/elements/button.less';
import 'semantic-ui-less/definitions/elements/container.less';

The LESS imports can e.g. be useful in conjunction with Webpack and semantic-ui-less-module-loader.

Both, the conversion of module imports and adding CSS/LESS imports, can be enabled/disabled separately. The import type for default imports can also be configured (e.g. es or commonjs).

Example repository

You can find an example of how to use this plugin together with the semantic-ui-less-module-loader in the tailored-semantic-ui-react-bundles-with-webpack repository.

Installation

npm install babel-plugin-transform-semantic-ui-react-imports --save-dev

Depending on how you use the plugin, you also need to install semantic-ui-react, semantic-ui-css and/or semantic-ui-less (see below).

Usage

Add the plugin to your Babel configuration (e.g. in .babelrc):

{
    "plugins": ["transform-semantic-ui-react-imports"]
}

Plugin options

The plugin supports the following options (these are the default values):

{
    "plugins": [
        [
            "transform-semantic-ui-react-imports", {
                "convertMemberImports": true,
                "importType": "es",
                "addCssImports": false,
                "importMinifiedCssFiles": false,
                "addLessImports": false,
                "addDuplicateStyleImports": false
            }
        ]
    ]
}

convertMemberImports (default: true)

If true, member imports from semantic-ui-react are converted to default imports.

This requires semantic-ui-react to be installed.

importType (default: 'es')

This must be either the name of a folder below semantic-ui-react/dist or src. 'es', 'commonjs' or 'umd':

  • importType='es' example output:
    import Button from 'semantic-ui-react/dist/es/elements/Button/Button.js';
  • importType='src' example output:
    import Button from 'semantic-ui-react/src/elements/Button/Button.js';

addCssImports (default: false)

If true, imports for CSS files from semantic-ui-css will be added according to what is required from semantic-ui-react. Also works with convertMemberImports=false.

This requires semantic-ui-css to be installed. See Limitations below.

importMinifiedCssFiles (default: false)

If true, pre-minified CSS files from semantic-ui-css will be added (semantic-ui-css/components/[COMPONENT].min.css).

addLessImports (default: false)

If true, imports for LESS files from semantic-ui-less will be added according to what is required from semantic-ui-react. Also works with convertMemberImports=false. See Limitations below.

This requires semantic-ui-less to be installed.

addDuplicateStyleImports (default: false)

By default, each style import (css file or less file) will only be added once. If addDuplicateStyleImports is true, style imports will be added multiple times:

// Input:
import { Menu, MenuItem } from 'semantic-ui-react';

// Output:
import Menu from 'semantic-ui-react/dist/es/collections/Menu/Menu.js';
import 'semantic-ui-css/components/menu.css';
import MenuItem from 'semantic-ui-react/dist/es/collections/Menu/MenuItem.js';
import 'semantic-ui-css/components/menu.css'; // only added if addDuplicateStyleImports is true

Limitations

When using this plugin to automatically add styles (CSS or LESS), only imports for files with names equal to the imported semantic-ui-react module will be added. Additional style dependencies of a semantic-ui-react module will not be added automatically.

Example:

import { Dropdown } from 'semantic-ui-react';

With addLessImports enabled, the following import will be added:

import 'semantic-ui-less/definitions/modules/dropdown.less';

The Dropdown module from semantic-ui-react also needs collections/menu.less and modules/transition.less as well as globals/reset.less and globals/site.less from semantic-ui-less/definitions/. These "additional" dependencies have to be added manually (also see issue #1).

If someone knows a way to obtain a list of all style dependencies of the semantic-ui-react modules, please let me know!

Combining with babel-plugin-lodash

babel-plugin-lodash is an alternative to this plugin for cherry-picking imports from semantic-ui-react. If you are using Webpack 2, I recommend to use babel-plugin-transform-semantic-ui-react-imports with convertMemberImports: true (default) instead of using babel-plugin-lodash with id: ['semantic-ui-react']. By default, babel-plugin-transform-semantic-ui-react-imports will generate imports from semantic-ui-react's es distribution, whereas babel-plugin-lodash will use the commonjs distribution. When using the es distribution, Webpack 2 can perform dead-code elimination (tree shaking) within semantic-ui-react, which results in even smaller builds.

If you prefer to use babel-plugin-lodash to convert the imports from semantic-ui-react and still want to use babel-plugin-transform-semantic-ui-react-imports to add imports from semantic-ui-css or semantic-ui-less, this is possible. Example Babel configuration for adding CSS imports:

{
    "plugins": [
        ["transform-semantic-ui-react-imports", {
            "convertMemberImports": false,
            "addCssImports": true
        }],
        ["lodash", { "id": ["semantic-ui-react"] }]
    ]
}

WARNING: You should not use both plugins to convert member imports from semantic-ui-react.

E.g., do not use the following configuration:

{
    "plugins": [
        "transform-semantic-ui-react-imports",
        ["lodash", { "id": ["semantic-ui-react"] }]
    ]
}

This would transpile the input

import { Button } from 'semantic-ui-react';`

to the undesired output (on Windows):

import _Button from 'semantic-ui-react/dist\\commonjs\\elements\\Button/Button'; // generated by babel-plugin-lodash
import Button from 'semantic-ui-react/dist/es/elements/Button/Button.js'; // generated by babel-plugin-transform-semantic-ui-react-imports

How it works

Converting member imports (convertMemberImports: true)

The folders addons, behaviors, collections, elements, modules and views in semantic-ui-react/src are searched for *.js files that have a name that begins with an uppercase letter. Each file is added to a map, which then looks like this:

{
    Confirm: '/addons/Confirm/Confirm.js',
    Portal: '/addons/Portal/Portal.js',
    Radio: '/addons/Radio/Radio.js',
    Select: '/addons/Select/Select.js',
    ...
}

Together with the importType setting, this map is used to get the import paths for the components, like semantic-ui-react/dist/es/addons/Confirm/Confirm.js for the Confirm component.

The plugin matches each member import from semantic-ui-react against the generated map. If an entry is found, the member import is removed and replaced with a default import with the import path as described above.

Adding css imports (addCssImports: true)

The folder semantic-ui-css/components is searched for .css files to generate two maps, one for .css files and one for .min.css files:

{
    unminified: {
        accordion: 'semantic-ui-css/components/accordion.css',
        ad: 'semantic-ui-css/components/ad.css',
        breadcrumb: 'semantic-ui-css/components/breadcrumb.css',
        button: 'semantic-ui-css/components/button.css',
        ...
    }
    minified: {
        accordion: 'semantic-ui-css/components/accordion.min.css',
        ad: 'semantic-ui-css/components/ad.min.css',
        breadcrumb: 'semantic-ui-css/components/breadcrumb.min.css',
        button: 'semantic-ui-css/components/button.min.css',
        ...
    }
}

Finding the matching css import for an import from semantic-ui-react works like this:

  • If the import statement has member imports, iterate over the member imports. For each member import, take the imported name (e.g. BreadcrumbDivider, get the first element of the camel-cased name (e.g. Breadcrumb), convert it to lowercase (e.g. breadcrumb), and look it up in the map.
  • If the import statement has only a default import, check if the path of the import statement matches either semantic-ui-react/dist/.../.../COMPONENT_FOLDER[/...] or semantic-ui-react/src/.../COMPONENT_FOLDER[/...]. If so, convert COMPONENT_FOLDER to lowercase and look it up in the map.

Lookups respect the importMinifiedCssFiles option. If there is a match, add the css import statement. Each css import is only added once.

Adding less imports (addLessImports: true)

All the @import statements in the semantic.less file in the semantic-ui-less package root are parsed to generate a map which looks like this:

{
    reset: 'semantic-ui-less/definitions/globals/reset.less',
    site: 'semantic-ui-less/definitions/globals/site.less',
    button: 'semantic-ui-less/definitions/elements/button.less',
    container: 'semantic-ui-less/definitions/elements/container.less',
    ...
}

The rest works like when adding css imports (except that there are no minified versions available).

Running the tests

git clone https://github.com/skleeschulte/babel-plugin-transform-semantic-ui-react-imports.git
cd babel-plugin-transform-semantic-ui-react-imports
npm install
npm run test

babel-plugin-transform-semantic-ui-react-imports's People

Contributors

skleeschulte avatar tcrossland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

babel-plugin-transform-semantic-ui-react-imports's Issues

Some module dependency is not loaded

Hi, thanks for this great plugins.

So, for example dropdown or search didn't work correctly without importing transition and menu component from react.

so I had to modify index.js to be like this:

/**
 * Import the global LESS files. Imports for component LESS files are added by
 * babel-plugin-transform-semantic-ui-react-imports depending on which components from semantic-ui-react are used.
 */

/* Global */
import 'semantic-ui-less/definitions/globals/reset.less';
import 'semantic-ui-less/definitions/globals/site.less';

import 'semantic-ui-less/definitions/modules/transition.less';
import 'semantic-ui-less/definitions/collections/menu.less';

Plugin doesn't work with babel 7

I can't find any documentation for breaking changes to the options babel passes to plugins, but I'm fairly certain the structure of these options has changed and it is causing this plugin to no longer work. The problem occurs in the "pre" function:

pre: function(state) {
        addedCssImports = {};
        addedLessImports = {};

        multipleStyleImportsChecked = false;

        foundLodashPluginWithIdSemanticReactUi = false;
        babelPluginLodashChecked = false;

        const lodashPlugins = state.opts.plugins.filter(function(plugin) {
            return plugin[0].key === 'lodash' || plugin[0].key === 'babel-plugin-lodash';
        });

Specifically this last part:

const lodashPlugins = state.opts.plugins.filter(function(plugin) {
        return plugin[0].key === 'lodash' || plugin[0].key === 'babel-plugin-lodash';
});

The error I'm getting is this:

Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js):
TypeError: Cannot read property 'key' of undefined
    at /[path hidden for privacy]/node_modules/babel-plugin-transform-semantic-ui-react-imports/index.js:234:34
    at Array.filter (<anonymous>)

Logging the 'plugin' values in that loop shows that it is an object, not an array. The object does indeed have a 'key' property. If I modify the code to no longer treat the resulting 'plugin' value as an array, the error goes away:

return plugin.key === 'lodash' || plugin.key === 'babel-plugin-lodash';

Jest errors

Hi! I have Test suite failed to run

node_modules/semantic-ui-react/dist/es/elements/Segment/Segment.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import _extends from "@babel/runtime/helpers/extends";
                                                                                                    ^^^^^^^^

    SyntaxError: Unexpected identifier

      14 |
      15 | @inject('rootStore')
    > 16 | @observer
         |                    ^
      17 | class ProjectPage extends Component {
      18 | 	render() {
      19 | 		const { rootStore } = this.props

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

My config:

[
			"transform-semantic-ui-react-imports",
			{
				"convertMemberImports": true,
				"importType": "es",
				"addCssImports": true,
				"importMinifiedCssFiles": true,
				"addLessImports": false,
				"addDuplicateStyleImports": false
			}
		]

If i set convertMemberImports and addCssImports to false - test work fine )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.