Skip to content

Commit

Permalink
Handle process.env correctly in build (#2267)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredyC authored Jan 28, 2020
1 parent 55a2fcb commit 8b45f14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "mobx",
"version": "0.15.3",
"description": "Simple, scalable state management.",
"main": "lib/mobx.js",
"main": "lib/index.js",
"umd:main": "lib/mobx.umd.js",
"module": "lib/mobx.module.js",
"browser": {
"./lib/mobx.js": "./lib/mobx.js",
"./lib/mobx.js": "./lib/mobx.min.js",
"./lib/mobx.module.js": "./lib/mobx.module.js"
},
"unpkg": "lib/mobx.umd.min.js",
Expand All @@ -29,6 +29,7 @@
"_prepublish": "yarn small-build",
"quick-build": "tsc --pretty",
"small-build": "node scripts/build.js",
"build": "node scripts/build.js",
"release": "node scripts/publish.js",
"publish-script": "yarn release",
"docs:build": "yarn --cwd website build",
Expand Down
42 changes: 25 additions & 17 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,16 @@ function runTypeScriptBuild(outDir, version, target, declarations) {
}
}

async function generateBundledModule(inputPath, version, outputFile, format, env = "development") {
async function generateBundledModule(inputPath, version, outputFile, format, env) {
console.log(`Generating ${version} ${outputFile} ${env} bundle.`)

const replaceEnv = env && replacePlugin({ "process.env.NODE_ENV": JSON.stringify(env) })

let plugins
if (env === "production") {
plugins = [
resolvePlugin(),
replacePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }),
terserPlugin(),
filesizePlugin()
]
plugins = [resolvePlugin(), replaceEnv, terserPlugin(), filesizePlugin()]
} else {
plugins = [
resolvePlugin(),
replacePlugin({ "process.env.NODE_ENV": JSON.stringify(env) }),
filesizePlugin()
]
plugins = [resolvePlugin(), replaceEnv, filesizePlugin()]
}

const bundle = await rollup({
Expand Down Expand Up @@ -104,7 +97,7 @@ const pkg = require("../package.json")
function writePackage(versionPath, version) {
// replace `0.y.z` with `v.y.z`, strip `v` prefix
const pkgVersion = pkg.version.replace(/^0/, version.replace("v", ""))
return fs.writeFile(
fs.writeFileSync(
path.resolve(versionPath, "package.json"),
JSON.stringify(
{
Expand All @@ -121,6 +114,19 @@ function writePackage(versionPath, version) {
)
}

function writeIndex(versionPath) {
fs.writeFileSync(
path.resolve(versionPath, "lib", "index.js"),
`
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
module.exports = require('./mobx.min.js');
} else {
module.exports = require('./mobx.js');
}
`
)
}

async function build(version) {
const distPath = "dist"
const es5Build = path.join(distPath, ".build.es5")
Expand All @@ -131,18 +137,20 @@ async function build(version) {
runTypeScriptBuild(es6Build, version, ts.ScriptTarget.ES2015, false)

await Promise.all([
generateBundledModule(es5Build, version, "mobx.js", "cjs"),
generateBundledModule(es5Build, version, "mobx.js", "cjs", "development"),
generateBundledModule(es5Build, version, "mobx.min.js", "cjs", "production"),

generateBundledModule(es5Build, version, "mobx.module.js", "es"),
generateBundledModule(es6Build, version, "mobx.es6.js", "es"),

generateBundledModule(es5Build, version, "mobx.umd.js", "umd"),
generateBundledModule(es5Build, version, "mobx.umd.min.js", "umd", "production")
generateBundledModule(es5Build, version, "mobx.umd.min.js", "umd", "production"),

copyAssets(versionPath)
])

await copyAssets(versionPath)
await writePackage(versionPath, version)
writeIndex(versionPath)
writePackage(versionPath, version)
}

Promise.all([build("v4"), build("v5")]).catch(e => {
Expand Down

0 comments on commit 8b45f14

Please sign in to comment.