generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: partial module path match fix (#23)
fix for partial matches #22 use typescript for validation in development #17 * fix: dont use pull request target * feat: update example storybook version * feat: use typescript and add node_module to path * fix: add project token for fork PRs * fix: add build step to action * refactor: remove babel config since it's no longer needed
- Loading branch information
Showing
7 changed files
with
468 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = { | ||
presets: [ | ||
'@babel/preset-env', | ||
'@babel/preset-typescript', | ||
'@babel/preset-react', | ||
], | ||
env: { | ||
esm: { | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
modules: false, | ||
}, | ||
], | ||
], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,5 @@ | ||
const webpack = require('webpack'); | ||
const path = require('path'); | ||
|
||
const getBabelPlugins = (options) => { | ||
let reactNativeWeb = 'react-native-web'; | ||
if (options.babelPlugins && Array.isArray(options.babelPlugins)) { | ||
return [reactNativeWeb, ...options.babelPlugins]; | ||
} | ||
return [reactNativeWeb]; | ||
}; | ||
|
||
const getModule = (name) => path.join('node_modules', name); | ||
|
||
// copied from https://github.com/expo/expo-cli/blob/master/packages/webpack-config/src/loaders/createBabelLoader.ts | ||
const DEFAULT_INCLUDES = [ | ||
getModule('react-native'), | ||
getModule('react-navigation'), | ||
getModule('expo'), | ||
getModule('unimodules'), | ||
getModule('@react'), | ||
getModule('@expo'), | ||
getModule('@use-expo'), | ||
getModule('@unimodules'), | ||
getModule('native-base'), | ||
getModule('styled-components'), | ||
]; | ||
|
||
const DEFAULT_EXCLUDES = [ | ||
'/node_modules', | ||
'/bower_components', | ||
'/.expo/', | ||
// Prevent transpiling webpack generated files. | ||
'(webpack)', | ||
]; | ||
const webpackFinal = require('./dist/cjs/webpack'); | ||
|
||
module.exports = { | ||
babel: async (config, options) => ({ | ||
...config, | ||
plugins: [...config.plugins, ...getBabelPlugins(options)], | ||
}), | ||
webpackFinal: async (config, options) => { | ||
// swap out react-native for react-native-web | ||
config.resolve.alias = { | ||
'react-native$': 'react-native-web', | ||
}; | ||
|
||
// Add __DEV__ global variable which is relied on by many libraries | ||
config.plugins.push( | ||
new webpack.DefinePlugin({ | ||
'process.env.NODE_ENV': JSON.stringify( | ||
process.env.NODE_ENV || 'development', | ||
), | ||
__DEV__: process.env.NODE_ENV !== 'production' || true, | ||
}), | ||
); | ||
|
||
const babelPlugins = getBabelPlugins(options); | ||
const root = (options && options.projectRoot) || process.cwd(); | ||
const modules = [ | ||
...DEFAULT_INCLUDES, | ||
...((options && options.modulesToTranspile) || []), | ||
]; | ||
|
||
// fix for uncompiled react-native dependencies | ||
config.module.rules.push({ | ||
test: /\.(js|jsx|ts|tsx)$/, | ||
loader: 'babel-loader', | ||
// include logic copied from https://github.com/expo/expo-cli/blob/master/packages/webpack-config/src/loaders/createBabelLoader.ts | ||
include(filename) { | ||
if (!filename) { | ||
return false; | ||
} | ||
|
||
for (const possibleModule of modules) { | ||
if (filename.includes(path.normalize(possibleModule))) { | ||
return true; | ||
} | ||
} | ||
|
||
if (filename.includes(root)) { | ||
for (const excluded of DEFAULT_EXCLUDES) { | ||
if (filename.includes(path.normalize(excluded))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
}, | ||
options: { | ||
root, | ||
presets: [ | ||
[ | ||
'module:metro-react-native-babel-preset', | ||
{ | ||
useTransformReactJSXExperimental: true, | ||
}, | ||
], | ||
[ | ||
'@babel/preset-react', | ||
{ | ||
runtime: 'automatic', | ||
}, | ||
], | ||
], | ||
plugins: [...babelPlugins, '@babel/plugin-proposal-class-properties'], | ||
}, | ||
}); | ||
|
||
config.resolve.extensions = ['.web.js', ...config.resolve.extensions]; | ||
|
||
return config; | ||
}, | ||
webpackFinal, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import webpack from 'webpack'; | ||
import path from 'path'; | ||
|
||
export type Options = { | ||
modulesToTranspile?: string[]; | ||
babelPlugins?: string[]; | ||
projectRoot?: string; | ||
}; | ||
|
||
export const getBabelPlugins = (options: Options) => { | ||
let reactNativeWeb = 'react-native-web'; | ||
if (options.babelPlugins && Array.isArray(options.babelPlugins)) { | ||
return [reactNativeWeb, ...options.babelPlugins]; | ||
} | ||
return [reactNativeWeb]; | ||
}; | ||
|
||
const getModule = (name: string) => path.join('node_modules', name); | ||
|
||
// copied from https://github.com/expo/expo-cli/blob/master/packages/webpack-config/src/loaders/createBabelLoader.ts | ||
const DEFAULT_INCLUDES = [ | ||
getModule('react-native'), | ||
getModule('react-navigation'), | ||
getModule('expo'), | ||
getModule('unimodules'), | ||
getModule('@react'), | ||
getModule('@expo'), | ||
getModule('@use-expo'), | ||
getModule('@unimodules'), | ||
getModule('native-base'), | ||
getModule('styled-components'), | ||
]; | ||
|
||
const DEFAULT_EXCLUDES = [ | ||
'/node_modules', | ||
'/bower_components', | ||
'/.expo/', | ||
// Prevent transpiling webpack generated files. | ||
'(webpack)', | ||
]; | ||
|
||
const webpackFinal = async (config: any, options: Options) => { | ||
// swap out react-native for react-native-web | ||
config.resolve.alias = { | ||
'react-native$': 'react-native-web', | ||
}; | ||
|
||
// Add __DEV__ global variable which is relied on by many libraries | ||
config.plugins.push( | ||
new webpack.DefinePlugin({ | ||
'process.env.NODE_ENV': JSON.stringify( | ||
process.env.NODE_ENV || 'development', | ||
), | ||
__DEV__: process.env.NODE_ENV !== 'production' || true, | ||
}), | ||
); | ||
|
||
const babelPlugins = getBabelPlugins(options); | ||
const root = options.projectRoot ?? process.cwd(); | ||
const userModules = options.modulesToTranspile?.map(getModule) ?? []; | ||
const modules = [...DEFAULT_INCLUDES, ...userModules]; | ||
|
||
// fix for uncompiled react-native dependencies | ||
config.module.rules.push({ | ||
test: /\.(js|jsx|ts|tsx)$/, | ||
loader: 'babel-loader', | ||
// include logic copied from https://github.com/expo/expo-cli/blob/master/packages/webpack-config/src/loaders/createBabelLoader.ts | ||
include(filename: string) { | ||
if (!filename) { | ||
return false; | ||
} | ||
|
||
for (const possibleModule of modules) { | ||
if (filename.includes(path.normalize(possibleModule))) { | ||
return true; | ||
} | ||
} | ||
|
||
if (filename.includes(root)) { | ||
for (const excluded of DEFAULT_EXCLUDES) { | ||
if (filename.includes(path.normalize(excluded))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
}, | ||
options: { | ||
root, | ||
presets: [ | ||
[ | ||
'module:metro-react-native-babel-preset', | ||
{ | ||
useTransformReactJSXExperimental: true, | ||
}, | ||
], | ||
[ | ||
'@babel/preset-react', | ||
{ | ||
runtime: 'automatic', | ||
}, | ||
], | ||
], | ||
plugins: [...babelPlugins, '@babel/plugin-proposal-class-properties'], | ||
}, | ||
}); | ||
|
||
config.resolve.extensions = ['.web.js', ...config.resolve.extensions]; | ||
|
||
return config; | ||
}; | ||
|
||
module.exports = webpackFinal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.