123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import fs from 'fs'
- import path from 'path'
- import alias from '@rollup/plugin-alias'
- import replace from '@rollup/plugin-replace'
- import resolveNode from '@rollup/plugin-node-resolve'
- import commonjs from '@rollup/plugin-commonjs'
- import babel from '@rollup/plugin-babel'
- import babelEnvConfig from './babel.env.config'
- import html from 'rollup-plugin-html'
- import deps from './scripts/rollup-plugin-deps'
- import buble from '@rollup/plugin-buble'
- import styles from 'rollup-plugin-styles'
- import { terser } from 'rollup-plugin-terser'
- const pkg = require(`./package.json`)
- const version = pkg.version
- const isProd = process.env.NODE_ENV === 'production'
- const dist = file => path.resolve('../../dist/sdk/' + file)
- function createReplacePlugin() {
- const replacements = {
- 'process.env.NODE_ENV': `"${process.env.NODE_ENV}"`,
- __COMMIT__: `"${process.env.COMMIT}"`,
- __VERSION__: `"${version}"`,
- __ENV__: `"${process.env.NODE_ENV}"`,
- __PROD__: isProd,
- __DEV__: !isProd,
- }
- Object.keys(replacements).forEach(key => {
- if (key in process.env) {
- replacements[key] = process.env[key]
- }
- })
- return replace({
- values: replacements,
- preventAssignment: true,
- })
- }
- const sdk_plugins = [
- alias({
- entries: [{ find: '@sdk', replacement: path.resolve(__dirname, 'src') }],
- }),
- resolveNode({
- preferBuiltins: true,
- browser: true,
- }),
- babel(babelEnvConfig()),
- commonjs(),
- createReplacePlugin(),
- styles(),
- ]
- const plg_plugins = [
- html({
- include: '**/*.html',
- htmlMinifierOptions: {
- collapseWhitespace: true,
- collapseBooleanAttributes: true,
- conservativeCollapse: true,
- minifyJS: true,
- },
- }),
- buble(),
- resolveNode({
- mainFields: ['jsnext', 'main'],
- preferBuiltins: true,
- browser: true,
- }),
- commonjs(),
- createReplacePlugin(),
- ]
- if (isProd) {
- sdk_plugins.push(terser())
- plg_plugins.push(terser())
- }
- const plugindir = './src/modules/plugins'
- const plugins = fs
- .readdirSync(plugindir)
- .map(name => {
- const file = path.join(plugindir, name)
- const input = path.join(file, 'index.js')
- let filename = ''
- if (name.indexOf('_') !== -1) {
- let temp = name.split('_')
- filename = temp[0].charAt(0).toUpperCase() + temp[0].substring(1) + temp[1].charAt(0).toUpperCase() + temp[1].substring(1)
- } else {
- filename = name.charAt(0).toUpperCase() + name.substr(1)
- }
- if (!fs.statSync(file).isDirectory() || !fs.existsSync(input)) {
- return null
- }
- return {
- input: input,
- output: [
- {
- file: dist(`plugins/${filename}.js`), //path.resolve(`../../dist/plugins/${filename}.js`),
- format: 'iife',
- sourcemap: !isProd,
- name: filename,
- },
- ],
- plugins: plg_plugins,
- }
- })
- .filter(entry => entry != null)
- export default [
- {
- input: 'src/index.js',
- external: ['three'],
- output: [
- {
- globals: {
- three: 'THREE',
- },
- file: dist('kankan-sdk.js'),
- format: 'umd',
- name: 'KanKan',
- sourcemap: !isProd,
- banner: pkg.banner.replace('${date}', new Date().toLocaleDateString()).replace('${author}', pkg.author).replace('${year}', new Date().getFullYear()),
- },
- ],
- plugins: sdk_plugins,
- },
- {
- input: 'kankan-sdk-deps.js',
- preserveEntrySignatures: false,
- output: [
- {
- file: dist(`kankan-sdk-deps.js`),
- },
- ],
- plugins: [
- deps({
- copy: {
- targets: [
- { dest: dist(`images`), name: 'img' },
- { dest: dist(`fonts`), name: 'font' },
- ],
- verbose: true,
- copyOnce: true,
- },
- mangle: isProd,
- }),
- ],
- onwarn: function (message) {
- if (/kankan-sdk-deps/.test(message)) return
- console.error(message)
- },
- },
- ].concat(plugins)
|