Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): add rollup plugins through config
Closes #472
- Loading branch information
1 parent
a8726be
commit b8abbce
Showing
11 changed files
with
300 additions
and
91 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
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,16 @@ | ||
import { Config } from '../../declarations'; | ||
import styleMinifyPlugin from '../style/style-minify-plugin'; | ||
import styleSassPlugin from '../style/style-sass-plugin'; | ||
|
||
|
||
export function validatePlugins(config: Config) { | ||
config.plugins = (config.plugins || []).filter(p => !!p); | ||
|
||
if (!config.plugins.some(p => p.name === 'styleSassPlugin')) { | ||
config.plugins.push(styleSassPlugin()); | ||
} | ||
|
||
if (!config.plugins.some(p => p.name === 'styleMinifyPlugin')) { | ||
config.plugins.push(styleMinifyPlugin()); | ||
} | ||
} |
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,183 @@ | ||
import { TestingCompiler } from '../../../testing'; | ||
import { wroteFile } from '../../../testing/utils'; | ||
import * as path from 'path'; | ||
import * as ts from 'typescript'; | ||
|
||
|
||
describe('plugin', () => { | ||
let c: TestingCompiler; | ||
|
||
beforeEach(async () => { | ||
c = new TestingCompiler(); | ||
await c.fs.writeFiles({'/src/index.html': `<cmp-a></cmp-a>`}); | ||
await c.fs.commit(); | ||
}); | ||
|
||
it('transform, async', async () => { | ||
c.config.bundles = [ { components: ['cmp-a'] } ]; | ||
|
||
await c.fs.writeFiles({ | ||
'/stencil.config.js': ` | ||
function myPlugin() { | ||
return { | ||
transform: function(sourceText) { | ||
return new Promise(resolve => { | ||
resolve(sourceText.replace('constructor() { }', 'constructor(){console.log("transformed!")}')); | ||
}); | ||
}, | ||
name: 'myPlugin' | ||
}; | ||
} | ||
exports.config = { | ||
plugins: [myPlugin()] | ||
}; | ||
`, | ||
'/src/cmp-a.tsx': ` | ||
@Component({ tag: 'cmp-a' }) export class CmpA { | ||
constructor() { } | ||
} | ||
` | ||
}, { clearFileCache: true }); | ||
await c.fs.commit(); | ||
|
||
c.loadConfigFile('/stencil.config.js'); | ||
|
||
const r = await c.build(); | ||
expect(r.diagnostics).toEqual([]); | ||
|
||
const cmpA = await c.fs.readFile('/www/build/app/cmp-a.js'); | ||
expect(cmpA).toContain('transformed!'); | ||
}); | ||
|
||
it('transform, sync', async () => { | ||
c.config.bundles = [ { components: ['cmp-a'] } ]; | ||
|
||
await c.fs.writeFiles({ | ||
'/stencil.config.js': ` | ||
function myPlugin() { | ||
return { | ||
transform: function(sourceText) { | ||
return sourceText.replace('constructor() { }', 'constructor(){console.log("transformed!")}'); | ||
}, | ||
name: 'myPlugin' | ||
}; | ||
} | ||
exports.config = { | ||
plugins: [myPlugin()] | ||
}; | ||
`, | ||
'/src/cmp-a.tsx': ` | ||
@Component({ tag: 'cmp-a' }) export class CmpA { | ||
constructor() { } | ||
} | ||
` | ||
}, { clearFileCache: true }); | ||
await c.fs.commit(); | ||
|
||
c.loadConfigFile('/stencil.config.js'); | ||
|
||
const r = await c.build(); | ||
expect(r.diagnostics).toEqual([]); | ||
|
||
const cmpA = await c.fs.readFile('/www/build/app/cmp-a.js'); | ||
expect(cmpA).toContain('transformed!'); | ||
}); | ||
|
||
it('resolveId, async', async () => { | ||
c.config.bundles = [ { components: ['cmp-a'] } ]; | ||
|
||
await c.fs.writeFiles({ | ||
'/stencil.config.js': ` | ||
function myPlugin() { | ||
return { | ||
resolveId: function(importee, importer) { | ||
if (importee === '#crazy-path!') { | ||
return Promise.resolve('/dist/my-dep-fn.js'); | ||
} | ||
}, | ||
name: 'myPlugin' | ||
}; | ||
} | ||
exports.config = { | ||
plugins: [myPlugin()] | ||
}; | ||
`, | ||
'/src/cmp-a.tsx': ` | ||
import { depFn } '#crazy-path!' | ||
@Component({ tag: 'cmp-a' }) export class CmpA { | ||
constructor() { | ||
depFn(); | ||
} | ||
} | ||
`, | ||
'/dist/my-dep-fn.js': ` | ||
export function depFn(){ | ||
console.log('imported depFun()'); | ||
} | ||
` | ||
}, { clearFileCache: true }); | ||
await c.fs.commit(); | ||
|
||
c.loadConfigFile('/stencil.config.js'); | ||
|
||
const r = await c.build(); | ||
expect(r.diagnostics).toEqual([]); | ||
|
||
const cmpA = await c.fs.readFile('/www/build/app/cmp-a.js'); | ||
expect(cmpA).toContain('imported depFun()'); | ||
}); | ||
|
||
it('resolveId, sync', async () => { | ||
c.config.bundles = [ { components: ['cmp-a'] } ]; | ||
|
||
await c.fs.writeFiles({ | ||
'/stencil.config.js': ` | ||
function myPlugin() { | ||
return { | ||
resolveId: function(importee, importer) { | ||
if (importee === '#crazy-path!') { | ||
return '/dist/my-dep-fn.js'; | ||
} | ||
}, | ||
name: 'myPlugin' | ||
}; | ||
} | ||
exports.config = { | ||
plugins: [myPlugin()] | ||
}; | ||
`, | ||
'/src/cmp-a.tsx': ` | ||
import { depFn } '#crazy-path!' | ||
@Component({ tag: 'cmp-a' }) export class CmpA { | ||
constructor() { | ||
depFn(); | ||
} | ||
} | ||
`, | ||
'/dist/my-dep-fn.js': ` | ||
export function depFn(){ | ||
console.log('imported depFun()'); | ||
} | ||
` | ||
}, { clearFileCache: true }); | ||
await c.fs.commit(); | ||
|
||
c.loadConfigFile('/stencil.config.js'); | ||
|
||
const r = await c.build(); | ||
expect(r.diagnostics).toEqual([]); | ||
|
||
const cmpA = await c.fs.readFile('/www/build/app/cmp-a.js'); | ||
expect(cmpA).toContain('imported depFun()'); | ||
}); | ||
|
||
|
||
}); |
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,42 +1,46 @@ | ||
import * as d from '../../declarations/index'; | ||
|
||
|
||
export class StyleMinifyPlugin implements d.Plugin { | ||
export default function styleMinifyPlugin(): d.Plugin { | ||
const name = 'styleMinifyPlugin'; | ||
|
||
async transform(sourceText: string, id: string, context: d.PluginCtx): Promise<d.PluginTransformResults> { | ||
if (!context.config.minifyCss || !this.usePlugin(id)) { | ||
return null; | ||
} | ||
return { | ||
|
||
const results: d.PluginTransformResults = {}; | ||
transform: async function(sourceText: string, id: string, context: d.PluginCtx): Promise<d.PluginTransformResults> { | ||
if (!context || !context.config.minifyCss || !usePlugin(id)) { | ||
return null; | ||
} | ||
|
||
const results: d.PluginTransformResults = {}; | ||
|
||
const cacheKey = context.cache.createKey(this.name, sourceText); | ||
const cachedContent = await context.cache.get(cacheKey); | ||
const cacheKey = context.cache.createKey(name, sourceText); | ||
const cachedContent = await context.cache.get(cacheKey); | ||
|
||
if (cachedContent != null) { | ||
results.code = cachedContent; | ||
if (cachedContent != null) { | ||
results.code = cachedContent; | ||
|
||
} else { | ||
const minifyResults = context.sys.minifyCss(sourceText); | ||
minifyResults.diagnostics.forEach(d => { | ||
context.diagnostics.push(d); | ||
}); | ||
} else { | ||
const minifyResults = context.sys.minifyCss(sourceText); | ||
minifyResults.diagnostics.forEach(d => { | ||
context.diagnostics.push(d); | ||
}); | ||
|
||
if (typeof minifyResults.output === 'string') { | ||
results.code = minifyResults.output; | ||
await context.cache.put(cacheKey, results.code); | ||
if (typeof minifyResults.output === 'string') { | ||
results.code = minifyResults.output; | ||
await context.cache.put(cacheKey, results.code); | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
return results; | ||
}, | ||
|
||
name: name | ||
|
||
usePlugin(id: string) { | ||
return /(.css)$/i.test(id); | ||
} | ||
}; | ||
|
||
} | ||
|
||
get name() { | ||
return 'StyleMinifyPlugin'; | ||
} | ||
|
||
function usePlugin(id: string) { | ||
return /(.css)$/i.test(id); | ||
} |
Oops, something went wrong.