How to Ignore Third Party Component css files while using babel-plugin-react-css-modules - react-css-modules

I have this situation where in one of my Component JSX file (For Eg. List.jsx) is importing the styles as follows :
import styles from "./css/list.rcss";
import "fixed-data-table.css";
import "./css/fixed-data-table.css";
1st import, the .rcss is our local css files where we use the react-css-modules rules
2nd import belongs to the FixedDataTable Component (Third Party)
3rd import is Global css file that uses standard css rules
This works well with react-css-modules but babel-plugin-react-css-modules throws the error because we are importing two anonymous imports.
Is there a work around or fix for this?

Related

How does Svelte handle double imported scripts (functions) in modules?

TL;DR: How does svelte handle importing the same script/functions multiple times in several components?
Let's say I have a svelte-project using a script "magic.js" which exports a function "greatFunction" which is huge and has some dependencies. I want to use this function in several components like "Profile.svelte", "Project.svelte", ...
I want to write as little code as possible and I want to have the compiled built as lean as possible.
I see the possibility of importing the "greatFunction" right into main.js, passing it as prop to App.svelte, then passing it to "Profile.svelte" and to "Project.svelte" and so on:
<Profile greatFunction={greatFunction} />
But what if I import magic.js and its greatFunction several times on a component-level in "Profile.svelte" and "Project.svelte" again and again?
Will this bloat my final production built?
What's the common way to do this right?

Enforcing use of format suffix patterns in DRF with ViewSets

I'm using Django Rest Framework to make a read-only API from an existing Django website. I'd like to require API URLs to include a format suffix (either .json, or .api for the browsable API) but I can't see how to do this when using ViewSets and Routers.
In my main site urls.py I have:
from django.urls import include
# ...
urlpatterns += [
path("api/v1/", include("api.urls", namespace="v1")),
]
And api/urls.py contains:
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from . import views
app_name = "api"
router = DefaultRouter(trailing_slash=False)
router.register(r"people", views.PersonViewSet)
urlpatterns = [
path("", include(router.urls)),
]
Two issues:
I can get a browsable API page by going to either /api/v1/people or /api/v1/people.api – I'd like to enforce the use of the latter.
The api-root view is visible at /api/v1/ or /api/v1/.api. That last looks ugly! But I can't see how to make it /api/v1.api, given I use a trailing slash on all URLs on the site, apart from the API.
Because I feel like I'm fighting DRF on this, I also feel I'm missing what the common best practice is.
At Andrew Backer's suggestion I tried using my own version of DefaultRouter and have, I think, solved point (1).
If I copy DefaultRouter, give it a new name, and make a single change, it enforces the use of format extensions. The only change is in the get_urls() method, where I change this:
if self.include_format_suffixes:
urls = format_suffix_patterns(urls)
To this:
if self.include_format_suffixes:
urls = format_suffix_patterns(urls, suffix_required=True, allowed=["api", "json"])
The suffix_required argument is the trick. It's a shame there's no way to pass an argument into DefaultRouter in order to toggle this on or off.
I tried subclassing DefaultRouter and only overriding the get_urls() method (rather than entirely copying and replacing the class) but that generated ImproperlyConfigured exceptions, "…is not a valid regular expression: redefinition of group name 'format' as group 2; was group 1 at position 42". I think that results in format_suffix_patterns() getting called twice, which is similar to an Issue here.

Is there any way of importing Query with a custom directive applied?

I want to import Mutation.updateUser() from "./generated/prisma.graphql", but I also want to add the #isAdmin directive. I can accomplish this by copying the query by hand:
type Mutation {
updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput!): User #isAdmin
}
But that means I have to copy every query/mutation/type I want to add a directive to, is there any sintax like this?
# import Mutation.updateUser from "./generated/prisma.graphql" #isAdmin
Unfortunately this is not possible at the moment. GraphQL SDL by default has no way of importing types or fields of types (as you want to do) into other files.
Based on the # import ...-syntax you show, I believe you're trying to use graphql-import. With this library it's possible to import entire types but not individual fields of types. If you'd like to have that functionality in the graphql-import library, please open an issue.

How can I create my own Material module using Angular Material 2?

