Skip to content

Commit

Permalink
feat: add mimeTest in mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Apr 4, 2021
1 parent 7bc9ef1 commit ac051eb
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 77 deletions.
2 changes: 1 addition & 1 deletion _site/doc/advanced/write-loader_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Loader 将会输出为 `JSON.stringify(pkg, null, indent)`.
]
```

用于分发文件使用哪个`loader`,默认都采用 hbs Loader。
用于分发文件使用哪个`loader`,默认都采用 hbs Loader(多媒体文件除外)

**注意:Edam@3 移除 LoDash Loader,默认引入 [Plop Handlebar](https://plopjs.com/documentation/#built-in-helpers) Loader**
Plop Handlebar 使用 Handlebar 模板,注入了一些 helper:
Expand Down
137 changes: 113 additions & 24 deletions packages/edam/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"debug": "^4.2.0",
"download": "^8.0.0",
"execa": "^1.0.0",
"file-type": "^16.3.0",
"filenamify": "^2.0.0",
"find-up": "^3.0.0",
"git-config-path": "^1.0.1",
Expand Down
7 changes: 3 additions & 4 deletions packages/edam/src/__tests__/compiler/Compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ describe('Compiler', function() {
})
cer.assets = {
'test.js': {
value: 'Hello,{{name}}'
value: 'Hello,{{name}}',
// load
},
'test.a.js': {
value: 'Hello,A,{{name}} {{paramCase name}}'
// load
value: 'Hello,A,{{name}} {{paramCase name}}',
}
}

Expand Down Expand Up @@ -144,7 +143,7 @@ describe('Compiler', function() {

cer.assets = {
'test.js': {
value: new Buffer('Hello,{{ name}}')
value: new Buffer('Hello,{{ name}}'),
// load
},
'test.a.js': {
Expand Down
41 changes: 41 additions & 0 deletions packages/edam/src/__tests__/edam/image-assets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable quotes */
/**
* @file spec
* @author Cuttle Cong
* @date 2018/3/28
* @description
*/
import { mockPrompts } from '../../index'
import { join, relative, normalize } from 'path'
import { sync } from 'rimraf'
import fileSystem from '../../lib/fileSystem'

async function readdirDeep(dest) {
const files = await fileSystem.readdirDeep(dest)
return files.filter(x => fileSystem.isFile(x)).map(x => relative(dest, x))
}
describe('image-assets', function() {
const tplPath = join(__dirname, '../fixture/edam')
const outputRoot = join(__dirname, '../fixture/edam-output')
beforeEach(() => {
sync(join(outputRoot, 'image-assets'))
})

it('should emit assets', async () => {
const ft = await mockPrompts(
join(tplPath, 'image-assets'),
{
},
join(outputRoot, 'image-assets')
)

console.log(Object.keys(ft.tree))
await ft.writeToFile()
expect(Object.keys(ft.tree)).toEqual(
expect.arrayContaining([
normalize('favicon.ico'),
normalize('logo.png'),
])
)
})
})
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions packages/edam/src/__tests__/fixture/edam/image-assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @file index
* @author Cuttle Cong
* @date 2018/3/28
* @description
*/

module.exports = function(/*options*/) {
return {
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 23 additions & 6 deletions packages/edam/src/core/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ import matchMeta from './matchMeta'
import parseQueryString from '../../lib/parseQueryString'
import DefaultLogger from '../DefaultLogger'
import EdamError from '../EdamError'
import { FileTypeResult } from 'file-type'
import map = require('lodash/fp/map')

const debug = require('debug')('edam:Compiler')

export type Asset = {
value: Buffer | string | any
meta: Partial<{
ext: string,
mime: string
}>
loaders?: Array<StrictLoader>
}

Expand Down Expand Up @@ -110,21 +116,31 @@ export default class Compiler extends AwaitEventEmitter {
public static defaultMappers: Array<Mapper> = [
{
test: '*',
mimeTest: 'text/*',
loader: ['hbs']
}
]

public loaders = {...Compiler.defaultLoaders}
public loaders = { ...Compiler.defaultLoaders }
public mappers = Compiler.defaultMappers.slice()
public variables = new VariablesImpl()

private _matchedLoaders(path: string) {
private _matchedLoaders(path: string, asset: Asset) {
let matchedLoader
_.some(this.mappers, function(mapper) {
if (isMatch(path, mapper.test)) {
matchedLoader = mapper.loader
return true
if (mapper.test) {
if (isMatch(path, mapper.test)) {
matchedLoader = mapper.loader
}
}
if (mapper.mimeTest) {
if (isMatch(asset.meta && asset.meta.mime || 'text/plain', mapper.mimeTest)) {
matchedLoader = mapper.loader
} else {
matchedLoader = null
}
}
return !!matchedLoader
})
return matchedLoader
}
Expand Down Expand Up @@ -231,7 +247,7 @@ export default class Compiler extends AwaitEventEmitter {
}

if (!data.loaders) {
data.loaders = this._matchedLoaders(path)
data.loaders = this._matchedLoaders(path, asset)
}
debug('loader path: %s, loaders: %o', path, data.loaders)
if (data.loaders) {
Expand All @@ -254,6 +270,7 @@ export default class Compiler extends AwaitEventEmitter {
}
return {
path,
output: data.input,
...data
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/edam/src/core/TreeProcessor/FileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import fileSystem from '../../lib/fileSystem'
import * as mm from 'micromatch'
import DefaultLogger from '../DefaultLogger'
import { ParsedPath } from 'path'
import * as Console from "console";
const debug = require('debug')('edam:FileProcessor')
const tildify = require('tildify')

Expand Down Expand Up @@ -51,6 +52,7 @@ export default class FileProcessor extends TreeProcessor {
}
await this.emitter.emit('pre', filepath)
const workers = []

_.each(this.tree, (data: State, path) => {
const filename = nps.join(filepath, path)

Expand Down
Loading

0 comments on commit ac051eb

Please sign in to comment.