Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the detection of helper script from resolved engine #171

Merged
merged 5 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

### Changed

- Upgrade [Marpit v1.4.1](https://github.com/marp-team/marpit/releases/tag/v1.4.1) and [Marp Core v0.14.0](https://github.com/marp-team/marp-core/releases/tag/v0.14.0) ([#169](https://github.com/marp-team/marp-cli/pull/169))
- Upgrade dependent packages to the latest version ([#164](https://github.com/marp-team/marp-cli/pull/164), [#169](https://github.com/marp-team/marp-cli/pull/169))

### Removed

- Remove the detection of helper script from resolved engine ([#171](https://github.com/marp-team/marp-cli/pull/171))

## v0.14.1 - 2019-09-15

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^5.1.2",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-url": "^3.0.0",
"rollup-plugin-url": "^2.2.4",
"sass": "^1.23.0",
"screenfull": "^5.0.0",
"stylelint": "^11.1.1",
Expand Down
3 changes: 0 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@ export class MarpCLIConfig {
lang: this.conf.lang || (await osLocale()).replace(/@/g, '-'),
options: this.conf.options || {},
pages: !!(this.args.images || this.conf.images),
readyScript: this.engine.browserScript
? `<script>${this.engine.browserScript}</script>`
: undefined,
watch:
pick(this.args.watch, this.conf.watch) || preview || server || false,
}
Expand Down
4 changes: 1 addition & 3 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface ConverterOption {
pages?: boolean | number[]
preview?: boolean
jpegQuality?: number
readyScript?: string
server?: boolean
template: string
templateOption?: TemplateOption
Expand Down Expand Up @@ -91,7 +90,7 @@ export class Converter {
}

async convert(markdown: string, file?: File): Promise<TemplateResult> {
const { lang, readyScript, globalDirectives, type } = this.options
const { lang, globalDirectives, type } = this.options
const isFile = file && file.type === FileType.File

let additionals = ''
Expand All @@ -107,7 +106,6 @@ export class Converter {
return await this.template({
...(this.options.templateOption || {}),
lang,
readyScript,
base:
isFile && type !== ConvertType.html
? file!.absoluteFileScheme
Expand Down
19 changes: 3 additions & 16 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { Marpit } from '@marp-team/marpit'
import fs from 'fs'
import path from 'path'
import pkgUp from 'pkg-up'
import { promisify } from 'util'
import importFrom from 'import-from'
import { CLIError } from './error'

const readFile = promisify(fs.readFile)

export type Engine = typeof Marpit
export type ResolvableEngine = Engine | string

export class ResolvedEngine {
browserScript?: string
klass: Engine
package?: { [key: string]: any }

private static browserScriptKey = 'marpBrowser'
package?: Record<string, any>

static async resolve(
engine: ResolvableEngine | ResolvableEngine[],
Expand All @@ -26,7 +19,7 @@ export class ResolvedEngine {
ResolvedEngine.resolveModule(engine, from)
)

await resolvedEngine.resolveBrowserScript()
await resolvedEngine.resolvePackage()
return resolvedEngine
}

Expand Down Expand Up @@ -56,20 +49,14 @@ export class ResolvedEngine {
this.klass = klass
}

private async resolveBrowserScript(): Promise<void> {
private async resolvePackage(): Promise<void> {
const classPath = this.findClassPath(this.klass)
if (!classPath) return

const pkgPath = await pkgUp({ cwd: path.dirname(classPath) })
if (!pkgPath) return

this.package = require(pkgPath)
const scriptPath = this.package![ResolvedEngine.browserScriptKey]
if (!scriptPath) return undefined

this.browserScript = (await readFile(
path.resolve(path.dirname(pkgPath), scriptPath)
)).toString()
}

// NOTE: It cannot test because of overriding `require` in Jest context.
Expand Down
8 changes: 0 additions & 8 deletions src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ interface TemplateCoreOption {
base?: string
lang: string
notifyWS?: string
readyScript?: string
renderer: (tplOpts: MarpitOptions) => RendererResult
}

Expand Down Expand Up @@ -89,13 +88,6 @@ export const bespoke: Template<TemplateBespokeOption> = async opts => {
osc: opts.osc === undefined ? true : opts.osc,
progress: opts.progress,
},
readyScript:
opts.readyScript ||
`<script>${await libJs(
require.resolve(
'@marp-team/marpit-svg-polyfill/lib/polyfill.browser.js'
)
)}</script>`,
watchJs: await watchJs(opts.notifyWS),
}),
}
Expand Down
1 change: 0 additions & 1 deletion src/templates/layout.pug
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ html(lang=lang)
body
block deck
!= html
!= readyScript

block script

Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async function outputVersion(config: MarpCLIConfig): Promise<0> {
const { engine } = config

if (isMarpCore(engine.klass)) {
engineVer = `bundled @marp-team/marp-core v${bundledCoreVer}`
engineVer = `@marp-team/marp-core v${bundledCoreVer}`

if (engine.package && engine.package.version !== bundledCoreVer) {
engineVer = `user-installed @marp-team/marp-core v${engine.package.version}`
Expand Down
7 changes: 1 addition & 6 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,11 @@ describe('Converter', () => {

it('returns the result of template', async () => {
const options: any = { html: true }
const readyScript = '<b>ready</b>'
const { result, rendered } = await instance({
options,
readyScript,
}).convert(md)
const { result, rendered } = await instance({ options }).convert(md)

expect(result).toMatch(/^<!DOCTYPE html>[\s\S]+<\/html>$/)
expect(result).toContain(rendered.html)
expect(result).toContain(rendered.css)
expect(result).toContain(readyScript)
expect(result).not.toContain('<base')
expect(rendered.css).toContain('@theme default')
})
Expand Down
21 changes: 2 additions & 19 deletions test/engine.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import Marp from '@marp-team/marp-core'
import resolve, { ResolvedEngine } from '../src/engine'
import resolve from '../src/engine'

describe('Engine', () => {
const coreModule = '@marp-team/marp-core'
const marpitModule = '@marp-team/marpit'

describe('.resolve', () => {
it('returns ResolvedEngine class with resolved class', async () => {
expect((await resolve(Marp)).klass).toBe(Marp)
expect((await resolve(coreModule)).klass.name).toBe('Marp')
expect((await resolve('@marp-team/marp-core')).klass.name).toBe('Marp')

// Return with the first resolved class
expect((await resolve(['__invalid_module__', Marp])).klass).toBe(Marp)
})

it("loads browser script from defined in module's marpBrowser section", async () => {
const finder = jest.spyOn(<any>ResolvedEngine.prototype, 'findClassPath')

// Core (defined marpBrowser)
finder.mockImplementation(() => require.resolve(coreModule))

expect((await resolve(Marp)).browserScript).toBeTruthy()
expect((await resolve(coreModule)).browserScript).toBeTruthy()

// Marpit (not defined marpBrowser)
finder.mockImplementation(() => require.resolve(marpitModule))
expect((await resolve(marpitModule)).browserScript).toBeUndefined()
})
})
})
4 changes: 1 addition & 3 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ describe('Marp CLI', () => {
expect.stringContaining(`@marp-team/marp-cli v${cliVersion}`)
)
expect(log).toBeCalledWith(
expect.stringContaining(
`bundled @marp-team/marp-core v${coreVersion}`
)
expect.stringContaining(`@marp-team/marp-core v${coreVersion}`)
)
})

Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6515,12 +6515,13 @@ rollup-plugin-typescript@^1.0.1:
resolve "^1.10.0"
rollup-pluginutils "^2.5.0"

rollup-plugin-url@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-url/-/rollup-plugin-url-3.0.0.tgz#38c1540eb02cf29aec2b7fb57ad3877505ed6a75"
integrity sha512-zeSVpWwsePvc6oTw5eyAlkfHhS6ZT9/pNVVgyzbeMTgd6rovxgg0FPE4o86b9Snembc0rCAaTvEsSfZinJMITw==
rollup-plugin-url@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/rollup-plugin-url/-/rollup-plugin-url-2.2.4.tgz#36a6dedb709f73647bed7b253b9dcd4f3e781af4"
integrity sha512-vQjMgJj3tYrg6nKYO/Tvc8s1WTqbaLzHXia17358E6vO0Iq4Ih5WcWYRPopLMUx0x63/31+9ezApAL0HFd998w==
dependencies:
mime "^2.4.4"
mkdirp "^0.5.1"
rollup-pluginutils "^2.8.2"

rollup-pluginutils@^2.0.1, rollup-pluginutils@^2.3.3, rollup-pluginutils@^2.5.0, rollup-pluginutils@^2.6.0, rollup-pluginutils@^2.8.1, rollup-pluginutils@^2.8.2:
Expand Down