Rollup bundling peer dependencies / Multiple instances of MUI make the ThemeProvider not work - npm

I have two different repos I'm working on, lib and apps.
lib has some React components made with MUI that I want to consume in apps, and that's built with Rollup.
apps is a Lerna monorepo with a Next.js app that imports lib as a dependency to consume its components, which can be customized wrapping them in a <ThemeProvider> with a custom theme.
However, that doesn't work because there are duplicated instances of MUI / ThemeProvider, which I was able to verify by adding this to lib's entry point:
if (process.browser) {
(window as any)._React = React;
(window as any)._ThemeProvider = ThemeProvider;
}
And then this into apps':
if (process.browser) {
const {
_React,
_ThemeProvider,
} = window as any;
console.log(_React ? (_React === React ? "✅ Unique React" : "❌ Duplicate React") : "-");
console.log(_ThemeProvider ? (_ThemeProvider === ThemeProvider ? "✅ Unique ThemeProvider" : "❌ Duplicate ThemeProvider") : "-");
}
Which prints:
✅ Unique React
❌ Duplicate ThemeProvider
lib's component usage in app looks like this:
<ThemeProvider theme={ MY_CUSTOM_THEME }>
<MyComponent{ ...myComponentProps } />
</ThemeProvider>
Where both MyComponent and MY_CUSTOM_THEME are imported from lib, while ThemeProvider is imported from #mui/material/styles, just like it is in lib.
However, all MUI components will be displayed with the default theme.
Here are some of the relevant build files for both repos:
lib > package.json:
{
"name": "#myscope/lib",
"version": "1.0.0-alpha",
"description": "",
"main": "dist/cjs/src/index.js",
"module": "dist/esm/src/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"private": true,
"scripts": {
"build": "rollup -c",
},
"dependencies": {
"#apollo/client": "^3.4.5",
"#apollo/link-context": "^2.0.0-beta.3",
"#hookform/resolvers": "^2.8.5",
"#mui/icons-material": "^5.0.4",
"#swyg/corre": "^1.0.1",
"apollo-upload-client": "^16.0.0",
"atob": "^2.1.2",
"axios": "^0.24.0",
"btoa": "^1.2.1",
"country-codes-list": "^1.6.8",
"country-region-data": "^1.6.0",
"next": "^11.0.1",
"next-images": "^1.8.2",
"openpgp": "^5.0.0",
"react-hook-form": "^7.22.0",
"react-payment-inputs": "^1.1.8",
"react-use-country-region": "^1.0.0",
"styled-components": "^5.3.0",
"use-callback-ref": "^1.2.5",
"uuidv4": "^6.2.12",
"yup": "^0.32.11"
},
"peerDependencies": {
"#mui/material": "^5.0.4",
"react-dom": "^17.0.2",
"react": "^17.0.2"
},
"devDependencies": {
"#auth0/auth0-react": "^1.6.0",
"#babel/preset-react": "^7.16.7",
"#emotion/react": "^11.5.0",
"#emotion/styled": "^11.3.0",
"#graphql-codegen/cli": "^1.21.5",
"#graphql-codegen/introspection": "^1.18.2",
"#graphql-codegen/typescript-operations": "^1.18.0",
"#graphql-codegen/typescript-react-apollo": "^2.2.5",
"#graphql-codegen/typescript": "^1.22.1",
"#mui/material": "^5.2.8",
"#rollup/plugin-babel": "^5.3.0",
"#rollup/plugin-commonjs": "^21.0.1",
"#rollup/plugin-node-resolve": "^13.1.2",
"#rollup/plugin-typescript": "^8.3.0",
"#testing-library/dom": "^8.1.0",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^12.0.0",
"#testing-library/user-event": "^13.2.1",
"#types/jest": "^26.0.24",
"#types/react-dom": "^17.0.11",
"#types/react": "^17.0.16",
"#types/styled-components": "^5.1.12",
"#typescript-eslint/eslint-plugin": "^4.29.0",
"#typescript-eslint/parser": "^4.29.0",
"babel-jest": "^27.0.6",
"babel-plugin-styled-components": "^2.0.2",
"eslint-config-next": "^11.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-unused-imports": "^1.1.2",
"eslint": "^7.32.0",
"graphql": "^16.2.0",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"rollup-plugin-dts": "^4.1.0",
"rollup-plugin-node-externals": "^3.1.2",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-typescript2": "^0.31.1",
"rollup": "^2.63.0",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
}
}
Note that #mui/material appears both in peerDependencies and devDependencies, but not react or rect-dom, which are only listed as peerDependencies. That's because there's #types/react and #types/react-dom, but nothing similar for MUI, so the Rollup build would fail if it can't find the right types.
lib > rollup.config.js:
import commonjs from '#rollup/plugin-commonjs';
import dts from 'rollup-plugin-dts'
import resolve from "#rollup/plugin-node-resolve";
import typescript from 'rollup-plugin-typescript2'
import babel from '#rollup/plugin-babel'
import pkg from "./package.json";
// Extensions handled by babel:
const EXTENSIONS = [".ts", ".tsx"];
// Exclude dev and peer dependencies:
const EXTERNAL = [
...Object.keys(pkg.devDependencies),
...Object.keys(pkg.peerDependencies),
];
export default [{
input: 'src/index.ts',
output: [{
dir: "dist/esm",
sourcemap: true,
format: "esm",
preserveModules: true,
globals: {
react: "React",
},
}, {
dir: "dist/cjs",
sourcemap: true,
format: "cjs",
preserveModules: true,
globals: {
react: "React",
},
}],
external: EXTERNAL,
plugins: [
resolve(),
commonjs({
exclude: "node_modules",
ignoreGlobal: true,
}),
typescript({
useTsconfigDeclarationDir: true,
tsconfig: "rollup.tsconfig.json",
}),
],
}, {
input: './dist/types/src/index.d.ts',
output: [{ file: './dist/index.d.ts', format: "esm" }],
external: EXTERNAL,
plugins: [dts()],
}];
apps > lerna.json:
{
"packages": ["apps/**/*", "packages/**/*"],
"version": "0.0.1",
"npmClient": "yarn",
"useWorkspaces": true
}
apps > package.json:
...
"dependencies": {
"#myscope/lib": "file:../lib"
},
...
Note this is a file: import just to make it easier to develop and test changes.
I've also tried adding resolutions to both apps > package.json and apps > app > package.json, but no luck with that either...:
"resolutions": {
"#mui/material": "5.2.8"
}

It might not be the best solution, but I fixed this by exporting the ThemeProvider from lib:
export { ThemeProvider as MyComponentThemeProvider } from "#mui/material/styles";
Now, when using this one in app:
<MyComponentThemeProvider theme={ MY_CUSTOM_THEME }>
<MyComponent{ ...myComponentProps } />
</MyComponentThemeProvider>
Everything works as expected:
✅ Unique React
✅ Unique ThemeProvider
Also, take a look at https://github.com/facebook/react/issues/19541

Related

React Native - Unable to resolve module `node_modules/react/index.js/jsx-runtime`

I'm in the process of upgrading react native and I've been stuck on the following error for the past few days:
BUNDLE ./index.ts
error: Error: Unable to resolve module
monorepo/app/node_modules/react/index.js/jsx-runtime from monorepo/mobileChat/src/screens/index.tsx:
None of these files exist:
* node_modules/react/index.js/jsx-runtime(.native|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
* node_modules/react/index.js/jsx-runtime/index(.native|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
> 1 | import React from 'react'
2 | import { Navigation } from 'react-native-navigation'
3 | import { Provider } from 'react-redux'
4 | import getStore from 'shared/store'
at ModuleResolver.resolveDependency (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:152:15)
at DependencyGraph.resolveDependency (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/node-haste/DependencyGraph.js:264:43)
at Object.resolve (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/lib/transformHelpers.js:170:21)
at resolveDependencies (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/DeltaBundler/graphOperations.js:466:33)
at processModule (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/DeltaBundler/graphOperations.js:232:31)
at async addDependency (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/DeltaBundler/graphOperations.js:361:18)
at async Promise.all (index 9)
at async processModule (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/DeltaBundler/graphOperations.js:279:3)
at async addDependency (/Users/tbrownio/repos/bloom/bloomtext-frontend/mobileChat/node_modules/metro/src/DeltaBundler/graphOperations.js:361:18)
at async Promise.all (index 3)
I'm trying to understand why the path is resolving to ...node_modules/react/index.js/jsx-runtime. I'm not what's causing the path to be created like this. the correct path should be ...node_modules/react/jsx-runtime.
Here is my package.json
{
"name": "mobileChat",
"workspaces": {
"nohoist": [
"jetifier",
"react-native",
"react-native/**",
"#react-native-community/**",
"react-native-navigation",
"react-native-onesignal",
"react-native-version-number",
"react-native-image-crop-picker",
"react-native-video",
"react-native-keyboard-aware-scroll-view",
"#sentry/**",
"#amplitude/react-native",
"react",
"#testing-library/react-native",
"#types/react-native"
]
},
"version": "3.22.0",
"private": true,
"scripts": {
"run-ios": "react-native run-ios",
"run-android": "react-native run-android",
"start": "node node_modules/react-native/local-cli/cli.js start",
"ts": "tsc --noEmit --jsx react-native --project tsconfig.json",
"ts:watch": "yarn ts --watch",
"test": "yarn jest",
"test:watch": "yarn jest --watch",
"version": "react-native-version --never-increment-build"
},
"dependencies": {
"#amplitude/react-native": "2.16.1",
"#babel/core": "^7.12.9",
"#babel/plugin-transform-runtime": "7.13.9",
"#babel/runtime": "7.12.5",
"#react-native-async-storage/async-storage": "1.15.17",
"#react-native-community/datetimepicker": "^3.5.2",
"#react-native-community/netinfo": "4.7.0",
"#react-native-community/push-notification-ios": "^1.10.0",
"#react-native-community/toolbar-android": "^0.2.1",
"#react-native-cookies/cookies": "6.0.11",
"#sentry/react-native": "4.6.1",
"#stream-io/flat-list-mvcp": "^0.10.1",
"linkify-it": "3.0.3",
"mdurl": "^1.0.1",
"prop-types": "^15.7.2",
"react": "18.1.0",
"react-native": "0.70.6",
"react-native-audio": "https://github.com/bloomapi/react-native-audio",
"react-native-bidirectional-infinite-scroll": "^0.3.3",
"react-native-deep-linking": "^2.2.0",
"react-native-get-random-values": "^1.8.0",
"react-native-image-crop-picker": "0.38.0",
"react-native-iphone-x-helper": "1.3.1",
"react-native-keyboard-aware-scroll-view": "0.9.5",
"react-native-linear-gradient": "2.5.6",
"react-native-navigation": "7.27.1",
"react-native-onesignal": "4.3.3",
"react-native-swipe-list-view": "^3.2.9",
"react-native-version-number": "0.3.6",
"react-native-video": "^6.0.0-alpha.3",
"react-redux": "7.2.2",
"redux": "4.0.5",
"redux-logger": "3.0.6",
"redux-persist": "4.10.2",
"rn-fetch-blob": "0.12.0",
"text-encoding-polyfill": "^0.6.7"
},
"devDependencies": {
"#babel/preset-env": "7.16.11",
"#testing-library/jest-native": "^4.0.1",
"#testing-library/react-hooks": "^7.0.1",
"#testing-library/react-native": "^8.0.0",
"#types/jest": "^26.0.23",
"#types/react": "^18.0.21",
"#types/react-native": "^0.70.6",
"#types/react-test-renderer": "18.0.0",
"babel-jest": "26.6.3",
"babel-plugin-module-resolver": "4.1.0",
"get-yarn-workspaces": "^1.0.2",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "0.72.3",
"react-native-version": "^4.0.0",
"react-test-renderer": "18.1.0",
"redux-devtools-extension": "^2.13.9",
"typescript": "^4.8.3"
}
}
Here's my metro.config.js
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* #format
*/
const path = require('path')
const getWorkspaces = require('get-yarn-workspaces')
const extraNodeModules = {
'mobile': path.resolve(__dirname, 'src'),
'components': 'mobile/components',
'screens': 'mobile/screens',
}
const workspaces = getWorkspaces(__dirname)
const watchFolders = [
path.resolve(__dirname, '..', 'node_modules'),
...workspaces.filter(dir => dir !== __dirname),
]
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
resolver: {
extraNodeModules,
},
watchFolders,
}
Here's my babel.config.js
const path = require('path')
module.exports = {
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
[
"module-resolver",
{
"extensions": [".ios.js", ".android.js", ".js", ".json"],
"alias": {
"react": require.resolve('react', { paths: [path.join(__dirname, './')] }),
'^react-native$': require.resolve('react-native', { paths: [path.join(__dirname, './')] }),
'^react-native/(.+)': ([, name]) => require.resolve(`react-native/${name}`, { paths: [path.join(__dirname, './')] }),
},
},
],
],
}
Been really stuck on this. Any help would greatly be appreciated.
I recently ran into the same issue when upgrading from expo SDK 45 to 46.
It turns out that completely removing the module-resolver from my babel.config.js file resolved the issue.
Even if you don't use expo, I still suggest you to investigate this way

react-native-testing-library with typescript does not work

I've tried for days many ways to set up #testing-library/react-native with typescript following a sort of guides but still not able to render a simple component.
Roughly, my stack is react native with typescript and jest with ts-jest and react-native-testing-library. I'm following these guidelines among others:
Typescript React Native with Jest
Using TypeScript with React Native
Using with React Native
This is the error I'm getting for the simple test component below:
ReferenceError: React is not defined
Component:
import React from 'react'
import { View, Text } from 'react-native'
const SomeComponent = () => {
return (
<View>
<Text>Hello</Text>
</View>
)
}
export default SomeComponent
Test:
import { render } from '#testing-library/react-native'
import SomeComponent from './SomeComponent'
describe('<SomeComponent>', () => {
it('should render some component', () => {
const { getByText } = render(<SomeComponent />)
expect(getByText(/hello/i)).toBeDefined()
})
})
Follow a bunch of configurations for reference:
Dependencies:
{
"name": "connect",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"debug": "open 'rndebugger://set-debugger-loc?host=localhost&port=8081' && react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"pod": "npx pod-install",
"test": "jest",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
},
"dependencies": {
"#airbrake/browser": "^2.1.7",
"#react-navigation/bottom-tabs": "^6.2.0",
"#react-navigation/native": "^6.0.8",
"#react-navigation/native-stack": "^6.5.1",
"#reduxjs/toolkit": "^1.8.0",
"#rneui/base": "^4.0.0-rc.1",
"#rneui/themed": "^4.0.0-rc.1",
"#types/react": "^17",
"#types/react-native-dotenv": "^0.2.0",
"#types/react-redux": "^7.1.23",
"axios": "^0.26.1",
"formik": "^2.2.9",
"http-status": "^1.5.0",
"moment": "^2.29.2",
"react": "17.0.2",
"react-native": "0.67.4",
"react-native-blob-util": "^0.13.8",
"react-native-dotenv": "^3.3.1",
"react-native-dropdownalert": "^4.5.1",
"react-native-error-boundary": "^1.1.12",
"react-native-loading-spinner-overlay": "^2.0.0",
"react-native-safe-area-context": "^4.2.2",
"react-native-screens": "^3.13.1",
"react-native-splash-screen": "^3.3.0",
"react-native-vector-icons": "^9.1.0",
"react-redux": "^7.2.6",
"redux-persist": "^6.0.0",
"redux-persist-filesystem-storage": "^4.0.0",
"use-debounce": "^7.0.1",
"yup": "^0.32.11"
},
"devDependencies": {
"#babel/core": "^7.12.9",
"#babel/preset-env": "^7.16.11",
"#babel/runtime": "^7.12.5",
"#react-native-community/eslint-config": "^3.0.1",
"#testing-library/jest-native": "^4.0.4",
"#testing-library/react-native": "^9.1.0",
"#types/jest": "^27.4.1",
"#types/react-native": "^0.66.15",
"#types/react-native-loading-spinner-overlay": "^0.5.3",
"#types/react-test-renderer": "^17.0.1",
"babel-jest": "^27.5.1",
"eslint": "7.32.0",
"jest": "^27.5.1",
"metro-react-native-babel-preset": "^0.70.1",
"react-addons-test-utils": "^15.6.2",
"react-native-typescript-transformer": "^1.2.13",
"react-test-renderer": "18.0.0",
"ts-jest": "^27.1.4",
"typescript": "^4.4.4"
},
"resolutions": {
"#types/react": "^17"
}
}
jest.config.js
const { defaults: tsjPreset } = require('ts-jest/presets')
module.exports = {
...tsjPreset,
preset: 'react-native',
transform: {
...tsjPreset.transform,
'\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
},
globals: {
'ts-jest': {
babelConfig: true,
},
},
transformIgnorePatterns: [
'/node_modules/(?!(#rneui|#react-native|react-native|react-native-size-matters|react-native-ratings|redux-persist-filesystem-storage|react-native-blob-util)/)',
],
setupFiles: ['./jest.setup.js'],
cacheDirectory: '.jest/cache',
}
babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module:react-native-dotenv',
{
moduleName: 'react-native-dotenv',
},
],
],
}
tsconfig.json
{
"compilerOptions": {
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"jsx": "react-native", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"noEmit": true, /* Do not emit outputs. */
"isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
"strict": true, /* Enable all strict type-checking options. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": false, /* Skip type checking of declaration files. */
"resolveJsonModule": true /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */
},
"exclude": [
"node_modules", "babel.config.js", "metro.config.js", "jest.config.js"
]
}
tsconfig.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"jsx": "react"
}
}
rn-cli.config.js
module.exports = {
getTransformModulePath() {
return require.resolve('react-native-typescript-transformer')
},
getSourceExts() {
return ['ts', 'tsx']
},
}
There is a similar open question about this setup here Setup for React Native/TypeScript/Testing Library/Jest not working
Thanks!
You need run npm install #babel/plugin-transform-react-jsx --save-dev into terminal and add plugin into babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module:react-native-dotenv',
{
moduleName: 'react-native-dotenv',
},
],
// add new plugin
[
'#babel/plugin-transform-react-jsx',
{
runtime: 'automatic',
},
],
],
}
You can use the official RNTL example that showcases a proper RNTL which also includes TypeScript.
Config issues like yours are hard to resolve as some small hard to notice issues might easily break the setup, so it's usually easiest to start with working setup.

