From 2c1eb8767180fc04b91fb409976b4fe1e0c3047d Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sat, 3 Apr 2021 14:42:02 +0200 Subject: [PATCH] fix: webpack compilation (#41) --- packages/kit/src/config/schema/_common.ts | 5 +- packages/nuxt3/package.json | 2 +- packages/nuxt3/src/webpack/configs/server.ts | 44 ++--- .../nuxt3/src/webpack/plugins/externals.ts | 151 ------------------ packages/nuxt3/src/webpack/webpack.ts | 10 +- playground/nuxt.config.ts | 2 +- yarn.lock | 8 +- 7 files changed, 35 insertions(+), 187 deletions(-) delete mode 100644 packages/nuxt3/src/webpack/plugins/externals.ts diff --git a/packages/kit/src/config/schema/_common.ts b/packages/kit/src/config/schema/_common.ts index 0e272f0765..3be87049de 100644 --- a/packages/kit/src/config/schema/_common.ts +++ b/packages/kit/src/config/schema/_common.ts @@ -98,7 +98,10 @@ export default { modulesDir: { $default: ['node_modules'], - $resolve: (val, get) => val.map(dir => resolve(get('rootDir'), dir)) + $resolve: (val, get) => [].concat( + val.map(dir => resolve(get('rootDir'), dir)), + resolve(process.cwd(), 'node_modules') + ) }, dir: { diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 0b4aef88c0..3be5fde013 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -76,7 +76,7 @@ "url-loader": "^4.1.1", "vite": "^2.1.5", "vue": "^3.0.11", - "vue-loader": "npm:@pi0/vue-loader@^16.1.2-patch.1", + "vue-loader": "^16.2.0", "vue-router": "^4.0.5", "vue-style-loader": "^4.1.3", "vuex5": "0.5.0-testing.3", diff --git a/packages/nuxt3/src/webpack/configs/server.ts b/packages/nuxt3/src/webpack/configs/server.ts index c09cdbec11..07cd76cd40 100644 --- a/packages/nuxt3/src/webpack/configs/server.ts +++ b/packages/nuxt3/src/webpack/configs/server.ts @@ -1,7 +1,4 @@ -import path from 'path' -import fs from 'fs' import { ProvidePlugin } from 'webpack' -import nodeExternals from '../plugins/externals' import { WebpackConfigContext, applyPresets, getWebpackConfig } from '../utils/config' import { nuxt } from '../presets/nuxt' import { node } from '../presets/node' @@ -34,26 +31,29 @@ function serverPreset (ctx: WebpackConfigContext) { } function serverStandalone (ctx: WebpackConfigContext) { - const { options, config } = ctx + // TODO: Refactor this out of webpack + const inline = [ + 'src/', + 'nuxt/app', + 'nuxt/build', + '@nuxt/app', + 'vuex5', + '-!', + ...ctx.options.build.transpile + ] - // https://webpack.js.org/configuration/externals/#externals - // https://github.com/liady/webpack-node-externals - // https://vue-loader.vuejs.org/migrating.html#ssr-externals - if (!options.build.standalone) { - options.modulesDir.forEach((dir) => { - if (fs.existsSync(dir)) { - (config.externals as any[]).push( - nodeExternals({ - whitelist: [ - modulePath => !['.js', '.json', ''].includes(path.extname(modulePath)), - ctx.transpile - ], - modulesDir: dir - }) - ) - } - }) - } + if (!Array.isArray(ctx.config.externals)) { return } + ctx.config.externals.push(({ request }, cb) => { + if ( + request[0] === '.' || + request[0] === '/' || + inline.find(prefix => request.startsWith(prefix)) + ) { + return cb(null, false) + } + // console.log('External:', request) + return cb(null, true) + }) } function serverPlugins (ctx: WebpackConfigContext) { diff --git a/packages/nuxt3/src/webpack/plugins/externals.ts b/packages/nuxt3/src/webpack/plugins/externals.ts deleted file mode 100644 index 704917c39e..0000000000 --- a/packages/nuxt3/src/webpack/plugins/externals.ts +++ /dev/null @@ -1,151 +0,0 @@ -import fs from 'fs' -import path from 'path' - -function contains (arr, val) { - return arr && arr.includes(val) -} - -const atPrefix = /^@/g - -function readDir (dirName) { - if (!fs.existsSync(dirName)) { - return [] - } - - try { - return fs - .readdirSync(dirName) - .map(function (module) { - if (atPrefix.test(module)) { - // reset regexp - atPrefix.lastIndex = 0 - try { - return fs - .readdirSync(path.join(dirName, module)) - .map(function (scopedMod) { - return module + '/' + scopedMod - }) - } catch (e) { - return [module] - } - } - return module - }) - .reduce(function (prev, next) { - return prev.concat(next) - }, []) - } catch (e) { - return [] - } -} - -function readFromPackageJson (options) { - if (typeof options !== 'object') { - options = {} - } - // read the file - let packageJson - try { - const fileName = options.fileName || 'package.json' - const packageJsonString = fs.readFileSync( - path.join(process.cwd(), './' + fileName), - 'utf8' - ) - packageJson = JSON.parse(packageJsonString) - } catch (e) { - return [] - } - // sections to search in package.json - let sections = [ - 'dependencies', - 'devDependencies', - 'peerDependencies', - 'optionalDependencies' - ] - if (options.include) { - sections = [].concat(options.include) - } - if (options.exclude) { - sections = sections.filter(function (section) { - return ![].concat(options.exclude).includes(section) - }) - } - // collect dependencies - const deps = {} - sections.forEach(function (section) { - Object.keys(packageJson[section] || {}).forEach(function (dep) { - deps[dep] = true - }) - }) - return Object.keys(deps) -} - -function containsPattern (arr, val) { - return ( - arr && - arr.some(function (pattern) { - if (pattern instanceof RegExp) { - return pattern.test(val) - } else if (typeof pattern === 'function') { - return pattern(val) - } else { - return pattern === val - } - }) - ) -} - -const scopedModuleRegex = /@[a-zA-Z0-9][.\\w-]+\/[a-zA-Z0-9][.\\w-]+([a-zA-Z0-9./]+)?/g - -function getModuleName (request, includeAbsolutePaths) { - let req = request - const delimiter = '/' - - if (includeAbsolutePaths) { - req = req.replace(/^.*?\/node_modules\//, '') - } - // check if scoped module - if (scopedModuleRegex.test(req)) { - // reset regexp - scopedModuleRegex.lastIndex = 0 - return req.split(delimiter, 2).join(delimiter) - } - return req.split(delimiter)[0] -} - -export default function nodeExternals (options) { - options = options || {} - const whitelist = [].concat(options.whitelist || []) - const binaryDirs = [].concat(options.binaryDirs || ['.bin']) - const importType = options.importType || 'commonjs' - const modulesDir = options.modulesDir || 'node_modules' - const modulesFromFile = !!options.modulesFromFile - const includeAbsolutePaths = !!options.includeAbsolutePaths - - // helper function - function isNotBinary (x) { - return !contains(binaryDirs, x) - } - - // create the node modules list - const nodeModules = modulesFromFile - ? readFromPackageJson(options.modulesFromFile) - : readDir(modulesDir).filter(isNotBinary) - - // return an externals function - return function ({ context: _context, request }, callback) { - const moduleName = getModuleName(request, includeAbsolutePaths) - if ( - contains(nodeModules, moduleName) && - !containsPattern(whitelist, request) - ) { - if (typeof importType === 'function') { - return callback(null, importType(request)) - } - // mark this module as external - // https://webpack.js.org/configuration/externals/ - return callback(null, importType + ' ' + request) - } - callback() - } -} diff --git a/packages/nuxt3/src/webpack/webpack.ts b/packages/nuxt3/src/webpack/webpack.ts index 9aae1ad6d8..736a3e2884 100644 --- a/packages/nuxt3/src/webpack/webpack.ts +++ b/packages/nuxt3/src/webpack/webpack.ts @@ -68,13 +68,9 @@ class WebpackBundler { this.getWebpackConfig('client') ] - if (options.modern) { - webpackConfigs.push(this.getWebpackConfig('modern')) - } - - if (options.build.ssr) { - webpackConfigs.push(this.getWebpackConfig('server')) - } + // if (options.build.ssr) { + webpackConfigs.push(this.getWebpackConfig('server')) + // } await this.nuxt.callHook('webpack:config', webpackConfigs) diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 70d274d93d..7ce07971f9 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -1,5 +1,5 @@ import { defineNuxtConfig } from '@nuxt/kit' export default defineNuxtConfig({ - vite: true + vite: !true }) diff --git a/yarn.lock b/yarn.lock index fce6c68496..36dfde550c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10909,10 +10909,10 @@ vue-eslint-parser@^7.6.0: esquery "^1.4.0" lodash "^4.17.15" -"vue-loader@npm:@pi0/vue-loader@^16.1.2-patch.1": - version "16.1.2-patch.1" - resolved "https://registry.yarnpkg.com/@pi0/vue-loader/-/vue-loader-16.1.2-patch.1.tgz#73e04d5c2f474fa5580a8296b3a89f695136fd9e" - integrity sha512-LP/ykOO2i2YlnVo57stE1CyNe36ECZaEQVhG0MBGY3CW5Y+OEvYOih3/9jWh7lt/aV6d++CWsTVFj6GsadBtMg== +vue-loader@^16.2.0: + version "16.2.0" + resolved "https://registry.yarnpkg.com/vue-loader/-/vue-loader-16.2.0.tgz#046a53308dd47e58efe20ddec1edec027ce3b46e" + integrity sha512-TitGhqSQ61RJljMmhIGvfWzJ2zk9m1Qug049Ugml6QP3t0e95o0XJjk29roNEiPKJQBEi8Ord5hFuSuELzSp8Q== dependencies: chalk "^4.1.0" hash-sum "^2.0.0"