Skip to content

Commit

Permalink
build: add banner
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Nov 27, 2019
1 parent 2abd321 commit 6caae23
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 214 deletions.
299 changes: 155 additions & 144 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,145 +1,156 @@
const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const rm = require('rimraf').sync
const babel = require('rollup-plugin-babel')
const cmd = require('rollup-plugin-commonjs')
const replace = require('rollup-plugin-replace')
const cleanup = require('rollup-plugin-cleanup')
const { terser } = require('rollup-plugin-terser')
const resolve = require('rollup-plugin-node-resolve')

const libName = 'mpstore'
const version = require('./package.json').version
const testLibPath = path.resolve(__dirname, './demo')
const devStoreDir = path.resolve(testLibPath, './store')
const entryPath = path.resolve(__dirname, './src/index.js')
const outputPath = filename => path.resolve(__dirname, './dist', filename)

const esm = {
input: entryPath,
output: {
file: outputPath(`${libName}.esm.js`),
format: 'es',
},
}

const es6m = {
input: entryPath,
output: {
file: outputPath(`${libName}.es6m.js`),
format: 'es',
},
}

const cjs = {
input: entryPath,
output: {
file: outputPath(`${libName}.common.js`),
format: 'cjs',
},
}

const uglifyCjs = {
input: entryPath,
output: {
file: outputPath(`${libName}.min.js`),
format: 'cjs',
},
}

// create env variable
const createReplacePlugin = () => {
return replace({
__VERSION__: `'${version}'`,
})
}

async function build (cfg, needUglify, sourcemap = false, needBabel = true) {
cfg.output.sourcemap = sourcemap

const buildCfg = {
input: cfg.input,
plugins: [
cleanup(),
resolve(),
cmd(),
createReplacePlugin(),
]
}

if (needBabel) {
buildCfg.plugins.splice(
2, 0,
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [['@babel/preset-env', { modules: false }]],
}),
)
}

if (needUglify) {
buildCfg.plugins.unshift(
terser({
sourcemap: false,
})
)
}

const bundle = await rollup.rollup(buildCfg)
await bundle.generate(cfg.output)
await bundle.write(cfg.output)
}

console.clear()
// delete old build files
rm('./dist')
rm(devStoreDir)

const transferfile = (from, desPath) => {
const readable = fs.createReadStream(from)
readable.on('open', () => readable.pipe(fs.createWriteStream(desPath)))
}

const buildVersion = sourcemap => {
const builds = [
build(esm, false, sourcemap),
build(cjs, false, sourcemap),
// build es6 version
build(es6m, false, sourcemap, false)
]

if (!sourcemap) {
builds.push(build(uglifyCjs, true, sourcemap))
}

Promise.all(builds).then(() => {
// transfer esm package to dev folder
if (fs.existsSync(testLibPath)) {
if (!fs.existsSync(devStoreDir)) {
fs.mkdirSync(devStoreDir)
}

const devStorePath = esm.output.file
const desPath = path.join(devStoreDir, `${libName}.esm.js`)
transferfile(devStorePath, desPath)
if (sourcemap) {
transferfile(devStorePath + '.map', desPath + '.map')
}
}
})
}

