Skip to content

Commit 8462001

Browse files
authored
Rollup build (#144)
* rollup build
1 parent 80138ff commit 8462001

File tree

11 files changed

+772
-13473
lines changed

11 files changed

+772
-13473
lines changed

package-lock.json

Lines changed: 650 additions & 13302 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,40 @@
4646
}
4747
},
4848
"devDependencies": {
49-
"@babel/core": "^7.16.7",
50-
"@babel/preset-env": "^7.16.8",
51-
"@babel/preset-react": "^7.16.7",
5249
"@playcanvas/eslint-config": "^1.0.16",
5350
"@playcanvas/observer": "^1.3.1",
5451
"@playcanvas/pcui": "^2.2.6",
52+
"@rollup/plugin-commonjs": "^21.0.1",
53+
"@rollup/plugin-node-resolve": "^13.1.3",
54+
"@rollup/plugin-replace": "^3.0.1",
5555
"@types/react": "^17.0.38",
5656
"@types/react-dom": "^17.0.11",
5757
"@typescript-eslint/eslint-plugin": "^5.9.1",
5858
"@typescript-eslint/parser": "^5.9.1",
59-
"awesome-typescript-loader": "^5.2.1",
60-
"babel-loader": "^8.2.3",
6159
"concurrently": "^7.0.0",
62-
"copy-webpack-plugin": "^10.2.0",
6360
"cross-env": "^7.0.2",
64-
"css-loader": "^6.5.1",
6561
"eslint": "^8.6.0",
66-
"html-webpack-plugin": "^5.5.0",
62+
"handlebars": "^4.7.7",
6763
"playcanvas": "^1.51.5",
6864
"prop-types": "^15.8.1",
6965
"react": "^17.0.1",
7066
"react-dom": "^17.0.1",
67+
"rollup": "^2.64.0",
68+
"rollup-plugin-copy": "^3.4.0",
69+
"rollup-plugin-module-replacement": "^1.2.1",
70+
"rollup-plugin-terser": "^7.0.2",
71+
"rollup-plugin-typescript": "^1.0.1",
7172
"serve": "^13.0.2",
72-
"style-loader": "^3.3.1",
73-
"typescript": "^4.5.4",
74-
"webpack": "^5.66.0",
75-
"webpack-bundle-analyzer": "^4.5.0",
76-
"webpack-cli": "^4.9.1"
73+
"tslib": "^2.3.1",
74+
"typescript": "^4.5.4"
7775
},
7876
"scripts": {
7977
"lint": "eslint --ignore-pattern 'src/lib' --ext .ts src",
8078
"serve": "serve dist",
81-
"build": "webpack --config webpack.config.js",
82-
"build:watch": "webpack --config webpack.config.js --watch",
83-
"build:server": "ENVIRONMENT=production PUBLIC_PATH=/viewer/static/ webpack --config webpack.config.js",
84-
"build:analyze": "ENVIRONMENT=production ANALYZE_BUNDLE=true webpack --config webpack.config.js",
79+
"build": "cross-env NODE_ENV=production rollup -c",
80+
"build:watch": "rollup -c -w",
81+
"build:server": "cross-env ENVIRONMENT=production PUBLIC_PATH=/viewer/static/ rollup -c",
8582
"develop": "concurrently --kill-others \"npm run build:watch\" \"npm run serve\"",
86-
"develop:local": "cross-env ENGINE_PATH=../engine npm run develop"
83+
"develop:local": "cross-env ENGINE_PATH=../engine/build/playcanvas.js npm run develop"
8784
}
8885
}

