Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
NotificationsYou must be signed in to change notification settings

ValeryG/babel-plugin-client-only-require

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What does it do

Converts import statements for less/css/scss into conditional requires in compiled file:

src/index.js:

import('./myStyles.less')

is converted into lib/index.js:

!!(typeofwindow!=='undefined'&&window.document&&window.document.createElement) ?require("./styles/myStyles.less") :void0;

Why is it needed?

If we run without this plugin:

babel-node src/index.js

or

babel src --out-dir libnode lib/index.js

we will get an error:

...myStyles.css:2.main {^SyntaxError: Unexpected token .

As node is trying to process less-css-scss file.So we do not want node on the server to process those files at all.

Why can't i use babel-plugin-transform-require-ignore

babel-plugin-transform-require-ignore will remove require statements for styles from transpiled outputso yourlib/index.js will have no mentioning of

require('myStyles.less')

But now, if you run your webpack to create client bundle from yourlib/*, your webpack style loader and ExtractTextPluginwill not find any less-css-scss files to grab and bundle.With the appoach of wrapping require to client only condition - require is still there for webpack to act on.

How to configure

Install as dev dependency:

npm install --save-dev babel-plugin-client-only-require

To configure add plugin to .babelrc plugins section:

{....    "plugins": [....["client-only-require", {                "extensions": ["less", "scss", "css"]        }]    ]}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp