From 7d6fd90f046d2396f10fb08ec772949bfb0d3081 Mon Sep 17 00:00:00 2001 From: Ian J Sikes Date: Mon, 4 Sep 2017 17:24:54 -0700 Subject: [PATCH] Add template files for plugin examples and tests --- .../examples/simple/src/index.js.tpl | 11 +++ .../examples/simple/src/lazy-module.js.tpl | 1 + .../simple/src/static-esm-module.js.tpl | 1 + .../examples/simple/webpack.config.js.tpl | 13 +++ .../test/fixtures/simple-file.js.tpl | 3 + .../templates/test/functional.test.js.tpl | 10 +++ .../templates/test/test-utils.js.tpl | 82 +++++++++++++++++++ 7 files changed, 121 insertions(+) create mode 100644 lib/generate-plugin/templates/examples/simple/src/index.js.tpl create mode 100644 lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl create mode 100644 lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl create mode 100644 lib/generate-plugin/templates/examples/simple/webpack.config.js.tpl create mode 100644 lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl create mode 100644 lib/generate-plugin/templates/test/functional.test.js.tpl create mode 100644 lib/generate-plugin/templates/test/test-utils.js.tpl diff --git a/lib/generate-plugin/templates/examples/simple/src/index.js.tpl b/lib/generate-plugin/templates/examples/simple/src/index.js.tpl new file mode 100644 index 00000000000..ed75d67d081 --- /dev/null +++ b/lib/generate-plugin/templates/examples/simple/src/index.js.tpl @@ -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 diff --git a/lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl b/lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl new file mode 100644 index 00000000000..f3dac067326 --- /dev/null +++ b/lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl @@ -0,0 +1 @@ +export default 'lazy'; diff --git a/lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl b/lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl new file mode 100644 index 00000000000..d02ba545bd3 --- /dev/null +++ b/lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl @@ -0,0 +1 @@ +export default 'foo'; diff --git a/lib/generate-plugin/templates/examples/simple/webpack.config.js.tpl b/lib/generate-plugin/templates/examples/simple/webpack.config.js.tpl new file mode 100644 index 00000000000..e50ecc3e588 --- /dev/null +++ b/lib/generate-plugin/templates/examples/simple/webpack.config.js.tpl @@ -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 %>() + ] +}; diff --git a/lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl b/lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl new file mode 100644 index 00000000000..908ed137dff --- /dev/null +++ b/lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl @@ -0,0 +1,3 @@ +import foo from "./foo"; // eslint-disable-line + +console.log(foo); diff --git a/lib/generate-plugin/templates/test/functional.test.js.tpl b/lib/generate-plugin/templates/test/functional.test.js.tpl new file mode 100644 index 00000000000..07f2099cec0 --- /dev/null +++ b/lib/generate-plugin/templates/test/functional.test.js.tpl @@ -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); +}); diff --git a/lib/generate-plugin/templates/test/test-utils.js.tpl b/lib/generate-plugin/templates/test/test-utils.js.tpl new file mode 100644 index 00000000000..0cd61e6700c --- /dev/null +++ b/lib/generate-plugin/templates/test/test-utils.js.tpl @@ -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 };