rollup.config.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import resolve from "@rollup/plugin-node-resolve";
2+
import commonjs from "@rollup/plugin-commonjs";
3+
import replace from '@rollup/plugin-replace';
4+
import typescript from 'rollup-plugin-typescript';
5+
import copy from 'rollup-plugin-copy';
6+
import { terser } from 'rollup-plugin-terser';
7+
import replacement from "rollup-plugin-module-replacement";
8+
import Handlebars from 'handlebars';
9+
import fs from 'fs';
10+
11+
const html = fs.readFileSync(`./src/index.mustache`, "utf8");
12+
const template = Handlebars.compile(html);
13+
if (!fs.existsSync('./dist')) fs.mkdirSync('./dist');
14+
fs.writeFileSync(`./dist/index.html`, template({
15+
hasPublicPath: !!process.env.PUBLIC_PATH,
16+
analyticsID: process.env.ANALYTICS_ID,
17+
oneTrustDomainKey: process.env.ONETRUST_DOMAIN_KEY,
18+
oneTrustDeveloperId: process.env.ONETRUST_DEVELOPER_ID
19+
}));
20+
21+
export default {
22+
input: 'src/index.tsx',
23+
output: {
24+
dir: 'dist',
25+
format: 'es'
26+
},
27+
plugins: [
28+
replacement({
29+
entries: [
30+
{
31+
find: /^playcanvas$/,
32+
replacement: (importee) => {
33+
if (!process.env.ENGINE_PATH) return;
34+
importee.replace('playcanvas', process.env.ENGINE_PATH);
35+
}
36+
}
37+
]
38+
}),
39+
replace({
40+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
41+
'__PUBLIC_PATH__': process.env.PUBLIC_PATH
42+
}),
43+
copy({
44+
targets: [
45+
{ src: './src/style.css', dest: 'dist/' },
46+
{ src: './src/fonts.css', dest: 'dist/' },
47+
{ src: './static/*', dest: 'dist/static/' }
48+
]
49+
}),
50+
commonjs(),
51+
resolve(),
52+
typescript(),
53+
(process.env.NODE_ENV === 'production' && terser())
54+
]
55+
};

