Skip to content

Commit

Permalink
Add .mjs support (#3239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored and gaearon committed Oct 28, 2017
1 parent 4add16d commit 5c8000f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = {
// https://github.com/facebookincubator/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: ['.web.js', '.js', '.json', '.web.jsx', '.jsx'],
extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
alias: {
// @remove-on-eject-begin
// Resolve Babel runtime relative to react-scripts.
Expand Down Expand Up @@ -126,7 +126,7 @@ module.exports = {
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx)$/,
test: /\.(js|jsx|mjs)$/,
enforce: 'pre',
use: [
{
Expand Down Expand Up @@ -164,7 +164,7 @@ module.exports = {
},
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports = {
// https://github.com/facebookincubator/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: ['.web.js', '.js', '.json', '.web.jsx', '.jsx'],
extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
alias: {
// @remove-on-eject-begin
// Resolve Babel runtime relative to react-scripts.
Expand Down Expand Up @@ -130,7 +130,7 @@ module.exports = {
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx)$/,
test: /\.(js|jsx|mjs)$/,
enforce: 'pre',
use: [
{
Expand Down Expand Up @@ -169,7 +169,7 @@ module.exports = {
},
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
Expand Down
24 changes: 17 additions & 7 deletions packages/react-scripts/scripts/utils/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,37 @@ module.exports = (resolve, rootDir, isEjecting) => {
// TODO: I don't know if it's safe or not to just use / as path separator
// in Jest configs. We need help from somebody with Windows to determine this.
const config = {
collectCoverageFrom: ['src/**/*.{js,jsx}'],
collectCoverageFrom: ['src/**/*.{js,jsx,mjs}'],
setupFiles: [resolve('config/polyfills.js')],
setupTestFrameworkScriptFile: setupTestsFile,
testMatch: [
'<rootDir>/src/**/__tests__/**/*.js?(x)',
'<rootDir>/src/**/?(*.)(spec|test).js?(x)',
'<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}',
'<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}',
],
testEnvironment: 'node',
testURL: 'http://localhost',
transform: {
'^.+\\.(js|jsx)$': isEjecting
'^.+\\.(js|jsx|mjs)$': isEjecting
? '<rootDir>/node_modules/babel-jest'
: resolve('config/jest/babelTransform.js'),
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
'^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
'^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve(
'config/jest/fileTransform.js'
),
},
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$'],
moduleNameMapper: {
'^react-native$': 'react-native-web',
},
moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
moduleFileExtensions: [
'web.js',
'mjs',
'js',
'json',
'web.jsx',
'jsx',
'node',
],
};
if (rootDir) {
config.rootDir = rootDir;
Expand Down

1 comment on commit 5c8000f

@leebyron
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this adds support for finding .mjs files, however encountering the mjs file can then result in a syntax error if --experimental-modules was not provided.

The module to load should be based on the same algorithm as node resolution.

Please sign in to comment.