I was checking the latest release of angular/material2 and in the release note they said MaterialModule has been marked as deprecated.
here are the words from release note.
MaterialModule
MaterialModule (and MaterialRootModule) have been marked as
deprecated. We've found that, with the current state of tree-shaking
in the world, that using an aggregate NgModule like MaterialModule
leads to tools not being able to eliminate code for components that
aren't used.
In order to ensure that users end up with the smallest code size
possible, we're deprecating MaterialModule, to be removed in the a
subsequent release.
To replace MaterialModule, users can create their own "Material"
module within their application (e.g., GmailMaterialModule) that
imports only the set of components actually used in the application._
So now MaterialModule is deprecated, i think i should not write it in the imports array of NgModules( please correct me if i am wrong ). Then how do i use <md-card> , md-button and other components. The release note says user can make their own Material module what does that mean.
can anyone show me a demo of how can i create my own material module?
I found the answer in the future commits to master where they update the docs.
https://github.com/angular/material2/compare/2.0.0-beta.3...master
Step 3: Import the component modules
Add MaterialModule as an import in your app's root NgModule.
Import the NgModule for each component you want to use:
import {MdButtonModule, MdCheckboxModule} from '#angular/material';
#NgModule({
imports: [MdButtonModule, MdCheckboxModule]
})
So in short, you need to separately specify components in the NgModel which you would like to use instead of one core module. This is done to remove redundant/unused code from the bundle when generating production build.

Unable to get webpack to fully ignore unnecessary JSX components

