Skip to content

Commit

Permalink
feat: support import "glob:./*"
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 9, 2021
1 parent b2fc93a commit 8d8e2cc
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 90 deletions.
49 changes: 49 additions & 0 deletions packages/playground/import-glob/__tests__/import-context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { isBuild } from '../../testUtils'

const filteredResult = {
'./foo.js': {
msg: 'foo'
}
}

// json exports key order is altered during build, but it doesn't matter in
// terms of behavior since module exports are not ordered anyway
const json = isBuild
? {
msg: 'baz',
default: {
msg: 'baz'
}
}
: {
default: {
msg: 'baz'
},
msg: 'baz'
}

const allResult = {
// JSON file should be properly transformed
'./dir/baz.json': json,
'./dir/foo.js': {
msg: 'foo'
},
'./dir/index.js': {
default: filteredResult
},
'./dir/nested/bar.js': {
msg: 'bar'
}
}

test('all', async () => {
expect(await page.textContent('.all')).toBe(
JSON.stringify(allResult, null, 2)
)
})

test('filtered', async () => {
expect(await page.textContent('.filtered')).toBe(
JSON.stringify(filteredResult, null, 2)
)
})
3 changes: 3 additions & 0 deletions packages/playground/import-glob/dir/baz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"msg": "baz"
}
1 change: 1 addition & 0 deletions packages/playground/import-glob/dir/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = 'foo'
9 changes: 9 additions & 0 deletions packages/playground/import-glob/dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import modules from 'glob:./*.js'

document.querySelector('.filtered').textContent = JSON.stringify(
modules,
null,
2
)

export default modules
1 change: 1 addition & 0 deletions packages/playground/import-glob/dir/nested/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg = 'bar'
12 changes: 12 additions & 0 deletions packages/playground/import-glob/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>All (./dir/**)</h2>
<pre class="all"></pre>

<h2>Filtered (./*.js from dir/index.js)</h2>
<pre class="filtered"></pre>

<script type="module" src="./dir/index.js"></script>
<script type="module">
import modules from 'glob:./dir/**'

document.querySelector('.all').textContent = JSON.stringify(modules, null, 2)
</script>
10 changes: 10 additions & 0 deletions packages/playground/import-glob/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "test-import-context",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite"
}
}
2 changes: 1 addition & 1 deletion packages/playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"esModuleInterop": true,
"baseUrl": ".",
"jsx": "preserve",
"types": ["vite/client"]
"types": ["vite/client", "jest"]
}
}
6 changes: 6 additions & 0 deletions packages/vite/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,9 @@ declare module '*?worker&inline' {
}
export default workerConstructor
}

// glob import
declare module 'glob:*' {
const modules: Record<string, Record<string, any>>
export default modules
}
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"es-module-lexer": "^0.3.26",
"etag": "^1.8.1",
"execa": "^5.0.0",
"fast-glob": "^3.2.4",
"http-proxy": "^1.18.1",
"isbuiltin": "^1.0.0",
"launch-editor-middleware": "^2.2.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import isBuiltin from 'isbuiltin'
import { Logger } from './logger'
import { TransformOptions } from 'esbuild'
import { CleanCSS } from 'types/clean-css'
import { dataURIPlugin } from './plugins/dataUri'
import { importGlobPlugin } from './plugins/importGlob'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -198,11 +200,13 @@ export function resolveBuildPlugins(
const options = config.build
return {
pre: [
buildHtmlPlugin(config),
commonjsPlugin({
include: [/node_modules/],
extensions: ['.js', '.cjs']
}),
buildHtmlPlugin(config),
dataURIPlugin(),
importGlobPlugin(config),
buildDefinePlugin(config),
dynamicImportVars({
warnOnError: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/dataUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/

const dataUriPrefix = `/@data-uri/`

/**
* Build only, since importing from a data URI works natively.
*/
export function dataURIPlugin(): Plugin {
const resolved: {
[key: string]: string
Expand Down
Loading

0 comments on commit 8d8e2cc

Please sign in to comment.