Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 29 additions & 0 deletions packages/gatsby/src/utils/__tests__/webpack-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const utils = require(`../webpack-utils`)

let config
beforeAll(async () => {
config = await utils({
stage: `develop`,
program: {
browserslist: [],
},
})
})

describe(`webpack utils`, () => {
describe(`mjs`, () => {
it(`adds .mjs rule`, () => {
expect(config.rules.mjs).toEqual(expect.any(Function))
})

it(`returns correct rule`, () => {
const rule = config.rules.mjs()

expect(rule).toEqual({
include: /node_modules/,
test: /\.mjs$/,
type: `javascript/auto`,
})
})
})
})
19 changes: 19 additions & 0 deletions packages/gatsby/src/utils/webpack-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ module.exports = async ({
rules.js = js
}

/**
* mjs loader:
* webpack 4 has issues automatically dealing with
* the .mjs extension, thus we need to explicitly
* add this rule to use the default webpack js loader
*/
{
let mjs = (options = {}) => {
return {
test: /\.mjs$/,
include: /node_modules/,
type: `javascript/auto`,
...options,
}
}

rules.mjs = mjs
}

{
let eslint = schema => {
return {
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ module.exports = async (
// Common config for every env.
// prettier-ignore
let configRules = [
rules.mjs(),
rules.js(),
rules.yaml(),
rules.fonts(),
Expand Down Expand Up @@ -359,10 +360,7 @@ module.exports = async (
// modules. But also make it possible to install modules within the src
// directory if you need to install a specific version of a module for a
// part of your site.
modules: [
directoryPath(path.join(`node_modules`)),
`node_modules`,
],
modules: [directoryPath(path.join(`node_modules`)), `node_modules`],
alias: {
gatsby$: directoryPath(path.join(`.cache`, `gatsby-browser-entry.js`)),
// Using directories for module resolution is mandatory because
Expand Down