-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add template files for plugin examples and tests
- Loading branch information
Showing
7 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
lib/generate-plugin/templates/examples/simple/src/index.js.tpl
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,11 @@ | ||
import esmModule from './static-esm-module'; | ||
|
||
const getLazyModule = () => System.import('./lazy-module'); | ||
|
||
setTimeout(() => { | ||
getLazyModule.then((modDefault) => { | ||
console.log(modDefault); //eslint-disable-line | ||
}); | ||
}, 300); | ||
|
||
console.log(esmModule); //eslint-disable-line |
1 change: 1 addition & 0 deletions
1
lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl
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 @@ | ||
export default 'lazy'; |
1 change: 1 addition & 0 deletions
1
lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl
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 @@ | ||
export default 'foo'; |
13 changes: 13 additions & 0 deletions
13
lib/generate-plugin/templates/examples/simple/webpack.config.js.tpl
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,13 @@ | ||
const path = require('path'); | ||
const <%= name %> = require('../../src/index.js'); | ||
|
||
module.exports = { | ||
entry: './src/index.js', | ||
output: { | ||
path: path.join(__dirname, 'example_dist'), | ||
filename: '[name].chunk.js', | ||
}, | ||
plugins: [ | ||
new <%= name %>() | ||
] | ||
}; |
3 changes: 3 additions & 0 deletions
3
lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl
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,3 @@ | ||
import foo from "./foo"; // eslint-disable-line | ||
|
||
console.log(foo); |
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,10 @@ | ||
import { | ||
runWebpackExampleInMemory, | ||
} from '../test/test-utils'; | ||
|
||
test('should run with no errors or warnings', async () => { | ||
const buildStats = await runWebpackExampleInMemory('simple'); | ||
const { errors, warnings } = buildStats; | ||
|
||
expect([...errors, ...warnings].length).toBe(0); | ||
}); |
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,82 @@ | ||
import path from 'path'; | ||
import webpack from 'webpack'; | ||
import Promise from 'bluebird'; | ||
import MemoryFs from 'memory-fs'; | ||
|
||
const fs = new MemoryFs(); | ||
const unitTestFixtures = path.resolve(__dirname, 'fixtures'); | ||
|
||
/** | ||
* | ||
* | ||
* @param {string} fixtureName | ||
* @param {string} [withQueryString=''] | ||
* @returns {string} Absolute path of a file with query that is to be run by a loader. | ||
*/ | ||
function getFixtureResource(fixtureName, withQueryString = '') { | ||
return `${getFixture(fixtureName)}?${withQueryString}`; | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @param {string} fixtureName | ||
* @returns {string} Absolute path of a file with query that is to be run by a loader. | ||
*/ | ||
function getFixture(fixtureName) { | ||
return path.resolve(unitTestFixtures, `${fixtureName}.js`); | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @param {Object} withOptions - Loader Options | ||
* @returns {{loader: string, options: Object}} | ||
*/ | ||
function getLoader(withOptions) { | ||
return [{ loader: path.resolve(__dirname, '../dist/index.js'), options: withOptions }]; | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @param {string} exampleName | ||
* @returns {Object|Array} - Returns an object or array of objects representing the webpack configuration options | ||
*/ | ||
function getExampleConfig(exampleName) { | ||
return require(`../examples/${exampleName}/webpack.config.js`); //eslint-disable-line | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @param {string} exampleName - name of example inside of examples folder | ||
* @returns | ||
*/ | ||
async function runWebpackExampleInMemory(exampleName) { | ||
const webpackConfig = getExampleConfig(exampleName); | ||
const compiler = webpack(webpackConfig); | ||
compiler.outputFileSystem = fs; | ||
const run = Promise.promisify(compiler.run, { context: compiler }); | ||
const stats = await run(); | ||
|
||
|
||
const { compilation } = stats; | ||
const { errors, warnings, assets, entrypoints, chunks, modules } = compilation; | ||
const statsJson = stats.toJson(); | ||
|
||
return { | ||
assets, | ||
entrypoints, | ||
errors, | ||
warnings, | ||
stats, | ||
chunks, | ||
modules, | ||
statsJson, | ||
}; | ||
} | ||
|
||
export { getExampleConfig, runWebpackExampleInMemory, fs, getFixtureResource, getLoader, getFixture }; |