-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: virtual module support for webpack
- Loading branch information
Showing
13 changed files
with
155 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
.DS_STORE | ||
.profile | ||
.vscode | ||
node_modules | ||
*.log* | ||
dist | ||
coverage | ||
.profile | ||
dist | ||
node_modules | ||
temp | ||
dist-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const UNPLUGIN_VMOD_PREFIX = '/__unplugin__/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { resolve } from 'path' | ||
import fs from 'fs-extra' | ||
|
||
const r = (...args: string[]) => resolve(__dirname, '../dist', ...args) | ||
|
||
describe('virtual-module build', () => { | ||
it('vite', async () => { | ||
const content = await fs.readFile(r('vite/main.js.es.js'), 'utf-8') | ||
|
||
expect(content).toContain('VIRTUAL:ONE') | ||
expect(content).toContain('VIRTUAL:TWO') | ||
}) | ||
|
||
it('rollup', async () => { | ||
const content = await fs.readFile(r('rollup/main.js'), 'utf-8') | ||
|
||
expect(content).toContain('VIRTUAL:ONE') | ||
expect(content).toContain('VIRTUAL:TWO') | ||
}) | ||
|
||
it('webpack', async () => { | ||
const content = await fs.readFile(r('webpack/main.js'), 'utf-8') | ||
|
||
expect(content).toContain('VIRTUAL:ONE') | ||
expect(content).toContain('VIRTUAL:TWO') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { rollup } = require('./unplugin') | ||
|
||
export default { | ||
input: './src/main.js', | ||
output: { | ||
dir: './dist/rollup' | ||
}, | ||
plugins: [ | ||
rollup() | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import msg1 from 'virtual-1' | ||
import msg2 from 'virtual-2' | ||
|
||
console.log(msg1, msg2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { createUnplugin } = require('unplugin') | ||
|
||
module.exports = createUnplugin(() => { | ||
return { | ||
name: 'virtual-module-fixture', | ||
resolveId (id) { | ||
return id.startsWith('virtual-') ? id : null | ||
}, | ||
load (id) { | ||
if (id === 'virtual-1') { | ||
return 'export default "VIRTUAL:ONE"' | ||
} | ||
if (id === 'virtual-2') { | ||
return 'export default "VIRTUAL:TWO"' | ||
} | ||
return null | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { resolve } = require('path') | ||
const { vite } = require('./unplugin') | ||
|
||
module.exports = { | ||
root: __dirname, | ||
plugins: [ | ||
vite() | ||
], | ||
build: { | ||
lib: { | ||
entry: resolve(__dirname, 'src/main.js'), | ||
name: 'main', | ||
fileName: 'main.js' | ||
}, | ||
outDir: 'dist/vite' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { resolve } = require('path') | ||
const { webpack } = require('./unplugin') | ||
|
||
module.exports = { | ||
mode: 'development', | ||
entry: resolve(__dirname, 'src/main.js'), | ||
output: { | ||
path: resolve(__dirname, 'dist/webpack'), | ||
filename: 'main.js' | ||
}, | ||
plugins: [ | ||
webpack() | ||
], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
loader: 'babel-loader', | ||
exclude: /node_modules/, | ||
options: { | ||
presets: [ | ||
'@babel/preset-env' | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} |