Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle process.env correctly in build #2267

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link

@ranyitz ranyitz Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as "small-build" I would remove one of them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to standardize to build but not to remove small-build in case someone still uses that for something.

"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"),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not critical, but IMHO it's better to write this file somewhere in the codebase and just copy it as part of the build process. It will make sure that we're not making any mistake and makes the code a bit clearer.

Copy link
Contributor Author

@danielkcz danielkcz Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, was thinking about it but did not want to come up with some artificial filename or folder where to keep it. It's such a small thing it's ok.

`
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