From c61e6d6198b381e782752c4efaeb6073ea27801f Mon Sep 17 00:00:00 2001 From: Rahul Kadyan Date: Tue, 5 Feb 2019 09:36:13 +0530 Subject: [PATCH] test: customTag option --- jest.config.js | 4 +- package.json | 2 +- src/index.ts | 7 ++- test/baseline.spec.ts | 2 +- test/forward-style-compiler-errors.spec.ts | 11 ++-- test/options/custom-blocks.spec.ts | 67 ++++++++++++++++++++++ test/setup/plugins.ts | 18 ++++++ typings/hash-sum.d.ts | 2 +- typings/puppeteer.d.ts | 6 +- typings/rollup-plugins.d.ts | 2 +- 10 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 test/options/custom-blocks.spec.ts diff --git a/jest.config.js b/jest.config.js index 5539500..2ccb882 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,8 +2,8 @@ module.exports = { collectCoverageFrom: ['src/**'], moduleFileExtensions: ['js', 'ts', 'json'], transform: { - '^.+\\.ts$': '/node_modules/ts-jest/preprocessor.js', + '^.+\\.ts$': 'ts-jest', }, - testMatch: ['**/?(*.)spec.ts'], + testMatch: ['**/*.spec.ts'], testEnvironment: 'node' } diff --git a/package.json b/package.json index 1b279a0..2422ffe 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "pre:docs": "cp CHANGELOG.md docs/changelog.md", ":docs": "vuepress dev docs/", "post:docs": "rm docs/CHANGELOG.md", - "lint": "prettier --no-semi --single-quote --write **/*.js src/*.ts **/*.vue !test/target/** !dist/**", + "lint": "prettier --no-semi --single-quote --write **/*.js **/*.ts **/*.vue !test/target/** !dist/**", "release": "standard-version -a", "test": "jest" }, diff --git a/src/index.ts b/src/index.ts index 06130f0..835fabe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,13 +58,13 @@ export interface VuePluginOptions { } /** * Exclude/Include customBlocks for final build. - * @default `['!*']` + * @default `() => false` * @example * ```js * VuePlugin({ customBlocks: ['markdown', '!test'] }) * ``` */ - customBlocks?: string[] | (() => boolean) + customBlocks?: string[] | ((tag: string) => boolean) /** * Inject CSS in JavaScript. * @default `true` @@ -244,12 +244,13 @@ export default function vue(opts: VuePluginOptions = {}): Plugin { ) ) + descriptors.set(filename, descriptor) + const scopeId = 'data-v-' + (isProduction ? hash(path.basename(filename) + source) : hash(filename + source)) - descriptors.set(filename, descriptor) const styles = await Promise.all( descriptor.styles.map(async style => { diff --git a/test/baseline.spec.ts b/test/baseline.spec.ts index b540a27..07ec426 100644 --- a/test/baseline.spec.ts +++ b/test/baseline.spec.ts @@ -10,7 +10,7 @@ let browser: Browser | null = null beforeAll(async () => { browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], - headless: Boolean(process.env.CI), + headless: Boolean(process.env.CI) }) }) diff --git a/test/forward-style-compiler-errors.spec.ts b/test/forward-style-compiler-errors.spec.ts index 90b2a2e..9cd19b5 100644 --- a/test/forward-style-compiler-errors.spec.ts +++ b/test/forward-style-compiler-errors.spec.ts @@ -1,16 +1,19 @@ import pluginVue from '../src' -describe("forward-style-compiler-errors", () => { - it("throws", async () => { +describe('forward-style-compiler-errors', () => { + it('throws', async () => { let plugin = pluginVue() - await expect((plugin as any).transform(` + await expect( + (plugin as any).transform( + ` - `, 'virtual-file.vue' + `, + 'virtual-file.vue' ) ).rejects.toBeInstanceOf(Error) }) diff --git a/test/options/custom-blocks.spec.ts b/test/options/custom-blocks.spec.ts new file mode 100644 index 0000000..5abf48b --- /dev/null +++ b/test/options/custom-blocks.spec.ts @@ -0,0 +1,67 @@ +import vue, { VuePluginOptions } from '../../src' +import { pluginInline } from '../setup/plugins' +import { rollup } from 'rollup' + +describe('customBlocks', () => { + async function setup(options?: Partial) { + return rollup({ + input: '/entry.vue', + plugins: [ + pluginInline( + '/entry.vue', + ` + + + // My Custom Block + + + // My Docs Block + + ` + ), + vue({ + ...options, + normalizer: 'vue-runtime-helpers/dist/normalize-component.mjs' + }) + ] + }) + .then(bundle => bundle.generate({ format: 'es' })) + .then(generated => generated.output[0]) + } + + it('default', async () => { + const { code } = await setup() + + expect(code).not.toEqual(expect.stringContaining('My Custom Block')) + expect(code).not.toEqual(expect.stringContaining('My Docs Block')) + }) + + it('array of tags', async () => { + const { code } = await setup({ + customBlocks: ['custom'] + }) + + expect(code).toEqual(expect.stringContaining('My Custom Block')) + expect(code).not.toEqual(expect.stringContaining('My Docs Block')) + }) + it('negative array of tags', async () => { + const { code } = await setup({ + customBlocks: ['*', '!custom'] + }) + + expect(code).not.toEqual(expect.stringContaining('My Custom Block')) + expect(code).toEqual(expect.stringContaining('My Docs Block')) + }) + it('function', async () => { + const { code } = await setup({ + customBlocks(tag) { + return tag === 'custom' + } + }) + + expect(code).toEqual(expect.stringContaining('My Custom Block')) + expect(code).not.toEqual(expect.stringContaining('My Docs Block')) + }) +}) diff --git a/test/setup/plugins.ts b/test/setup/plugins.ts index d4bf483..99de442 100644 --- a/test/setup/plugins.ts +++ b/test/setup/plugins.ts @@ -1,3 +1,5 @@ +import { Plugin } from 'rollup' + const pluginBabel = require('rollup-plugin-babel') const pluginNodeResolve = require('rollup-plugin-node-resolve') const pluginCommonJS = require('rollup-plugin-commonjs') @@ -6,6 +8,22 @@ const pluginMarkdown = require('rollup-plugin-md') const pluginTypescript = require('rollup-plugin-typescript') const pluginReplace = require('rollup-plugin-replace') +export function pluginInline(filename: string, code: string): Plugin { + return { + name: 'inline', + resolveId(id: string) { + if (id === filename) return filename + + return null + }, + load(id: string) { + if (id === filename) return code + + return null + } + } +} + export const plugins = [ pluginImage({ emitFiles: false }), pluginMarkdown(), diff --git a/typings/hash-sum.d.ts b/typings/hash-sum.d.ts index c710871..8a199cd 100644 --- a/typings/hash-sum.d.ts +++ b/typings/hash-sum.d.ts @@ -1,4 +1,4 @@ declare module 'hash-sum' { const sum: (any: string) => string export = sum -} \ No newline at end of file +} diff --git a/typings/puppeteer.d.ts b/typings/puppeteer.d.ts index 08af6c1..be51570 100644 --- a/typings/puppeteer.d.ts +++ b/typings/puppeteer.d.ts @@ -1,3 +1,3 @@ -interface Element { } -interface Node { } -interface NodeListOf { } \ No newline at end of file +interface Element {} +interface Node {} +interface NodeListOf {} diff --git a/typings/rollup-plugins.d.ts b/typings/rollup-plugins.d.ts index 6b70f05..626e65e 100644 --- a/typings/rollup-plugins.d.ts +++ b/typings/rollup-plugins.d.ts @@ -22,4 +22,4 @@ declare module 'rollup-plugin-md' { declare module 'rollup-pluginutils' { export function createFilter(a: any, b: any): (any: any) => boolean -} \ No newline at end of file +}