Jest Testing React Native cannot use import statement outside of a module

I have been trying to find out how to fix the following error when trying to run my Jest test using React Native:
FAIL tests/App-test.js ● Test suite failed to run
/home/marijkebuurman/Desktop/sport-data-valley-questionnaire-app/node_modules/react-native/index.js:13
import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at ScriptTransformer._transformAndBuildScript (node_modules/#jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/#jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (node_modules/#react-navigation/native/lib/commonjs/useBackButton.tsx:3:1)
package.json
{
...
"dependencies": {
"#react-native-community/datetimepicker": "^2.3.2",
"#react-native-community/masked-view": "^0.1.8",
"#react-native-community/push-notification-ios": "^1.1.1",
"#react-native-firebase/app": "^6.7.1",
"#react-native-firebase/auth": "^6.7.1",
"#react-native-firebase/firestore": "^6.7.1",
"#react-navigation/bottom-tabs": "^5.2.6",
"#react-navigation/material-top-tabs": "^5.1.9",
"#react-navigation/native": "^5.1.5",
"#react-navigation/stack": "^5.2.10",
"axios": "^0.19.2",
"prop-types": "^15.7.2",
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-background-fetch": "^3.0.5",
"react-native-elements": "^1.2.7",
"react-native-gesture-handler": "^1.6.1",
"react-native-image-picker": "^2.3.1",
"react-native-keychain": "^5.0.1",
"react-native-push-notification": "^3.1.9",
"react-native-reanimated": "^1.8.0",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.4.0",
"react-native-tab-view": "^2.14.0",
"react-native-vector-icons": "^6.6.0"
},
"devDependencies": {
"#babel/core": "^7.6.2",
"#babel/preset-flow": "^7.10.1",
"#babel/runtime": "^7.6.2",
"#bam.tech/react-native-make": "^2.0.0",
"#react-native-community/eslint-config": "^0.0.5",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-plugin-module-resolver": "^4.0.0",
"eslint": "^6.5.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.4",
"react-native-dotenv": "^0.2.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native",
"setupFiles": [
"./jest.setup.js"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
]
}
}
I've tried to fix the problem in my package.json file by usingtransformIgnorePatterns to try and ignore react-navigation, but this hasn't worked.
.babelrc
{
"presets": [
"module:metro-react-native-babel-preset",
"module:react-native-dotenv",
"#babel/preset-flow"
],
"plugins": [
[
"module-resolver",
{
"cwd": "babelrc",
"root": [
"./src"
],
"extensions": [
".js",
".ios.js",
".android.js"
],
"alias": {
"api": "./src/api",
"assets": "./src/assets",
"services": "./src/services",
"styles": "./src/styles",
"components": "./src/components",
"app": "./src"
}
}
]
]
}
I've also installed and added #babel/preset-flow to the presets in my .babelrc file because I read somewhere this could help, but with no success.
My test
// import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', async () => {
renderer.create(<App />);
});
The test itself is pretty much the default provided React Native Jest test. I have commented the import statement for React Native as this was giving a similar error when running the test. When that line is uncommented the test still gives the error about AccessibilityInfo import statement, but there is no mention about #react-navigation but rather about ScriptTransformer.js.
My guess is that all this has something to do with my babel configuration, but I haven't been able to figure it out.
I've tried out a lot of changes to my .babelrc file, but I eventually fixed the error by converting my .babelrc file to a babel.config.js file that currently looks like this:
module.exports = function(api) {
api.cache(true);
return {
presets: [
'module:metro-react-native-babel-preset',
'module:react-native-dotenv',
'#babel/preset-flow',
],
plugins: [
[
'module-resolver',
{
root: ['./src'],
extensions: ['.js', '.ios.js', '.android.js'],
alias: {
api: './src/api',
assets: './src/assets',
services: './src/services',
styles: './src/styles',
components: './src/components',
app: './src',
},
},
],
['transform-class-properties'],
],
};
};
Aside from this I've added a lot of mocks inside my __mocks__ directory for different packages like for example react-native-firebase. I pretty sure that the mock files I added didn't have a part in fixing the error in my question, but there were other similar errors that were fixed by adding mock files.
By similar errors I mean errors that have an error message like:
SyntaxError: Cannot use import statement outside a module
In some cases, fixing this error requires adding the library, which the error complains about to the transformIgnorePatterns, which is in the file jest.config.js.
// jest.config.js
module.exports = {
preset: 'react-native',
setupFilesAfterEnv: [
// 1. specific setup for react-native
'#testing-library/jest-native/extend-expect',
// 2. global setup & mocking for some services:
'./jest.setup.js',
// 3. mocking for more services:
'./__mocks__/react-native-firebase.js',
"./__mocks__/#react-native-community/google-signin.ts",
],
// 4. You need to add the library to the RegEx below <- Problem Fix
// For example, if the error was related to "react-native-elements", you need to add it to the list (as shown below.)
transformIgnorePatterns: ["node_modules/(?!((jest-)?react-native|react-native-elements|#react-native(-community)?)/)"],
}
"react-native": "0.66.4"
"#testing-library/jest-native": "^4.0.4"
"#testing-library/react-native": "^9.0.0"
"babel-core": "^7.0.0-bridge.0"
"babel-jest": "^27.4.6"
"jest": "^27.4.7"
"metro-react-native-babel-preset": "^0.66.2"