After spending a few years locked into an ancient tech stack for a project, I'm finally getting a chance to explore more current frameworks and dip a toe into React and Webpack. So far, it's for the most part been a refreshing and enjoyable experience, but I've run across some difficulty with Webpack that I'm hoping the hive-mind can help resolve.
I've been poring over the Webpack 2.0 docs and have searched SO pretty exhaustively, and come up short (I've only turned up this question, which is close, but I'm not sure it applies). This lack of information out there makes me think that I'm looking at one of two scenarios:
What I'm trying to do is insane.
What I'm trying to do is elementary to the point that it should be a no-brainer.
...and yet, here I am.
The short version of what I'm looking for is a way to exclude certain JSX components from being included in the Webpack build/bundle, based on environment variables. At present, regardless of what the dependency tree should look like based on what's being imported from the entry js forward, Webpack appears to be wanting to bundle everything in the project folder.
I kind of assume this is default behavior, because it makes some sense -- an application may need components in states other than the initial state. In this case, though, our 'application' is completely stateless.
To give some background, here are some cursory requirements for the project:
Project contains a bucket of components which can be assembled into a page and given a theme based on a config file.
Each of these components contains its own modular CSS, written in SASS.
Each exported page is static, and the bundle actually gets removed from the export directory. (Yes, React is a bit of overkill if the project ends at simply rendering a series of single, static, pages. A future state of the project will include a UI on top of this for which React should be a pretty good fit, however -- so best to have these components written in JSX now.)
CSS is pulled from the bundle using extract-text-webpack-plugin - no styles are inlined directly on elements in the final export, and this is not a requirement that can change.
As part of the page theme information, SASS variables are set which are then used by the SASS for each of the JSX components.
Only SASS variables for the JSX components referenced in a given page's config file are compiled and passed through sass-loader for use when compiling the CSS.
Here's where things break down:
Let's say I have 5 JSX components, conveniently titled component_1, component_2, component_3, and so on. Each of these has a matching .scss file with which it is associated, included in the following manner:
import React from 'react';
import styles from './styles.scss';
module.exports = React.createClass({
propTypes: {
foo: React.PropTypes.string.isRequired,
},
render: function () {
return (
<section className={`myClass`}>
<Stuff {...props} />
</section>
);
},
});
Now let's say that we have two pages:
page_1 contains component_1 and component_2
page_2 contains component_3, component_4, and component_5
These pages both are built using a common layout component that decides which blocks to use in a manner like this:
return (
<html lang='en'>
<body>
{this.props.components.map((object, i) => {
const Block = component_templates[object.component_name];
return <Block key={i}{...PAGE_DATA.components[i]} />;
})}
</body>
</html>
);
The above iterates through an object containing my required JSX components (for a given page), which is for now created in the following manner:
load_components() {
let component_templates = {};
let get_component = function(component_name) {
return require(`../path/to/components/${component_name}/template.jsx`);
}
for (let i = 0; i < this.included_components.length; i++) {
let component = this.included_components[i];
component_templates[component] = get_component(component);
}
return component_templates;
}
So the general flow is:
Collect a list of included components from the page config.
Create an object that performs a require for each such component, and stores the result using the component name as a key.
Step through this object and include each of these JSX components into the layout.
So, if I just follow the dependency tree, this should all work fine. However, Webpack is attempting to include all of our components, regardless of what the layout is actually loading. Due to the fact that I'm only loading SASS variables for the components that are actually used on a page, this leads to Webpack throwing undefined variable errors when the sass-loader attempts to process the SASS files associated with the unused modules.
Something to note here: The static page renders just fine, and even works in Webpack's dev server... I just get a load of errors and Webpack throws a fit.
My initial thought is that the solution for this is to be found in the configuration for the loader I'm using for the JSX files, and maybe I just needed to tell the loader what not to load, if Webpack was trying to load everything. I'm using `babel-loader for that:
{ test: /\.jsx?$/,
loader: 'babel-loader',
exclude: [
'./path/to/components/component_1/',
],
query: { presets: ['es2015', 'react'] }
},
The exclude entry there is new, and does not appear to work.
So that's kind of a novella, but I wanted to err on the side of too much information, as opposed to being scant. Am I missing something simple, or trying to do something crazy? Both?
How can I get unused components to not be processed by Webpack?
TL;DR
Don't use an expression in require and use multiple entry points, one for each page.
Detailed answer
Webpack appears to be wanting to bundle everything in the project folder.
That is not the case, webpack only includes what you import, which is determined statically. In fact many people that are new to webpack are confused at first that webpack doesn't just include the entire project. But in your case you have hit kind of an edge case. The problem lies in the way you're using require, specifically this function:
let get_component = function(component_name) {
return require(`../path/to/components/${component_name}/template.jsx`);
}
You're trying to import a component based on the argument to the function. How is webpack supposed to know at compile time which components should be included? Well, webpack doesn't do any program flow analysis, and therefore the only possibility is to include all possible components that match the expression.
You're sort of lucky that webpack allows you to do that in the first place, because passing just a variable to require will fail. For example:
function requireExpression(component) {
return require(component);
}
requireExpression('./components/a/template.jsx');
Will give you this warning at compile time even though it is easy to see what component should be required (and later fails at run-time):
Critical dependency: the request of a dependency is an expression
But because webpack doesn't do program flow analysis it doesn't see that, and even if it did, you could potentially use that function anywhere even with user input and that's essentially a lost cause.
For more details see require with expression of the official docs.
Webpack is attempting to include all of our components, regardless of what the layout is actually loading.
Now that you know why webpack requires all the components, it's also important to understand why your idea is not going to work out, and frankly, overly complicated.
If I understood you correctly, you have a multi page application where each page should get a separate bundle, which only contains the necessary components. But what you're really doing is using only the needed components at run-time, so technically you have a bundle with all the pages in it but you're only using one of them, which would be perfectly fine for a single page application (SPA). And somehow you want webpack to know which one you're using, which it could only know by running it, so it definitely can't know that at compile time.
The root of the problem is that you're making a decision at run-time (in the execution of the program), instead this should be done at compile time. The solution is to use multiple entry points as shown in Entry Points - Multi Page Application. All you need to do is create each page individually and import what you actually need for it, no fancy dynamic imports. So you need to configure webpack that each entry point is a standalone bundle/page and it will generate them with their associated name (see also output.filename):
entry: {
pageOne: './src/pageOne.jsx',
pageTwo: './src/pageTwo.jsx',
// ...
},
output: {
filename: '[name].bundle.js'
}
It's that simple, and it even makes the build process easier, as it automatically generates pageOne.bundle.js, pageTwo.bundle.js and so on.
As a final remark: Try not to be too smart with dynamic imports (pretty much avoid using expressions in imports completely), but if you decide to make a single page application and you only want to load what's necessary for the current page you should read Code Splitting - Using import() and Code Splitting - Using require.ensure, and use something like react-router.

Resources