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

Use script tag for engine in examples #4255

Merged
merged 3 commits into from
May 19, 2022
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
4 changes: 2 additions & 2 deletions examples/scripts/example-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BabelParser from '@babel/parser';
import Prettier from 'prettier/standalone';
import Babel from '@babel/standalone';
import formatters from '../src/app/helpers/formatters.mjs';
import readDirectoryNames from '../src/app/helpers/read-dir-names.mjs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

Expand All @@ -17,8 +18,7 @@ if (!fs.existsSync(`${MAIN_DIR}/dist/`)) {
fs.mkdirSync(`${MAIN_DIR}/dist/`);
}

fs.readdirSync(`${MAIN_DIR}/src/examples/`).forEach(function (category) {
if (category.includes('index.mjs')) return;
readDirectoryNames(`${MAIN_DIR}/src/examples/`).forEach(function (category) {
exampleData[category] = {};
const examples = fs.readdirSync(`${MAIN_DIR}/src/examples/${category}`);
examples.forEach((exampleName) => {
Expand Down
5 changes: 2 additions & 3 deletions examples/scripts/example-directory/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import Handlebars from 'handlebars';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import readDirectoryNames from '../../src/app/helpers/read-dir-names.mjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -16,9 +17,7 @@ function loadHtmlTemplate(data) {

const categoriesList = [];

let categories = fs.readdirSync(`${MAIN_DIR}/src/examples/`);
categories = categories.filter(c => c !== 'index.mjs');
categories.forEach((category) => {
readDirectoryNames(`${MAIN_DIR}/src/examples/`).forEach((category) => {
const dir = `${MAIN_DIR}/dist/${category}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
Expand Down
5 changes: 2 additions & 3 deletions examples/scripts/iframe/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fse from 'fs-extra';
import Babel from '@babel/standalone';
import Handlebars from 'handlebars';
import formatters from '../../src/app/helpers/formatters.mjs';
import readDirectoryNames from '../../src/app/helpers/read-dir-names.mjs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

Expand Down Expand Up @@ -63,9 +64,7 @@ if (!fs.existsSync(`${MAIN_DIR}/dist/iframe/`)) {
if (process.env.EXAMPLE && process.env.CATEGORY) {
buildExample(process.env.CATEGORY, `${process.env.EXAMPLE}.tsx`);
} else {
const categories = fs.readdirSync(`${MAIN_DIR}/src/examples/`);
categories.forEach((category) => {
if (category.includes('index.mjs')) return;
readDirectoryNames(`${MAIN_DIR}/src/examples/`).forEach((category) => {
const exampleFilenames = fs.readdirSync(`${MAIN_DIR}/src/examples/${category}`);
exampleFilenames.forEach((exampleFilename) => {
if (exampleFilename.includes('index.mjs')) return;
Expand Down
17 changes: 7 additions & 10 deletions examples/scripts/iframe/index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@
function loadEngine(callback) {
const enginePath = urlParams && urlParams.get('use_local_engine') || "{{{enginePath}}}";

fetch(enginePath)
.then(function(response) { return response.text() })
.then(function(data) {
var module = {
exports: {}
};
window.pc = (Function('module', 'exports', data).call(module, module, module.exports), module).exports;
window.top.pc = window.pc;
slimbuck marked this conversation as resolved.
Show resolved Hide resolved
callback();
});
const script = document.createElement('script');
script.setAttribute('src', enginePath);
script.onload = function() {
window.top.pc = window.pc;
callback();
};
document.head.appendChild(script);
}

function setupApplication(app) {
Expand Down
9 changes: 9 additions & 0 deletions examples/src/app/helpers/read-dir-names.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from 'fs';

const readDirectoryNames = (dir) => {
return fs.readdirSync(dir, { withFileTypes: true })
.filter(result => result.isDirectory())
.map(result => result.name);
};

export default readDirectoryNames;