diff --git a/packages/core/src/resolver.js b/packages/core/src/resolver.js index 52803afe84..bcded60ef6 100644 --- a/packages/core/src/resolver.js +++ b/packages/core/src/resolver.js @@ -3,7 +3,12 @@ import fs from 'fs-extra' import consola from 'consola' import esm from 'esm' -import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/utils' +import { + startsWithRootAlias, + startsWithSrcAlias, + isExternalDependency, + clearRequireCache +} from '@nuxt/utils' export default class Resolver { constructor (nuxt) { @@ -138,9 +143,17 @@ export default class Resolver { lastError = e } + const isExternal = isExternalDependency(resolvedPath) + + // in dev mode make sure to clear the require cache so after + // a dev server restart any changed file is reloaded + if (this.options.dev && !isExternal) { + clearRequireCache(resolvedPath) + } + // By default use esm only for js,mjs files outside of node_modules if (useESM === undefined) { - useESM = /.(js|mjs)$/.test(resolvedPath) && !/node_modules/.test(resolvedPath) + useESM = !isExternal && /.(js|mjs)$/.test(resolvedPath) } // Try to require diff --git a/packages/core/test/resolver.test.js b/packages/core/test/resolver.test.js index 8c3ac46e53..4b0deb4482 100644 --- a/packages/core/test/resolver.test.js +++ b/packages/core/test/resolver.test.js @@ -442,7 +442,7 @@ describe('core: resolver', () => { const resolver = new Resolver({ options: {} }) - resolver.resolvePath = jest.fn() + resolver.resolvePath = jest.fn().mockReturnValue('/var/nuxt/resolver/file.js') resolver.esm = jest.fn() resolver.requireModule('/var/nuxt/resolver/file.js', { esm: true }) diff --git a/packages/utils/src/cjs.js b/packages/utils/src/cjs.js index 453fca7da2..8b06c43507 100644 --- a/packages/utils/src/cjs.js +++ b/packages/utils/src/cjs.js @@ -1,6 +1,10 @@ +export function isExternalDependency (id) { + return /[/\\]node_modules[/\\]/.test(id) +} + export function clearRequireCache (id) { const entry = require.cache[id] - if (!entry || id.includes('node_modules')) { + if (!entry || isExternalDependency(id)) { return } @@ -17,7 +21,7 @@ export function clearRequireCache (id) { export function scanRequireTree (id, files = new Set()) { const entry = require.cache[id] - if (!entry || id.includes('node_modules') || files.has(id)) { + if (!entry || isExternalDependency(id) || files.has(id)) { return files } diff --git a/packages/utils/test/cjs.test.js b/packages/utils/test/cjs.test.js new file mode 100644 index 0000000000..4b5c1b9772 --- /dev/null +++ b/packages/utils/test/cjs.test.js @@ -0,0 +1,14 @@ +import { isExternalDependency, getPKG } from '../src/cjs' + +describe('util: cjs', () => { + test('isExternalDependency works', () => { + expect(isExternalDependency('/var/nuxt/node_modules/dependency.js')).toBe(true) + expect(isExternalDependency('/var/nuxt/dependency.js')).toBe(false) + expect(isExternalDependency('C:\\nuxt\\node_modules\\dependency.js')).toBe(true) + expect(isExternalDependency('C:\\nuxt\\dependency.js')).toBe(false) + }) + + test('getPKG should not throw error on non existing package', () => { + expect(() => getPKG('this-doesnt-existsw')).not.toThrow() + }) +})