CSS Loader has been initialised using an options object that does not match the API schema

Production build fails with the following error:
ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
- options has an unknown property 'minimize'. These properties are valid:
object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals? }
at validate (/Users/username/Sites/projectname/node_modules/css-loader/node_modules/schema-utils/dist/validate.js:85:11)
at Object.loader (/Users/username/Sites/projectname/node_modules/css-loader/dist/index.js:34:28)
Using this webpack template: http://vuejs-templates.github.io/webpack/, updated to Webpack 4 and faced build issues, package.json:
{
"name": "projectname",
"private": true,
"scripts": {
"dev": "webpack-dev-server --inline --progress --config webpack.config.js --port 3000 --hot",
"start": "npm run dev",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"#vue/cli-service": "^3.2.0",
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
"cross-env": "^5.2.0",
"css-loader": "^3.2.1",
"jquery": "^3.3.1",
"moment": "^2.22.2",
"style-loader": "^1.0.1",
"swiper": "^4.5.0",
"v-tabs-router": "^1.4.0",
"vee-validate": "^2.1.0-beta.9",
"vue": "^2.5.2",
"vue-awesome-swiper": "^3.1.3",
"vue-backtotop": "^1.6.1",
"vue-carousel": "^0.18.0",
"vue-cleave-component": "^2.1.2",
"vue-instant": "^1.0.2",
"vue-jsonp": "^0.1.8",
"vue-magic-line": "^0.2.1",
"vue-masked-input": "^0.5.2",
"vue-resource": "^1.5.1",
"vue-router": "^3.0.2",
"vue-select": "^2.5.1",
"vue-slider-component": "^2.8.0",
"vue-smooth-scroll": "^1.0.13",
"vuejs-datepicker": "^1.5.3",
"vuelidate": "^0.7.4",
"vuex": "^3.0.1"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-loader": "^7.1.1",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-vue-jsx": "^3.5.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"chalk": "^2.0.1",
"copy-webpack-plugin": "^4.0.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^5.0.2",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^3.2.0",
"node-notifier": "^5.1.2",
"node-sass": "^4.13.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"ora": "^1.2.0",
"portfinder": "^1.0.13",
"postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0",
"postcss-url": "^8.0.0",
"rimraf": "^2.6.0",
"sass-loader": "^8.0.0",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"uglifyjs-webpack-plugin": "^1.1.1",
"url-loader": "^1.1.2",
"vue-loader": "^14.2.2",
"vue-resource": "^1.5.1",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.5.2",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
},
"engines": {
"node": ">= 6.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
I've checked with grep -rl 'minimize' . in a project root, but didn't found any related options. Wth..
This has to do with a breaking change in the module. According to semver rules, a major change (first digit x of version x.y.z) is a breaking change.
In this case the old version syntax uses
{
loader: 'postcss-loader',
options: {
plugins: postCSSPlugins
}
}
whereas the new version in options uses nested postcssOptions before plugins
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: postCSSPlugins
}
}
}
I also encountered this error when updating to webpack 4 and using MiniCssExtractPlugin
I was able to eliminate the error and get a successful build by using an older version of ccs-loader
These versions worked for me
"devDependencies": {
"css-loader": "1.0.1",
OR
"css-loader": "^0.28.0",
css-loader 2* and 3* gave me the error.
UPDATE after testing publish: using the old version of the css-loader causes the css to fail to load when rendering the web site in a browser. So this appears to not be a solution.
I was having the same issue after I ran yarn upgrade --latest command.
It changed sass-loader version from 7.1.0 to 8.0.0. After that getting the same error.
There might be change in sass-loader options and due to that, this error occurred. You can check release: https://github.com/webpack-contrib/sass-loader/releases and updated options https://github.com/webpack-contrib/sass-loader
Try to remove/change sass-loader options, that can help you to resolve it.
Hope this can help, it would be great if can you share webpack.config.js file.
Cheers~
I had a similar problem upgrading to webpack 4. I used Vue.js CLI which generated a build/utils.js file that effectively has a configurable webpack based on whether I am in a development or a production environment.
Pertinent utils.js lines in auto generated (failing after upgrading) code:
var ExtractTextPlugin = require('extract-text-webpack-plugin')
exports.cssLoaders = function (options) {
options = options || {}
var cssLoader = {
loader: 'css-loader',
options: {
minimize: process.env.NODE_ENV === 'production',
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
var loaders = [cssLoader]
if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.loader({
use: loaders,
fallback: 'vue-style-loader'
})
} else {
return ['vue-style-loader'].concat(loaders)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
I tried the simple, "Let's just update the module.":
var ExtractTextPlugin = require('mini-css-extract-plugin')
Which gave me the error that you had. After much messing around with the files, I used the "Easter Egg Troubleshooting Strategy", and found that I could do the following and get it to work. What worked was the following:
npm i extract-loader --save-dev
then modify build/utils.js:
exports.cssLoaders = function (options) {
options = options || {}
var cssLoader = {
loader: 'css-loader',
options: {
// minimize: process.env.NODE_ENV === 'production',
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
var loaders = [cssLoader]
if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ['extract-loader'].concat(loaders)
// return ExtractTextPlugin.loader({
// use: loaders,
// fallback: 'vue-style-loader'
// })
} else {
return ['vue-style-loader'].concat(loaders)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
Notice, I commented out line 9, which is the source of my error. I also removed my sole dependency to the mini-css-extract-plugin with the extract-loader module.
// minimize: process.env.NODE_ENV === 'production',
Now I have to figure out why eslint is not liking my Vue syntax.

Sass-loader error 'options has an unknown property 'indentedSyntax'' when upgrading Vuetify from 1.5 to 2

I am upgrading Vuetify in my Vue CLI 3 project from version 1.5 to 2. I have followed these instructions, doing a full install. Since the upgrade, running 'npm run serve' gives me a ton of errors looking like this:
error in ./node_modules/vuetify/src/components/VGrid/_grid.sass
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
ValidationError: Invalid options object. Sass Loader has been initialised using an options object that does not match the API schema.
- options has an unknown property 'indentedSyntax'. These properties are valid:
object { implementation?, sassOptions?, prependData?, sourceMap?, webpackImporter? }
at validate (C:\Users\kristoffer.dahl\Documents\Prosjekter\fridgein\fridgein_fe\node_modules\sass-loader\node_modules\schema-utils\dist\validate.js:50:11)
at Object.loader (C:\Users\kristoffer.dahl\Documents\Prosjekter\fridgein\fridgein_fe\node_modules\sass-loader\dist\index.js:36:28)
All errors look the same, except for the Vuetify component mentioned.
This is my package.json:
{
"name": "fridgein_fe",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.22",
"#fortawesome/free-brands-svg-icons": "^5.10.2",
"#fortawesome/free-regular-svg-icons": "^5.10.2",
"#fortawesome/free-solid-svg-icons": "^5.10.2",
"#fortawesome/vue-fontawesome": "^0.1.7",
"#vue/cli": "^3.11.0",
"#vue/cli-shared-utils": "^3.11.0",
"auth0-js": "^9.11.3",
"axios": "^0.18.1",
"dotenv": "^8.1.0",
"es6-promise": "^4.2.8",
"jquery": "^3.4.1",
"js-cookie": "^2.2.1",
"popper.js": "^1.14.7",
"register-service-worker": "^1.6.2",
"sockjs-client": "^1.3.0",
"vue": "^2.6.10",
"vue-float-action-button": "^0.4.4",
"vue-google-charts": "^0.3.2",
"vue-plugin-load-script": "^1.2.0",
"vue-router": "^3.0.2",
"vuetify": "^2.1.0",
"vuex": "^3.1.1",
"vuex-persistedstate": "^2.5.4",
"webstomp-client": "^1.2.6"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.4.0",
"#vue/cli-plugin-eslint": "^3.4.0",
"#vue/cli-plugin-pwa": "^3.11.0",
"#vue/cli-service": "^3.5.1",
"babel-eslint": "^10.0.1",
"babel-plugin-module-resolver": "^3.2.0",
"css-loader": "^2.1.1",
"deepmerge": "^4.0.0",
"eslint": "^5.0.0",
"eslint-plugin-vue": "^5.2.3",
"eslint-plugin-vuetify": "^1.0.0-beta.3",
"fibers": "^4.0.1",
"sass": "^1.23.0",
"sass-loader": "^8.0.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"vue-cli-plugin-vuetify": "^0.5.0",
"vue-template-compiler": "^2.6.10",
"vuetify-loader": "^1.3.0",
"webpack": "^4.41.0"
},
This is my vue.config.js:
module.exports = {
css: {
loaderOptions: {
sass: {
data: `#import "~#/sass/main.scss"`
},
},
},
}
I have checked out and tried every solution presented in relevant issues on the developers' Github pages, but none has has any effect.
Has anyone else encountered this?
I had the same issue and fixed it by downgrading sass-loader by setting
"sass-loader": "7.3.1",
in package.json.
This was suggested on the Vuetify Discord
Actually you are using sass-loader 8+ and it has an option a little bit different.
Try using prependData instead of data.
Check this github issue
module.exports = {
css: {
loaderOptions: {
sass: {
prependData: `#import "~#/sass/main.scss"`
}
}
}
}
Update for those using version 10+. prependData is no longer valid. However you can use additionalData as a drop-in replacement.
Using the webpack 5.1.3 and sass-loader version 10.0.3, I had to use a different syntax (additionalData instead of prependData), as bellow:
webpack.config.js
{
test: /\.(css|scss|sass|less)$/,
use: [
'style-loader',
'css-loader',
'sass-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true,
additionalData: '#import "assets/scss/_variables.scss";'
},
},
},
],
},
Bear in mind the syntax requires the double quotes in the path and the semicolon before the end of the single quote just as if you were writing a normal scss file.
Example:
'#import "path/_file.scss";'
In case you need to import multiple files you can use the template string symbol (``) as bellow:
{
test: /\.(css|scss|sass|less)$/,
use: [
...
{
...
options: {
sourceMap: true,
additionalData: `#import "assets/scss/_variables.scss";
#import "assets/scss/_mixins.scss";
#import "assets/scss/_misc.scss";`
},
},
},
],
},
You can also take the advantage of template string as bellow:
`#import "${path.resolve(APP_DIR, '/assets/scss/')}/_variables.scss";`
Be advised: I tried this approach to reduce bundle size and the result was terrible as when you start to see what the webpack is actually doing you return to the basic import all needed styles in your main component as sass-loader will actually prepend in multiple components the styles you inform in the additional data. In my case the bundle got 3 times it's original size.
Cheers
If you use NUXT you should configure like this:
//nuxt.config.js
loaders: {
less: {
lessOptions: {
javascriptEnabled: true,
modifyVars: {
'primary-color': '#5c6ac4',
'layout-body-background': '#fff'
}
}
}
}