Skip to content

Commit

Permalink
feat: add option to specify output file (#72) (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
bencelang committed Oct 11, 2020
1 parent f3579d5 commit b232c8c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { generateRoutes, GenerateConfig } from 'vue-route-generator'

const pluginName = 'VueAutoRoutingPlugin'

interface Options extends GenerateConfig {}
interface Options extends GenerateConfig {
outFile?: string
}

namespace VueAutoRoutingPlugin {
export type AutoRoutingOptions = Options
Expand All @@ -20,7 +22,7 @@ class VueAutoRoutingPlugin {
apply(compiler: Compiler) {
const generate = () => {
const code = generateRoutes(this.options)
const to = path.resolve(__dirname, '../index.js')
const to = this.options.outFile || path.resolve(__dirname, '../index.js')

if (
fs.existsSync(to) &&
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/webpack-plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,37 @@ function users__id() {
/******/ ]);"
`;

exports[`webpack plugin should write to custom output file if specified 1`] = `
"function index() {
return import(/* webpackChunkName: \\"index\\" */ '@/pages/index.vue')
}
function users_foo() {
return import(/* webpackChunkName: \\"users-foo\\" */ '@/pages/users/foo.vue')
}
function users__id() {
return import(/* webpackChunkName: \\"users-id\\" */ '@/pages/users/_id.vue')
}
export default [
{
name: 'index',
path: '/',
component: index,
},
{
name: 'users-foo',
path: '/users/foo',
component: users_foo,
},
{
name: 'users-id',
path: '/users/:id?',
component: users__id,
},
]
"
`;

exports[`webpack plugin watches adding a page 1`] = `
"/******/ (function(modules) { // webpackBootstrap
/******/ // install a JSONP callback for chunk loading
Expand Down
20 changes: 18 additions & 2 deletions test/webpack-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ const compiler = (plugin: Plugin): webpack.Compiler => {
})
}

const matchOutputWithSnapshot = () => {
const out = fse.readFileSync(resolve('./fixtures/out/main.js'), 'utf8')
const matchOutputWithSnapshot = (path?: string) => {
const out = fse.readFileSync(
resolve(path || './fixtures/out/main.js'),
'utf8'
)
expect(out).toMatchSnapshot()
}

Expand Down Expand Up @@ -191,4 +194,17 @@ describe('webpack plugin', () => {
}
})
})

it('should write to custom output file if specified', (done) => {
const plugin = new Plugin({
pages: resolve('fixtures/pages'),
outFile: resolve('fixtures/out/custom.js'),
})

compiler(plugin).run(() => {
expect(fse.existsSync(resolve('./fixtures/out/custom.js'))).toBeTruthy()
matchOutputWithSnapshot('fixtures/out/custom.js')
done()
})
})
})

0 comments on commit b232c8c

Please sign in to comment.