Skip to content

Commit

Permalink
fix(config): don't use module condition (import.meta.resolve) (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 20, 2022
1 parent f328f61 commit 64f19b9
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 33 deletions.
82 changes: 82 additions & 0 deletions packages/vite/LICENSE.md
Expand Up @@ -1651,6 +1651,88 @@ Repository: git+https://github.com/css-modules/icss-utils.git
---------------------------------------

## import-meta-resolve
License: MIT
By: Titus Wormer
Repository: wooorm/import-meta-resolve

> (The MIT License)
>
> Copyright (c) 2021 Titus Wormer <mailto:tituswormer@gmail.com>
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> 'Software'), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
>
> ---
>
> This is a derivative work based on:
> <https://github.com/nodejs/node>.
> Which is licensed:
>
> """
> Copyright Node.js contributors. All rights reserved.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to
> deal in the Software without restriction, including without limitation the
> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
> sell copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> IN THE SOFTWARE.
> """
>
> This license applies to parts of Node.js originating from the
> https://github.com/joyent/node repository:
>
> """
> Copyright Joyent, Inc. and other Node contributors. All rights reserved.
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to
> deal in the Software without restriction, including without limitation the
> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
> sell copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> IN THE SOFTWARE.
> """
---------------------------------------

## inflight
License: ISC
By: Isaac Z. Schlueter
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/package.json
Expand Up @@ -88,13 +88,15 @@
"cors": "^2.8.5",
"cross-spawn": "^7.0.3",
"debug": "^4.3.4",
"dep-types": "link:./src/types",
"dotenv": "^14.3.2",
"dotenv-expand": "^5.1.0",
"es-module-lexer": "^1.0.3",
"estree-walker": "^3.0.1",
"etag": "^1.8.1",
"fast-glob": "^3.2.12",
"http-proxy": "^1.18.1",
"import-meta-resolve": "^2.1.0",
"json5": "^2.2.1",
"launch-editor-middleware": "^2.6.0",
"magic-string": "^0.26.7",
Expand All @@ -118,7 +120,6 @@
"strip-literal": "^0.4.2",
"tsconfck": "^2.0.1",
"tslib": "^2.4.0",
"dep-types": "link:./src/types",
"types": "link:./types",
"ufo": "^0.8.5",
"ws": "^8.9.0"
Expand Down
59 changes: 30 additions & 29 deletions packages/vite/src/node/config.ts
Expand Up @@ -8,6 +8,7 @@ import type { Alias, AliasOptions } from 'dep-types/alias'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'
import type { RollupOptions } from 'rollup'
import { resolve as importMetaResolve } from 'import-meta-resolve'
import type { HookHandler, Plugin } from './plugin'
import type {
BuildOptions,
Expand All @@ -25,7 +26,6 @@ import {
createDebugger,
createFilter,
dynamicImport,
isBuiltin,
isExternalUrl,
isObject,
lookupFile,
Expand All @@ -49,7 +49,7 @@ import {
ENV_ENTRY
} from './constants'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import { resolvePlugin } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer'
Expand Down Expand Up @@ -949,6 +949,7 @@ async function bundleConfigFile(
platform: 'node',
bundle: true,
format: isESM ? 'esm' : 'cjs',
mainFields: ['main'],
sourcemap: 'inline',
metafile: true,
define: {
Expand All @@ -960,38 +961,38 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
isRequire: !isESM,
preferRelative: false,
tryIndex: true,
mainFields: [],
browserField: false,
conditions: [],
dedupe: [],
extensions: DEFAULT_EXTENSIONS,
preserveSymlinks: false
}

build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => {
// externalize bare imports
if (id[0] !== '.' && !path.isAbsolute(id) && !isBuiltin(id)) {
// partial deno support as `npm:` does not work in `tryNodeResolve`
// externalize bare imports
build.onResolve(
{ filter: /^[^.].*/ },
async ({ path: id, importer, kind }) => {
if (kind === 'entry-point' || path.isAbsolute(id)) {
return
}

// partial deno support as `npm:` does not work with esbuild
if (id.startsWith('npm:')) {
return { external: true }
}
let idFsPath = tryNodeResolve(id, importer, options, false)?.id
if (idFsPath && (isESM || kind === 'dynamic-import')) {
idFsPath = pathToFileURL(idFsPath).href
}
return {
path: idFsPath,
external: true

const resolveWithRequire =
kind === 'require-call' ||
kind === 'require-resolve' ||
(kind === 'import-statement' && !isESM)

let resolved: string
if (resolveWithRequire) {
const require = createRequire(importer)
resolved = require.resolve(id)
} else {
resolved = await importMetaResolve(
id,
pathToFileURL(importer).href
)
}

return { path: resolved, external: true }
}
})
)
}
},
{
Expand Down
4 changes: 3 additions & 1 deletion playground/config/__tests__/load.spec.ts
Expand Up @@ -5,7 +5,8 @@ import { expect, it } from 'vitest'
it('loadConfigFromFile', async () => {
const { config } = await loadConfigFromFile(
{} as any,
resolve(__dirname, '../packages/entry/vite.config.ts')
resolve(__dirname, '../packages/entry/vite.config.ts'),
resolve(__dirname, '../packages/entry')
)
expect(config).toMatchInlineSnapshot(`
{
Expand All @@ -19,6 +20,7 @@ it('loadConfigFromFile', async () => {
4,
],
],
"moduleCondition": "require condition",
}
`)
})
5 changes: 4 additions & 1 deletion playground/config/packages/entry/package.json
@@ -1,3 +1,6 @@
{
"name": "@vite/test-config-entry"
"name": "@vite/test-config-entry",
"dependencies": {
"@vite/test-config-plugin-module-condition": "link:../plugin-module-condition"
}
}
4 changes: 3 additions & 1 deletion playground/config/packages/entry/vite.config.ts
@@ -1,5 +1,7 @@
import moduleCondition from '@vite/test-config-plugin-module-condition'
import { array } from '../siblings/foo'

export default {
array
array,
moduleCondition
}
@@ -0,0 +1 @@
module.exports = 'require condition'
2 changes: 2 additions & 0 deletions playground/config/packages/plugin-module-condition/index.d.ts
@@ -0,0 +1,2 @@
const str: string
export default str
@@ -0,0 +1 @@
export default 'import condition'
@@ -0,0 +1 @@
export default 'module condition'
11 changes: 11 additions & 0 deletions playground/config/packages/plugin-module-condition/package.json
@@ -0,0 +1,11 @@
{
"name": "@vite/test-config-plugin-module-condition",
"exports": {
".": {
"types": "./index.d.ts",
"module": "./module.mjs",
"import": "./index.mjs",
"require": "./index.cjs"
}
}
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64f19b9

Please sign in to comment.