Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .mjs support back to webpack (it has native ESM support) #5258

Merged
merged 4 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ module.exports = {
// https://github.com/facebook/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: ['.mjs', '.web.js', '.js', '.json', '.web.jsx', '.jsx'],
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
Expand Down Expand Up @@ -174,11 +174,20 @@ module.exports = {
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// webpack 4 turned on stricter defaults for .mjs, preventing it from
// performing ESM/CommonJS interoperation. While the ecosystem is
// fleshing out experimental .mjs support, we need to relax this
// constraint.
// https://github.com/facebook/create-react-app/pull/5258#discussion_r222155465
{
test: /\.mjs$/,
type: 'javascript/auto',
},

// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx)$/,
test: /\.(js|mjs|jsx)$/,
enforce: 'pre',
use: [
{
Expand All @@ -199,16 +208,6 @@ module.exports = {
],
include: paths.appSrc,
},
{
// `mjs` support is still in its infancy in the ecosystem, so we don't
// support it.
// Modules who define their `browser` or `module` key as `mjs` force
// the use of this extension, so we need to tell webpack to fall back
// to auto mode (ES Module interop, allows ESM to import CommonJS).
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
{
Timer marked this conversation as resolved.
Show resolved Hide resolved
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
Expand All @@ -228,7 +227,7 @@ module.exports = {
// Process application JS with Babel.
// The preset includes JSX, Flow, and some ESnext features.
{
test: /\.(js|jsx)$/,
test: /\.(js|mjs|jsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
Expand Down Expand Up @@ -274,7 +273,7 @@ module.exports = {
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.js$/,
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
Expand Down Expand Up @@ -361,7 +360,7 @@ module.exports = {
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|jsx)$/, /\.html$/, /\.json$/],
exclude: [/\.(js|mjs|jsx)$/, /\.html$/, /\.json$/],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
Expand Down
29 changes: 14 additions & 15 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module.exports = {
// https://github.com/facebook/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: ['.mjs', '.web.js', '.js', '.json', '.web.jsx', '.jsx'],
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
Expand Down Expand Up @@ -240,11 +240,20 @@ module.exports = {
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// webpack 4 turned on stricter defaults for .mjs, preventing it from
// performing ESM/CommonJS interoperation. While the ecosystem is
// fleshing out experimental .mjs support, we need to relax this
// constraint.
// https://github.com/facebook/create-react-app/pull/5258#discussion_r222155465
{
test: /\.mjs$/,
type: 'javascript/auto',
},

// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx)$/,
test: /\.(js|mjs|jsx)$/,
enforce: 'pre',
use: [
{
Expand All @@ -267,16 +276,6 @@ module.exports = {
],
include: paths.appSrc,
},
{
// `mjs` support is still in its infancy in the ecosystem, so we don't
// support it.
// Modules who define their `browser` or `module` key as `mjs` force
// the use of this extension, so we need to tell webpack to fall back
// to auto mode (ES Module interop, allows ESM to import CommonJS).
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
Expand All @@ -295,7 +294,7 @@ module.exports = {
// Process application JS with Babel.
// The preset includes JSX, Flow, and some ESnext features.
{
test: /\.(js|jsx)$/,
test: /\.(js|mjs|jsx)$/,
include: paths.appSrc,

loader: require.resolve('babel-loader'),
Expand Down Expand Up @@ -340,7 +339,7 @@ module.exports = {
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.js$/,
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
Expand Down Expand Up @@ -445,7 +444,7 @@ module.exports = {
// it's runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|jsx)$/, /\.html$/, /\.json$/],
exclude: [/\.(js|mjs|jsx)$/, /\.html$/, /\.json$/],
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
Expand Down