src/index.ejs renamed to src/index.mustache

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
<meta name="keywords" content="glTF, GLB, WebGL, PlayCanvas, Viewer">
77
<meta charset="utf-8">
88
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
9-
<% if (htmlWebpackPlugin.options.hasPublicPath) { %>
9+
{{#hasPublicPath}}
1010
<link rel="icon" type="image/png" href="/viewer/static/playcanvas-logo.png" />
1111
<link rel="manifest" href="/viewer/static/manifest.json">
12-
<% if (htmlWebpackPlugin.options.analyticsID) { %>
12+
<link ref="stylesheet" href="/viewer/style.css" />
13+
{{#analyticsID}}
1314
<!-- Global site tag (gtag.js) - Google Analytics -->
1415
<script>
1516
window.dataLayer = window.dataLayer || [];
1617
function gtag() { dataLayer.push(arguments); }
1718
18-
if ('<%= htmlWebpackPlugin.options.oneTrustDeveloperId %>') {
19-
gtag('set', '<%= htmlWebpackPlugin.options.oneTrustDeveloperId %>', true); // OneTrust developer id
19+
if ({{{ oneTrustDeveloperId }}}) {
20+
gtag('set', {{{ oneTrustDeveloperId }}}, true); // OneTrust developer id
2021
}
2122
2223
gtag('consent', 'default', {
@@ -26,7 +27,7 @@
2627
});
2728
2829
gtag('js', new Date());
29-
gtag('config', '<%= htmlWebpackPlugin.options.analyticsID %>');
30+
gtag('config', {{{ analyticsID }}});
3031
3132
if (!window._optanonCallbacks) {
3233
window._optanonCallbacks = [];
@@ -43,18 +44,21 @@
4344
});
4445
});
4546
</script>
46-
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= htmlWebpackPlugin.options.analyticsID %>"></script>
47-
<% } %>
48-
<% } else { %>
47+
<script async src="https://www.googletagmanager.com/gtag/js?id={{{ analyticsID }}}"></script>
48+
{{/analyticsID}}
49+
{{/hasPublicPath}}
50+
{{^hasPublicPath}}
4951
<link rel="icon" type="image/png" href="./static/playcanvas-logo.png" />
5052
<link rel="manifest" href="./static/manifest.json">
51-
<% } %>
53+
<link rel="stylesheet" href="./style.css" />
54+
{{/hasPublicPath}}
5255
</head>
5356

5457
<body>
5558
<div id='app'></div>
56-
<% if (htmlWebpackPlugin.options.hasPublicPath) { %>
57-
<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript" charset="UTF-8" data-domain-script="<%= htmlWebpackPlugin.options.oneTrustDomainKey %>" ></script>
59+
{{#hasPublicPath}}
60+
<script src="/viewer/index.js"></script>
61+
<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript" charset="UTF-8" data-domain-script="{{{ oneTrustDomainKey }}}" ></script>
5862
<script type="text/javascript">
5963
if (!window._optanonCallbacks) {
6064
window._optanonCallbacks = [];
@@ -66,6 +70,9 @@
6670
}
6771
}
6872
</script>
69-
<% } %>
73+
{{/hasPublicPath}}
74+
{{^hasPublicPath}}
75+
<script src="./index.js"></script>
76+
{{/hasPublicPath}}
7077
</body>
7178
</html>

src/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as pc from 'playcanvas';
22
import React from 'react';
33
import ReactDOM from 'react-dom';
44
// @ts-ignore: library file import
5-
import { wasmSupported, loadWasmModuleAsync } from 'lib/wasm-loader.js';
5+
import { wasmSupported, loadWasmModuleAsync } from '../lib/wasm-loader.js';
66

77
import Viewer from './viewer';
88
// import { Skybox, setSkyboxes } from './controls';
@@ -18,9 +18,6 @@ import Spinner from '@playcanvas/pcui/Spinner/component';
1818
import { getAssetPath, getRootPath } from './helpers';
1919
import { Skybox, Option } from './types';
2020

21-
import './style.css';
22-
import './fonts.css';
23-
2421
// initialize the apps state
2522
const observer: Observer = new Observer({
2623
show: {

src/load-ui.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Container from '@playcanvas/pcui/Container/component';
44
import Label from '@playcanvas/pcui/Label/component';
55
// @ts-ignore: library file import
66
import Button from '@playcanvas/pcui/Button/component';
7-
import React, { useRef, useContext } from 'react';
8-
import { Observer, URL } from './types';
7+
import React, { useRef } from 'react';
8+
import { Observer, File } from './types';
99

1010
const ObserverContext = React.createContext(null);
1111
const ObserverProvider = ObserverContext.Provider;
@@ -22,7 +22,7 @@ const LoadButton = () => {
2222
// `event` points to the selected file
2323
const viewer = (window as any).viewer;
2424
if (viewer && event.target.files.length) {
25-
const urls: Array<URL> = [];
25+
const urls: Array<File> = [];
2626
urls.push({
2727
url: URL.createObjectURL(event.target.files[0]),
2828
filename: event.target.files[0].name

src/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ input {
164164
-ms-user-select: none;
165165
}
166166

167+
#load-controls .pcui-container {
168+
position: absolute;
169+
}
170+
167171
.load-button-panel {
168172
width: 320;
169173
width: 320;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface Morph {
44
weight?: number
55
}
66

7-
export interface URL {
7+
export interface File {
88
url: string,
99
filename?: string
1010
}

src/viewer.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as pc from 'playcanvas';
33
import * as pcx from 'playcanvas/build/playcanvas-extras.js';
44
import DebugLines from './debug';
55
// @ts-ignore: library file import
6-
import * as MeshoptDecoder from 'lib/meshopt_decoder.js';
6+
import * as MeshoptDecoder from '../lib/meshopt_decoder.js';
77
import { getAssetPath } from './helpers';
8-
import { Morph, URL, Observer, HierarchyNode } from './types';
8+
import { Morph, File, Observer, HierarchyNode } from './types';
99
// @ts-ignore: library file import
1010
import * as VoxParser from 'playcanvas/scripts/parsers/vox-parser.js';
1111

@@ -475,7 +475,7 @@ class Viewer {
475475

476476
// load the image files into the skybox. this function supports loading a single equirectangular
477477
// skybox image or 6 cubemap faces.
478-
private loadSkybox(files: Array<URL>) {
478+
private loadSkybox(files: Array<File>) {
479479
const app = this.app;
480480

481481
if (files.length !== 6) {
@@ -517,7 +517,7 @@ class Viewer {
517517
return 0;
518518
};
519519

520-
const sortPred = (first: URL, second: URL) => {
520+
const sortPred = (first: File, second: File) => {
521521
const firstOrder = getOrder(first.filename);
522522
const secondOrder = getOrder(second.filename);
523523
return firstOrder < secondOrder ? -1 : (secondOrder < firstOrder ? 1 : 0);
@@ -658,7 +658,7 @@ class Viewer {
658658
}
659659

660660
// load gltf model given its url and list of external urls
661-
private loadGltf(gltfUrl: URL, externalUrls: Array<URL>) {
661+
private loadGltf(gltfUrl: File, externalUrls: Array<File>) {
662662

663663
// provide buffer view callback so we can handle models compressed with MeshOptimizer
664664
// https://github.com/zeux/meshoptimizer
@@ -690,7 +690,7 @@ class Viewer {
690690
};
691691

692692
const processImage = function (gltfImage: any, continuation: (err: string, result: any) => void) {
693-
const u: URL = externalUrls.find((url) => {
693+
const u: File = externalUrls.find((url) => {
694694
return url.filename === pc.path.normalize(gltfImage.uri || "");
695695
});
696696
if (u) {
@@ -757,25 +757,25 @@ class Viewer {
757757
// load the list of urls.
758758
// urls can reference glTF files, glb files and skybox textures.
759759
// returns true if a model was loaded.
760-
load(urls: Array<URL>) {
760+
load(files: Array<File>) {
761761
// convert single url to list
762-
if (!Array.isArray(urls)) {
763-
urls = [urls];
762+
if (!Array.isArray(files)) {
763+
files = [files];
764764
}
765765

766766
// step through urls loading gltf/glb models
767767
let result = false;
768-
urls.forEach((url) => {
769-
const filenameExt = pc.path.getExtension(url.filename).toLowerCase();
768+
files.forEach((file) => {
769+
const filenameExt = pc.path.getExtension(file.filename).toLowerCase();
770770
if (modelExtensions.indexOf(filenameExt) !== -1) {
771-
this.loadGltf(url, urls);
771+
this.loadGltf(file, files);
772772
result = true;
773773
}
774774
});
775775

776776
if (!result) {
777777
// if no models were loaded, load the files as skydome images instead
778-
this.loadSkybox(urls);
778+
this.loadSkybox(files);
779779
}
780780

781781
// return true if a model/scene was loaded and false otherwise
@@ -996,7 +996,7 @@ class Viewer {
996996
// use webkitGetAsEntry to extract files so we can include folders
997997
private dropHandler(event: DragEvent) {
998998

999-
const removeCommonPrefix = (urls: Array<URL>) => {
999+
const removeCommonPrefix = (urls: Array<File>) => {
10001000
const split = (pathname: string) => {
10011001
const parts = pathname.split(pc.path.delimiter);
10021002
const base = parts[0];
@@ -1021,22 +1021,22 @@ class Viewer {
10211021
};
10221022

10231023
const resolveFiles = (entries: Array<FileSystemFileEntry>) => {
1024-
const urls: Array<URL> = [];
1024+
const files: Array<File> = [];
10251025
entries.forEach((entry: FileSystemFileEntry) => {
1026-
entry.file((file: File) => {
1027-
urls.push({
1028-
url: URL.createObjectURL(file),
1026+
entry.file((entryFile: any) => {
1027+
files.push({
1028+
url: URL.createObjectURL(entryFile),
10291029
filename: entry.fullPath.substring(1)
10301030
});
1031-
if (urls.length === entries.length) {
1031+
if (files.length === entries.length) {
10321032
// remove common prefix from files in order to support dragging in the
10331033
// root of a folder containing related assets
1034-
if (urls.length > 1) {
1035-
removeCommonPrefix(urls);
1034+
if (files.length > 1) {
1035+
removeCommonPrefix(files);
10361036
}
10371037

10381038
// if a scene was loaded (and not just a skybox), clear the current scene
1039-
if (this.load(urls) && !event.shiftKey) {
1039+
if (this.load(files) && !event.shiftKey) {
10401040
this.resetScene();
10411041
}
10421042
}

tsconfig.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"allowSyntheticDefaultImports" : true,
1414
"esModuleInterop" : true
1515
},
16-
"include": [
17-
"src"
18-
],
16+
"include": ["src"],
17+
"exclude": ["node_modules"]
1918
}

0 commit comments

Comments
 (0)