Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 8, 2019
0 parents commit 6d5b1a2
Show file tree
Hide file tree
Showing 6 changed files with 2,819 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Darwin Education, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# craco-cesium

Let's use 🌍[Cesium](https://cesiumjs.org) with [create-react-app](https://github.com/facebook/create-react-app) today!

This is a plugin for [@craco/craco](https://github.com/sharegate/craco).

## Very very easy usage

### 1. Create a React project

```sh
npm -g i create-react-app # or yarn global add create-react-app
create-react-app example
cd example
```

### 2. Install modules

In your create-react-app project, install modules:

```sh
npm install --save @craco/craco craco-cesium cesium
# or
yarn add @craco/craco craco-cesium cesium
```

### 3. Rewrite npm scripts

Rewrite npm scripts in `package.json` as following:

```js
{
// ...
"scripts": {
"start": "craco start", // react-scripts -> craco
"build": "craco build", // react-scripts -> craco
"test": "craco test", // react-scripts -> craco
"eject": "react-scripts eject"
},
// ...
}
```

### 4. Create craco config file

Create `craco.config.js` in the proejct root:

```js
const CracoCesiumPlugin = require("craco-cesium");

module.exports = {
plugins: [
{
plugin: CracoCesiumPlugin(/* options */)
}
]
};
```

### 5. Congratulations! 🎉

Set up is complete! Enjoy your Cesium life.

[Resium](https://resium.darwineducation.com) is also recommended.

You can import Cesium as following:

```js
import { Viewer, Entity, Color } from "cesium";
```

## Options

If the option is omiited, the default options is used:

```json
{
"loadPartially": false
}
```

### loadPartially

If false, whole Cesium will be loaded in HTML and `window.Cesium` is used in `import { ... } from "cesium";`.

If true, Cesium will be load partially and bundled in the JS. For details, refer to [Cesium official tutorial](https://cesium.com/docs/tutorials/cesium-and-webpack/).
111 changes: 111 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"use strict";

const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackIncludeAssetPlugin = require("html-webpack-include-assets-plugin");

const cesiumSource = "node_modules/cesium/Source";
const cesiumWorkers = "../Build/Cesium/Workers";

module.exports = ({ loadPartially } = { loadPartially: false }) => ({
overrideWebpackConfig: ({ webpackConfig, context: { env } }) => {
const prod = env === "production";

if (loadPartially) {
// https://cesium.com/docs/tutorials/cesium-and-webpack/

if (prod) {
// Strip cesium pragmas
webpackConfig.module.push({
test: /.js$/,
enforce: "pre",
include: path.resolve(__dirname, cesiumSource),
use: [
{
loader: "strip-pragma-loader",
options: {
pragmas: {
debug: false
}
}
}
]
});
}

webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
cesium$: "cesium/Cesium",
cesium: "cesium/Source"
};

webpackConfig.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(cesiumSource, cesiumWorkers),
to: "cesium/Workers"
},
{
from: path.join(cesiumSource, "Assets"),
to: "cesium/Assets"
},
{
from: path.join(cesiumSource, "Widgets"),
to: "cesium/Widgets"
}
]),
new HtmlWebpackIncludeAssetPlugin({
append: false,
assets: ["cesium/Widgets/widgets.css"]
}),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify("cesium")
})
);

webpackConfig.output = {
...webpackConfig.output,
// Needed to compile multiline strings in Cesium
sourcePrefix: ""
};

webpackConfig.amd = {
...webpackConfig.amd,
// Enable webpack-friendly use of require in Cesium
toUrlUndefined: true
};

webpackConfig.node = {
...webpackConfig.node,
// Resolve node module use of fs
fs: "empty"
};
} else {
// https://resium.darwineducation.com/installation1

webpackConfig.plugins.push(
new CopyWebpackPlugin([
{
from: `node_modules/cesium/Build/Cesium${prod ? "" : "Unminified"}`,
to: "cesium"
}
]),
new HtmlWebpackIncludeAssetPlugin({
append: false,
assets: ["cesium/Widgets/widgets.css", "cesium/Cesium.js"]
}),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify("cesium")
})
);

webpackConfig.externals = {
...webpackConfig.externals,
cesium: "Cesium"
};
}

return webpackConfig;
}
});
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "craco-cesium",
"version": "0.1.0",
"description": "Let's use Cesium with create-react-app today!",
"main": "index.js",
"repository": "https://github.com/darwin-education/craco-cesium.git",
"author": "rot1024 <[email protected]>",
"license": "MIT",
"dependencies": {
"copy-webpack-plugin": "^5.0.0",
"html-webpack-include-assets-plugin": "^1.0.7",
"strip-pragma-loader": "^1.0.0",
"webpack": "^4.29.6"
},
"peerDependencies": {
"@craco/craco": "*"
}
}
Loading

0 comments on commit 6d5b1a2

Please sign in to comment.