// watch, use in dev and test
if (process.argv.includes('-w')) {
let i = 0
fs.watch('./src', () => {
console.clear()
console.log('Rebuild: ' + ++i)
buildVersion(true)
})
buildVersion(true)
} else {
buildVersion()
const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const rm = require('rimraf').sync
const babel = require('rollup-plugin-babel')
const cmd = require('rollup-plugin-commonjs')
const replace = require('rollup-plugin-replace')
const cleanup = require('rollup-plugin-cleanup')
const { terser } = require('rollup-plugin-terser')
const resolve = require('rollup-plugin-node-resolve')

const libName = 'mpstore'
const version = require('./package.json').version
const testLibPath = path.resolve(__dirname, './demo')
const devStoreDir = path.resolve(testLibPath, './store')
const entryPath = path.resolve(__dirname, './src/index.js')
const outputPath = filename => path.resolve(__dirname, './dist', filename)

const banner =
'/*!\n' +
` * Mpstore.js v${version}\n` +
` * (c) 2019-${new Date().getFullYear()} Imtaotao\n` +
' * Released under the MIT License.\n' +
' */'

const esm = {
input: entryPath,
output: {
banner,
format: 'es',
file: outputPath(`${libName}.esm.js`),
},
}

const es6m = {
input: entryPath,
output: {
banner,
format: 'es',
file: outputPath(`${libName}.es6m.js`),
},
}

const cjs = {
input: entryPath,
output: {
banner,
format: 'cjs',
file: outputPath(`${libName}.common.js`),
},
}

const uglifyCjs = {
input: entryPath,
output: {
banner,
format: 'cjs',
file: outputPath(`${libName}.min.js`),
},
}

// create env variable
const createReplacePlugin = () => {
return replace({
__VERSION__: `'${version}'`,
})
}

async function build (cfg, needUglify, sourcemap = false, needBabel = true) {
cfg.output.sourcemap = sourcemap

const buildCfg = {
input: cfg.input,
plugins: [
cleanup(),
resolve(),
cmd(),
createReplacePlugin(),
]
}

if (needBabel) {
buildCfg.plugins.splice(
2, 0,
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [['@babel/preset-env', { modules: false }]],
}),
)
}

if (needUglify) {
buildCfg.plugins.unshift(
terser({
sourcemap: false,
})
)
}

const bundle = await rollup.rollup(buildCfg)
await bundle.generate(cfg.output)
await bundle.write(cfg.output)
}

console.clear()
// delete old build files
rm('./dist')
rm(devStoreDir)

const transferfile = (from, desPath) => {
const readable = fs.createReadStream(from)
readable.on('open', () => readable.pipe(fs.createWriteStream(desPath)))
}

const buildVersion = sourcemap => {
const builds = [
build(esm, false, sourcemap),
build(cjs, false, sourcemap),
// build es6 version
build(es6m, false, sourcemap, false)
]

if (!sourcemap) {
builds.push(build(uglifyCjs, true, sourcemap))
}

Promise.all(builds).then(() => {
// transfer esm package to dev folder
if (fs.existsSync(testLibPath)) {
if (!fs.existsSync(devStoreDir)) {
fs.mkdirSync(devStoreDir)
}

const devStorePath = esm.output.file
const desPath = path.join(devStoreDir, `${libName}.esm.js`)
transferfile(devStorePath, desPath)
if (sourcemap) {
transferfile(devStorePath + '.map', desPath + '.map')
}
}
})
}

// watch, use in dev and test
if (process.argv.includes('-w')) {
let i = 0
fs.watch('./src', () => {
console.clear()
console.log('Rebuild: ' + ++i)
buildVersion(true)
})
buildVersion(true)
} else {
buildVersion()
}
9 changes: 7 additions & 2 deletions demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*!
* Mpstore.js v0.1.7
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
Expand Down Expand Up @@ -812,7 +817,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.6';
this.version = '0.1.7';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1076,7 +1081,7 @@ function () {
return Store;
}();

var version = '0.1.6';
var version = '0.1.7';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
9 changes: 7 additions & 2 deletions dist/mpstore.common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*!
* Mpstore.js v0.1.7
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down Expand Up @@ -816,7 +821,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.6';
this.version = '0.1.7';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1080,7 +1085,7 @@ function () {
return Store;
}();

var version = '0.1.6';
var version = '0.1.7';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
9 changes: 7 additions & 2 deletions dist/mpstore.es6m.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*!
* Mpstore.js v0.1.7
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
function warning (message, noError) {
message = `\n\nMpStore warning: ${message}\n\n`;
if (noError) {
Expand Down Expand Up @@ -645,7 +650,7 @@ class Store {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.6';
this.version = '0.1.7';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -886,7 +891,7 @@ class Store {
}
}

const version = '0.1.6';
const version = '0.1.7';
const nativePage = Page;
const nativeComponent = Component;
function expandConfig (config, expandMethods, isPage) {
Expand Down
Loading

0 comments on commit 6caae23

Please sign in to comment.