mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix: webpack compilation (#41)
This commit is contained in:
parent
efabacd8e2
commit
2c1eb87671
@ -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: {
|
||||
|
@ -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",
|
||||
|
@ -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) {
|
||||
|
@ -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<string[]>(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()
|
||||
}
|
||||
}
|
@ -68,13 +68,9 @@ class WebpackBundler {
|
||||
this.getWebpackConfig('client')
|
||||
]
|
||||
|
||||
if (options.modern) {
|
||||
webpackConfigs.push(this.getWebpackConfig('modern'))
|
||||
}
|
||||
|
||||
if (options.build.ssr) {
|
||||
// if (options.build.ssr) {
|
||||
webpackConfigs.push(this.getWebpackConfig('server'))
|
||||
}
|
||||
// }
|
||||
|
||||
await this.nuxt.callHook('webpack:config', webpackConfigs)
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { defineNuxtConfig } from '@nuxt/kit'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
vite: true
|
||||
vite: !true
|
||||
})
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user